XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models
Yixin Dong, Charlie F. Ruan, Yaxing Cai, Ruihang Lai, Ziyi Xu, Yilong Zhao, Tianqi Chen
TL;DR
XGrammar tackles the overhead of constrained generation for large language models by using a byte-level pushdown automaton to represent context-free grammars and by dividing tokens into context-independent and context-dependent classes. It introduces an adaptive token mask cache, a persistent execution stack, and context expansion to drastically reduce runtime checks, while overlapping grammar computation with GPU inference to minimize overhead. The system delivers up to 100x per-token speedups and up to 80x end-to-end serving improvements, with strong gains in syntactic correctness for structured outputs. The work is open-sourced and designed for integration with major LLM frameworks, enabling scalable, structure-aware generation across diverse applications.
Abstract
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.
