Abstract
NumPyro is a lightweight probabilistic programming library that brings Pyro’s flexible modeling power to the high-performance JAX ecosystem. By redesigning the No-U-Turn Sampler (NUTS) into an iterative format, the authors achieved end-to-end JIT compilation, resulting in inference speeds up to 340x faster than existing frameworks.
Introduction
Probabilistic Programming Languages (PPLs) allow researchers to define complex statistical models, but they often face a trade-off between flexibility and speed. Libraries like Pyro (built on PyTorch) offer great modeling abstractions but can struggle with the overhead of Python's dynamic nature during execution.
On the other hand, JAX provides incredible performance through Just-In-Time (JIT) compilation and hardware acceleration (GPU/TPU). However, JAX requires programs to be "purely functional" and has trouble tracing complex recursive algorithms. The core challenge addressed here is: How do we build a PPL that keeps the user-friendly, modular "effect handling" of Pyro while fully leveraging the raw speed of JAX?
Methods
The authors introduce NumPyro, which uses several novel approaches to bridge the gap between flexible modeling and high-performance execution.
Effect Handlers in JAX
They adapted Pyro’s "effect handlers" (like trace, seed, and condition) to work within JAX’s functional constraints. This allows users to write models using standard NumPy-like syntax while the library handles the complex logic of tracking random variables under the hood.
Iterative NUTS
Standard NUTS (the gold standard for Bayesian inference) relies on a recursive "tree-building" algorithm. JAX’s compiler (XLA) cannot efficiently trace deep recursion. The authors developed an iterative version of NUTS that replaces recursion with a loop-based approach. This allows the entire inference algorithm to be compiled into a single, highly optimized machine-code kernel.
Vectorization with vmap
Because NumPyro is built on JAX, it can use vmap to automatically vectorize subroutines. This makes tasks like running multiple MCMC chains in parallel or generating posterior predictive samples trivial and extremely fast.
Datasets and Benchmarks
The authors evaluated NumPyro using three distinct benchmarks:
- Hidden Markov Model (HMM): A synthetic dataset (600 data points) used to measure CPU overhead and the efficiency of differentiating through loops.
- Forest CoverType (COVTYPE): A large-scale dataset (over 500,000 data points, 54 features) used for logistic regression to test GPU acceleration and large-tensor performance.
- Sparse Kernel Interaction Model (SKIM): Synthetic datasets with varying dimensionality ($p$) to test how the framework scales with model complexity.
Key Insights
- The Power of Composability: JAX transformations (like
jitandvmap) and PPL effect handlers (liketrace) are perfectly composable. Wrapping a probabilistic model in a handler to record its state, then wrapping that in a JAX transform, enables massive parallelization on GPUs. - Iteration > Recursion for XLA: By rewriting NUTS to be iterative, the authors enabled "operator fusion." The XLA compiler can optimize memory movement between the CPU/GPU and cache, which is impossible with recursive calls.
- Functional PRNG Handling: NumPyro hides JAX's stateless, explicit random number generation via a
seedeffect handler, maintaining a clean modeling API while satisfying JAX's strict requirements.
Key Results
NumPyro demonstrated massive performance gains over established frameworks like Stan and the original PyTorch-based Pyro.
- HMM (CPU): NumPyro was approximately 340x faster than Pyro and 6x faster than Stan.
- Logistic Regression (GPU): On the large COVTYPE dataset, NumPyro was roughly 2x faster than Pyro, proving its efficiency in the large-data regime.
- SKIM: NumPyro showed consistently lower overhead than Stan as the dimensionality of the problem increased.
- Precision Flexibility: The framework allows users to easily switch between 32-bit and 64-bit precision; 32-bit precision often provides a better "speed-to-accuracy" trade-off for many models.
Limitations and Future Work
While the paper highlights NUTS, performance on other inference methods like Variational Inference is discussed less extensively. Additionally, iterative NUTS requires an array to store nodes ($O(\log N)$ memory), a departure from standard recursive implementations. Future directions include parallelizing Stochastic Variational Inference (SVI) across many particles using vmap.
Summary updated