arXiv:2503.01804cs.CLcs.AI2025-03

让大模型输出既语法正确又语义合规,无需微调

$\texttt{SEM-CTRL}$: Semantically Controlled Decoding

  • 用逻辑形式化约束直接控制解码过程
  • 小模型经此方法可超越大模型和顶尖推理模型
  • 适合需要严格输出规范的实用场景

尽管对大语言模型(LLM)输出的句法与语义正确性要求至关重要,但在实际部署中仍面临重大挑战。本文提出$ exttt{SEM-CTRL}$,一种统一的方法,可在不微调的前提下,直接在解码器层面施加丰富的上下文敏感约束及任务与实例特定的语义规则。该方法结合了基于特定句法与语义约束引导的分词级蒙特卡洛树搜索(MCTS),并使用答案集语法(Answer Set Grammars)表达目标输出的约束——这是一种广义上下文敏感语法的形式化框架,能融入背景知识以刻画任务特异性语义。实验结果表明,$ exttt{SEM-CTRL}$ 能确保任何现成的LLM生成有效输出。我们在合成语法生成、组合推理、JSON解析和规划等任务上进行了评估,结果显示,即使小型预训练模型经此方法后也能高效超越更大规模模型及当前最优推理模型(如$ extit{o4-mini}$),同时保证语义有效性。

原文摘要 · Abstract (English)

Ensuring both syntactic and semantic correctness in Large Language Model (LLM) outputs remains a significant challenge, despite being critical for real-world deployment. In this paper, we introduce $\texttt{SEM-CTRL}$, a unified approach that allows for enforcing rich context-sensitive constraints, and task and instance specific semantics directly on the LLM decoder. Our approach integrates token-level MCTS which is guided by specific syntactic and semantic constraints. The constraints over desired outputs are expressed using Answer Set Grammars, which is a logic-based formalism that generalizes context sensitive grammars while incorporating background knowledge to represent task-specific semantics. We show that our approach helps guarantee valid completions for any off-the-shelf LLM without the need for fine-tuning. We evaluate $\texttt{SEM-CTRL}$ on a range of tasks, including synthetic grammar synthesis, combinatorial reasoning, JSON parsing, and planning. Our experimental results demonstrate that $\texttt{SEM-CTRL}$ allows even small pre-trained LLMs to efficiently outperform larger variants and state-of-the-art reasoning models (e.g., $\textit{o4-mini}$) while simultaneously guaranteeing semantic validity.

语义控制解码优化大模型推理

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。