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:
- The Model Drafter: A standard small model (like an n-gram or a Multi-Token Prediction head) makes its usual guesses.
- 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
GRAMMARslot. - 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
GRAMMARslots automatically without checking the target model's math (logits). This saves a significant amount of computation.
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.
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