一句话:提出逐行实时语义监督框架,在解码过程中定位并回滚错误行后重新生成,无需执行代码即可降低语义错误率。
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 上也有类似增益,表明方法对模型与语言均具通用性。
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 原文 ·
@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}
}