arXiv:2510.02917cs.SEcs.LG2025-10被引 5

用稀疏自编码器揭示大模型代码正确性的内在机制。

Mechanistic Interpretability of Code Correctness in LLMs via Sparse Autoencoders

  • 通过统计方法提取代码正确性方向,定位模型内部的纠错信号。
  • 模型生成正确代码依赖测试用例注意力,而非问题描述。
  • 该机制在微调后仍有效,适合用于错误预警与精准干预。

随着大型语言模型在软件开发中广泛应用,其生成代码进入生产环境的比例越来越高,理解其内部代码正确性机制对安全部署至关重要。我们采用稀疏自编码器分解模型表示,识别对应于代码正确性的方向。通过t统计量选择预测方向,利用基模型表示中的分离评分确定引导方向,并通过控制、注意力分析和权重正交化分析其机制属性。结果表明,代码正确性方向能可靠预测错误代码;尽管修正能力具有统计显著性,但修复错误与保持正确代码之间存在权衡。机制上,成功生成代码依赖于关注测试用例而非问题描述。此外,基模型中识别出的方向在指令微调后仍具有效性,说明预训练阶段学习的代码正确性机制在微调中被重用。我们的机制洞见提出三项实际应用:提示策略应优先使用测试样例而非详尽的问题描述;预测方向可作为开发者审查的错误警报;这些方向也可用于选择性控制,在预期错误时介入,避免持续干预导致代码退化。

原文摘要 · Abstract (English)

As Large Language Models become integral to software development, with substantial portions of AI-suggested code entering production, understanding their internal correctness mechanisms becomes critical for safe deployment. We apply sparse autoencoders to decompose LLM representations, identifying directions that correspond to code correctness. We select predictor directions using t-statistics and steering directions through separation scores from base model representations, then analyze their mechanistic properties through steering, attention analysis, and weight orthogonalization. We find that code correctness directions in LLMs reliably predict incorrect code, while correction capabilities, though statistically significant, involve tradeoffs between fixing errors and preserving correct code. Mechanistically, successful code generation depends on attending to test cases rather than problem descriptions. Moreover, directions identified in base models retain their effectiveness after instruction-tuning, suggesting code correctness mechanisms learned during pre-training are repurposed during fine-tuning. Our mechanistic insights suggest three practical applications: prompting strategies should prioritize test examples over elaborate problem descriptions, predictor directions can serve as error alarms for developer review, and these same predictors can guide selective steering, intervening only when errors are anticipated to prevent the code corruption from constant steering.

机制解释代码生成自编码器模型可解释性

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