Abstract
Large Language Models (LLMs) often struggle with generating code that adheres to specific API versions or syntactic constraints, leading to "hallucinations." While constrained decoding techniques attempt to mitigate this by blocking invalid tokens, they often suffer from "intent distortion" due to their greedy nature. We present AdapTrack, a novel decoding method that introduces an adaptive backtracking mechanism based on rejection sampling. AdapTrack allows models to "re-think" earlier token choices when they lead to invalid sequences, ensuring that the final output remains both constraint-compliant and mathematically aligned with the model's original distribution. Our evaluations show significant improvements in API accuracy and general code generation benchmarks.
Introduction: The Problem of Intent Distortion
LLMs are powerful tools for code generation, yet they frequently produce snippets that are syntactically incorrect or rely on non-existent APIs. To address this, researchers employ constrained decoding, which uses a "constrainer" (such as a compiler or parser) to prune invalid tokens during the generation process.
However, standard constrained decoding is fundamentally greedy. When a model begins generating a sequence that eventually violates a constraint (e.g., an API name not present in the target library version), the constrainer blocks the subsequent invalid character. Because traditional decoding cannot backtrack, the model is forced to select the next most likely valid token at that specific step. This often results in "Frankenstein code"—sequences that are technically valid according to the grammar but deviate significantly from the model's original intent. This process distorts the model's output distribution, leading to suboptimal performance and "hallucinated" logic.
Methods: Adaptive Backtracking with AdapTrack
AdapTrack replaces the standard greedy elimination with an Adaptive Backtracking mechanism. The core idea is to treat the generation process as a form of rejection sampling rather than a simple filtering task.
- Rejection Sampling: Instead of zeroing out illegal tokens, AdapTrack maintains an estimate of the "validity" of a specific path. It treats the constrained generation as sampling from the original model distribution conditioned on the constraints.
- Dynamic Backtracking: If the probability of the model's intended tokens on a specific branch drops significantly—indicating that the constrainer is forcing the model into a "corner"—AdapTrack triggers a backtrack. It deletes the problematic tokens and restarts from an earlier point in the sequence. This allows the model to explore alternative, valid branches that better align with its original intent.
- Theoretical Alignment: A key contribution of this work is the mathematical proof that the distribution produced by AdapTrack is identical to the model’s original distribution conditioned on the constraints. This ensures that the model's "intent" is preserved throughout the decoding process.
- Efficiency Optimizations: To manage the computational overhead of backtracking, the authors implement parallel updates on the GPU and utilize "limited backtrack distances." Experiments show that even short-range "undo" operations (2-8 tokens) capture the majority of performance gains without excessive slowdowns.
Evaluation and Results
The authors evaluated AdapTrack across several benchmarks, ranging from synthetic API transitions to real-world code generation.
Datasets
- TFv1 (Synthetic): A dataset focused on TensorFlow v1 to v2 API transitions, testing the model's ability to switch between version-specific APIs under constraints.
- TFv1Real: 1,000 real-world code snippets from GitHub using version-sensitive TensorFlow APIs.
- General Code Generation: Standard benchmarks HumanEval and MBPP, utilizing a type-constrainer for TypeScript.
- Domain-Specific Languages (DSL): Specialized datasets (SLIA, INV-BV, CP, and bit-string tasks) used to measure distributional alignment via KL divergence.
Key Results
AdapTrack demonstrated substantial improvements over standard constrained decoding:
- API Accuracy: On the synthetic TFv1 dataset, AdapTrack achieved a staggering 360.87% improvement in Exact Match (EM@1).
- Real-World Performance: On the TFv1Real dataset, it showed a 38.93% improvement.
- General Coding: Performance on HumanEval and MBPP improved by up to 7.84% and 6.42%, respectively.
- Distributional Accuracy: In DSL experiments, AdapTrack consistently yielded the lowest KL divergence, confirming that its outputs are the most "natural" and aligned with the model's internal logic.
Discussion: The "Undo" Button for LLMs
The primary insight of AdapTrack is the realization that constrained decoding failures are often a result of the model being "trapped" by its own previous choices. By providing an "undo" button, AdapTrack mimics human programming behavior: when a developer realizes they have typed a deprecated or incorrect API, they backspace and try a different approach.
While AdapTrack is more computationally expensive—requiring 20% to 60% more model calls due to backtracking—the gains in accuracy and intent preservation are significant. Future work may involve optimizing the interaction between the constrainer and the decoder or exploring how to align models with constraints during training to minimize the need for extensive backtracking.
Conclusion
AdapTrack offers a robust solution to the problem of intent distortion in constrained decoding. By leveraging adaptive backtracking and rejection sampling, it ensures that LLMs can follow complex constraints without sacrificing the quality or coherence of their output.
Summary updated