NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Instead of Rewriting Foreign Code for Machine Learning, Automatically Synthesize Fast Gradients

William S. Moses, Valentin ChuravyNeurIPS 2020

This paper introduces Enzyme, a high-performance automatic differentiation (AD) tool that operates as a compiler plugin. Unlike traditional tools that require rewriting code in specific machine learning frameworks, Enzyme synthesizes gradients directly from low-level intermediate code. This makes it possible to differentiate programs written in C, C++, Fortran, Rust, and more with state-of-the-art efficiency.

The Challenge of Differentiating "Foreign" Code

To train neural networks or perform Bayesian inference, gradients are essential. Currently, integrating a complex piece of code—like a physics simulator or a climate model—into a machine learning workflow presents two painful choices:

  1. Rewrite the entire codebase in a framework like PyTorch or TensorFlow.
  2. Use existing AD tools (like source-rewriting or operator overloading), which often require significant code changes, lack support for all language features, or produce inefficient gradients because they differentiate the code before the compiler can optimize it.

The authors identify this "rewriting" requirement as a quintessential challenge in scientific machine learning. Enzyme bridges this gap by providing a tool that operates directly on the low-level code (LLVM IR) produced by most modern compilers.

How Enzyme Works: AD on Optimized IR

Enzyme’s core innovation is performing Automatic Differentiation on optimized Intermediate Representation (IR).

Enzyme compilation pipeline vs. Reference

By running after standard compiler optimizations (like loop-invariant code motion), Enzyme differentiates a simplified, optimized version of the program. This "Optimization First" approach prevents the "gradient explosion" in complexity often seen when differentiating raw source code. The process involves three main stages:

  • Type Analysis: Low-level IR often loses high-level type information (e.g., a void* pointer could be anything). Enzyme uses an interprocedural fixed-point analysis to "re-discover" types, which is crucial since the gradient of a float is calculated differently than that of a double.
  • Activity Analysis: This determines which instructions actually contribute to the output. By identifying "inactive" instructions, Enzyme avoids generating unnecessary and costly gradient calculations.
  • Synthesis: Enzyme creates a "forward pass" (running the original code and caching necessary values) and a "reverse pass" (calculating the gradients using "adjoints"). It automatically manages "shadow memory" to store gradients and implements a sophisticated caching system.

Because Enzyme operates on LLVM IR, it is inherently language-agnostic. Any language with an LLVM frontend (like Clang for C/C++, or rustc for Rust) can be differentiated.

Key Results and Performance

The authors evaluated Enzyme using several high-performance computing and ML-focused benchmarks, including Microsoft’s ADBench (LSTM, GMM, Bundle Analysis), ODE Integrators (Euler, RK4), and mathematical simulations (FFT, Brusselator).

Performance comparison of Enzyme against Reference, Tapenade, and Adept
  • State-of-the-Art Speedup: Enzyme achieved a 4.2x geometric mean speedup over a "Reference" pipeline that performed AD before optimization. On the ADBench suite, Enzyme consistently outperformed or matched established tools like Tapenade (source-rewriting) and Adept (operator overloading).
  • Seamless Integration: The authors successfully integrated Enzyme with PyTorch and TensorFlow, allowing users to call C++ functions and receive gradients back as standard Python tensors.
  • Julia Support: Enzyme was also demonstrated within the Julia ecosystem, differentiating scalar code significantly faster than existing Julia-native AD tools like Zygote.

Limitations and Future Work

While powerful, Enzyme has some limitations. It requires the code to be statically analyzable and struggles with complex type unions or undefined memory copies. It also cannot differentiate code without available LLVM IR (e.g., pre-compiled closed-source libraries) and currently lacks support for C++ exception-handling instructions.

Future directions include extending Enzyme to support forward-mode AD, improving its checkpointing heuristics, and enhancing integration with GPU-parallel code generators.

Summary updated

BibTeX
@inproceedings{moses2020instead,
  author = {William S. Moses and Valentin Churavy},
  title = {{Instead of Rewriting Foreign Code for Machine Learning, Automatically Synthesize Fast Gradients}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2020},
}
Figure