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

Forward-Mode Automatic Differentiation in Julia

Jarrett Revels, Miles Lubin, Theodore PapamarkouarXiv 2016

Abstract

ForwardDiff is a high-performance Julia package that implements forward-mode automatic differentiation (AD) with speeds competitive with low-level languages like C++. By leveraging Julia’s Just-In-Time (JIT) compilation and a novel "chunk mode" strategy for stack-allocating dual numbers, the package enables efficient gradient, Jacobian, and Hessian calculations even for "AD-unaware" user code. This paper explores the implementation strategies that allow ForwardDiff to overcome traditional performance bottlenecks in high-level languages.

Introduction

Automatic Differentiation (AD) is a cornerstone of modern scientific computing and optimization. While reverse-mode AD is theoretically superior for high-dimensional gradients, it often introduces significant implementation overhead due to the need for "tapes" or graph construction. Traditional forward-mode AD is simpler but typically scales poorly as the number of input variables increases, primarily due to frequent heap allocations of derivative vectors in memory-managed languages.

ForwardDiff addresses these challenges by providing a tool that is both easy to use—transparently working with custom Julia types—and fast enough to compete with low-level implementations.

Methods

The core of ForwardDiff is the multidimensional dual number, which tracks both the value of a function and its derivative components. To optimize performance, the package employs several key strategies:

Chunk Mode and Stack Allocation

Instead of allocating a derivative vector equal to the full input dimension $k$, ForwardDiff breaks the computation into "chunks" of size $N$. This allows the derivative vectors to be stack-allocated rather than heap-allocated, drastically reducing memory management overhead. As shown in the performance benchmarks, the choice of chunk size $N$ is critical for balancing computational passes against memory bandwidth pressure.

Performance comparison of ForwardDiff vs. C++ across different chunk sizes for Rosenbrock and Ackley gradients.

Type Nesting and JIT Specialization

Higher-order derivatives, such as Hessians, are supported through the nesting of dual types (e.g., Dual{M, Dual{N, T}}). Because Julia uses JIT compilation, ForwardDiff generates specialized, highly optimized machine code for specific user functions at runtime. This ensures that even functions not originally written with AD in mind can be differentiated efficiently.

Key Results

Benchmarking vs. C++ and Python

The performance of ForwardDiff was evaluated using standard mathematical benchmarks: the Rosenbrock and Ackley functions. For an input size of $k=12,000$, ForwardDiff frequently outperformed naive C++ implementations.

Furthermore, when compared against Python's autograd (a reverse-mode AD package), ForwardDiff demonstrated superior performance for dimensions up to $k \le 10,000$. For example, at $k=1,000$ on the Ackley function, ForwardDiff was approximately 13x faster than autograd.

Benchmark results comparing ForwardDiff (single and multithreaded) against Python's autograd across varying input sizes.

Scaling and Multithreading

While forward-mode AD eventually yields to reverse-mode AD as dimensionality grows (typically around $k > 100,000$), ForwardDiff remains highly competitive for a wide range of practical problems. Experimental multithreading support has also shown a roughly 2x speedup using 4 threads, further extending the package's utility in high-performance contexts.

Conclusion

ForwardDiff demonstrates that a highly optimized forward-mode AD implementation can outperform reverse-mode alternatives for moderate-to-high dimensions by minimizing constant-factor overheads. Its integration into the Julia ecosystem, such as the JuMP modeling language, has already yielded significant performance gains in real-world optimization tasks. Future work will focus on SIMD vectorization and expanding support for complex linear algebra operations.

Summary updated

BibTeX
@misc{revels2016forward,
  author = {Jarrett Revels and Miles Lubin and Theodore Papamarkou},
  title = {{Forward-Mode Automatic Differentiation in Julia}},
  year = {2016},
  eprint = {1607.07892},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1607.07892},
}
Figure