Flexible and Efficient Grammar-Constrained Decoding

Fuente: arXiv
Saved in:
Bibliographic Details
Main Authors: Park, Kanghee, Zhou, Timothy, D'Antoni, Loris
Format: Preprint
Published: 2025
Subjects:
Online Access:
Tags: Add Tag
No Tags, Be the first to tag this record!
_version_ 1866915391469518848
author Park, Kanghee
Zhou, Timothy
D'Antoni, Loris
author_facet Park, Kanghee
Zhou, Timothy
D'Antoni, Loris
contents Large Language Models (LLMs) are often asked to generate structured outputs that obey precise syntactic rules, such as code snippets or formatted data. Grammar-constrained decoding (GCD) can guarantee that LLM outputs matches such rules by masking out tokens that will provably lead to outputs that do not belong to a specified context-free grammar (CFG). To guarantee soundness, GCD algorithms have to compute how a given LLM subword tokenizer can align with the tokens used by a given context-free grammar and compute token masks based on this information. Doing so efficiently is challenging and existing GCD algorithms require tens of minutes to preprocess common grammars. We present a new GCD algorithm together with an implementation that offers 17.71x faster offline preprocessing than existing approaches while preserving state-of-the-art efficiency in online mask computation.
format Preprint
id arxiv_https___arxiv_org_abs_2502_05111
institution arXiv
publishDate 2025
record_format arxiv
spellingShingle Flexible and Efficient Grammar-Constrained Decoding
Park, Kanghee
Zhou, Timothy
D'Antoni, Loris
Computation and Language
Artificial Intelligence
Large Language Models (LLMs) are often asked to generate structured outputs that obey precise syntactic rules, such as code snippets or formatted data. Grammar-constrained decoding (GCD) can guarantee that LLM outputs matches such rules by masking out tokens that will provably lead to outputs that do not belong to a specified context-free grammar (CFG). To guarantee soundness, GCD algorithms have to compute how a given LLM subword tokenizer can align with the tokens used by a given context-free grammar and compute token masks based on this information. Doing so efficiently is challenging and existing GCD algorithms require tens of minutes to preprocess common grammars. We present a new GCD algorithm together with an implementation that offers 17.71x faster offline preprocessing than existing approaches while preserving state-of-the-art efficiency in online mask computation.
title Flexible and Efficient Grammar-Constrained Decoding
topic Computation and Language
Artificial Intelligence
url https://arxiv.org/abs/2502.05111