From Grammar Deltas to Token Masks: Incremental Constrained Decoding under Runtime Grammar Evolution
Organizations: College of Computer Science and Technology, National University of Defense Technology · College of Systems Engineering, National University of Defense Technology
Abstract
Grammar-constrained decoding enables large language models to generate outputs that satisfy formal constraints, but in code and domain-specific language generation, the relevant constraints are not always fully available before decoding and may emerge progressively from new declarations, inferred type information, or runtime analysis feedback. Existing structured-decoding engines efficiently handle fixed, predeclared, or previously seen constraint structures; however, when a first-seen grammar update changes the parser state induced by an already committed prefix, the straightforward solution reconstructs the constraint state under the updated grammar, repeating parsing work already performed over the prefix. We introduce GramDelta, an incremental framework for constrained decoding with append-only runtime grammar updates. GramDelta maintains parse state over the committed prefix and propagates only changes induced by newly added grammar rules through historical dependencies, avoiding prefix replay. It then maps the updated parse state to exact next-token masks via compilation-free factorized projection over shared historical charts, avoiding per-update mask-cache recompilation. GramDelta therefore preserves the result of full reconstruction while making update cost sensitive to the actual impact of a grammar change. Experiments across four model configurations show that GramDelta adds negligible overhead without updates; under sustained runtime grammar evolution, it reduces median first-seen grammar-update latency by 10.7--11.5x and reduces the additional TPOT overhead over unconstrained decoding by 53.1--81.9% relative to the strongest reconstruction baseline. These results enable efficient constrained decoding when constraints progressively emerge and evolve with the generated prefix.
Figures & tables
| Model | Method | Update Lat. (ms) | TPOT (ms/tok) | TPOT Ovhd. (%) |
|---|---|---|---|---|
| Qwen3-8B | Unconstrained | – | 19.645 | 0.0 |
| XGrammar | 500.680 | 124.953 | 536.1 | |
| llguidance | 49.437 | 27.078 | 37.8 | |
| Fresh | 42.066 | 28.855 | 46.9 | |
| GramDelta | 3.817 | 21.174 | 7.8 | |
| Qwen3-32B | Unconstrained | – | 61.117 | 0.0 |
| Variant | Narrow Upd. | Wide Proj. | GPU Update | GPU TPOT |
|---|---|---|---|---|
| (ms) | (ms) | (ms) | (ms/tok) | |
| Fresh (Reconstruct + Replay) | 2.398 | 0.038 | 41.324 | 29.659 |
| GramDelta w/o HistWait | 0.126 | 0.036 | 6.316 | 21.233 |
| GramDelta w/o Shared History | 13.771 | 0.752 | 193.368 | 242.196 |
| GramDelta w/o Complement Partition | 0.109 | 543.081 | 3.852 | 21.240 |
| Full GramDelta | 0.104 | 0.034 | 3.757 | 21.208 |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
| Model | Method | Update p50 [CI] | Upd. p95 | TPOT p50 [CI] | TPOT Ovhd. [CI] | |
|---|---|---|---|---|---|---|
| (ms) | (ms) | (ms/tok) | (%) | |||
| 16 | Qwen3-8B | Unconstrained | – | – | 19.41 [19.40, 19.42] | 0.0 |
| XGrammar | 493.01 [488.85, 495.61] | 518.79 | 79.51 [79.21, 79.73] | 309.6 [308.0, 310.7] | ||
| XGrammar-Cache | 636.64 [635.03, 639.35] | 716.12 | 98.08 [97.52, 98.68] | 405.3 [402.4, 408.3] | ||
| llguidance | 49.76 [48.97, 50.63] | 57.31 | 23.93 [23.75, 24.08] | 23.3 [22.3, 24.0] | ||
| Fresh | 40.09 [39.50, 40.59] | 149.43 | 25.22 [25.20, 25.47] | 29.9 [29.8, 31.2] |
| Model | Method | Update p50 [CI] | Upd. p95 | TPOT p50 [CI] | TPOT Ovhd. [CI] |
|---|---|---|---|---|---|
| (ms) | (ms) | (ms/tok) | (%) | ||
| Qwen3-8B | Unconstrained | – | – | 19.70 [19.42, 19.91] | 0.0 |
| XGrammar | 362.07 [359.35, 365.79] | 391.88 | 78.90 [78.54, 80.57] | 300.5 [297.5, 307.4] | |
| XGrammar-Cache | 376.24 [373.15, 382.09] | 425.65 | 82.41 [81.90, 83.18] | 318.3 [313.3, 324.9] | |
| llguidance | 42.40 [41.59, 44.77] | 50.43 | 24.19 [23.97, 24.79] | 22.8 [21.4, 25.9] | |
| Fresh | 33.67 [32.95, 34.93] | 134.89 | 26.55 [26.38, 26.78] | 34.8 [33.3, 36.8] |
| Model | Uncon. | GD (Idx On) | GD (Idx Off) | XGrammar | llguidance | Index Tax |
|---|---|---|---|---|---|---|
| ( / ) | ( / ) | ( / ) | ( / ) | ( / ) | ( / ) | |
| Qwen3-8B | 17.79 / 18.85 | 18.05 / 19.19 | 18.03 / 19.19 | 17.94 / 19.01 | 18.03 / 19.09 | / |
| Qwen3-32B | 57.10 / 59.50 | 57.45 / 59.96 | 57.44 / 59.96 | 57.32 / 59.71 | 57.42 / 59.83 | / |
| Granite-4.1-3B | 15.18 / 15.14 | 15.36 / 15.45 | 15.34 / 15.41 | 15.30 / 15.23 | 15.39 / 15.40 | / |
| Granite-4.1-8B | 18.02 / 19.29 | 18.26 / 19.64 | 18.25 / 19.63 | 18.15 / 19.44 | 18.25 / 19.54 | / |
| Workload | Stage | Method (ms) | ||||
|---|---|---|---|---|---|---|
| GramDelta | Fresh | llguidance | XGrammar | XGrammar-Cache | ||
| BFCL | Dispatch + host preparation | 1.487 | 38.658 | 12.256 | 12.521 | 12.381 |
| Backend construction/update | 1.480 | 10.918 | 23.001 | 514.676 | 663.804 | |
| Prefix replay | 0.000 | 7.235 | 20.655 | 1.927 | 2.330 | |
| Historical lookup/closure | 0.001 | 0.000 | 0.000 | 0.000 | 0.000 | |
| Token projection | 0.098 | 0.054 | 0.062 | 0.008 | 0.008 | |
Grammar-Constrained Decoding
Grammar-constrained decoding uses formal grammars to restrict language-model outputs to valid structures such as code, SQL, and JSON.
See all 34 papers