Grammar-Aligned Decoding

Large Language Models (LLMs) struggle with reliably generating highly structured outputs, such as program code, mathematical formulas, or well-formed markup. Constrained decoding approaches mitigate this problem by greedily restricting what tokens an LLM can output at each step to guarantee that the output matches a given constraint. Specifically, in grammar-constrained decoding (GCD), the LLM's output must follow a given grammar. In this paper, we demonstrate that GCD techniques (and in general constrained decoding techniques) can distort the LLM's distribution, leading to outputs that are grammatical but appear with likelihoods that are not proportional to the ones given by the LLM, and so ultimately are low-quality. We call the problem of aligning sampling with a grammar constraint, grammar-aligned decoding (GAD), and propose adaptive sampling with approximate expected futures (ASAp), a decoding algorithm that guarantees the output to be grammatical while provably producing outputs that match the conditional probability of the LLM's distribution conditioned on the given grammar constraint. Our algorithm uses prior sample outputs to soundly overapproximate the future grammaticality of different output prefixes. Our evaluation on code generation and structured NLP tasks shows how ASAp often produces outputs with higher likelihood (according to the LLM's distribution) than existing GCD techniques, while still enforcing the desired grammatical constraints.

Paper

References (28)

Scroll for more · 16 remaining

Similar papers

Peer review

Reviewer gp9U5/10 · confidence 4/52024-07-07

Summary

This paper focuses on the constrained decoding scenario where LLMs are expected to produce high-quality and grammatically correct outputs. It presents the adaptive sampling with approximate expected futures (ASAp) method, which is designed to enhance the quality of the output by adjusting the conditional probability to align with the model’s original output distribution while ensuring grammaticality. Central to ASAp is the development of effective approximations to the expected future grammaticality (EFG). More specifically, ASAp involves an extra sampling process, where the estimates of EFG are iteratively refined after each sampling and will eventually converge to exact EFG in the limit of infinite iterations. Experimental results show that the output probability distribution of ASAp is closer to the original one than that of GCD methods.

Strengths

Advantages of ASAp include facilitating more accurate grammatical predictions. ASAp approximates the desired distribution by considering the potential future grammaticality of sequences, whereas GCD typically samples greedily based on current probabilities without considering the future development of the sequence. This approach allows ASAp to more accurately predict and generate text sequences that comply with grammatical rules.

Weaknesses

The algorithm heavily relies on prior samples to estimate grammatical probabilities, which can be computationally expensive and difficult to manage, especially in cases where the dataset is large or the environment is dynamically changing. Moreover, a fair comparison of decoding speed between ASAp and GCD methods is required, as ASAp needs this sampling process to produce high-quality outputs, but conducting sampling on LLMs is very time-consuming. Only evaluating the quality of the output by KL divergence and expectations is limited because the decoding sampling algorithm, except for the greedy search, does not always obey the probability distribution. For example, when the decoding temperature is set to 1, models may not output tokens with the highest probability. To conduct a more intuitive evaluation, one can choose some constrained decoding tasks with explicit quality scores, such as constrained translation [1] with BLEU and Exact Match, and code generation, which can be evaluated by the passing rate. Since the experiments in this paper are based on prompt-motivated LLMs, a natural step is to fine-tune LLMs and see if they can produce grammatically correct output with high quality. On the other hand, the authors can also try ASAp based on fine-tuned LLMs, which may further boost the performance of ASAp. To enhance the readability of the article, the following suggestions can be considered. 1. It’s more intuitive to give specific input and output examples for SLIA and INV-BV tasks. 2. The names GCD and GAD are too similar to easily differentiate, even after reading the paper. 3. Other typos: In line 289, the word “both” is typed as “bot”. [1] Wang, S., Li, P., Tan, Z., Tu, Z., Sun, M., & Liu, Y. (2022). A Template-based Method for Constrained Neural Machine Translation. arXiv preprint arXiv:2205.11255.

