DEV Community

Cover image for Why Rust Is the Optimal Choice for Deep Learning, and How to Start Your Journey with the Burn Deep Learning Framework
Louis Fortier-Dubois
Louis Fortier-Dubois

Posted on

Why Rust Is the Optimal Choice for Deep Learning, and How to Start Your Journey with the Burn Deep Learning Framework

The comprehensive, open-source deep learning framework in Rust, Burn, has recently undergone significant advancements in its latest release, highlighted by the addition of The Burn Book 🔥. There has never been a better moment to embark on your deep learning journey with Rust, as this book will guide you through your initial project, providing extensive explanations and links to relevant resources.

Whether you're a newcomer to deep learning or have experience with frameworks like PyTorch, transitioning to Burn promises to spare you numerous headaches, both when initially learning the tool and when the time will come to deploy your models in the near future.

Why switch to Rust?

It would be unwise to overlook how much the Python programming language and its ecosystem have helped deep learning grow as it did. Its high-level abstractions and low barrier of entry have assisted researchers in developing their ideas quickly, disregarding implementation and performance details that can often slow things down.

Today, the field of deep learning is full of extremely complex models, relying on gigantic computations. While Python is practical for prototyping, alone, it is too slow to handle such workloads. The mainstream solution has been, to this day, to rely on bindings to low-level programming languages such as C/C++.

This solution has certainly proved its worth, as the most widespread active model today, ChatGPT, was programmed exactly that way. However, this divides the deep learning ecosystems into two parties: researchers and optimization engineers, both working in two separate worlds with a small interface. While any high-level language is too slow for such computations, low-level languages are too detail-sensitive to be used for such complex tasks.

Enter Rust 🦀.

Its approach to abstractions makes it versatile enough to tackle this dichotomy. Indeed, thanks to the borrow-checker, which prevents the programmer from using a variable without explicitly stating if it can be changed or just looked at, Rust is able to provide high-level abstractions for concurrent programming and memory safety guarantees without incurring any runtime overhead.

Rust also comes with the Cargo package manager, which makes it incredibly easy to build, test, and deploy. The latter is usually painstaking in a Python environment.

A word on the difficulty of learning Rust: It is commonly stated that Rust is hard to learn, but that its compiler can save you from encountering obscure bugs in the long run. It is true that, compared to other languages, it might take longer in the beginning to get your code to compile, often because of the aforementioned borrow-checker. Although this and other mechanics may feel like complications at first, they result in the programmer dialoguing with the compiler to converge on a solution that, once it compiles, the programmer can almost trust blindfolded.

One example of the borrow-checker being useful in deep learning is for tensor manipulations, such as determining whether an operation should be done in place or not. Rust abstractions can also be used to encapsulate backend computations, enabling them to run efficiently on any machine. With the capacity to delve deep into low-level computations while maintaining high-level abstractions, Rust has everything it takes to become the go-to language for deep learning.

The Burn Deep Learning Framework

These are some motivations that led to the creation of Burn. This deep learning framework leverages Rust's advantages, with an innovative flexible architecture, to be used from training to inference and from prototyping to deployment. It represents a novel approach, placing significant emphasis on making well-considered design decisions to support exceptional flexibility, increased performance, and an outstanding developer experience.

Burn is incredibly easy to use, even to those not yet well-acquainted with Rust. Now equipped with its comprehensive book, it should be very easy to jump from the messy Python / (C/C++) dichotomy present in frameworks such as TensorFlow and PyTorch, to the harmonized, Rusty vision of Burn.

The Burn Book 🔥 starts with a presentation of a basic workflow that will lead you from training a model to deploying it for inference. It is a short 5-page read, filled with code snippets, that should be enough to start using Burn in the most typical deep learning scenarios.

As for the subsequent sections, they delve into the details of everything that one would expect from a deep learning library (modules, tensors, learning algorithms and metrics, compatibility with other deep learning ecosystems...) and, especially, what sets Burn apart from other frameworks. Take, for example, the Backend trait, which allows flexibility over different low-level implementations, for optimal computations depending on your machine. Or the backend extensions, which are decorators over the backend trait, the most remarkable being the Autodifferentiation backend, which automatically enables the autodiff functionality on any chosen backend.

With the addition of the book, which simplifies the onboarding process, Burn is now unquestionably the main entry point to the field of deep learning in Rust, which promises to be the language of the next generation of deep learning.

Top comments (3)

baba_ali profile image
Baba-Ali AbdulAziz

Amazing article. I've been learning Rust for some time now, and it's encouraging to see tools for deep learning in the ecosystem. Definitely checking Burn out for sure.

nazaroni profile image

same here... I like Python, but it's "rusty time!!!" :D

asynchio profile image
Joseph Jacks

Awesome post!