Gram2Token: Enabling Run-time GPU-Native Grammar-Constrained Decoding for LLMs
Abstract
Lay Summary
Large language models are increasingly used to produce structured outputs, such as JSON objects, database queries, and commands that other programs can read. These outputs must follow strict formatting rules: a missing bracket or a wrong structure can make the result unusable even when the content is otherwise correct. Existing constrained decoding systems can keep outputs valid, but they often slow generation down because formatting rules are usually checked character by character, while language models generate larger text pieces at a time, and this rule-checking can require extra coordination between the GPU running the model and other parts of the system. Thus, we propose Gram2Token, a system that moves much of this work before generation begins. It matches formatting rules to the pieces of text a model can generate, groups pieces that behave the same under those rules, and stores the resulting decisions as compact lookup tables on the GPU. During generation, Gram2Token still enforces the rules at every step, but it replaces complex parser-style reasoning with fast table lookups that identify valid next pieces and update the current formatting state. This keeps outputs structurally valid while improving efficiency in high-throughput structured generation, especially when formats are reused, outputs are longer, or many requests are served together. This can make structured generation more practical for AI agents, data tools, and software systems that need to produce reliable machine-readable outputs at scale.