← Latest papers
🤖 AI

Accelerating Constrained Decoding with Token Space Compression

This paper introduces CFGzip, an offline token space compression technique that significantly reduces the computational overhead of constrained decoding, achieving up to a 7.5x speedup in total generation time for complex context-free grammars.

Original authors: Michael Sullivan, Alexander Koller

Published 2026-05-29
📖 5 min read🧠 Deep dive

Original authors: Michael Sullivan, Alexander Koller

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you have a very talented but slightly chaotic chef (the LLM) who can cook almost anything. However, you need them to prepare a dish that follows a very strict, complex recipe (a Context-Free Grammar or CFG), like a specific programming language or a precise data format.

If the chef guesses the wrong ingredient, the whole dish is ruined. To prevent this, you hire a strict Grammar Engine (like a head chef or a food safety inspector) who stands next to the chef. Before the chef adds any ingredient, the inspector checks the entire pantry to see if that specific ingredient is allowed at this exact step in the recipe.

The Problem: The "Pantry" is Too Big

The problem is that the chef's pantry (the token vocabulary) is massive, containing hundreds of thousands of different ingredients (words, symbols, code snippets).

Every time the chef wants to add one ingredient, the inspector has to run through the entire pantry to check if that specific item is valid. For simple recipes (like JSON data), this is fast. But for complex recipes (like C++ code or a made-up language called "Bython"), the inspector gets overwhelmed. They have to check so many possibilities that the cooking process slows down drastically—sometimes taking 2 to 10 times longer than normal. The paper calls this "intractably high overhead."

The Solution: CFGZIP (The "Grouping" Trick)

The authors introduce a new tool called CFGZIP. Instead of making the inspector check every single ingredient in the pantry, CFGZIP reorganizes the pantry before cooking even starts.

Here is the analogy:

  1. Grouping Ingredients: CFGZIP looks at the pantry and realizes that many ingredients are interchangeable for the purpose of the recipe. For example, in a specific part of a code recipe, the words if, else, and while might all act the same way grammatically. Or, in a different context, the numbers 1, 2, and 3 might all be valid placeholders.
  2. Creating "Representative" Buckets: CFGZIP groups these interchangeable ingredients into buckets. It picks one "representative" ingredient from each bucket (usually the shortest one) to stand in for the whole group.
  3. The New Workflow:
    • Before Cooking (Offline): The system does the hard work of sorting the pantry into these buckets. This is done once and saved.
    • During Cooking (Inference): When the chef picks an ingredient, the system quickly swaps it for its "representative" from the bucket. The inspector only has to check the representative against the recipe, not the whole pantry.
    • The Result: Because the inspector is now checking a tiny list of representatives instead of the massive original pantry, the process becomes incredibly fast.

Why This is a Big Deal

The paper claims that using CFGZIP with a top-tier grammar engine (XGrammar2) creates a massive speedup:

  • Latency Reduction: The time it takes to check the rules drops by 10 to 100 times (two orders of magnitude).
  • Total Speedup: The entire process of generating the text becomes 7.5 times faster for complex tasks.
  • No Quality Loss: This is a "lossless" compression. The final output is byte-for-byte identical to what you would get without the speedup. The chef still produces the exact same perfect dish; they just got there much faster.

Real-World Results from the Paper

The researchers tested this on three different AI models (Llama, Qwen, and GPT) and four different tasks:

  1. JSON & XML: Standard data formats.
  2. C++: A complex programming language.
  3. Bython: A fictional, made-up programming language (similar to Python but with braces and semicolons instead of spaces).

The Findings:

  • For standard formats (JSON), the speedup was good but not revolutionary because those rules are already simple.
  • For complex and unfamiliar languages (like C++ and Bython), the difference was huge. Without CFGZIP, the grammar engine was so slow it made the AI practically unusable for these tasks. With CFGZIP, the AI could generate complex code quickly and correctly.
  • Interestingly, for the "Bython" task (which the AI had never seen before), using this constrained method improved the AI's ability to write working code from 2.3% to 46.9% (for one model), proving that strict rules help AI when the task is hard.

The Catch (Limitations)

The paper notes one main limitation: Preparation Time.
Sorting the pantry into buckets (the "offline pre-computation") takes time.

  • If you need to generate a JSON file for a one-off, quick task, the time it takes to sort the pantry might be longer than just doing the task normally.
  • However, if you are doing large-scale code generation or using the same complex rules over and over again, the initial setup time is worth it because the cooking (generation) becomes so much faster.

Summary

CFGZIP is like a smart librarian who reorganizes a massive library into "topic buckets" before you arrive. Instead of you searching every single book to find the right one, the librarian just points you to the "topic bucket" representative. This makes finding the right information (or in this case, generating the right code) dramatically faster without ever losing a single book or changing the story.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →