AI and deep learning define the future of programming, will Kotlin fly or die?

AI technologies in general and deep learning specifically are becoming mainstream and many if not most cloud, embedded and handheld applications are be expected to provide some AI based behavior in the near future. There is an opportunity for Kotlin to become the language of choice for such applications and as such become a mainstream language along with the AI technologies.

Kotlin is an amazing programming language and it’s target, JVM, has maintained it’s place as one of the major execution environments for software code. However programming as we know it is changing by the rising adoption of AI technologies and specifically deep learning based approaches. This is an area where JVM is not strong and cannot compete well due to lack of library support. How can Kotlin benefit from this transition into AI technologies becoming part of main stream software development and how can Kotlin make this transition easier for the software teams?

The deep learning, the dominant branch of AI techniques, can be divided into two steps: i) training deep learning based AI models and ii) running the pretrained models in the final execution environment. For training deep learning models people predominantly use Python because of the extensive library support that Python provides for machine learning applications combined with ability to write complex distributed applications with Python (something required for training models in the cloud). Some examples of what makes Python stand out are the natively implemented machine learning libraries with Python bindings including TensorFlow, Numpy, SciPy, Pandas, SciKit Learn and OpenCV. Building and training deep learning models will be mostly a feature of back-end systems or corporate data pipelines. Data scientists experiment with the models on their desktop but the final models are trained in the cloud. Training the models require efficient numerical libraries that allow compiling of the models to GPUs or other specific hardware. TensorFlow is the most widely used of such deep learning libraries and currently only fully supports Python for training. It’s likely that most cloud, mobile and IoT applications will require some live backend system collecting data and training deep learning models based on the data either live or in batches.

The second part of the deep learning based AI models is the actual usage of the trained models for predictions and intelligent decision making. This can mean speech recognition on a handheld device or movie recommendations in a cloud app. This can mean steering a self driving car based on a video stream or running a chat bot on an iOS, Android and web app. These models will be everywhere. Executing the models is again numerically intensive task and requires support for special purpose libraries that compile the models to special purpose hardware such as GPUs. Because the models are trained with some specific frameworks (e.g. TensorFlow) the execution of the models requires that the execution system supports the same framework or otherwise the workflow would just be painful for the developers. The full support for such framework is not needed for the execution environment but purely support for running the pre-trained models. Typically models are trained in Python but the model execution can occur in Python or C/C++ or some other languages.

How does this relate to Kotlin? Right now JVM is not widely used for deep learning. As such Kotlin cannot currently become widely used as a language for building AI based systems. Because AI based systems will find their way to most everyday software applications, lack of JVM support for deep learning is a disadvantage to Kotlin as a language and an obstacle for wider adoption of the language several years from now. However at the same time there is an arising opportunity for Kotlin. Right now Python is the main language people use for developing deep learning models and deep learning based applications. There is no competition. There is no elegant strongly typed language in the data science space. Granted data scientists like dynamic languages but it’s a different thing playing around with deep learning models on your desktop than developing distributed production systems where dynamic typing becomes a disadvantage. I’d much rather prefer Kotlin as a language for such systems than Python.

With this is mind, how could Kotling become the language that bridges AI and deep learning with modern strongly typed programming. The easiest way would be probably make Kotlin to target Python or Python bytecode. That way the machine learning libraries would not need to be ported for JVM or some other target, something that is likely to not happen or lag behind. The second step would be having a Kotlin to compile to native (LLVM) into code that does not use garbage collector or that allows to not use garbage collector for certain real-time parts of the code. This becomes important when executing the deep learning models on an embedded platforms to perform real-time tasks. You don’t want your drone’s balancing model to stop executing to purge the memory (my friend tried this and it didn’t end up well for the drone). Or you don’t want the next generation of Siri to have pauses in the speech generation. Or you don’t want your self driving car to pause steering to clear the unused objects. Python is not a suitable target language for these real-time applications and most people use C/C++ or Objective C for those cases. For this reason there is a C APIs for deep learning frameworks like TensorFlow to execute pretrained models in embedded environments.

