Introduction
Large Language Models (LLMs) are increasingly tasked with generating highly structured outputs, ranging from source code in C++ to data interchange formats like JSON. While these models possess significant latent knowledge of syntax, they frequently struggle with strict adherence to formal grammars. To address this, researchers have traditionally employed Grammar-Constrained Decoding (GCD). GCD ensures syntactic validity by masking invalid tokens during the decoding process, effectively forcing the model to select only from a set of grammatically permissible options.
However, this paper identifies a critical flaw in standard GCD: distribution distortion. While GCD guarantees that an output is technically valid, it often forces the model into "greedy traps" that degrade the semantic quality and statistical likelihood of the generated text. This paper introduces ASAp, an adaptive decoding algorithm designed to ensure that outputs are both grammatically correct and aligned with the model’s original learned distribution.
The Limitations of Grammar-Constrained Decoding
The fundamental issue with GCD is its short-sightedness. It evaluates token validity at the current step without considering whether that choice leads to a high-probability sequence in the future. By forbidding certain tokens to satisfy a grammar, GCD can inadvertently steer the model into a corner of the search space where the only remaining valid completions are extremely unlikely according to the LLM's training.
For example, if a model’s most probable next sentence is blocked by a single character constraint at the start, GCD might force it to begin with a rare token. This rare token may then necessitate a sequence of subsequent tokens that, while syntactically "correct," result in nonsensical or low-utility gibberish. In essence, GCD prioritizes local validity over global coherence.
Methodology: Grammar-Aligned Decoding (GAD)
The authors propose a new framework called Grammar-Aligned Decoding (GAD). Unlike GCD, which imposes constraints as a post-hoc filter, GAD seeks to sample from the LLM’s distribution conditioned on the grammar. The core challenge lies in calculating the Expected Future Grammaticality (EFG)—the probability that a specific prefix will eventually lead to a valid, complete sequence.
Because calculating EFG is mathematically intractable for complex neural networks, the authors developed ASAp (Adaptive Sampling with Approximate Expected Futures). ASAp functions through three key mechanisms:
- Iterative Learning: The algorithm begins by acting like standard GCD but maintains a memory of every sample generated.
- Refining Bounds: ASAp uses a "sound overapproximation" strategy. It initially assumes a path is viable until sampling reveals it leads to low-probability or ungrammatical outcomes. It then updates an upper bound on the EFG, effectively "back-propagating" this failure to inform future sampling runs.
- Convergence: As the algorithm draws more samples, its internal map of the "grammatical landscape" becomes increasingly accurate. The authors provide mathematical proof that ASAp eventually converges to the true conditional distribution of the LLM.
Experimental Evaluation
The researchers evaluated ASAp using the Mistral-7B model across several demanding benchmarks:
- Syntax-Guided Synthesis (SyGuS): Specifically the SLIA (strings with linear integer arithmetic) and INV-BV (loop invariant generation using bit-vectors) subsets, which require precise program synthesis.
- Constituency Parsing (CP): A natural language task requiring the generation of well-parenthesized parse trees.
- Synthetic Tasks: Custom grammars designed to isolate and demonstrate the specific failure modes of GCD.
Key Results and Insights
The study yielded several significant findings regarding the efficacy of ASAp compared to traditional methods:
- Statistical Fidelity: Using Kullback–Leibler (KL) divergence, the authors demonstrated that ASAp’s output distribution aligns with the ideal target distribution much faster than GCD, which remains permanently biased.
- Improved Problem Solving: On INV-BV program synthesis tasks, ASAp achieved a 38% increase (geomean) in correct solutions over standard GCD. This suggests that by staying aligned with the model's internal logic, the algorithm finds better functional solutions.
- Expectation Matching: In synthetic tasks where GCD was trapped into picking low-probability strings, ASAp successfully learned to avoid those paths, eventually matching the LLM's preference for more useful, high-probability strings.
- The Limits of Fine-tuning: Interestingly, the authors found that even models fine-tuned for specific tasks (via QLoRA) still suffer from the biases introduced by constrained decoding. This highlights that ASAp is a necessary architectural improvement rather than something that can be solved by data alone.
Conclusion and Future Work
ASAp represents a shift in how we approach constrained generation, moving from simple filtering to true distribution alignment. By ensuring that constraints do not silence the "intelligence" gained during pre-training, ASAp produces outputs that are both formally correct and substantively superior.
The primary trade-off for this accuracy is computational cost. As an iterative algorithm, ASAp requires multiple samples (up to 2,000 in this study) to converge, making it slower than one-shot greedy decoding. Future research will focus on accelerating this convergence through techniques like Beam Search, targeted exploration, and more efficient back-propagation of probability mass to make the algorithm viable for real-time applications.
Summary updated