Skip to main content
QUICK REVIEW

[Paper Review] Instead of Rewriting Foreign Code for Machine Learning, Automatically Synthesize Fast Gradients

William S. Moses, Valentin Churavy|arXiv (Cornell University)|Oct 4, 2020
Parallel Computing and Optimization TechniquesComputer Science49 references10 citations
TL;DR

This paper introduces Enzyme, a high-performance automatic differentiation (AD) compiler plugin for LLVM that synthesizes fast gradients directly from optimized LLVM intermediate representation (IR), enabling efficient, language-agnostic AD without rewriting foreign code. By performing AD after compiler optimizations, Enzyme achieves a 4.5x geometric mean speedup over traditional AD on unoptimized IR, outperforming state-of-the-art tools on ML benchmarks.

ABSTRACT

Applying differentiable programming techniques and machine learning algorithms to foreign programs requires developers to either rewrite their code in a machine learning framework, or otherwise provide derivatives of the foreign code. This paper presents Enzyme, a high-performance automatic differentiation (AD) compiler plugin for the LLVM compiler framework capable of synthesizing gradients of statically analyzable programs expressed in the LLVM intermediate representation (IR). Enzyme synthesizes gradients for programs written in any language whose compiler targets LLVM IR including C, C++, Fortran, Julia, Rust, Swift, MLIR, etc., thereby providing native AD capabilities in these languages. Unlike traditional source-to-source and operator-overloading tools, Enzyme performs AD on optimized IR. On a machine-learning focused benchmark suite including Microsoft's ADBench, AD on optimized IR achieves a geometric mean speedup of 4.5x over AD on IR before optimization allowing Enzyme to achieve state-of-the-art performance. Packaging Enzyme for PyTorch and TensorFlow provides convenient access to gradients of foreign code with state-of-the art performance, enabling foreign code to be directly incorporated into existing machine learning workflows.

Motivation & Objective

  • To eliminate the need to rewrite foreign code in machine learning frameworks by enabling native, high-performance automatic differentiation.
  • To address the performance bottleneck caused by applying AD before compiler optimizations, which can lead to inefficient gradient code.
  • To enable cross-language and cross-framework differentiation of pre-compiled or third-party libraries without source access.
  • To demonstrate that efficient AD is feasible on low-level, optimized IR, challenging the assumption that AD is only effective in high-level languages.
  • To provide a seamless integration path for scientific computing and simulation code into modern machine learning workflows.

Proposed method

  • Enzyme operates as a plugin within the LLVM compiler framework, performing reverse-mode automatic differentiation directly on optimized LLVM IR.
  • It leverages LLVM’s full suite of optimizations—including loop-invariant code motion—before applying AD, preserving performance gains.
  • The system synthesizes gradients by analyzing the dataflow and control flow of the optimized IR, generating efficient adjoint code.
  • Enzyme uses a recursive tape structure for gradient computation, reducing memory overhead compared to stack-based approaches.
  • It supports multiple languages targeting LLVM IR, including C, C++, Fortran, Julia, Rust, and Swift, via a unified IR-based differentiation pipeline.
  • The framework integrates with PyTorch and TensorFlow, enabling direct differentiation of foreign code in existing ML workflows.

Experimental results

Research questions

  • RQ1Can automatic differentiation be efficiently performed on optimized LLVM IR, rather than unoptimized source or IR?
  • RQ2Does performing AD after optimization lead to significantly faster gradient computation compared to traditional AD on unoptimized code?
  • RQ3Can a compiler plugin-based AD system achieve state-of-the-art performance across diverse ML and scientific computing workloads?
  • RQ4To what extent can Enzyme enable seamless integration of foreign code—such as physics simulators or game engines—into machine learning pipelines without source modification?
  • RQ5Is it feasible to build a high-performance, cross-language AD system that operates at the IR level and supports advanced features like GPU code generation?

Key findings

  • Enzyme achieves a 4.5x geometric mean speedup over AD on unoptimized IR (the Reference pipeline) across a diverse benchmark suite, including Microsoft’s ADBench.
  • The performance gain is primarily due to post-optimization AD, which allows optimizations like loop-invariant code motion to eliminate redundant computations in gradients.
  • Enzyme outperforms the two fastest C/C++ AD tools evaluated in ADBench on all benchmarks, demonstrating state-of-the-art performance.
  • The recursive tape structure used by Enzyme reduces memory overhead compared to stack-based tapes, contributing to superior performance on FFT and Bruss benchmarks.
  • Enzyme enables direct differentiation of precompiled libraries and header-only code, which are infeasible for source-to-source AD tools.
  • The framework successfully differentiates complex scientific workloads such as Euler and RK4 solvers, LSTM, and GMMs, with performance competitive with or better than specialized tools.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.