r/rust • u/Rusty_devl std::{autodiff/offload/batching} • Nov 27 '24
Using std::autodiff to replace JAX
Hi, I'm happy to share that my group just published the first application using the experimental std::autodiff Rust module. https://github.com/ChemAI-Lab/molpipx/ Automatic Differentiation allows applying the chain rule from calculus to code to compute gradients/derivatives. We used it here because Python/JAX requires Just-In-Time (JIT) compilation to achieve good runtime performance, but the JIT times are unbearably slow. JIT times were unfortunately hours or even days in some configurations. Rust's autodiff can compile the equivalent Rust code in ~30 minutes, which of course still isn't great, but at least you only have to do it once and we're working on improving the compile times further. The Rust version is still more limited in features than the Python/JAX one, but once I fully upstreamed autodiff (The current two open PR's here https://github.com/rust-lang/rust/issues/124509, as well as some follow-up PRs) I will add some more features, benchmarks, and usage instructions.
82
u/bahwi Nov 27 '24
Very cool. But there's autodiff in std lib???!?! Crazy
31
u/Mr_Ahvar Nov 27 '24
Wow it’s insane how dumb I feel, either the doc is lacking or I just straight up don’t understand what autodiff is doing
16
u/v_0ver Nov 27 '24
I've been waiting for this for almost a year, now I can rewrite the code from Julia to Rust =)
6
u/hans_l Nov 28 '24
That's very cool work, but I was wondering why trying to get this into
stdrather than just a regular crate? Is there any advantages?5
u/_jbu Nov 28 '24
This looks fantastic. Autodiff will be a complete game changer for the work I do in robotics and autonomous systems (CBFs / CLFs, custom optimization algorithms, ML/RL, etc.). C++ has a library for this, but it has some limitations.
The documentation only shows examples for simple Rust arrays. Is support for linear algebra crates such as ndarray or algebra planned?
Also, are there plans to support custom frules / rrules like in ChainRules.jl? It would be very helpful to be able to make user-defined gradients (e.g. using the implicit function theorem)
2
u/global-gauge-field Nov 29 '24
Congrats on the project!!
Speaking of Jax, there are a few framework libraries built on top of Jax (e.g. for physics for simulation, conventional NN api, probabilistic computing). Do you need/envision such libraries for this project ?
8
u/Noxime Nov 27 '24
Woah, that is properly cool. Im not sure what to use it for, but being able to automatically differentiate is really cool conceptually.
4
u/mcoveri Nov 28 '24
Excellent. This will be a nice capability for rust scientific computing!
1
u/naequs Nov 28 '24
i didn't even know this was underway, geat work! maybe i am not understanding correctly but
DuplicateNoNeedbasically meansUpdateInPlace?1
u/sthornington Nov 30 '24
Very cool. How does it handle discontinuous/piecewise functions?
1
u/sthornington Dec 01 '24
note that to get this to build on MacOS I had to compile with:
export RUSTFLAGS="-L ~/git/enzyme/rust/build/aarch64-apple-darwin/enzyme/lib"
in order for it to find libEnzyme-19.dylib while building rustc_driver.
1
u/PackImportant5397 Mar 16 '25
wow this is insanity, pls keep posting more of this. Is this production ready ?