Optimising Density Computations in Probabilistic Programs via Automatic Loop Vectorisation

Sangho Lim, Hyoungjin Lim, Wonyeol Lee, Xavier Rival, Hongseok Yang

TL;DR

This paper introduces a fully automatic loop vectorization method for probabilistic programming languages (PPLs) to accelerate slow density and gradient computations during inference. By combining speculative parallel execution with a fixed-point check, the approach successfully parallelizes complex loops containing nested structures and data-dependent control flow. Implemented on top of the Pyro PPL, it achieves up to $6\times$ speedups over existing vectorization baselines while significantly reducing GPU memory overhead.

What Problem Does This Paper Address?

Probabilistic programming languages (PPLs) like Pyro and Stan allow developers to build complex statistical models easily, but running inference on them (e.g., estimating posterior distributions via MCMC or SVI) is computationally expensive. A primary bottleneck is loop repetition—specifically, iterating over large datasets or long sequences of random samples.

While GPUs are excellent at parallelizing independent operations via vectorization, doing this manually in PPLs is highly error-prone and requires deep expertise. Existing automatic vectorization tools fall short because they cannot handle general repetition structures. If a loop contains nested loops, if-then-else branches, or iterations that depend on the output of previous iterations (like in Markov models), current PPL frameworks either fail or require significant manual code restructuring.

Methods

The authors tackle this by formalizing a compiler optimization that translates standard imperative PPL code into a lower-level target language optimized for tensor operations. The core machinery relies on two novel ideas:

  1. Speculative Parallel Execution: The compiler forces all $n$ iterations of a loop to run simultaneously on the GPU. For iterations that depend on previous steps, the runtime "speculates" (guesses) their starting states rather than waiting sequentially.
  2. Fixed-Point Check: Because speculative guesses can be wrong, the parallel execution loop is repeated. After each round, a fixed-point check determines if the variables have stabilized (i.e., the current iteration's inputs match the previous iteration's outputs). For models with short-range data dependencies (like a first-order Hidden Markov Model), the states converge globally in just a few rounds, allowing the loop to terminate early.

To make this work seamlessly on modern tensor libraries like PyTorch, the authors introduce a unique state representation that maps string-integer variable indices into fixed-length tensor dimensions, utilizing tensor broadcasting to handle variable reads and updates efficiently.

Datasets

The method was evaluated using eight different models across five classes of probabilistic benchmarks:

  • Hidden Markov Models (hmm-ord1, hmm-ord2, hmm-neural): Evaluated using a standard polyphonic piano score dataset.
  • Deep Markov Model (dmm): Evaluated using the same polyphonic music dataset but utilizing continuous hidden states and deep neural networks.
  • Nested Hidden Markov Models (nhmm-train, nhmm-stock): Tested on hourly passenger arrival data from the Bay Area Rapid Transit (BART) system and historical stock price data from Yahoo Finance.
  • Autoregressive Model (arm): Evaluated using speech data from the LibriSpeech corpus.
  • Temperature Controller Model (tcm): A synthetic state-space tracking model designed specifically to test data-dependent control flow (if-then-else statements).

Key Insights and Ideas

  • The Connection to Abstract Interpretation: The authors note that their vectorization framework essentially implements an executable version of collecting semantics from static analysis literature, where loops are naturally solved using fixed-point iterations.
  • Turning $O(N)$ Sequential Steps into $O(1)$ Parallel Rounds: For a loop of length $N$ with a dependency lookback of $K$ steps, the speculative framework reaches a fixed point in exactly $K+1$ iterations. In practice, this means a sequential loop over 2,000 time steps can be resolved in just a handful of parallel GPU passes.
  • Broad Applicability Without Code Rewriting: Unlike specialized baselines (such as Pyro's DiscreteHMM distribution) which require users to completely rewrite their models to fit strict mathematical templates, this method works directly on standard, intuitive for loops.

Key Results

  • Speedups: The proposed method achieved $1.1\times$ to $6\times$ speedups over existing automatic vectorization baselines. Compared to purely sequential execution, it demonstrated massive speedups of up to $14,302\times$.
  • Memory Reduction: For seven out of the eight benchmark models, the method reduced peak GPU memory usage to between $14\%$ and $80\%$ of the baseline requirements. This is because allocating larger, unified tensor chunks at once is more efficient for GPU memory managers than allocating thousands of tiny sequential chunks.
  • Robustness: Existing baselines (plate, vmarkov, discHMM) failed completely on models containing nested loops or control flow (like nhmm and tcm). The proposed method successfully vectorized all eight models without any user intervention.

Limitations and Future Work

  • Long-Range Dependencies: If a loop has long-range data dependencies (where an iteration depends on states from far in the past), the number of rounds needed to reach a fixed point approaches $n$. In these scenarios, the fixed-point check introduces unnecessary computational overhead. The authors suggest a hybrid runtime that dynamically switches back to sequential execution if long-range dependencies are detected.
  • Limited to Density Computations: The framework currently optimizes density and gradient calculations (crucial for SVI and HMC inference). However, it does not address or optimize the sample-generation phase used in sampling-based algorithms like importance sampling or Sequential Monte Carlo.

Summary updated

BibTeX
@article{lim2026optimising,
  author = {Sangho Lim and Hyoungjin Lim and Wonyeol Lee and Xavier Rival and Hongseok Yang},
  title = {{Optimising Density Computations in Probabilistic Programs via Automatic Loop Vectorisation}},
  journal = {Proc. ACM Program. Lang.},
  year = {2026},
  doi = {10.1145/3776663},
}
Figure