Towards verified stochastic variational inference for probabilistic programs

Wonyeol Lee, Hangyeol Yu, Xavier Rival, Hongseok Yang

TL;DR

Stochastic Variational Inference (SVI) is the algorithmic engine powering modern deep probabilistic programming, but it relies on strict, implicit mathematical assumptions to work correctly. This paper formalizes these assumptions and introduces a static analysis tool for the Pyro programming language that automatically verifies them, successfully proving the correctness of complex models and even catching subtle mathematical bugs in official tutorials.

What Problem Does This Paper Address?

Probabilistic Programming Languages (PPLs) like Pyro, Edward, and ProbTorch allow developers to easily mix deep neural networks with Bayesian probabilistic models. To figure out the hidden variables in these models (posterior inference), these languages rely heavily on Stochastic Variational Inference (SVI).

SVI turns a difficult inference problem into an optimization problem. It asks the user to provide a "guide" (an approximate probability distribution) and then tweaks the guide's parameters to minimize the difference—specifically, the KL divergence—between the guide and the true "model."

The catch? SVI algorithms blindly assume your model and guide are mathematically compatible. For instance, if your guide assigns a non-zero probability to a value that your model says is impossible (a "support mismatch"), the KL divergence explodes to infinity. Other assumptions require the distributions to be differentiable and integrable. If these implicit assumptions are violated, the optimization objective becomes undefined, the gradient estimates become biased, and the SVI engine might crash or silently return garbage results. The authors tackle the problem of automatically verifying that a given model-guide pair actually satisfies the mathematical rules required by SVI.

Methods

The authors bridge the gap between continuous mathematics and program analysis using the following steps:

  1. Formal Semantics: They use denotational semantics to rigorously define exactly what the most common SVI algorithm (REINFORCE / score estimator) does to a probabilistic program.
  2. Extracting Requirements: They identify six core mathematical requirements (R1-R6) that must hold for the SVI algorithm to be unbiased and guaranteed to converge.
  3. Automation-Friendly Math: Directly checking properties like "integrability" (required for KL divergence) is notoriously difficult for automated code analyzers. The authors cleverly use continuous mathematics to prove that simpler conditions—like checking if a function's growth is bounded by an affine-exponential function, or checking for basic differentiability—are sufficient to guarantee the harder requirements.
  4. Static Analysis via Abstract Interpretation: They designed a static program analysis framework to track these properties. They then built a working prototype for the Pyro language specifically designed to check Requirement R1: the "model-guide support match." This analyzer tracks tensor shapes, distribution types, and variable names through complex PyTorch and Pyro operations without actually running the code.

Datasets

The authors evaluated their prototype static analyzer on two sets of benchmarks:

  • Pyro Regression Test Suite: 39 basic model-guide pairs covering standard programming patterns in Pyro.
  • Pyro Webpage Examples: 8 advanced, real-world machine learning models from the official Pyro documentation (including Variational Autoencoders, Attend-Infer-Repeat (AIR), and Deep Markov Models).

Key Insights and Ideas

  • Verification for ML is more than just catching crashes: Program verification in machine learning usually focuses on the final output (e.g., adversarial robustness). This paper shows that verifying the mathematical preconditions of the training algorithms themselves is a massive, untapped opportunity.
  • Math hacks make static analysis possible: The realization that you can swap out an impossible-to-automate integrability check for a highly-automatable boundedness/differentiability check is a fantastic example of using math to enable software engineering tools.
  • Even the experts make SVI mistakes: Because SVI fails silently or unpredictably when its assumptions are violated, even official, widely-used tutorials can harbor subtle mathematical bugs that human reviewers miss but formal analysis catches instantly.

Key Results

  • Bug Discovery: The analyzer found a critical bug in Pyro's official Bayesian Regression tutorial. The model used a bounded Uniform(0, 10) distribution, but the guide used an unbounded Normal distribution. This support mismatch made the SVI objective mathematically undefined.
  • Non-Standard Usage Flagged: It also flagged an Latent Dirichlet Allocation (LDA) example. Upon inspection, the developers were using the SVI engine to perform a different algorithm entirely (variational EM) using a Delta distribution, which violates standard SVI reference measure assumptions.
  • High Verification Rate: The tool successfully verified 31 out of 39 regression tests and 6 out of 8 advanced examples, proving that their supports matched perfectly.
  • Speed: The analysis is highly efficient. It verified most programs in under 1 second, and the most logically complex model (Attend-Infer-Repeat, featuring high-dimensional tensors and nested loops) took only ~4 seconds.

Limitations and Future Work

  • Implementation Gap: While the paper outlines theoretical checks for all six SVI requirements (R1-R6), the actual implemented OCaml prototype only checks the first one (R1: support match).
  • Theoretical Constraints: The theoretical conditions for R3-R6 currently assume the program uses a bounded number of random variables and relies heavily on normal distributions (mean-field approximations). Relaxing these constraints to allow for unbounded variables or heavier-tailed distributions is a future challenge.
  • Engineering Limits: The prototype crashed on 8 of the regression test cases. This wasn't due to a flaw in the SVI theory, but rather the engineering difficulty of statically analyzing complex Python for loops and Pyro's data subsampling features. Improving the analyzer's handling of standard Python constructs is an immediate next step.

Summary updated

BibTeX
@inproceedings{lee2020towards,
  author = {Wonyeol Lee and Hangyeol Yu and Xavier Rival and Hongseok Yang},
  title = {{Towards verified stochastic variational inference for probabilistic programs}},
  booktitle = {Proc. ACM Program. Lang},
  year = {2020},
  url = {https://openreview.net/forum?id=KAICfHqsyMq},
}

Loading related content...

Figure