Efficient Grammar-Constrained Decoding via Parser Stack Classification
Yongmin Li, Yihong Dong, Jia Li, Ge Li
Key points
From the paper's conclusion- To address the issue of linear latency scaling with vocabulary size in existing grammar-constrained decoding (GCD) methods, we propose PSC, a novel grammar-constrained decoding method.
- PSC computes masks up to 700× faster than baselines on complex programming language grammars, and up to 30× faster for schema-conformant JSON; end-to-end LLM throughput with PSC approaches that of unconstrained decoding.
- PSC computes masks up to 700× faster than baselines on complex programming language grammars.
Abstract
LLMs are widely used to generate structured output like source code or JSON. Grammar-constrained decoding (GCD) can guarantee the syntactic validity of the generated output, by masking out tokens that violate rules specified by a context-free grammar. However, the online computational overhead of existing GCD methods, with latency typically scaling linearly with vocabulary size, limits the throughput of LLMs, especially for models with large vocabularies. To address this issue, we propose PSC, a novel grammar-constrained decoding method. By combining acceptance conditions of all vocabulary tokens into a single classifier of the parser stack during preprocessing, PSC can compute the complete vocabulary mask by checking the parser stack exactly once per decoding step, with time complexity independent of the vocabulary size. Experiments show that PSC computes masks up to 700× faster than baselines on complex programming language grammars, and up to 30× faster for schema-conformant JSON; end-to-end LLM throughput with PSC approaches that of unconstrained decoding. We analyze the preprocessing overhead for preprocessing providers and decoding users, and provide a break-even point analysis to help users decide whether to do preprocessing by themselves.
Read the paper
Discussion · 1
Science Skim's AI key points, plus readers' public thoughts
- To address the issue of linear latency scaling with vocabulary size in existing grammar-constrained decoding (GCD) methods, we propose PSC, a novel grammar-constrained decoding method.
- PSC computes masks up to 700× faster than baselines on complex programming language grammars, and up to 30× faster for schema-conformant JSON; end-to-end LLM throughput with PSC approaches that of unconstrained decoding.
- PSC computes masks up to 700× faster than baselines on complex programming language grammars.
Readers' thoughts
No reader thoughts yet.
Related papers
- Grammar-as-a-Draft: More Tokens per Speculative Decoding Step on Grammar-Constrained Structured OutputsarXiv · 2026
- From Grammar Deltas to Token Masks: Incremental Constrained Decoding under Runtime Grammar EvolutionarXiv · 2026
- Breaking the Space Barrier and its Application to Language Model InferencearXiv · 2026
- DConstrain: Compiling Context-Parameterized Transitions for Diffusion Language Model Constrained DecodingarXiv · 2026
- Lookahead-Then-Verify: Reliable Constrained Decoding for Diffusion LLMs under Context-Free GrammarsProceedings of the ACM on Software Engineering · 2026
- SKIM: Pruning Large Language Model Agents via Selective Knowledge Informed MaskingarXiv · 2026