From Grammar Deltas to Token Masks: Incremental Constrained Decoding under Runtime Grammar Evolution
Authors: Hantao Hua, Jiming Su, Yonggang Che, Feng Zhu
Organizations: College of Computer Science and Technology, National University of Defense Technology · College of Systems Engineering, National University of Defense Technology
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
Figure 1: Overview of GramDelta . The top panel shows a committed code prefix, the current grammar, a late grammar update, and the resulting change in the valid next-token set. The middle panel depicts the reference reconstruction path, which replays the committed prefix under the updated grammar. The bottom panel shows the incremental GramDelta pipeline: historical dependency lookup finds the earliest affected waiting positions, incremental Earley closure propagates only newly enabled states while reusing historical completions, and factorized token projection computes the exact updated next-token mask. The completed augmented item @START→Start∙ indicates that the statement is fully accepted after consuming the closing parenthesis.
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
Table 1: Main GPU decoding results across all four model configurations ( BFCL , 8K prefix, K=32 first-seen updates). Update Latency is the median constraint-update time; TPOT Overhead is relative to unconstrained decoding for the same model.
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
Table 2: Component ablation on Qwen3-8B across narrow local CPU updates, wide open-string projection, and 8K,K=32 GPU decoding.
Figure 2: Scaling and locality behavior on Qwen3-8B: (a) TPOT Overhead across first-seen update counts K∈{0,1,4,8,16,32} ( 8K prefix); (b) Update Latency across committed prefix lengths ( 512 – 16K tokens, K=8 ); (c) same-engine Update Latency vs. affected historical chart rows ( 4K prefix); and (d) fused batched decoding throughput across batch sizes B∈{1,4,8} ( 8K,K=8 ).
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
K
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]
Appendix
Table 3: Complete four-model GPU results on BFCL ( 8K prefix, K∈{16,32} ) with 95% bootstrap confidence intervals and p95 update latency.
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]
Appendix
Table 4: Complete four-model GPU results on declaration-driven code synthesis ( CodeEvo , 8K prefix, K=32 ) with 95% bootstrap confidence intervals and p95 update latency.
Model
Uncon.
GD (Idx On)
GD (Idx Off)
XGrammar
llguidance
Index Tax
( 1K / 4K )
( 1K / 4K )
( 1K / 4K )
( 1K / 4K )
( 1K / 4K )
( 1K / 4K )
Qwen3-8B
17.79 / 18.85
18.05 / 19.19
18.03 / 19.19
17.94 / 19.01
18.03 / 19.09
+0.03% / +0.02%
Qwen3-32B
57.10 / 59.50
57.45 / 59.96
57.44 / 59.96
57.32 / 59.71
57.42 / 59.83
+0.01% / +0.00%
Granite-4.1-3B
15.18 / 15.14
15.36 / 15.45
15.34 / 15.41
15.30 / 15.23
15.39 / 15.40
−0.13% / +0.12%
Granite-4.1-8B
18.02 / 19.29
18.26 / 19.64
18.25 / 19.63
18.15 / 19.44
18.25 / 19.54
−0.03% / −0.11%
Appendix
Table 5: Static no-update GPU controls ( K=0 , 30 paired runs per cell) reporting median TPOT ( 1K / 4K , ms/tok) and paired index-retention tax.
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
Appendix
Table 6: Mean update-stage decomposition (ms) for Qwen3-8B on BFCL and CodeEvo .
Department of Computer Engineering, Bahçeşehir University, Istanbul, Turkey · Department of Industrial Engineering, Bahçeşehir University, Istanbul, Turkey