JAX MD: A Framework for Differentiable Physics

Samuel S. Schoenholz, Ekin Dogus CubukNeurIPS 2020

In the physical sciences, there is a growing "efficiency gap" between traditional simulation tools and modern machine learning. Classical molecular dynamics (MD) packages like LAMMPS or OpenMM are highly optimized for speed but are notoriously difficult to integrate with deep learning libraries. Conversely, while researchers have developed incredibly accurate neural network models for quantum mechanical energies, incorporating them into a simulation usually requires building custom, brittle software bridges.

To address this, researchers have introduced JAX MD, a new software framework that brings the power of automatic differentiation and hardware acceleration to molecular dynamics. By treating entire physics simulations as differentiable functions, it allows researchers to seamlessly integrate neural networks into physical models and optimize complex systems end-to-end.

A Functional Approach to Physics

Built on top of JAX (Google’s framework for high-performance numerical computing), JAX MD departs from the traditional Object-Oriented Programming (OOP) style of physics software. Instead, it adopts a functional, data-driven approach:

  • Functional Primitives: Instead of complex class hierarchies, JAX MD uses lightweight functions to define "Spaces" (boundary conditions like periodic boxes) and "Interactions" (energy potentials).
  • Automatic Differentiation: The library automatically computes forces as the negative gradient of energy functions using jax.grad.
  • Just-In-Time (JIT) Compilation: Using the XLA (Accelerated Linear Algebra) compiler, JAX MD converts Python code into highly optimized kernels for CPUs, GPUs, and TPUs.
  • Vectorization: The library uses vmap to easily scale simulations from a single pair of particles to hundreds of thousands without manual looping.
  • Spatial Partitioning: It includes optimized neighbor lists and cell lists, essential for scaling simulations to large numbers of particles by only calculating interactions between nearby neighbors.

Machine Learning Accuracy

To demonstrate the library's utility in training machine learning potentials, the authors utilized a dataset of 50,000 configurations of 64 silicon atoms. Generated using high-fidelity Density Functional Theory (DFT) simulations at various temperatures, this dataset serves as the ground truth for training neural networks to predict atomic energies and forces.

Energy prediction accuracy of the Graph Neural Network on the Silicon dataset.

Using JAX MD, a Graph Neural Network (GNN) was implemented and trained on this dataset. The model achieved a root-mean-squared error of only 2.8 meV/atom on the silicon test set, demonstrating that the framework can accurately capture complex quantum mechanical interactions and seamlessly integrate them into the simulation environment.

Meta-Optimization and Key Insights

The most profound shift enabled by JAX MD is treating an entire simulation trajectory as a differentiable function. This allows for "meta-optimization"—differentiating through a minimization process to find which physical parameters lead to specific macroscopic states.

Differentiating through energy minimization to find the jammed state in a sphere-packing simulation.

In a sphere-packing vignette, the authors successfully used gradients to identify the "point of maximum disorder" (the jammed state) by differentiating directly through the energy minimization code. Furthermore, by avoiding internal state and classes, JAX MD makes it trivial to apply JAX’s powerful transformations, allowing researchers to instantly vectorize a function to simulate thousands of systems in parallel. The primitives are also general enough to model non-physical systems, such as flocking behaviors in biological agents.

Performance and Limitations

JAX MD offers competitive performance, particularly on hardware accelerators. On small systems (N=216 particles) using a GPU, JAX MD is actually faster than traditional packages like LAMMPS. On larger systems (N=64k), it incurs a roughly 2.4x slowdown compared to production-grade CUDA kernels—a respectable trade-off given the added flexibility of automatic differentiation.

However, the framework does face some limitations:

  • XLA Constraints: XLA requires array shapes to be statically known at compile-time. This complicates neighbor lists, where the number of neighbors for a particle changes dynamically. JAX MD works around this with padding, which can reduce efficiency.
  • CPU/TPU Scaling: While JAX MD excels on GPUs, it currently struggles to scale to very large systems on CPUs and TPUs due to how these architectures handle the irregular memory access patterns common in neighbor lists.
  • Precision: TPUs have limited support for 64-bit floating-point arithmetic, which is often required for high-precision physics simulations.

Future work will focus on closing the performance gap on CPU and TPU architectures and expanding the library of pre-built neural network architectures, further solidifying JAX MD as a versatile tool for differentiable physics.

Summary updated

BibTeX
@inproceedings{schoenholz2020jax,
  author = {Samuel S. Schoenholz and Ekin Dogus Cubuk},
  title = {{JAX MD: A Framework for Differentiable Physics}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2020},
}
Figure