TL;DR
This paper introduces Adaptive Weighted Rejection Sampling (AWRS), a novel algorithm that significantly speeds up constrained text generation from large language models (LLMs). AWRS addresses the computational cost and distribution distortion issues of traditional methods by using an adaptive rejection sampling approach and providing unbiased estimates for correcting greedy sampling behavior, leading to improved accuracy and efficiency across various tasks.
What Problem Does This Paper Address?
Generating text from language models while adhering to specific constraints (e.g., producing valid SQL queries, molecules with certain properties) is a crucial but challenging task. The standard approach, Locally Constrained Decoding (LCD), involves checking constraints at each step of token generation. However, this method suffers from two major drawbacks:
- Computational Expense: Evaluating constraints for every token in a large LLM vocabulary (often >100,000 tokens) is prohibitively slow.
- Distribution Distortion: LCD can lead to "dead-end paths" by myopically focusing on local token choices, potentially distorting the overall distribution of generated strings and leading to suboptimal or invalid outputs.
This paper aims to overcome these limitations by developing a faster and more accurate method for constrained generation.
Methods
The core contribution is the Adaptive Weighted Rejection Sampling (AWRS) algorithm. It builds upon rejection sampling principles but introduces key innovations:
- Adaptive Rejection Sampling (ARS): Instead of checking the entire vocabulary, AWRS adaptively samples tokens and learns from rejected ones. It maintains a set of rejected tokens and re-normalizes the sampling distribution, significantly reducing the number of constraint evaluations needed. This makes it much faster than traditional token masking.
- Weighted Sampling for Correction: AWRS is extended to provide unbiased estimates of the "constraint satisfaction probability" (denoted as Z). These estimates are crucial for correcting the greedy behavior inherent in local decoding. By integrating these estimates into Sequential Monte Carlo (SMC) methods, the algorithm can produce samples that better approximate the true global distribution of valid sequences.
- Dynamic Computation: The runtime of AWRS scales with the difficulty of the constraint (measured by KL divergence between the unconstrained and constrained distributions), meaning it becomes more efficient with more accurate base models.
The paper also discusses related concepts like Weighted Rejection Sampling (WRS) as a precursor and provides theoretical analysis using the Recursive Auxiliary-Variable Inference (RAVI) framework to prove the proper weighting and unbiasedness of their estimators.
Datasets
The paper evaluates AWRS on five challenging controlled generation benchmarks:
- Text-to-SQL (Spider): Generating SQL queries from natural language questions.
- JSON: Generating documents conforming to specific JSON schemas.
- Goal Inference (Planetarium): Defining agent goals in a planning language.
- Molecular Synthesis: Producing drug-like compounds using SMILES notation.
- Pattern Matching: Generating strings that adhere to complex pattern-matching specifications.
The paper does not introduce new datasets but uses existing, well-established benchmarks for evaluation.
Key Insights and Ideas
- Constraint Evaluation is the Bottleneck: The primary cost in constrained generation isn't the LLM itself, but the repeated, exhaustive constraint checks. AWRS directly tackles this by making constraint checking adaptive.
- Greediness is a Feature, Not a Bug (to be Corrected): Local constraint satisfaction can lead to sampling paths that are globally suboptimal. The paper's key insight is that the "greediness" can be quantified (via Z) and corrected using importance weights within SMC, turning a potential weakness into a solvable problem.
- Adaptive Sampling Scales Better: By intelligently reusing information about rejected tokens, AWRS avoids redundant checks, leading to orders-of-magnitude speedups compared to naive token masking, especially for complex constraints.
- Model Quality Benefits AWRS: The runtime efficiency of AWRS is tied to the LLM's ability to capture the constraint. Better models, which have a tighter grip on the desired output distribution, lead to faster AWRS performance.
Key Results
- Significant Speedups: AWRS-SMC consistently outperforms state-of-the-art baselines in terms of runtime, often by orders of magnitude, especially when compared to token masking (e.g., >50x faster on pattern matching).
- Improved Accuracy: AWRS-SMC matches or beats ARS-LCD (a faster LCD baseline without correction) in accuracy, significantly improving performance in four out of five domains. It also achieves comparable or higher accuracy than Sample-Verify and Twisted SMC with fewer particles.
- Scalability: AWRS-SMC shows promising scalability with smaller LMs, achieving better runtime and accuracy than larger LMs using older SMC approaches.
- Broad Constraint Support: The method is designed to work with arbitrary black-box constraints, making it highly versatile.
Limitations and Future Work
- Stochastic Runtime: AWRS is a Las Vegas algorithm, meaning its runtime is stochastic. While it offers significant average-case improvements, worst-case scenarios can still be slow, though the paper provides theoretical bounds and empirical evidence of favorable scaling.
- Approximation in SMC: While AWRS provides unbiased estimates of Z, its integration into SMC is still an approximate inference method.
- Future Work: The authors suggest exploring extensions like partial concurrency for further speedups, clipped-probability early stopping to manage difficult constraints, and hybridization with truncation sampling methods. They also highlight the need for more efficient implementations and numerical stability for production use.
Summary updated