Grammar-as-a-Draft: More Tokens per Speculative Decoding Step on Grammar-Constrained Structured Outputs

John Zhang, Silky Arora

Imagine you’re watching a high-speed typist transcribe a form. They’re incredibly fast, but every time they reach a field like "First Name":, they hesitate, occasionally misspelling the label or adding an extra space, only to have a supervisor tap them on the shoulder and say, "No, do it exactly like the template."

In the world of Large Language Models (LLMs), this is exactly what happens during speculative decoding for structured outputs (like JSON or code). We use a small, "cheap" drafter model to guess the next few tokens, and then a large "target" model verifies them. But these drafters are often "grammar-blind." They waste precious compute cycles guessing keys, quotes, and colons that the grammar engine already knows with 100% certainty.

This paper introduces Grammar-as-a-Draft (GaaD), a clever way to stop guessing the obvious and start using the grammar as a co-pilot.

TL;DR

GaaD turns the grammar engine into a second "drafter" that works alongside traditional speculative models. By merging guaranteed grammar tokens into the draft, it significantly increases the number of tokens accepted per step, resulting in up to a 2.8x speedup for structured tasks like JSON generation.

What Problem Does This Paper Address?

When an LLM generates structured data (like a JSON object), it follows a strict set of rules—a grammar. While the values (like a person's name) are creative, the structure (the keys, the braces, the indentation) is deterministic.

Standard speculative decoding uses a small model to propose a sequence of tokens. However, because this small model doesn't "see" the grammar, it often proposes tokens that are technically invalid (e.g., a lowercase key when the schema requires uppercase). These drafts are rejected, forcing the system to fall back to the slow target model. The authors realized that the grammar engine already knows exactly what tokens must come next for these fixed parts of the output. Why guess when you already have the answer key?

Methods

GaaD modifies the "scheduler" of the inference engine to merge two different draft sources into a single row of tokens:

  1. The Model Drafter: A standard small model (like an n-gram or a Multi-Token Prediction head) makes its usual guesses.
  2. The Grammar Drafter: The grammar engine identifies "forced" strings—sequences of text that are the only valid continuation of the current state.

The system merges them using a simple set of rules:

  • If both agree on a token, it's kept and flagged as a GRAMMAR slot.
  • If they disagree, the model's draft is cut short, and the grammar fills the remaining slots in the draft row.
  • Crucially, the "verify" step is updated to accept GRAMMAR slots automatically without checking the target model's math (logits). This saves a significant amount of computation.
Figure 2: Combining the grammar’s draft with a model drafter’s (Qwen tokenization). An illustration: the drafter proposes five tokens into a row of K=7, whereas the evaluated configurations use K=8 with KM=3 for MTP and KM=7 for DFlash and n-gram. Model tokens at free positions are kept as M, those that agree with a forced token as G; the model draft stops where the two disagree, and forced tokens continue from there.

Datasets

The authors evaluated GaaD using a JSON character-profile benchmark. This involves 50 unique prompts (cycled to create larger workloads) that require the model to extract information into a complex JSON schema with eleven fixed keys, nested objects, and specific data types (enumerations, numbers, and free-text).

Key Insights and Ideas

  • The Grammar is the Cheapest Drafter: Most speculative decoding research focuses on making better models. GaaD shows that for structured data, the most efficient "model" is just the rulebook you're already using to validate the output.
  • Tokenization Drift: This is a fascinating "aha" moment. LLMs can often represent the same text using different combinations of tokens (e.g., four individual spaces vs. one "four-space" token). GaaD forces the "canonical" (most efficient) tokenization.
  • Lossless-ish Decoding: Standard speculative decoding is mathematically "lossless"—it produces the exact same probability distribution as the large model alone. GaaD technically breaks this by forcing specific tokenizations. However, the authors show that the resulting text is still valid and just as likely to be "correct" as the original.

Key Results

The performance gains are substantial, particularly when the model is generating one response at a time (Batch Size 1):

  • Acceptance Length: The average number of tokens accepted per step jumped from ~2.0 to ~5.4 for some models.
  • Speedups: GaaD achieved 2.66x to 2.81x faster decoding at batch size 1 across various drafter types (MTP, DFlash, n-gram).
  • Scalability: Even at a batch size of 64, GaaD remained 1.14x to 1.70x faster than the best standalone drafter configurations.
The speedup of GaaD over standard speculative decoding. While the advantage is highest at small batch sizes, it remains significant even as the workload increases.

Limitations and Future Work

  • The "Tax" on Plain Text: In "mixed traffic" scenarios (where some requests are JSON and others are plain text), the plain-text requests actually slow down slightly (7–42%). This is because the whole batch has to wait for the host to plan the grammar-based draft.
  • Synchronous Bottleneck: Currently, the system has to wait for the previous step to finish before it can plan the next grammar draft. Moving this planning onto the GPU/TPU hardware could unlock even more speed.
  • Narrow Benchmark: The results are currently focused on a specific JSON task. While JSON is the "lingua franca" of structured LLM output, testing on other formats like SQL or specific programming languages would be a natural next step.

Summary updated

Abstract

Under structured outputs (JSON schemas, regexes, tool calls), speculative drafters do not see the grammar constraint and repeatedly propose invalid keys, delimiters, and indentation, while the grammar engine already knows which tokens must come next. We present Grammar-as-a-Draft (GaaD), which treats the grammar as a second drafter. Before each verify step, the scheduler merges the drafter's proposal with grammar-forced tokens into one draft row: matching tokens stay as GRAMMAR, the proposal is cut where it leaves the forced path, and forced tokens fill the rest of a row wider than the drafter's proposal. The verify step accepts GRAMMAR drafts without computing their logits, composing with any drafter with no new kernels or compilation on the critical path. Because grammars fix text rather than tokens, committing canonical forced tokens changes the output distribution relative to grammar-masked target decoding. We implement GaaD in vllm-torchtpu and an independent Rust serving runtime, evaluating on TPU v7x with MTP, DFlash, and n-gram drafters on a JSON character-profile benchmark. GaaD raises average acceptance length from 2.06 to 5.43 (MTP, Qwen3.5-0.8B), 1.99 to 5.31 (MTP, Qwen3.5-35B-A3B), 1.57 to 4.13 (DFlash, Qwen3-4B), and 1.30 to 3.64 (n-gram, Qwen3-4B), decoding 2.66-2.81x faster at batch size 1 and 1.14-1.70x at batch size 64. In Rust, MTP+GaaD is 2.23x faster on Qwen3.5-0.8B, 2.02x on Qwen3.5-397B-A17B-FP8, and 1.22x at 128 concurrent requests. Under mixed traffic (25-50% structured), GaaD finishes the workload 1.03-1.23x sooner (structured requests 2.2-3.2x sooner) while plain-text TPOT rises 7-42%. We also show grammar-masked greedy decoding can pick non-canonical tokenizations, so GaaD can yield different valid outputs that an fp32 reference finds equally likely.
BibTeX
@misc{zhang2026grammar,
  author = {John Zhang and Silky Arora},
  title = {{Grammar-as-a-Draft: More Tokens per Speculative Decoding Step on Grammar-Constrained Structured Outputs}},
  year = {2026},
  eprint = {2611.28349},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.28349},
}
Figure