paperbot · PL 论文追踪

全部论文 待读 收藏

P2 SemGuard: Real-Time Semantic Evaluator for Correcting LLM-Generated Code

SemGuard:面向 LLM 生成代码纠错的实时语义评估器
ASE 2025 40th IEEE/ACM International Conference on Automated Software Engineering (ASE)2025匹配度 3/10 · 论文核心是提升 LLM 生成代码的语义正确性,属于 LLM 代码生成/修复方向,而非将静态分析应用于 ML 系统或 Agentic 程序,也不是用 LLM/Agent 改进静态分析或形式化验证;仅在程序语义错误定位与 PL+ML 交叉上边缘相关。引用 1 inbox
Qinglin Wang, Zhihong Sun, Ruyun Wang, Tao Huang, Zhi Jin, Ge Li, Chen Lyu
crossref: 2026-09-12

AI 速览

一句话:提出逐行实时语义监督框架,在解码过程中定位并回滚错误行后重新生成,无需执行代码即可降低语义错误率。

LLM 能将自然语言需求转译为代码,但对代表性模型的实证分析显示,语义错误(可编译但行为不正确)占所观测缺陷的多数(如 DeepSeek-Coder-6.7B 与 QwenCoder-7B 上超过 60%)。事后修复流水线只能在执行后检测此类缺陷,带来延迟、依赖不完整测试集且常误定位缺陷。由于语义漂移源自自回归解码过程,在生成途中干预可阻断错误传播;但 ROCODE 等受限解码方法仍要等整程序运行才获得反馈,并依赖无法真正刻画语义的熵启发式。本文提出 SemGuard——由语义评估器驱动的实时行级语义监督框架。为训练该评估器,作者构建了 SemDiff:首个细粒度标注正确与错误实现分歧所在精确行的数据集。评估器嵌入 LLM 解码器后,可对部分代码标记偏离、回滚到出错行并引导重新生成,全程无需执行程序或测试用例。在四个基准上 SemGuard 一致优于 SOTA:相对 ROCODE 在 SemDiff 上语义错误率降低 19.86%,在真实场景 LiveCodeBench 上使 CodeLlama-7B 的 Pass@1 提升 48.92%;StarCoder2-7B 在 MBPP、DeepSeekCoder-6.7B 在 Java 基准 SemDiff-Java 上也有类似增益,表明方法对模型与语言均具通用性。

问题
LLM 生成的代码中语义错误占比很高,而现有事后修复流程延迟高、依赖不完整测试集且定位不准;已有受限解码方法仍依赖执行反馈与熵启发式,无法在生成过程中及时捕捉语义偏离。
方法
构建首个细粒度标注正确/错误实现分歧行的数据集 SemDiff,并训练一个语义评估器嵌入 LLM 解码器,对部分代码进行实时行级语义监督,发现偏离即回滚至出错行并引导重新生成,无需执行程序或测试用例。
评估
在四个基准上与 SOTA 基线对比:SemDiff 上语义错误率较 ROCODE 降低 19.86%,LiveCodeBench 上 CodeLlama-7B 的 Pass@1 提升 48.92%,并在 MBPP(StarCoder2-7B)与 SemDiff-Java(DeepSeekCoder-6.7B)上验证模型与语言无关的有效性。
原文摘要(Abstract)

Large Language Models (LLMs) can translate natural language requirements into code, yet empirical analyses of representative models reveal that semantic errors—programs that compile but behave incorrectly—constitute the majority of observed faults (e.g., >60% on DeepSeek-Coder-6.7B and QwenCoder-7B). Post-hoc repair pipelines detect such faults only after execution, incurring latency, relying on incomplete test suites, and often mis-localizing the defect. Since semantic drift originates in the autoregressive decoding process, intervening while the code is being generated is a direct way to stop error propagation. Constrained-decoding approaches such as ROCODE attempt this, but still wait until the entire program runs to obtain feedback and use entropy heuristics that do not truly capture semantics. A more effective solution must inject semantic signals—early and precisely—into the decoding process. We present SemGuard, a semantic-evaluator-driven framework that performs real-time, line-level semantic supervision. To train the evaluator, we build SemDiff, the first dataset with fine-grained annotations that mark the exact line where a correct and an incorrect implementation diverge. The evaluator, once embedded in the LLM’s decoder, flags deviations on partial code, rolls back to the faulty line, and guides regeneration—without executing the program or requiring test cases. Across four benchmarks, SemGuard consistently outperforms state-of-the-art baselines. It lowers the semantic error rate by 19.86% on SemDiff relative to ROCODE, and lifts Pass@1 by 48.92% on the realworld LiveCodeBench with CodeLlama-7B. Similar gains hold for StarCoder2-7B on MBPP and for DeepSeekCoder-6.7B on the Java benchmark SemDiff-Java, demonstrating model- and language-agnostic effectiveness.

链接与引用

DOI 原文 ·

BibTeX
@inproceedings{paperbot9435,
  title = {SemGuard: Real-Time Semantic Evaluator for Correcting LLM-Generated Code},
  author = {Qinglin Wang and Zhihong Sun and Ruyun Wang and Tao Huang and Zhi Jin and Ge Li and Chen Lyu},
  booktitle = {ASE},
  year = {2025},
  doi = {10.1109/ase63991.2025.00160}
}