Abstract
C3 is a source-to-source transformation technique that brings massive performance gains to probabilistic programming by enabling incremental re-execution. By combining continuation-passing style (CPS) with intelligent callsite caching, it allows MCMC inference to "jump" directly to modified variables and "short-circuit" execution as soon as changes stop propagating, turning linear-time updates into constant-time ones for many common models.
Introduction
Probabilistic Programming Languages (PPLs) allow researchers to define complex statistical models as computer programs. To perform inference, systems often use Markov Chain Monte Carlo (MCMC) algorithms like Metropolis-Hastings (MH). The popular "Lightweight MH" framework is favored for its simplicity: it treats the PPL as a black box and re-runs the entire program every time it proposes a change to a single random variable.
However, in a program with thousands of variables, such as a long Hidden Markov Model (HMM), changing one variable at the end of the chain shouldn't require re-calculating everything from the start. C3 addresses this inefficiency by making re-execution incremental without requiring a complex, custom interpreter, allowing high-performance execution on standard runtimes like Google’s V8 JavaScript engine.
Methods: Continuations and Callsite Caching
C3 (Continuations and Callsite Caching) employs three main compile-time transformations to achieve efficiency:
- Continuation-Passing Style (CPS): The program is transformed so that every function receives a "continuation"—a function representing the remainder of the program. This allows the inference engine to start re-execution exactly at the point where a random choice was modified, rather than at the beginning of the script.
- Callsite Caching: Every function call is wrapped in a cache. If a function is called with the same inputs as a previous run, the system returns the cached result.
- Short-Circuiting (Early Termination): This is the core innovation. If a function finishes re-executing and its return value is identical to its previous value, C3 stops re-execution immediately, realizing that downstream values will not change.
To manage caching overhead, the authors implemented Automatic Cache Adaptation, which identifies and disables caching for functions that change too frequently to justify the cost.
Evaluation and Key Insights
The authors evaluated C3 using standard generative models—Hidden Markov Models (HMM), Latent Dirichlet Allocation (LDA), Gaussian Mixture Models (GMM), and Hierarchical Linear Regression (HLR)—as well as a complex inverse procedural modeling task for 3D graphics.
A key insight is the synergy of CPS and Caching. While CPS allows starting in the middle and caching allows skipping unchanged work, their combination enables early termination. This allows for "constant-time" updates, where the time to process a change is independent of the total model size. Additionally, the use of a "Write-Only" Query Table allows the program to record results without creating data dependencies that would break short-circuiting logic.
Key Results
C3 demonstrates significant performance improvements over traditional Lightweight MH across various benchmarks:
- Complexity Reduction: For HMMs, C3 reduced the runtime complexity of a proposal from $O(N)$ (linear in the number of observations) to $O(1)$ (constant time).
- Throughput Gains: On large-scale models, C3 delivered 20x to 100x speedups. In graphics applications, it was nearly an order of magnitude faster than the baseline.
Limitations and Future Work
Caching is not without cost; for very small or tightly coupled models, the overhead of cache lookups can make C3 slower than raw re-execution. The system also assumes function purity, meaning global variable mutations could lead to incorrect results. Future work includes using static analysis to reduce runtime cache checks and optimizing the CPS transform to apply only to code involving random choices.
Summary updated