Probabilistic Programming with Programmable Variational Inference

McCoy R. Becker, Alexander K. Lew, Xiaoyan Wang, Matin Ghavami, Mathieu Huot, Martin C. Rinard, Vikash K. Mansinghka · DOI

Introduction

While Probabilistic Programming Languages (PPLs) have significantly advanced Monte Carlo methods, support for Variational Inference (VI) has remained rigid. Most existing PPLs offer a "fixed menu" of variational objectives, such as the Evidence Lower Bound (ELBO), and standard gradient estimators like the reparameterization trick.

When researchers wish to explore novel objectives—such as hierarchical, nested, or importance-weighted variants—they are typically forced to manually derive complex gradients and hand-code estimators. This process is mathematically intensive, error-prone, and results in monolithic code where model logic and differentiation logic are inextricably tangled. This paper introduces a modular, programmable framework that treats the variational objective itself as a first-class program, allowing for rapid and safe experimentation with complex VI strategies.

The genjax.vi Framework

The authors propose a system, implemented as genjax.vi, grounded in three key pillars designed to decouple the inference strategy from the model logic.

1. Programmable Objectives

Instead of selecting from a library of pre-defined losses, users write objectives in a specialized "differentiable language." This language provides high-level constructs for taking expectations ($\mathbb{E}$), computing densities, and simulating traces from generative models.

2. Compositional Program Transformations

The core of the system relies on automated transformations that convert high-level probabilistic programs into executable gradient estimators:

  • sim{·}: Converts a generative model into a trace simulator.
  • density{·}: Converts a model into a differentiable density evaluator.
  • adev{·}: An extended version of the Automatic Differentiation of Expected Values (ADEV) algorithm. This transforms the user's objective program into an unbiased stochastic gradient estimator.
The modular architecture of the programmable variational inference framework, showing the flow from generative language to gradient estimator.

3. Formal Correctness

Unlike many PPL implementations, genjax.vi is grounded in formal semantics using simply-typed $\lambda$-calculi. This ensures that if a model and objective are well-typed, the generated gradient estimator is guaranteed to be unbiased by construction. Built on JAX, the system leverages GPU acceleration and high-performance reverse-mode automatic differentiation.

Key Insights and Expressivity

A central innovation is treating the variational objective as a program rather than a static formula. This allows the application of standard programming language techniques, such as type checking and automated transformations, to the inference process itself.

By decoupling the strategy from the model, users can change gradient estimation strategies (e.g., switching from REINFORCE to Measure-Valued Derivatives) via simple type annotations or primitive calls. This flexibility enables the construction of sophisticated variational approximations, such as those incorporating Importance Sampling or MCMC steps, which are often difficult to implement in standard PPLs.

Visualization of the system's performance on the Toy 3D Cone dataset, demonstrating its ability to handle complex hierarchical variational families.

Evaluation and Results

The system was evaluated on several benchmarks, including MNIST (for VAE variants), the Attend-Infer-Repeat (AIR) model for multi-object scenes, and a Toy 3D Cone dataset for hierarchical families.

  • Performance: genjax.vi is competitive with established PPLs like Pyro. For the AIR model, it achieved significantly faster per-epoch speeds.
  • Convergence: On the AIR model, a custom hybrid estimator enabled by the framework converged an order of magnitude faster than standard recommended estimators.
  • Efficiency: The automated gradient estimators introduce a minimal runtime overhead of only 3–10% compared to hand-coded, highly optimized JAX implementations.

Limitations and Future Work

Despite its strengths, the current system has a conservative type system that may reject safe programs using non-smooth functions like ReLU. Additionally, it does not yet automate gradient estimation for programs with discontinuous control flow based on learned parameters. While the system guarantees unbiased estimators, identifying the lowest-variance estimator within the newly accessible combinatorial space of options remains a challenge for the user. Future work aims to address these parametric discontinuities and reduce friction with host language features like native loops and mutation.

Summary updated

BibTeX
@article{becker2024probabilistic,
  author = {McCoy R. Becker and Alexander K. Lew and Xiaoyan Wang and Matin Ghavami and Mathieu Huot and Martin C. Rinard and Vikash K. Mansinghka},
  title = {{Probabilistic Programming with Programmable Variational Inference}},
  journal = {Proc. ACM Program. Lang.},
  year = {2024},
  doi = {10.1145/3656463},
}
Figure