Generating Efficient MCMC Kernels from Probabilistic Programs

Lingfeng Yang, Pat Hanrahan, Noah D. GoodmanAISTATS 2014

Introduction

Universal Probabilistic Programming Languages (PPLs) offer a powerful abstraction for defining complex models. However, this flexibility often comes with a significant "abstraction tax." Standard implementations of inference algorithms, such as Metropolis-Hastings MCMC, frequently re-execute entire programs and incur heavy "naming overhead" to track random variables. Consequently, high-performance probabilistic modeling often requires error-prone, hand-coded C++ kernels. This paper presents "Shred," a system that utilizes compiler techniques—specifically tracing and slicing—to automatically generate efficient MCMC kernels that rival hand-optimized code.

Methodology: Tracing and Slicing

The core innovation lies in treating probabilistic programs as code to be optimized. The system employs two primary techniques:

  1. Tracing: By running the program once and recording all operations, the system creates a "trace" that eliminates interpreter and naming overhead. Crucially, the system distinguishes between structural proposals (which alter control flow) and structure-preserving proposals (which only update values).
  2. Slicing: To avoid redundant calculations, the system uses program slicing to identify the minimal subset of the trace affected by a single variable change. This automates the discovery of "Markov Blankets," a task traditionally performed manually by researchers.
The system distinguishes between structural proposals, which require re-tracing, and structure-preserving proposals, which allow for incremental updates.

These optimized slices are then compiled into Static Single Assignment (SSA) form C++ code for direct execution during the MCMC loop.

Experimental Evaluation

The authors evaluated Shred on seven diverse machine learning models, including linear and hierarchical regression, mixture models (including infinite mixtures), topic models (LDA and HDP), and citation-matching. These benchmarks include "open-universe" scenarios where the number of variables is not fixed, testing the system's ability to handle dynamic program structures.

Results and Discussion

The experimental results demonstrate that automated optimization can achieve performance levels previously reserved for hand-coded implementations. Tracing alone yields an average 32x speedup over baseline Church implementations.

Performance comparison of Shred (slicing) vs. Tracing (a) and the associated compilation cost (b).

As shown in Figure 3, the addition of slicing (the Shred system) provides a further 5x average speedup over tracing, with some models like hierarchical regression seeing gains up to 600x. While slicing introduces a modest compilation overhead (averaging 1.25x vs. tracing), the resulting inference kernels are significantly faster. In several instances, Shred-generated kernels were competitive with or even outperformed hand-coded C++ kernels.

Conclusion

Shred demonstrates that the "abstraction tax" of universal PPLs can be largely mitigated through sophisticated compiler optimizations. By automating the derivation of efficient MCMC kernels, researchers can focus on model design without sacrificing performance. Future work includes implementing "hot path" detection to further optimize frequently executed program segments and exploring specialized hardware generation.

Summary updated

BibTeX
@inproceedings{yang2014generating,
  author = {Lingfeng Yang and Pat Hanrahan and Noah D. Goodman},
  title = {{Generating Efficient MCMC Kernels from Probabilistic Programs}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2014},
}
Figure