Automatic Alignment in Higher-Order Probabilistic Programming Languages

Daniel Lundén, Gizem Çaylak, Fredrik Ronquist, David Broman

Probabilistic Programming Languages (PPLs) have revolutionized the way researchers build and reason about complex stochastic models, ranging from phylogenetic trees to aircraft localization systems. However, the performance of these languages often hinges on the efficiency of underlying inference algorithms like Sequential Monte Carlo (SMC) and Markov chain Monte Carlo (MCMC). A significant bottleneck in these algorithms is the challenge of "alignment"—ensuring that different execution traces of a program synchronize at the same logical points.

Presented at ESOP 2023, this work introduces a static analysis technique to automatically identify aligned execution points in higher-order probabilistic programs. By leveraging this "automatic alignment," the authors developed optimized versions of SMC and MCMC that achieve up to 7x speedups and vastly improved accuracy without requiring manual code modifications.

The Challenge of Stochastic Branching

In PPLs, inference engines rely on checkpoints. In SMC, these are resampling points where the algorithm prunes unlikely simulations. In MCMC, checkpoints are random draws that the algorithm attempts to reuse across iterations to minimize computation.

The primary obstacle is stochastic branching. When a program's control flow depends on a random value (e.g., an if statement based on a sample), different execution paths will hit different checkpoints in varying orders. This lack of alignment forces SMC to resample inefficiently and requires MCMC to maintain complex runtime databases to track random draws. Traditionally, developers had to manually rewrite models to ensure alignment, a process that is both error-prone and labor-intensive.

Static Analysis for Automatic Alignment

The researchers proposed a formal definition of alignment and a static analysis tool based on 0-CFA (Control Flow Analysis) to detect it automatically.

  1. Stochastic Tainting: The analysis tracks "stochasticity" throughout the program. It identifies variables "tainted" by random samples and marks branches influenced by these variables as "stochastic branches."
  2. Identifying Aligned Points: The tool identifies "checkpoints" (likelihood updates or random draws) that are guaranteed to occur in the same order across all possible executions, regardless of stochastic branching.
Visualization of aligning 'assume' statements (random draws) across two execution traces, s1 and s2. Shaded boxes indicate aligned points identified by the analysis.

The "aligned" versions of the algorithms leverage these synchronization points:

  • Aligned SMC: Resamples only at likelihood updates guaranteed to be aligned, ensuring all "particles" are at the same execution stage.
  • Aligned Lightweight MCMC: Uses aligned draws as anchors, allowing the algorithm to match and reuse random samples through simple counting rather than expensive runtime lookups.

Performance and Accuracy Gains

The approach was evaluated on several complex models, including Constant Rate Birth-Death (CRBD) and Cladogenetic Diversification Rate Shift (ClaDS) models from phylogenetics, as well as Latent Dirichlet Allocation (LDA) for topic modeling.

The results demonstrate substantial improvements:

  • Execution Speed: Aligned SMC was 2x faster for CRBD and nearly 7x faster for ClaDS. Aligned MCMC achieved a 3x speedup on the LDA model.
  • Inference Accuracy: For the CRBD model, Aligned SMC converged with 10,000 particles, while the standard unaligned version failed to converge even with 1,000,000 particles.
  • Robustness: In the ClaDS experiment, the unaligned version frequently crashed due to stack memory exhaustion, whereas the aligned version remained stable.

The static analysis itself is highly efficient, adding only 5ms to 30ms to the total compilation time, making it a practical addition to any PPL compiler pipeline.

Conclusion

By treating alignment as a compiler-level optimization problem, this research removes a significant barrier to the performance of higher-order PPLs. While the current implementation uses context-insensitive 0-CFA, the foundational framework for automatic alignment paves the way for more sophisticated analyses and broader language support in the future.

Summary updated

BibTeX
@inproceedings{lunden2023automatic,
  author = {Daniel Lundén and Gizem Çaylak and Fredrik Ronquist and David Broman},
  title = {{Automatic Alignment in Higher-Order Probabilistic Programming Languages}},
  booktitle = {ESOP},
  year = {2023},
  doi = {10.1007/978-3-031-30044-8\_20},
}
Figure