用改进的Earley算法动态剪枝,让大模型生成结构化文本更快更准。
Earley-Driven Dynamic Pruning for Efficient Structured Decoding
- 基于Earley算法实时剔除无效状态,减少内存占用。
- 在JSON生成等任务中推理速度最高提升2倍,输出精度不变。
- 适用于多种大模型架构,开源可用。
大型语言模型在生成结构化输出(如函数调用、领域特定语言)时,常因无法满足严格的格式或语法约束而面临挑战。传统的上下文无关文法约束解码需在每一步检查词汇表中所有词元的有效性,导致显著开销。为此,我们提出ZapFormat,一种基于Earley算法的新型动态剪枝策略,可实时识别并消除无效或冗余的Earley状态,大幅降低状态存储开销。这使得我们能引入状态缓存机制,在大量查询上加速结构化生成。我们将该技术集成到新开发的Formatron约束解码引擎中,并结合现有优化。在包括JSON生成、JSON Schema验证和语义解析在内的多项任务中,实验表明Formatron不仅始终保证高精度合规输出,且推理速度相比当前最优实现最高提升2倍。更重要的是,Formatron对多种大模型架构具有通用性。代码已开源:https://github.com/Dan-wanna-M/formatron。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。