Smart token caching helps LLMs speak JSON and SQL without breaking a sweat.
XGrammar introduces a high-performance engine for structured LLM outputs, dividing tokens into context-independent and context-dependent categories. This enables efficient constrained decoding through adaptive caching and persistent execution stacks, achieving 100x speedup over existing solutions.
-----
https://arxiv.org/abs/2411.15100
π€ Original Problem:
β LLM applications need structured outputs like JSON, SQL, and function calls, but current context-free grammar approaches face performance bottlenecks due to stack state tracking and vocabulary checking overhead.
-----
π οΈ Solution in this Paper:
β XGrammar splits vocabulary tokens into context-independent ones (prechecked and cached) and context-dependent ones (checked at runtime).
β It uses an adaptive token mask cache with smart storage formats to minimize memory usage.
β A persistent execution stack enables fast state branching and rollback for efficient token validation.
β The system overlaps grammar computations with GPU operations to minimize overhead.
-----
π‘ Key Insights:
β Most tokens can be validated independently of stack state
β Adaptive storage formats reduce memory usage to 0.2% of original size
β Context expansion reduces context-dependent tokens by 90%
-----
π Results:
β Up to 100x reduction in per-token latency compared to existing methods
β 80x speedup in end-to-end LLM serving with structured output on H100 GPU
β Near-zero overhead for structured generation










