SGMCMCJax: a lightweight JAX library for stochastic gradient Markov chain Monte Carlo algorithms

Jeremie Coullon, Christopher NemethJ. Open Source Softw 2022 · DBLP

Abstract

SGMCMCJax is an open-source Python library designed to facilitate high-performance Bayesian inference using stochastic gradient Markov chain Monte Carlo (SGMCMC) algorithms. Built on the JAX ecosystem, the library leverages automatic differentiation and XLA compilation to scale complex models to massive datasets. This paper describes the library's modular architecture, its integration with the broader JAX ecosystem, and its role in bridging the gap between research-focused prototyping and production-level performance.

Introduction

Bayesian inference provides a robust framework for quantifying uncertainty in machine learning models. However, traditional Markov Chain Monte Carlo (MCMC) methods often struggle with scalability, as they typically require evaluating the entire dataset at each iteration to compute the posterior distribution. Stochastic gradient MCMC (SGMCMC) addresses this bottleneck by utilizing data subsampling (mini-batches) to estimate gradients, making Bayesian methods viable for "big data" and deep learning architectures.

Despite the rise of JAX as a preferred framework for high-performance machine learning, the ecosystem initially lacked a dedicated, mature library for SGMCMC. Existing Probabilistic Programming Languages (PPLs) like NumPyro or PyMC often prioritize standard MCMC or variational inference, leaving a gap for researchers focused on scalable gradient-based sampling. SGMCMCJax was developed to fill this void, providing a lightweight and modular engine for SGMCMC.

Methodology and Architecture

SGMCMCJax is built entirely on JAX, which provides two fundamental advantages for Bayesian computation:

  1. Automatic Differentiation (AD): The library automatically calculates the gradients of log-posteriors required for SGMCMC samplers, removing the need for manual derivation and reducing implementation errors.
  2. XLA Compilation: By compiling Python code into highly optimized machine code, SGMCMCJax achieves performance levels competitive with C++ or Fortran, while remaining hardware-agnostic. This allows the same code to run on CPUs, GPUs, and TPUs with minimal changes.

Modular Framework

Unlike monolithic PPLs that attempt to provide a full modeling language, SGMCMCJax adopts a "building block" philosophy. It provides a suite of modular components for gradient estimation and numerical discretization. This design allows researchers to easily swap out components to test new diffusion types or stability techniques. The library supports a wide range of standard and state-of-the-art algorithms, including Stochastic Gradient Langevin Dynamics (SGLD) and Stochastic Gradient Hamiltonian Monte Carlo (SGHMC).

Bridging the "PPL Gap"

A key design choice in SGMCMCJax is the intentional absence of a proprietary modeling language. Instead, it acts as a high-performance sampling engine that can be used with models defined in raw JAX or other libraries like Flax. This makes it particularly effective for training Bayesian Neural Networks (BNNs). Practitioners can define a standard neural network in Flax and simply replace the traditional optimizer with an SGMCMCJax sampler to perform Bayesian training.

This research-first design encourages innovation. By exposing the underlying components rather than hiding them behind a high-level API, the library enables researchers to prototype and iterate on novel SGMCMC techniques more rapidly than in traditional, more rigid frameworks.

Performance and Impact

SGMCMCJax provides "very competitive speed performance" thanks to its XLA backend, which is essential for the high-dimensional parameter spaces found in BNNs. Its versatility and ease of use have led to immediate adoption in the academic community. Notably, the library has been integrated into major educational resources, including the code accompanying Kevin Murphy’s influential textbook, Machine Learning: A Probabilistic Perspective.

Limitations and Future Directions

While the lack of a modeling language is a strength for modularity, it presents a steeper learning curve for users who are not comfortable defining likelihoods and priors manually in JAX. Furthermore, as a software-focused introduction, this work focuses on architecture rather than exhaustive benchmarking against non-JAX frameworks like R's sgmcmc or TensorFlow-based tools.

Future work will involve the community-driven addition of novel SGMCMC samplers as they are developed in the literature. The modular nature of the library ensures that as the field of stochastic gradient inference evolves, SGMCMCJax can evolve alongside it.

Summary updated

BibTeX
@article{coullon2022sgmcmcjax,
  author = {Jeremie Coullon and Christopher Nemeth and Jeremie Coullon},
  title = {{SGMCMCJax: a lightweight JAX library for stochastic gradient Markov chain Monte Carlo algorithms}},
  journal = {J. Open Source Softw.},
  year = {2022},
  doi = {10.21105/joss.04113},
}
Figure