Introduction
Probabilistic programming languages (PPLs) aim to bridge the "inference gap" between complex model design and the manual derivation of inference algorithms. While tools like Stan have popularized Hamiltonian Monte Carlo (HMC) for continuous models, they often struggle with models containing discrete variables or stochastic branching. Turing, a Julia-based PPL, addresses this by introducing composable inference, allowing researchers to partition models and apply specialized samplers to different variable subsets.
The Composable Inference Engine
Turing's primary innovation is its modular MCMC engine. Rather than treating the entire model as a single black box, users can define a Composable Gibbs sampler. For instance, a model might use HMC for high-dimensional continuous parameters while employing Particle Gibbs (PG) for discrete latent variables. This hybrid approach leverages the efficiency of gradient-based methods where applicable while maintaining the flexibility of simulation-based methods for non-differentiable components.
Implementation and Performance
Built as a shallow embedding in Julia, Turing utilizes the language's high-performance numerical stack and multiple dispatch system. It supports both forward and reverse-mode automatic differentiation (AD) for HMC gradients. To implement Particle Gibbs efficiently, Turing uses coroutines to pause and resume model execution, avoiding the overhead of traditional multi-processing. This allows Turing programs to remain standard Julia programs, capable of integrating with any existing library for tasks like differential equations or signal processing.
Experimental Results: Mode Exploration
The effectiveness of composable inference is most evident in multi-modal distributions. In a Gaussian Mixture Model (GMM) with widely separated components, traditional black-box samplers like the No-U-Turn Sampler (NUTS) often fail to transition between modes, becoming trapped in local high-probability regions.
As shown in the trace plots above, NUTS tends to get trapped in local regions, whereas the composable Gibbs sampler (mixing PG and HMC) successfully jumps between modes. This translates directly to the accuracy of the estimated posterior density. In benchmarks, NUTS fails to recover the full distribution, while Turing's composable engine matches the exact target density.
While Turing is currently 0.7x to 20x slower than the highly optimized C++ backend of Stan, its flexibility allows for model-specific optimizations. For example, in Latent Dirichlet Allocation (LDA) benchmarks, custom Julia implementations within Turing outperformed Stan's optimized versions by leveraging specific model structures.
Conclusion
Turing provides a universal and efficient framework for probabilistic programming by treating program execution as the state space. While manual partitioning of variables remains a current requirement, future work aims to automate this process and improve the efficiency of the Gibbs implementation through dynamic dependency tracking and computation graphs.
Summary updated