arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2506.01151cs.LGcs.AIcs.CL
收藏

基于Earley算法的动态剪枝以实现高效结构化解码

Earley-Driven Dynamic Pruning for Efficient Structured Decoding

Rice University(莱斯大学)

Xintong Sun, Chi Wei, Minghao Tian, Shiwen Ni

AI总结:

本文提出基于Earley算法的动态剪枝策略ZapFormat及约束解码引擎Formatron,通过实时消除无效状态并缓存状态,在保证结构化输出精度的同时将推理速度提升至最高2倍。

摘要:

大型语言模型(LLMs)已展现出卓越的能力,但确保其输出符合严格的结构或语法约束仍然具有挑战性,这在函数调用和领域特定语言(DSL)生成中至关重要。基于上下文无关文法的约束解码是一种灵活的方法,通过动态构建token logits掩码来保证LLM遵循特定格式。然而,创建该掩码需要在每个解码步骤检查LLM词汇表中所有token的有效性,这在现有约束解码引擎中通常会产生显著开销。为应对这一挑战,我们提出ZapFormat,一种基于Earley算法的新型动态剪枝策略,该策略实时识别并消除无效或冗余的Earley状态,显著减少Earley算法状态的内存占用。这进一步使我们能够使用状态缓存来加速大量查询上的结构化生成。我们在名为Formatron的新约束解码引擎中实现了ZapFormat,该引擎还融合了现有的优化。通过在结构化生成任务(包括JSON生成、JSON Schema验证和语义解析)上的全面实验,我们证明Formatron不仅始终如一地保持高精度合规输出,而且与最先进的实现相比,推理速度实现了高达2倍的显著提升。更重要的是,Formatron普遍适用于各种LLM架构。我们将Formatron以开源形式发布在https://github.com/Dan-wanna-M/formatron。

英文摘要:

Large Language Models (LLMs) have shown remarkable capabilities, yet ensuring their outputs conform to strict structural or grammatical constraints remains challenging, which is critical in function calls and domain-specific language (DSL) generation. Constrained decoding with context-free grammar is a flexible approach to guarantee LLMs' adherence to a specific format by dynamically building a token logits mask. However, creating this mask requires checking the validity of all tokens in the LLM vocabulary at every decoding step, which often incurs significant overheads in existing constrained decoding engines. To address this challenge, we propose $\textbf{ZapFormat}$, a novel $\textbf{dynamic pruning}$ strategy based on the Earley algorithm that identifies and eliminates invalid or redundant Earley states in real-time, significantly reducing memory occupation of the Earley algorithm's states. This further enables us to use a state cache to speed up structured generations on a large number of queries. We implemented ZapFormat in a new constrained decoding engine called Formatron which also incorporates existing optimizations. Through comprehensive experiments on structured generation tasks, including JSON generation, JSON Schema validation, and semantic parsing, we demonstrate that Formatron not only $\textbf{consistently maintains}$ high-precision compliant outputs but also achieves $\textbf{significant improvements}$ in inference speed up to 2x compared to state-of-the-art implementations. More importantly, Formatron is generally applicable across various LLM architectures. We release Formatron as open source at https://github.com/Dan-wanna-M/formatron.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