Fast and Correct Gradient-Based Optimisation for Probabilistic Programming via Smoothing

Basim Khajwal, C.-h. Luke Ong, Dominik Wagner

Probabilistic programming languages (PPLs) have revolutionized statistical modeling by allowing developers to express complex distributions through code. However, performing efficient inference—calculating the posterior distribution of latent variables—remains a significant hurdle. Variational Inference (VI) is a popular approach that frames inference as an optimization problem, typically solved using Stochastic Gradient Descent (SGD). While the "reparameterization trick" is the gold standard for low-variance gradient estimation in VI, it falters when programs contain discrete control flow, such as if-then-else statements.

The Challenge of Discontinuity

The reparameterization trick relies on the assumption that the program's execution path is differentiable with respect to its parameters. In practice, most programs are replete with discontinuities. When an if statement's condition depends on a latent variable, the resulting objective function becomes non-differentiable. In these scenarios, standard reparameterization gradients become biased, leading the optimization process toward incorrect results.

Figure 1: The bias of the reparameterization gradient estimator. The dashed red line represents the biased estimate, which deviates significantly from the true gradient shown in green.

Previous attempts to rectify this bias often involved complex "boundary terms" (e.g., the LYY18 estimator), which are difficult to implement and computationally expensive.

The SMOOTH Approach

Presented at ESOP 2023, the SMOOTH framework offers a mathematically rigorous alternative. Instead of attempting to fix the gradient of a discontinuous function, SMOOTH transforms the program itself into a differentiable approximation.

The core idea is to replace sharp "Heaviside" step functions—the mathematical equivalent of an if statement—with smooth sigmoid functions. This smoothing parameter, $\eta$, controls the tightness of the approximation. As $\eta$ approaches zero, the smooth function converges to the original discrete jump.

Figure 2: Approximating the sharp Heaviside step function (solid red) with logistic sigmoid functions (dotted/dashed) of varying steepness.

By making the entire program differentiable, SMOOTH allows for the direct application of the reparameterization trick without introducing bias relative to the smoothed objective.

Theoretical Foundations and Correctness

SMOOTH is more than just an engineering heuristic; it is grounded in novel programming language theory. The authors introduce vector Frölicher spaces (VectFr) to provide a formal denotational semantics for smoothed programs. To ensure that this approach is both safe and effective, they developed two annotation-based type systems:

  1. Unbiasedness and Convergence: The first type system guarantees that the reparameterization gradient is an unbiased estimator for the smoothed problem, ensuring that SGD will converge to the optima of the approximation.
  2. Uniform Convergence: The second system proves that as the smoothing becomes sharper ($\eta \to 0$), the solution to the smoothed problem correctly converges to the solution of the original, discontinuous problem.

Results and Benchmarks

The authors evaluated SMOOTH against standard PPL benchmarks (temperature, textmsg, influenza, cheating) and a specialized neural network, xornet, featuring step-function activations.

The results demonstrate significant improvements over existing methods:

  • Variance Reduction: SMOOTH achieved a reduction in work-normalized variance of 2 to 3,000 times compared to the LYY18 baseline.
  • Computational Efficiency: Because it avoids complex boundary terms, SMOOTH is faster per iteration and easily integrates with modern automatic differentiation libraries like JAX.
  • Robustness: SMOOTH successfully optimized the xornet model, which proved too complex for competing estimators due to its non-affine guards.

Conclusion and Future Directions

SMOOTH provides a powerful path forward for gradient-based inference in probabilistic programming, reconciling the need for speed with the necessity of mathematical correctness. While currently limited by a conservative type system and a lack of support for recursion, the framework opens the door for advanced techniques like adaptive smoothing, where the approximation is refined dynamically during optimization. By turning "broken" gradients into smooth, manageable surfaces, SMOOTH brings us closer to universal, efficient automated inference.

Summary updated

BibTeX
@inproceedings{khajwal2023fast,
  author = {Basim Khajwal and C.-H. Luke Ong and Dominik Wagner},
  title = {{Fast and Correct Gradient-Based Optimisation for Probabilistic Programming via Smoothing}},
  booktitle = {ESOP},
  year = {2023},
  doi = {10.1007/978-3-031-30044-8\_18},
}
Figure