Inferensys

Glossary

Automatic Differentiation

A computational technique for accurately and efficiently calculating the derivatives of a function specified by a computer program, the core engine enabling the training of neural network potentials on forces and energies.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
COMPUTATIONAL GRAPH THEORY

What is Automatic Differentiation?

Automatic differentiation (AD) is a family of techniques for numerically evaluating the exact derivative of a function specified by a computer program, distinct from both symbolic differentiation and numerical approximation.

Automatic differentiation is the algorithmic backbone of modern deep learning, computing exact gradients by applying the chain rule to elementary arithmetic operations within a program's execution trace. Unlike numerical differentiation, it introduces no truncation error, and unlike symbolic differentiation, it avoids expression swell by operating on intermediate values rather than mathematical formulas.

AD operates in two primary modes: forward mode, which propagates derivatives from inputs to outputs and is efficient for functions with few inputs, and reverse mode (backpropagation), which propagates adjoints from outputs to inputs and is optimal for scalar-valued functions with many parameters—precisely the scenario encountered when training neural network potentials on forces and energies.

CORE ENGINE OF NEURAL NETWORK POTENTIALS

Key Characteristics of Automatic Differentiation

Automatic differentiation (AD) is the algorithmic workhorse that computes exact derivatives of arbitrary computer functions, enabling the training of neural network potentials on quantum mechanical forces and energies without manual derivation or numerical approximation errors.

01

Forward-Mode AD

Computes derivatives by propagating tangent values alongside the primal computation from inputs to outputs. For a function with n inputs and m outputs, forward mode requires n passes to compute a full Jacobian.

  • Mechanism: Dual numbers carry both value and derivative component through each operation
  • Efficiency: Ideal when n < m (few inputs, many outputs)
  • Use case: Computing directional derivatives or Jacobian-vector products for sensitivity analysis in molecular property prediction
  • Memory cost: Proportional to the number of operations, not the number of inputs
O(n)
Time Complexity
n passes
Full Jacobian
02

Reverse-Mode AD (Backpropagation)

Computes derivatives by propagating adjoint values from outputs backward through the computation graph to inputs. This is the algorithm underlying backpropagation in deep learning frameworks like PyTorch and JAX.

  • Mechanism: Records a computational graph (tape) during forward pass, then traverses it in reverse
  • Efficiency: Ideal when n >> m (many inputs, few outputs) — exactly the case for neural network potentials where millions of parameters map to a single scalar energy
  • Memory cost: Must store all intermediate activations, leading to the checkpointing trade-off in large molecular dynamics simulations
  • Gradient of energy: Computes forces on all atoms in a single backward pass
O(m)
Time Complexity
1 pass
Full Gradient
03

Computational Graph Construction

AD operates on a directed acyclic graph (DAG) where nodes represent variables and edges represent functional dependencies. Each elementary operation (addition, multiplication, sin, exp) has a known derivative rule.

  • Dynamic graphs: PyTorch builds the graph on-the-fly during execution, enabling flexible control flow in equivariant neural networks
  • Static graphs: TensorFlow compiles the graph before execution, enabling aggressive operator fusion and memory optimization
  • Tracing vs. source transformation: Operator overloading intercepts each operation at runtime, while source-to-source AD rewrites the program before execution
  • Custom gradients: Users can register hand-derived derivatives for operations like spherical harmonics where automatic rules may be suboptimal
04

Exact Derivatives vs. Numerical Approximation

Unlike numerical differentiation (finite differences) which introduces truncation and round-off errors, AD computes derivatives to machine precision by applying the chain rule to elementary operations.

  • Numerical differentiation error: f'(x) ≈ [f(x+h) - f(x)]/h suffers from O(h) truncation error and catastrophic cancellation as h → 0
  • Symbolic differentiation: Produces exact expressions but suffers from expression swell — the derivative formula can be exponentially larger than the original function
  • AD advantage: Delivers exact derivatives with only a small constant factor overhead (typically 2-5x the original function cost)
  • Critical for force matching: Training neural network potentials requires accurate forces; numerical noise in gradients would corrupt the learned potential energy surface
05

Checkpointing and Memory Management

Reverse-mode AD stores all intermediate values during the forward pass, creating a memory bottleneck for deep networks and long molecular dynamics trajectories. Gradient checkpointing trades computation for memory.

  • Checkpointing strategy: Discard intermediate activations, then recompute them on-demand during the backward pass from stored checkpoints
  • Optimal schedule: The binomial checkpointing algorithm achieves O(log n) memory with O(n log n) recomputation
  • Application: Training equivariant neural networks on large molecular systems where storing per-atom features for thousands of atoms exceeds GPU memory
  • JAX's approach: Functional transforms like jax.checkpoint automatically apply checkpointing to arbitrary functions
06

Higher-Order Derivatives

AD can be applied recursively to compute Hessians (second derivatives) and higher-order tensors required for vibrational analysis and transition state optimization in quantum chemistry.

  • Forward-over-reverse: Combine forward and reverse modes to efficiently compute Hessian-vector products without materializing the full Hessian matrix
  • Application: Computing harmonic vibrational frequencies from a neural network potential by differentiating the energy gradient
  • Force constants: The Hessian of the potential energy with respect to atomic coordinates yields the force constant matrix for normal mode analysis
  • Framework support: JAX provides jax.hessian, PyTorch provides torch.autograd.functional.hessian
GRADIENT COMPUTATION COMPARISON

Automatic Differentiation vs. Other Methods

A comparison of automatic differentiation with numerical and symbolic differentiation for computing gradients in neural network potentials and quantum chemistry machine learning.

FeatureAutomatic DifferentiationNumerical DifferentiationSymbolic Differentiation

Computational complexity for gradient of scalar function with n inputs

O(1) cost per gradient evaluation (forward mode) or O(n) for reverse mode

O(n) function evaluations minimum

O(1) evaluation but expression size can grow exponentially

Numerical precision

Machine precision (no truncation error)

Subject to truncation and round-off errors; typically 10^-5 to 10^-8

Exact arithmetic (no approximation error)

Handles control flow (loops, conditionals)

Handles arbitrary code (black-box functions)

Expression swell (intermediate expression growth)

Requires source code access

Typical use in neural network training

Memory overhead for reverse mode

Proportional to number of operations in computation graph

Minimal

Can be exponential in expression complexity

AUTOMATIC DIFFERENTIATION

Frequently Asked Questions

Clear, technically precise answers to the most common questions about automatic differentiation, the computational engine that powers gradient-based optimization in neural network potentials and modern quantum chemistry.

Automatic differentiation (AD) is a computational technique that evaluates the exact derivative of a function specified by a computer program by systematically applying the chain rule to elementary arithmetic operations and functions. Unlike numerical differentiation, which approximates derivatives using finite differences and suffers from truncation and round-off errors, AD computes derivatives to machine precision. Unlike symbolic differentiation, which manipulates mathematical expressions to produce a closed-form derivative that can suffer from expression swell, AD operates directly on the program's computational graph, generating derivative code without an explosion in complexity. AD achieves this by decomposing any function, no matter how complex, into a sequence of elementary operations—addition, multiplication, sine, exponential—whose derivatives are trivially known, and then composing these via the chain rule in either the forward mode or reverse mode.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.