If Kotlin would target Python (source code or bytecode) and native (LLVM) it would have the opportunity to become the strongly typed language that everyone would prefer to use when developing their AI based production software applications allowing to cover both the training of such AI models in the cloud and execution of such models on in the cloud or embedded platforms.

8 Likes

Interesting.

I would also like to use kotlin for AI and deep learning.

The current work on Jetbrains kotlin native is an opportunity for the use cases you are describing. It would allow to use it on environments without JVM.

I’m not sure of the interest of targeting Python source code or byte code. Kotlin, with java ecosystem and great features like coroutines, should be enough to bring people to build or port some library code on it.

What do you think about deeplearning4j?

If you think of why Silicon Valley became the hot spot for software and technology, it’s because things were changing rapidly at the time when this happened and the ecosystem that had most support for doing these new things became the center of attention. The fact that things were moving fast in Silicon Valley attracted doers, innovators and investors all of which supported the exponential growth of Silicon Valley over other metropolitan area. Now that dominance is impossible to challenge or dispute unless new technological revolution comes and takes the spot. The same way Pyhon for some reason has become the defacto language for machine learning and deep learning engineers to build data models. Because of this all new frameworks and all AI startups tend to target Python first. This results in exponential growth of Python machine learning and deep learning support. The fact that things are changing rapidly and that no single library of framework can solve all the problems results in this exponential growth situation. There just isn’t such a strong ecosystem for Java in the machine learning space. JVM was shining in the big data space but hasn’t taken off similarly in the machine learning space.

So no I don’t think JVM can compete because of the nature of dynamics of the exponential growth phenomena. For JVM to catch up would require a massive corporate support similar to what Android had at the time when Apple came up with iOS. Otherwise JVM will stay behind for long.

To illustrate the situation let’s look at Google Trends comparison between TensorFlow and deeplearning4j. It’s not looking good for deeplearning4j. And even if deeplearning4j was catching up it wouldn’t be enough because you would lack all the other supporting libraries of today and all the libraries that are going to come up in the next two years that nobody knows about yet.

1 Like

did you use scala or java for AI and\or deep learning? if so, kotlin could fit in

Here is an observation. Tensorflow and most other “Python” deep learning frameworks are actually written in C++. Python is used just as a scripting language to “glue” various moving pieces together. Any other scripting language could be used instead, with the corresponding (relatively thin) bridge. Of course, the current momentum is on the Python side. If the past history on innovation and rise/fall of languages teaches us anything, the lesson we can learn is that it does not really matter who came first to the field and it does not even matter who is the leader now.

Kotlin is, without doubt, considerably more productive for any project of non-trivial size due to its static types and emphasis on toolability. Even at 10K+ Python LOCs you start to feel pains of a dynamic language. Python works nicely in slide-ware and in small code snippets of the kind you can put into iPython notebooks, where you can actually execute the code on your data and then enjoy code completion and integrated help on the actual, dynamically resolved object instances. As soon as you start writing the actual non-tirival code, abstracting it into modules, etc, it all starts to fail utterly – code completion and help becomes useless for any non-trivial framework even in state-of-the-art Python IDEs like PyCharm.

The first player in ML field that will realise that Python is roadblock to further scale will reap the benefits. All we can do in Kotlin team is to make sure that when this realisation comes, Kotlin is in good shape to serve as a viable alternative to be considered.

19 Likes

There is a project called Jython that attempts to bring Python to JVM. Another project called JyNI (Jython Native Interface), which is aiming to bring unmodified Python native packages to be callable from Jython. If you look at the list of packages JyNI is attempting to bring to Jython, it’s exactly the set of mathematical and scientific libraries we are talking about. I’ve not looked into the details but JyNI (if it works from any JVM language) or something similar for Kotlin might actually be a working solution.

1 Like

Here is an interesting article what Java needs to be good suited for Machine Learning. I think Kotlin also lack of these features.

I like kotlin but I think without ML Kotlin can’t be a major language in the future.

1 Like

I think @darksnake is working on a kotlin math library which contains numPy like features as well. I’m not sure however how far development is.

What I’ve see from looking through the article, the main problem the author has with java (or kotlin in our case) is that something like numPy is missing.

