Language Models (LMs) are increasingly deployed in high-stakes, specialized domains where outputs must adhere to strict syntactic or semantic rules. Whether generating executable code, formulating SQL queries, or designing novel molecular structures, a single structural hallucination can render an output useless.
Currently, researchers face a frustrating trade-off when enforcing these rules, choosing between speed and mathematical fidelity. A new approach, Constrained Adaptive Rejection Sampling (CARS), bridges this gap, offering an algorithm that strictly improves the sample efficiency of rejection sampling without distorting the model's underlying probability distribution.
The Constrained Generation Dilemma
When forcing an LM to follow strict constraints, existing methods typically fall at two extremes of a spectrum:
- Greedy Constrained Decoding (GCD): This approach forces the model to select only "valid" tokens at every decoding step. While highly efficient, it fundamentally distorts the model's natural probability distribution. This distortion leads to a severe loss of sample diversity and can even trap the model in infinite generation loops.
- Rejection Sampling (RS): In this method, the model samples freely, and any invalid results are simply discarded. RS is mathematically "exact"—meaning it perfectly preserves the model's true distribution and diversity. However, it is incredibly wasteful. In complex domains, the acceptance rate can plummet below 1%, meaning 99% of computational resources are thrown away.
The field has lacked an algorithm that is both exact (preserving the true distribution) and amortized-efficient (becoming faster and less wasteful over time).
Introducing CARS: Methodology and Mechanics
CARS builds upon the foundation of adaptive rejection sampling but introduces a novel way to handle generation failures. It begins with unconstrained LM sampling and adaptively rules out constraint-violating continuations.
The system relies on four core mechanics:
- The Trie Structure: CARS maintains a prefix tree (trie) that records invalid sequences as they are discovered.
- Adaptive Pruning: Unlike standard adaptive methods that only remember a single failed string, CARS utilizes a constraint checker (such as a grammar parser) to identify all immediate continuations of a prefix that would violate the rules.
- Probability Mass Redirection: When a prefix is proven invalid, CARS subtracts its probability mass from the total distribution. In future generation attempts, the sampler is mathematically "pushed" away from these known dead ends.
- Monotonic Improvement: Because the algorithm continuously records new forbidden paths, the probability of generating a valid sample strictly increases with every iteration. Prefixes proven invalid are never revisited.
Crucially, CARS does not alter the relative probabilities of valid sequences. It simply prevents the model from wasting compute on paths it already knows are impossible.
Key Insights and Experimental Results
The researchers evaluated CARS across several structured domains, including grammar-based software fuzzing (JSON, SQL, XML), molecular synthesis (SMILES strings), Text-to-SQL (the Spider dataset), and planning/logic benchmarks (PDDL, SyGuS). The experiments utilized state-of-the-art models, including Llama-3.1 (8B and 70B) and Qwen2.5 (7B and 14B).
Efficiency and Accuracy Gains
CARS demonstrated massive improvements in efficiency over standard Rejection Sampling and superior accuracy over Greedy Constrained Decoding:
- XML Fuzzing: Standard rejection sampling failed to produce 100 valid samples within a 2,000-sample budget. CARS achieved the 100-sample goal in just 440 generations.
- Text-to-SQL: CARS outperformed GCD by 5.3% in execution accuracy. GCD's distribution distortion often forces the model toward simpler, incorrect queries, whereas CARS preserves the model's full reasoning capabilities.
- Molecular Discovery: Generating valid chemical notations required 4.3x fewer samples than standard RS and 1.3x fewer than previous adaptive methods, all while maintaining top scores for chemical validity and diversity.
The Importance of Diversity
In fields like molecular discovery or software fuzzing, generating 100 distinct valid samples is vastly more valuable than generating the same safe sample 100 times. Because CARS avoids the distributional distortion of GCD, it maintains significantly higher sample diversity.
Sublinear Growth and Diminishing Returns
The authors proved a "Diminishing Returns" theorem, demonstrating that the most helpful invalid prefixes are usually discovered early in the sampling process. This allows users to "freeze" the trie after a few hundred samples, saving memory while retaining most efficiency gains. Furthermore, despite tracking millions of invalid pairs, trie growth remains sublinear. In many tasks, up to 70% of token decisions can be served directly from the trie cache rather than re-querying a computationally heavy grammar parser.
Limitations and Future Directions
While CARS represents a significant leap forward, it is not without limitations:
- Distributional Misalignment: CARS relies on the model's underlying knowledge. If a model's internal distribution is completely at odds with the required constraints (e.g., it has a 0% natural tendency to generate the required syntax), CARS will still struggle. Rejection sampling cannot extract knowledge that isn't there.
- Memory Overhead: Although trie growth is sublinear, it can still become massive in highly complex environments. In one extreme planning task, the trie reached 285GB.
Future work will likely focus on mitigating these memory demands through sparse representations or bitmasks. Additionally, the authors suggest exploring how the knowledge captured within the CARS trie could be used to "distill" constrained generation capabilities back into smaller, more efficient models.
Summary updated