Introduction
Generating structured outputs from Language Models (LMs)—such as valid code, SQL queries, or JSON—is typically handled via Grammar-Constrained Decoding (GCD). GCD ensures 100% validity by masking invalid tokens during generation. However, this constraint often distorts the model's learned probability distribution, leading to repetitive or low-quality outputs. While Rejection Sampling maintains distribution fidelity, it is prohibitively slow. This paper introduces a Markov Chain Monte Carlo (MCMC) framework that bridges this "faithfulness vs. efficiency" gap, providing a mathematically grounded approach to high-fidelity constrained sampling.
The MCMC Framework
The proposed method utilizes the Metropolis-Hastings algorithm to treat sampling as a "walk" through the space of valid sequences. Unlike standard GCD, which generates a single sequence, the MCMC approach iteratively refines samples:
- Initialization: An initial valid sequence is generated using standard GCD.
- Proposal Mechanism: To generate a candidate for the next state, the algorithm selects a prefix from the current sequence and uses GCD to generate a new, valid suffix. This ensures every proposal is grammatically correct.
- Acceptance Criterion: A Metropolis-Hastings step determines whether to accept the new sequence based on the LM's likelihood, ensuring the chain converges to the model's true distribution.
The authors explored three prefix selection strategies: Uniform (random selection), Priority (focusing on high-perplexity regions), and Restart (generating a new sequence from scratch).
Evaluating Faithfulness
The researchers evaluated the "faithfulness" of the sampler—how well it matches the model's original distribution—using synthetic benchmarks like SyGuS (Syntax-Guided Synthesis) and Constituency Parsing.
As shown in the synthetic evaluations, MCMC variants consistently achieve lower KL divergence than standard GCD and the state-of-the-art ASAp method. Notably, the MCMC approach is mathematically guaranteed to converge toward the true distribution with each step taken, unlike previous heuristic attempts to fix GCD distortion.
Real-World Impact: Program Fuzzing
The practical utility of this framework was tested in the context of program fuzzing for major software libraries like libxml2 and SQLite. Fuzzing requires diverse, valid inputs to explore deep code paths and uncover edge-case bugs.
In the SQLite experiments, the MCMC-Priority method achieved 33.7% branch coverage, a significant jump over standard GCD (28.26%) and the specialized Grammarinator tool (25.04%). The results indicate that the increased diversity and faithfulness of MCMC samples directly translate to more effective software testing. High-quality samples were found to emerge in as few as 5 to 10 MCMC steps, making the method viable for real-world engineering pipelines.
Conclusion and Future Work
This MCMC perspective on constrained sampling demonstrates that it is possible to achieve both perfect grammatical validity and high distributional fidelity. While the current study focused on the Llama-3.1-8B model and 1-hour fuzzing windows, future work will explore scaling these benefits to larger models and longer testing durations. The authors also suggest exploring more complex proposal distributions and integrating MCMC with other decoding strategies like beam search or nucleus sampling.
Summary updated