I’ve moved away from concept of numpy-clone and decided to created something more type-safe and kotlinish. And the niche is currently occupied by koma. There is ongoing discussion in slack #mathematics and #science channels. Currently it is a common consensus that we should join efforts to support one common API and basic implementation for mathematics in kotlin. The repository is here: KEEP-math.

As for latest news. kmath now shows slightly better performance for large nd-structures than numpy. Of course it requires a lot of additional work. And even more work needed to bring together existing solutions. All contributions to the KEEP and kmath or koma are welcome.

2 Likes

Can you link the discussions on slack please? I am interest as well for contributing :slight_smile:

This one for mathematics: Slack
This one for general science: Slack
And this one for data science: Slack

Right, for example in Apache Spark you can define some query in Python which is handed over to Spark that executes it by running Scala and Java code since it is written in those languages.

The general assumption that something written in C will be in general faster than something run on JVM is wrong. Yes, one can write super-optimized code in C, but the reality is that it runs super-fast if it is super-optimized for specific task. Nobody does that. There are limited number of optimized solutions which everyone are forced to use. It creates really ugly patterns which are not necessary good from performance point of view. For example, in numpy you are forced to work with matrices and basic operations even if you do not want to, otherwise the performance drops dramatically. I saw an example where one of my colleagues used a matrix comparison on single element instead of simple if (I am not sure it is has better performance, more probably he just took some ugly example from somewhere).

The simple example of case, where python approach is really bad is a function application to the ndarray. In python in order to perform something like a**2 + 2*a + 1, one needs to create few intermediate matrices. If the dimension is large, it is really a problem (one can use in-place operations, but hey have other problems). In kotlin you can just map it by function or event make lazy evaluation.

Yes, because the speed as in Spark/Hadoop and other systems comes from parallel execution with data being devided into buckets that are searched through in parallel. What matters is good understanding of network programming, data partitioning, etc. The speed of the programming language is not the primary factor. And Apache Spark would probably still be in development if developed using C oder C++ …

1 Like

Kotlin will never die. To enhance your learning with AI and Deep Learning kotlin will definitely will help you. To better learning and wants to make new learning in AI then professionals can go for AI Training Course.

Is it possible to be added to that slack workspace? I’m interested in the Data Science discussion

The form for registration is here: Kotlin Slack Sign-up

@tmaila and @elizarov - thanks for getting those points together!
I’d be interested to hear yours and everyone’s opinion on this. And can talk directly too.

Tensorflow and most other “Python” deep learning frameworks are actually written in C++. Python is used just as a scripting language to “glue” various moving pieces together. Any other scripting language could be used instead, with the corresponding (relatively thin) bridge. Of course, the current momentum is on the Python side.

If tensorflow and many other “Python” DL/AI frameworks are written in C/C++ and Python is just the most popular wrapper right now, what stops us from:

  1. Writing Kotlin wrappers instead for those C libs as is.
  2. If/when helpful - consider and gradually convert C libs to pure java/kotlin as things move forward, and as other comments here also mention, often C impl is faster than JVM for specialized cases, which also has disadvantages we can avoid.
  3. That means no Python in the loop at all is possible.
  4. jython (or other python full support bridging) or is less attractive as it’s an additional layer, but could be helpful to get things working soon and support all of python’s echosystem at once.

Would those be helpful for strengthening Kotlin in the AI/DL community?

You can already use TensorFlow from kotlin-jvm. See here for reference: Installer TensorFlow Java. Also you can use TensorFlow directly with kotlin-native: https://github.com/JetBrains/kotlin-native/tree/master/samples/tensorflow.

Pure kotlin probably won’t be ever as fast as optimized native version (the CPU time difference is not large, it is only about x1.5 - x2, but memory consumption is different). Currently, what we need is a good Kotlin API for all that. If you have some ideas about it, please join slack channels mentioned above and let us discuss it. Also you are welcome to contribute to kmath. I currently do not plan to use TensorFlow since I do not have experience with it, but integration with ND4J and deeplearning4j is planned. Probably we can use deeplearning4J for kotlin-jvm and TensorFlow for native.