测试大模型对代码语义保持修改的鲁棒性,发现其推理常不靠谱。
Are Large Language Models Robust in Understanding Code Against Semantics-Preserving Mutations?
- 用五种语法改写但语义不变的代码变换测试模型
- 10%到50%正确预测基于错误推理,性能最高降70%
- 适合关注代码理解可靠性的开发者和研究者
随着智能编程普及,评估大语言模型(LLMs)在编程任务中理解与推理的可靠性至关重要。现有研究多只关注预测准确率,未考察其推理过程是否合理。我们针对Python程序,采用五类保持语义的代码变换:变量重命名、比较表达式镜像、if-else分支交换、for循环转while、循环展开。测试了九个主流LLM(含开源与闭源模型),并借助LiveCodeBench进行专家评估,判断正确预测是否基于合理推理。结果表明,尽管闭源模型在准确率和推理质量上表现最优,但在语义保持变换下仍表现出显著脆弱性。约10%至50%的正确预测源于错误推理;且多数模型在代码变换后预测发生变化,性能下降最高达70%,说明当前大模型虽能给出正确答案,却缺乏稳定、语义一致的推理能力。
原文摘要 · Abstract (English)
With the widespread adoption of vibe coding, understanding the reasoning and robustness of Large Language Models (LLMs) is critical for their reliable use in programming tasks. While recent studies assess LLMs' ability to predict program outputs, most focus on accuracy alone, without evaluating the underlying reasoning. Moreover, it has been observed on mathematical reasoning tasks that LLMs can arrive at correct answers through flawed logic, raising concerns about similar issues in code understanding. In this paper we assess whether state-of-the-art LLMs can reason about Python programs or are simply guessing. We apply five semantics-preserving code mutations: renaming variables, mirroring comparison expressions, swapping if-else branches, converting for loops to while, and loop unrolling. These mutations maintain program semantics while altering its syntax. We evaluated nine LLMs, including both open-source and closed-access models, and performed a human expert analysis using LiveCodeBench to assess whether correct predictions are based on sound reasoning. We also evaluated prediction stability across different code mutations on LiveCodeBench and CruxEval. While proprietary models achieve the strongest predictive accuracy and reasoning quality in the expert evaluation, our robustness analysis reveals substantial fragility under semantics-preserving transformations. Our findings show that LLMs trained for code produce correct predictions based on flawed reasoning in between 10% and 50% of cases. Furthermore, LLMs often change predictions in response to our code mutations, with performance drops reaching up to 70%, indicating that they do not yet exhibit stable, semantically grounded reasoning, even when initial accuracy is high.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。