Questions

Given previous samples with the prefix $w_{1:i}$, there exist abundant future tokens beyond just the next token. However, ASAp only uses the next token likelihoods to facilitate a better estimate of EFG. Why not use other future tokens?

Rating

5

Confidence

4

Soundness

2

Presentation

3

Contribution

3

Limitations

Yes

Reviewer CJH37/10 · confidence 3/52024-07-12

Summary

This paper proposes adaptive sampling with approximate expected futures (ASAp) for grammar-aligned decoding for LLMs. The main objective of this method is to match the conditional probability of the LLM’s distribution conditioned on the given grammar. The evaluation is performed on code generation and structured NLP tasks showing a better likelihood for LLM’s outputs being grammar-constrained.

Strengths

- The method is well-formalized and clear. The examples are very useful for faster understanding - The method is well-motivated (there isn’t an independent motivation section but each choice in the design of the method is appropriately justified) - The paper provides empirical validation that the method works and that it improves the benchmark scores

Weaknesses

- Nothing in particular. Maybe I have found Section 2 of the paper somewhat difficult to read but it is probably because of my lack of recent readings on CFG.

Questions

I suggest maybe some improvement in Section 2. Restructuring it maybe and providing slightly more context on CFG (or giving a concrete example of how it works as a reminder, I found Fig 1 not illustrative enough), I'm sure I won't be the only reader who doesn't have recent experience with CFG.

Rating

7

Confidence

3

Soundness

4

Presentation

4

Contribution

3

Limitations

Yes, limitations are discussed. What I liked in particular in this paper is that the limitations of ASAp are well exposed and discussed throughout the paper and not just in the limitations section.

Reviewer cHBv6/10 · confidence 4/52024-07-12

Summary

This paper points out that prior methods on grammar-constrained decoding (GCD) distorts the language model's learned distribution over sequences. At the heart of this exposition is the notion of _expected future grammaticality_ (EFG), where prior GCD methods can be cast an an upper-bound approximation to the EFG. To ameliorate this problem, the authors proposed the ASAp method: through many iterations, the EFG of a prefix can be better estimated.

Strengths

This is an important finding that conventional methods on grammar-constrained decoding distorts the language model's learned distribution. As far as I know, this important fact has not been pointed out before. The notion of expected future grammatically will be impactful in the area of sequence decoding.

Weaknesses

The proposed method seems very slow to run, requiring many iterations. Additionally, it requires storing a table of all seen prefixes and their future grammatically, which would be very large if the grammar itself is large. It seems to me that the ASAp method only depends on a grammar -- it does not specifically requires a training or test set to run. I wonder if it would be possible to first sample extensively and train the ASAp EFG estimates, then decode on a set. In this way ASAp can be cast as a preprocessing step of the grammar: if it is very slow, it wouldn't matter much since it'll only be processed once. For example we are doing NL2SQL under many conditions. The target grammar stays the same: SQL. One can utilize a LM and generate massive amount of prefixes that has EFG > 0, then compute this estimation of $\tilde c_S$ for all these distinct prefixes.

Questions

- L10: "prorblem" => "problem" - Missing related work: R Shin, et al (2021): Constrained language models yield few-shot semantic parsers. https://aclanthology.org/2021.emnlp-main.608/ - Alg 1: What is "ancestral sampling"? Please explain.

Rating

6

Confidence

4

Soundness

4

Presentation

3

Contribution

3

Limitations

The authors adequately discussed the limitations of their work.

Reviewer gp9U2024-08-12

Thank you for your response, which has addressed some of my concerns. I think this is an interesting work. However, I will maintain my rating score as the issues warrant consideration in a major revision of this submission.

Reviewer cHBv2024-08-12

Thanks for the response. I will keep my score since I believe that some additional work (e.g. training a neural estimate of EFG) will make this paper more contained.

Program Chairsdecision2024-09-25

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC