WGRAMMAR: Leverage Prior Knowledge to Accelerate Structured Decoding

cs.AI 2025-07-23 36 次浏览
Ran Wang Xiaoxuan Liu Hao Ren Gang Chen Fanchao Qi Maosong Sun

AI 总览摘要

深度解读

术语表

Constraint Decomposition(约束分解)

将输出结构拆分为静态预编译和动态实例化两部分,提升解码效率。

系统通过预编译静态结构,动态生成参数实现快速结构化输出。

FSM (有限状态机)

一种状态追踪机制,用于替代推导自动机,减少状态切换延迟。

用以实时追踪解码状态,替代复杂的推导自动机。

Grammar Snippets(语法片段)

预定义的语法模板,用于快速实例化动态参数,支持多样化结构。

在运行时结合参数快速生成结构。

Mask Caching(掩码缓存)

全局缓存GPU掩码,避免重复生成,提升GPU端推理速度。

加速GPU端的结构约束掩码生成。

Domain Knowledge(领域知识)

任务相关的先验知识,用于简化语法设计和结构预编译。

利用任务特定的结构和规则优化解码流程。

开放问题 这项研究留下的未解疑问

  • 1 如何进一步支持更复杂或深层嵌套的结构,提升FSM的表达能力?
  • 2 在模板频繁更新的场景中,如何高效维护和重建预编译结构?
  • 3 能否结合学习机制,使模板自动适应不同任务的变化?

应用场景

近期应用

内容自动生成

在新闻、报告等场景中,快速生成符合格式的内容,提升生产效率。

智能问答

确保答案符合特定结构,提升系统的可靠性和用户体验。

远期愿景

自动化内容创作

实现全流程自动化,从内容生成到排版,极大降低人力成本。

原文摘要

Structured decoding enables large language models (LLMs) to generate outputs in formats required by downstream systems, such as HTML or JSON. However, existing methods suffer from efficiency bottlenecks due to grammar compilation, state tracking, and mask creation. We observe that many real-world tasks embed strong prior knowledge about output structure. Leveraging this, we propose a decomposition of constraints into static and dynamic components -- precompiling static structures offline and instantiating dynamic arguments at runtime using grammar snippets. Instead of relying on pushdown automata, we employ a compositional set of operators to model regular formats, achieving lower transition latency. We introduce wgrammar, a lightweight decoding engine that integrates domain-aware simplification, constraint decomposition, and mask caching, achieving up to 250x speedup over existing systems. wgrammar's source code is publicly available at https://github.com/wrran/wgrammar.

cs.AI