in ,

Swift: Google’s bet on differentiable programming, Hacker News

Swift: Google’s bet on differentiable programming, Hacker News
Read time: 82 minutes              

Two years ago, a small team at Google started working on making Swift the first mainstream language with

first-class language-integrated differentiable programming

capabilities. The scope and initial results of the project have been remarkable, and general public usability is not very far off. Despite this, the project hasn’t received a lot of interest in the machine learning community and remains unknown to most practitioners. This can be attributed in part to the choice of language, which has largely been met with confusion and indifference, as Swift has almost no presence in the data science ecosystem and has mainly been used for building iOS apps.

This is unfortunate though, as even a cursory glance at Google’s project will show that it’s a massive and ambitious undertaking, which could establish Swift as a key player in the area. Furthermore, even though we mainly work with Python at Tryolabs, we think that choosing Swift was a superb idea, and decided to write this (short ™ post to help spread the word about Google’s plans.

But before we get into Swift and what the term differentiable programming actually means, we should first review the current state of affairs. due to the popularity of the post,      We will be hosting a Swift for ML live webinar.      Register to be notified when date & time are confirmed.      Be updated

What is wrong with you, Python?!

Python is by far the most used language in machine learning, and Google has a ton of machine learning libraries and tools written in it. So, why Swift? What’s wrong with Python?

To put it bluntly, Python is

slow

. Also, Python is

not great for parallelism .

To get around these facts, most machine learning projects run their compute-intensive algorithms via libraries written in C / C / Fortran / CUDA, and use Python to glue the different low-level operations together. For the most part, this has worked really well, but as with all abstractions, it can create some problems. Let’s go over some of those. External binaries

Calling external binaries for every compute-intensive operation limits developers to working on a small portion of the algorithm’s surface area. Writing a custom way to perform convolutions, for example, becomes off limits unless the developer is willing to step down into a language like C. Most programmers choose not to do so, either because they have no experience with writing low level performant code, or because jumping back and forth between Python’s development environment and some low level language’s environment becomes too cumbersome to justify.

This leads to the unfortunate situation in which programmers are motivated to write the least amount of sophisticated code they can, and default to calling external library operations. This is the opposite of what’s desirable in an area as dynamic as machine learning, where so much is still not settled, and new ideas are very much needed.

Library abstractions

Having your Python code call lower level code is not as easy as mapping Python’s functions to C functions. The unfortunate reality is that the creators of machine learning libraries have had to make certain development choices in the name of performance, and that has complicated matters a bit. For example, in Tensorflow graph mode , which is the only performant mode in the library, your Python code doesn’t generally run when you think it would. Python actually acts as a sort of metaprogramming language for the underlying Tensorflow graph.

The development flow is as follows: the developer first defines the network using Python, then the TensorFlow backend uses this definition to build the network and compile it into a blob whose internals the developer can no longer access. After compilation, the network is finally ready to run, and the developer can start feeding it data for training / inference jobs. This way of working makes debugging quite hard, as you can’t use Python to dig into what’s happening inside your network as it runs. You can’t use something like pdb . Even if you wish to engage in good old print debugging, you’ll have to use tf.print and build a print node into your network , which has to connect to another node in your network, and be compiled before anything can be printed.

More straightforward solutions exist, though. In PyTorch [15, 10] , your code runs imperatively as is expected in Python, and the only non transparent abstraction is that the operations that run on the GPU execute asynchronously. This is generally a non-issue as PyTorch is smart about this and waits for all the async calls that are dependencies of any user interactive operations to finish before ceding control. Still, this is something to keep in mind, especially with things such as benchmarking.

Industry lag

All these usability problems aren't just making it more difficult to write code, they are unnecessarily causing the industry to lag behind academia

. There have been several papers that tweak low level operations used in neural networks, introducing improvements in accuracy of a few percentage points in the process, and have still taken a long time for the industry to adopt.

One reason for this is that even though these algorithmic changes tend to be quite simple themselves, the tooling problems mentioned above make them extremely difficult to implement. Hence, they may not be deemed worth the effort for what often results in only a 1% improvement in accuracy. This is especially problematic for small machine learning dev shops that usually lack the economies of scale to justify paying for their implementation / integration.

Therefore, companies tend to ignore these improvements until they get added to a library like PyTorch or TensorFlow. This saves them the implementation and integration costs, but it also causes industry to lag behind academia by 1 or 2 years, as the library maintainers can't be expected to immediately implement the findings of every new paper that is published.

One concrete example of this issue are Deformable Convolutions

, which seem to improve the performance of most Convolutional Neural Networks (CNNs). An open source implementation appeared about 2 years ago. Nevertheless, the implementation was cumbersome to integrate into PyTorch / TensorFlow and the algorithm didn’t gain widespread use. Only just recently has PyTorch added support for it, and as of yet I am not aware of there being an official TensorFlow version.

Now, let’s say this happens for several papers that each contribute a performance enhancement of 2%; the industry could be

missing out on significant accuracy improvements

of 1. (^ n%) for no reason other than inadequate tooling. This is regrettable, considering the n

could be quite high.

Speed ​​

Using Python fast libraries can still be slow in some cases. Yes, for CNNs running classification on images, using Python and PyTorch / TensorFlow will be really fast. What’s more, there is probably not much performance to be gained by coding your whole network in CUDA, as most of the inference time is spent on big convolutions that are already running in well-optimized implementations. This isn’t always the case though.

Networks that consist of many small operations are often the most susceptible to taking

performance hits

, if they are not fully implemented in a low level language. As an example, in a blogpost in which he professes his love for using Swift for deep learning, (Fast.AI) (s) Jeremy Howard reports that despite using PyTorch’s great JIT compiler, he still couldn ‘ t make a particular RNN work as fast as a version completely implemented in pure CUDA.

Furthermore, Python is not a very good language for cases where

latency

is important, nor for very

low level tasks

such as communicating with sensors. The way some companies choose to get around this is to start by developing their models in PyTorch / TensorFlow-Python. In this way, they take advantage of Python’s ease of use when experimenting with and training new models. After this, they rewrite their model in C for production purposes. I’m not sure if they rewrite it completely, or if they simply serialize it using PyTorch’s tracing functionality or TensorFlow’s graph mode, and then rewrite the Python code around it in C . Either way, a lot of Python code would need to be rewritten, which oftentimes is too costly for small companies to do.

All these problems are well known. Yann LeCun , who is widely considered one of the godfathers of deep learning, has stated that there is a need for a new machine learning language. In a thread PyTorch co-creator Soumith Chintala and him discussed several languages ​​as possible candidates, with Julia, Swift, and even improving Python being mentioned. On the other hand, Fast AI’s Jeremy Howard seems to be decidedly on the Swift train. Google accepts the challenge

Lucky for us, Google’s Swift for Tensorflow

(S4TF) team took the matter into their own hands. What’s even better, they have been remarkably transparent about the whole process. In an extremely thorough document , they detail the journey that got them to this decision, explaining what languages ​​they considered for the task and why they ended up using Swift.

Most notably, they considered:

(Go) : In the document, they state that Go relies too heavily on the dynamic dispatching that its interfaces provide, and that they would have had to make big changes to the language in order to implement the features they wanted. This goes against Go’s philosophy of being simple and having a small surface area. Conversely, Swift’s protocols & extensions afford a lot of freedom in terms of how static you want your dispatch to be. Also, the language is quite complex (and getting more complex every day , so making it a bit more complex to accommodate the features that Google wanted wouldn’t pose as big of a problem.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Ridiculous Nintendo Switch Lawsuit to Ban U.S Imports Goes Full Stupid, Crypto Coins News

Ridiculous Nintendo Switch Lawsuit to Ban U.S Imports Goes Full Stupid, Crypto Coins News

Anthony Di Iorio takes on Charlie Lee in “Crypto vs COVID”