Gram2Token: Enabling Run-time GPU-Native Grammar-Constrained Decoding for LLMs

Hantao Hua, Jiming Su, Hao Tang, Yiping Yao, Feng ZhuICML 2026

Large Language Models (LLMs) are increasingly being used as the "engine" for structured tasks like generating JSON for APIs or SQL for database queries. However, LLMs are inherently probabilistic text generators, not rigid parsers. To ensure they don't hallucinate a missing bracket or a malformed command, researchers use Grammar-Constrained Decoding (GCD).

The problem? Most current GCD systems are slow. They act like a backseat driver, checking every single byte the model produces against a grammar rule. This creates a massive bottleneck between the CPU (doing the checking) and the GPU (doing the generating).

A new paper, "Gram2Token: Enabling Run-time GPU-Native Grammar-Constrained Decoding for LLMs," proposes a clever shift: move the grammar logic out of the "live" generation loop and into a preprocessing step.

TL;DR

Gram2Token eliminates the CPU-GPU synchronization bottleneck in structured LLM generation by preprocessing byte-level grammar rules into GPU-resident token transition tables. This approach achieves up to a 1.85× throughput improvement over state-of-the-art baselines, making high-speed, perfectly formatted LLM serving a reality.

What Problem Does This Paper Address?

The core issue is the "Byte-Token Mismatch." Formal grammars (like those for JSON or SQL) operate on characters or bytes. LLMs, however, operate on "tokens"—chunks of text that can be multiple bytes long.

Existing systems like XGrammar or Pre3 handle this by running a parser inside the decoding loop. Every time the LLM wants to pick a next token, the system has to traverse the grammar byte-by-byte to see which tokens are valid. This requires the GPU to stop and wait for the CPU to finish its logic, which kills performance—especially when you are trying to process hundreds of requests at once (continuous batching).

Methods

The authors introduce Gram2Token, which treats grammar enforcement as a simple table lookup rather than a live parsing task.

  1. Token-Level Preprocessing: Before the LLM starts "thinking," Gram2Token uses a tokenizer trie to map out how every possible token in the model's vocabulary interacts with the grammar. It pre-calculates the "outcome" (is it valid? what is the next grammar state?) for every token.
  2. State-Aware Token Categorization: LLM vocabularies are huge (often 100k+ tokens). Storing a transition table for every token-state pair would eat up all the GPU memory. The authors realized that many tokens are "functionally equivalent" for a given grammar. For example, in a JSON schema expecting a string, the tokens for "apple" and "banana" behave identically. Gram2Token groups these into categories, drastically shrinking the size of the lookup tables.
  3. GPU-Native Execution: At runtime, the grammar logic is reduced to three simple GPU operations: a category lookup, a bitmask application (to hide invalid tokens), and a state update. No CPU intervention is required during the steady-state generation.

Datasets

The researchers evaluated Gram2Token using two primary benchmarks for structured generation:

  • JSON-mode-eval: A dataset designed to test if models can strictly follow complex JSON schemas.
  • StructEval: A benchmark covering various structured formats, including SQL queries.

They tested these across four popular model families: LLaMA3-8B, Qwen3-8B, DeepSeek-R1-Distill-Qwen-7B, and Mistral-7B-Instruct.

Key Insights and Ideas

  • Moving the "Critical Path": The biggest "aha moment" is realizing that grammar logic is deterministic. If you know the grammar and the tokenizer, you can solve the puzzle before the model even starts. By moving the complexity to a preprocessing phase, the "live" generation becomes "GPU-native."
  • Amortization is King: Preprocessing isn't free—it adds a bit of delay before the first token appears (TTFT). However, the paper shows that this cost is quickly "paid off" (amortized) as the model generates longer sequences or handles larger batches.
  • Categorization as Compression: The use of token categories isn't just a memory-saving trick; it’s what makes the system scalable. It allows the system to handle complex, nested grammars without the lookup tables exploding in size.

Key Results

  • Throughput Boost: Gram2Token achieved a geometric-mean throughput improvement of 1.38× over the strongest baseline (Pre3), with peak speedups reaching 1.85×.
  • Scalability: Unlike previous methods that slow down significantly as batch sizes increase, Gram2Token maintains "near-native" performance, making it ideal for high-volume production environments.
  • Break-Even Point: For a batch size of 64, Gram2Token becomes faster than the best baseline after generating just 227 tokens. In very large batches (512), the break-even point is a mere 46 tokens.

Limitations and Future Work

  • Deterministic Grammars Only: The current system focuses on grammars that can be represented by a Deterministic Pushdown Automaton (DPDA). It doesn't yet support "ambiguous" grammars that might require backtracking.
  • Cold-Start Overhead: For very short, one-off tasks with a brand-new grammar, the preprocessing time might outweigh the generation speedup.
  • Memory vs. Complexity: While categorization helps, extremely deep or recursive grammars could still lead to large table sizes. The authors suggest a "parser-driven fallback" for states that fall outside the preprocessed region as a potential solution.
  • Future Directions: The authors note that Gram2Token is naturally compatible with speculative decoding, where the precomputed tables could help quickly verify "draft" tokens.

Summary updated

BibTeX
@inproceedings{hua2026gram,
  author = {Hantao Hua and Jiming Su and Hao Tang and Yiping Yao and Feng Zhu},
  title = {{Gram2Token: Enabling Run-time GPU-Native Grammar-Constrained Decoding for LLMs}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=h3K23f6tLU},
}
Figure