Smoothness Analysis for Probabilistic Programs with Application to Optimised Variational Inference

Wonyeol Lee, Xavier Rival, Hongseok Yang

TL;DR

This paper introduces a formal static analysis framework that automatically identifies differentiable and "smooth" components within probabilistic programs. By using this analysis, the authors developed an improved gradient estimator that combines the efficiency of pathwise gradients with the generality of score-function estimators, ensuring accurate and high-performance inference even for models with non-differentiable branches or functions.

What Problem Does This Paper Address?

In the world of Bayesian machine learning, Probabilistic Programming Languages (PPLs) like Pyro allow researchers to define complex statistical models as code. To make these models useful, we need to perform Variational Inference (VI)—an optimization process that uses gradients to find the best approximation of a model's posterior distribution.

There are two standard ways to estimate these gradients:

  1. Score Estimator (SCE): Works for almost any model but is very "noisy" (high variance), making optimization slow.
  2. Pathwise Gradient Estimator (PGE): Much more efficient (low variance) but requires the model to be mathematically differentiable.

The problem? Real-world programs are full of non-differentiable elements like if-else branches, loops, and functions like ReLU. If you apply the efficient PGE to these models, you get "biased" (mathematically incorrect) results. Currently, developers have to manually pick which parts of a program are "safe" for PGE, a process that is tedious and highly prone to error.

Methods

The authors tackle this by building a bridge between program analysis and probability theory. Their approach consists of two main parts:

  • Smoothness Static Analysis: They developed a system to automatically scan a program's source code and determine which variables are "smooth" (differentiable or locally Lipschitz) relative to the model's parameters. This analysis tracks both smoothness and dependency (which inputs affect which outputs) to ensure that even if one part of a program is "jagged," the smooth parts can still be identified and exploited.
  • Selective Pathwise Gradient Estimator (SPGE): Using the results of the static analysis, the system automatically applies the efficient PGE to the smooth parts of the program and falls back to the more robust SCE for the non-differentiable parts.

Crucially, the authors formulated five mathematical assumptions that a smoothness property must satisfy for the analysis to be "sound" (guaranteed to be correct). They discovered that some common mathematical definitions of continuity actually fail these tests when applied to sequential code, explaining why previous attempts at this kind of analysis were often flawed.

Datasets

The authors evaluated their tool on 13 representative probabilistic models from the official Pyro documentation, plus one custom "toy" model designed to highlight edge cases. These include:

  • Deep Learning Models: Variational Autoencoders (VAE), Attend-Infer-Repeat (AIR).
  • Time-Series Models: Deep Markov Models (DMM).
  • Statistical Models: Dirichlet Process Mixture Models, Hidden Markov Models (HMM), and Bayesian Regression.

These benchmarks represent the "gold standard" of models used by the PPL community.

Key Insights and Ideas

  • Composition is the Culprit: The biggest challenge in analyzing code is that functions are composed (chained together). The authors point out that properties like "partial differentiability" are not preserved by function composition. This insight allowed them to build a more rigorous framework than prior work.
  • Local Lipschitzness is the "Sweet Spot": While most literature focuses on pure differentiability, the authors found that "local Lipschitzness" (a slightly broader definition of smoothness that includes functions like ReLU) is the ideal target. It covers more real-world code while still providing the mathematical guarantees needed for unbiased gradient estimation.
  • Automating the Expert: The tool was able to automatically replicate the manual optimizations performed by Pyro experts and, in some cases, found that the "expert" settings were actually mathematically unsound for specific models.

Key Results

  • Perfect Accuracy: In all 14 test cases, the static analysis correctly identified the exact set of smooth variables, matching manual "ground truth" labels.
  • Uncovering Unsoundness: The analysis revealed that Pyro's default settings for the spnor and air models were technically unsound, potentially leading to suboptimal learning. Their tool automatically corrected these settings to ensure unbiasedness.
  • High Efficiency: The analysis is extremely fast. For most models, it took less than 0.1 seconds to run, meaning it can be integrated into standard development workflows without adding noticeable overhead.

Limitations and Future Work

  • Regularity Conditions: The proof that the gradient estimator is unbiased relies on certain "regularity conditions" (related to the integrability of the model's density). Currently, the tool assumes these hold; automating the check for these conditions is a goal for future work.
  • Tensor Granularity: The current implementation treats an entire tensor (an array of numbers) as a single block. If one element of a 1,000-element tensor is non-smooth, the whole tensor is marked as non-smooth. Finer-grained "element-wise" analysis could unlock even more performance.
  • Language Coverage: The tool currently supports a subset of the Pyro language. Expanding this to handle the full complexity of Python and PyTorch's dynamic features remains a significant engineering challenge.

Summary updated

BibTeX
@article{lee2023smoothness,
  author = {Wonyeol Lee and Xavier Rival and Hongseok Yang},
  title = {{Smoothness Analysis for Probabilistic Programs with Application to Optimised Variational Inference}},
  journal = {Proc. ACM Program. Lang.},
  year = {2023},
  doi = {10.1145/3571205},
}
Figure