[Paper Review] XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models
XGrammar introduces a flexible, efficient CFG-based structured generation engine for LLMs by dividing tokens into context-independent and context-dependent sets, with adaptive caching, persistent stacks, context expansion, and CPU-GPU overlap to achieve large speedups over prior methods.
The applications of LLM Agents are becoming increasingly complex and diverse, leading to a high demand for structured outputs that can be parsed into code, structured function calls, and embodied agent commands. These developments bring significant demands for structured generation in LLM inference. Context-free grammar is a flexible approach to enable structured generation via constrained decoding. However, executing context-free grammar requires going through several stack states over all tokens in vocabulary during runtime, bringing non-negligible overhead for structured generation. In this paper, we propose XGrammar, a flexible and efficient structure generation engine for large language models. XGrammar accelerates context-free grammar execution by dividing the vocabulary into context-independent tokens that can be prechecked and context-dependent tokens that need to be interpreted during runtime. We further build transformations to expand the grammar context and reduce the number of context-independent tokens. Additionally, we build an efficient persistent stack to accelerate the context-dependent token checks. Finally, we co-design the grammar engine with LLM inference engine to overlap grammar computation with GPU executions. Evaluation results show that XGrammar can achieve up to 100x speedup over existing solutions. Combined with an LLM inference engine, it can generate near-zero overhead structure generation in end-to-end low-LLM serving.
Motivation & Objective
- Motivate the need for reliable structured generation outputs (e.g., JSON, SQL, DSLs) in LLM inference.
- Propose a CFG-based constrained decoding engine that reduces runtime overhead for structured outputs.
- Develop techniques to precompute context-independent tokens and accelerate runtime checks.
- Create a persistent stack and context expansion to speed up context-dependent token validation.
- Demonstrate integration with LLM serving to achieve end-to-end speedups in constrained generation.
Proposed method
- Divide the vocabulary into context-independent and context-dependent tokens at each pushdown automaton position.
- Precompute and cache context-independent token validity in an adaptive token mask cache.
- Expand grammar context to reduce the number of context-dependent tokens via context expansion.
- Implement a persistent execution stack to enable fast branching and rollback of PDA states.
- Overlap mask generation with GPU-based LLM inference to minimize overall overhead.
- Co-design the grammar engine with LLM serving to achieve near-zero overhead during end-to-end generation.

Experimental results
Research questions
- RQ1How can constrained decoding for CFG-based structured generation be made efficient in LLM inference?
- RQ2What cache and data-structure designs best reduce runtime token checks for CFG constraints?
- RQ3To what extent can grammar context expansion and persistent stacks reduce context-dependent token checks?
- RQ4How well can CPU-GPU overlap mitigate overhead in structured generation when deployed in end-to-end LLM serving?
- RQ5What end-to-end speedups are achievable when integrating XGrammar with existing LLM frameworks?
Key findings
- XGrammar achieves up to 100x reduction in per-token latency for CFG constrained generation versus current state-of-the-art methods.
- Combined with an LLM inference engine, XGrammar attains up to 80x speedup in end-to-end structured generation on Llama-3.1 models.
- Adaptive token mask caching reduces context-independent token checks to under 40 μs per token.
- Context expansion reduces context-dependent tokens by about 90% for JSON grammar on Llama-3.1 with JSON grammar.
- A persistent execution stack enables fast state branching and rollback, improving preprocessing speed and token checks.
- Overlapping mask generation with GPU inference yields near-zero overhead for structured generation in end-to-end serving.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.