Swift: Compiled Inference for Probabilistic Programming Languages

Yi Wu, Lei Li, Stuart J. Russell, Rastislav Bodík

Probabilistic Programming Languages (PPLs) are a dream for researchers: they allow you to define complex, "open-universe" models where the number of objects is unknown, and then let a generic inference engine do the heavy lifting of answering queries. However, the reality has often been a nightmare of slow execution. A new paper from researchers at UC Berkeley, Toutiao, and the University of Washington introduces Swift, a compiler that promises to close the performance gap between generic PPLs and hand-optimized code.

TL;DR

Swift is a high-performance compiler for the BLOG probabilistic programming language that generates model-specific C++ code. By eliminating runtime interpretation overhead and using a novel framework for incrementally updating dynamic dependencies, Swift achieves speedups of 12x to 326x over existing PPL engines.

What Problem Does This Paper Address?

The core promise of PPLs is "model once, infer anywhere." You write a program describing a generative process, and the engine uses algorithms like Markov Chain Monte Carlo (MCMC) to find the posterior distribution.

The bottleneck is interpretation overhead. Most PPL engines act like interpreters: for every single sampling step (and there are often millions), the engine must consult an internal data structure to figure out which variables depend on which others. This is particularly painful in "open-universe" models (like tracking an unknown number of aircraft) where the dependency structure changes constantly as new objects are "discovered" or removed. Existing compilers often miss optimization opportunities because they don't jointly analyze the model structure alongside the specific inference algorithm being used.

Methods

The authors introduce Swift, which takes a BLOG program and an inference algorithm (like Likelihood Weighting or Metropolis-Hastings) and spits out optimized C++ code. The "secret sauce" is a framework called FIDS (Framework for Incrementally updating Dynamic Slices).

Key technical components include:

  • Dynamic Backchaining (DB): A form of lazy evaluation. The generated code only samples a random variable when its value is actually needed to compute a query or evidence, skipping unnecessary work.
  • Reference Counting (RC): To handle open-universe models efficiently, Swift uses reference counting to track which variables are currently "active." When a variable is no longer needed by any other part of the model, it is automatically removed, keeping the "possible world" as small as possible.
  • Adaptive Contingency Updating (ACU): This is the most novel part. Instead of re-calculating the entire Markov blanket (the set of neighbors) for a variable every time its value changes, Swift uses static analysis to identify "switching variables." It then generates code that only updates the specific dependencies that could have changed, making the update process incredibly fast.
  • Lightweight Memoization: Swift uses static memory allocation and efficient C++ vectors to avoid the overhead of frequent dynamic memory allocation during sampling.

Datasets

The researchers evaluated Swift against a diverse set of benchmark models:

  • Classic Models: Burglary (simple Bayes net), Hurricane, and TrueSkill (player ranking).
  • Open-Universe Models: Urn-ball models (where the number of balls is unknown).
  • Clustering Models: 1D Gaussian Mixture Models (GMM) and Infinite GMMs ($\infty$-GMM).
  • Real-World Data: The MNIST dataset of handwritten digits, modeled using Probabilistic Principal Component Analysis (PPCA).

Key Insights and Ideas

  • Interpretation is the Enemy: The paper argues that the massive performance gap in PPLs isn't usually due to bad algorithms, but due to the "tax" of looking up model structures at runtime.
  • Incrementalism is Key: In MCMC, only one variable changes at a time. Swift’s insight is that the dependency structure usually only changes locally. By tracking these changes incrementally (via ACU), you can avoid global re-computations.
  • Static Analysis for Dynamic Models: Even though the model is dynamic, you can use static analysis at compile-time to figure out which parts of the program control the dependency structure, allowing the compiler to generate highly targeted update code.

Key Results

Swift's performance gains are dramatic across the board:

  • Speedups: Compared to established PPLs like BLOG (the original interpreter), Figaro, and Church, Swift was 12x to 326x faster.
  • Efficiency: In the Urn-ball model, Swift's reference counting and dependency tracking reduced the number of calls to the likelihood function by up to 81%.
  • Convergence: On the MNIST/PPCA task, Swift (using Gibbs sampling) generated visually meaningful principal components in just 5 seconds, whereas competitors like Infer.NET or Stan took significantly longer to reach similar levels of perplexity.
  • Hand-tuned Performance: In many cases, Swift-generated code performed nearly as well as code manually written and optimized by a human for a specific model.

Limitations and Future Work

While Swift is a major step forward, the authors note several areas for expansion:

  • Algorithm Support: Currently, Swift focuses on Likelihood Weighting, Metropolis-Hastings, and Gibbs sampling. The authors plan to extend it to Sequential Monte Carlo (SMC) and more advanced samplers like HMC.
  • Partial Evaluation: The compiler could be even smarter by using partial evaluation to simplify the program logic further before generating code.
  • Language Scope: While the principles are general, the current implementation is specific to the BLOG language. Applying these optimizations to other PPLs (like Stan or Pyro) would require significant engineering effort.

Summary updated

BibTeX
@inproceedings{wu2016swift,
  author = {Yi Wu and Lei Li and Stuart J. Russell and Rastislav Bodík},
  title = {{Swift: Compiled Inference for Probabilistic Programming Languages}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2016},
  url = {https://openreview.net/forum?id=Sk4s5mfObB},
}
Figure