TL;DR
Grid Beam Search (GBS) is a popular, efficient method for forcing LLMs to include specific words in their output, but it suffers from a "greedy bias" that favors common words over rare ones. This paper introduces Fair Grid Beam Search, which uses a simple cost-to-go heuristic to eliminate this bias, resulting in higher-probability generated text with negligible computational overhead.
What Problem Does This Paper Address?
When we want an autoregressive model (like an LLM) to generate text containing specific keywords—a task called lexically constrained decoding—we usually choose between two methods. The first, DFA-constrained beam search, is theoretically sound but computationally expensive (exponential in the number of constraints). The second, Grid Beam Search (GBS), is much faster (linear) and thus more popular.
However, the authors point out a major flaw in GBS: it is unnecessarily greedy. In GBS, hypotheses compete against each other if they have satisfied the same number of constraints, regardless of which constraints they satisfied. Because common words naturally have higher probabilities than rare ones, GBS tends to pick the "easy" common words first and push the "hard" rare words to the very end of the sentence. This often leads to awkward phrasing and lower-quality (lower-probability) sequences.
Methods
The authors propose Fair Grid Beam Search, a modification that levels the playing field for rare constraint words.
- The DFA Framework: Like previous methods, they represent the constraints as a Deterministic Finite Automaton (DFA) that tracks which words have been generated.
- Cost-to-Go Heuristic: Inspired by A* search, they assign a "cost" $C(q)$ to every state in the DFA. This cost represents the estimated difficulty of satisfying the remaining constraints from that state.
- Weighting by Frequency: The cost is calculated by finding the shortest path to the "finished" state, where each step is weighted by the negative log unigram probability of the required tokens. Essentially, rare words are assigned a higher "cost-to-go."
- Fair Competition: During decoding, instead of just looking at the model's probability for the tokens generated so far, Fair GBS subtracts the cost-to-go: $\ln P(h) - C(h)$.
This heuristic rewards hypotheses that have already tackled "difficult" (rare) constraints by reducing their remaining cost, preventing them from being discarded in favor of hypotheses that only took the "easy" path.
Datasets
The authors evaluated their method using two setups:
- Random Constraints: They selected 1,000 sets of five random words from the Corpus of Contemporary American English (COCA). These were used to prompt TinyLlama-1.1B to write one-sentence stories.
- CommonGen: A more naturalistic benchmark for generative commonsense reasoning. They used Llama-3.1 8B Instruct to generate sentences based on the "concepts" provided in the dataset, allowing for various inflections of the constraint words (e.g., "run," "ran," "running").
Key Insights and Ideas
- Visualizing the Greedy Bias: The paper provides a conceptual illustration of how GBS fails. If the model wants to generate "dcba" (where 'd' is rare and 'a' is common), standard GBS might discard the path starting with 'd' because 'a' is locally more likely, even if 'd' is better for the sentence overall.
- "Free" Improvements: While the precomputation of costs is technically exponential, it happens on the CPU and doesn't involve the LLM. For typical numbers of constraints, this step is so fast that the overall generation time is virtually identical to standard GBS.
- Global vs. Local Optimization: By incorporating a look-ahead heuristic, Fair GBS moves away from the purely local decisions that hamper standard beam search in constrained environments.
Key Results
The experiments confirmed three main points:
- Bias Correction: In the random constraint task, GBS showed a significant negative correlation between word frequency and its position in the sentence (common words came first). Fair GBS eliminated this correlation.
- Higher Quality: Fair GBS consistently found strings with higher model probability (lower decoding entropy) than standard GBS. In the CommonGen task, it improved the decoding entropy by over one "nat."
- Efficiency: As shown in the table below, Fair GBS maintains the speed of GBS while approaching the quality of the much slower DFA-constrained search.
Limitations and Future Work
- Scaling Constraints: The precomputation step is exponential in the number of constraints ($2^k$). While fine for 5-10 constraints, it would eventually become a bottleneck for very large constraint sets.
- Unigram Simplification: The heuristic relies on unigram (single-word) probabilities. While effective, it doesn't account for how the difficulty of a word changes based on the specific context of the sentence.
- Future Directions: The authors suggest applying this "fair" approach to more complex constraints, such as those defined by context-free grammars or more elaborate logical structures.
Summary updated