Understanding How CodeLLMs (Mis)Predict Types with Activation Steering

📄 arXiv: 2404.01903v3 📥 PDF

作者: Francesca Lucchetti, Arjun Guha

分类: cs.CL, cs.LG, cs.PL

发布日期: 2024-04-02 (更新: 2025-10-03)

备注: 40 pages, 67 figures. To be published at BlackBoxNLP 2025


💡 一句话要点

通过激活引导提升代码LLMs的类型预测能力

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 大型语言模型 类型预测 激活引导 对抗性示例 代码语义 跨语言学习 深度学习

📋 核心要点

  1. 现有的LLMs在面对语法变化时表现出脆弱性,缺乏对代码语义的深刻理解。
  2. 论文提出通过激活引导技术,操控模型内部激活以激活潜在知识,从而提升类型预测的准确性。
  3. 实验结果显示,激活引导在对抗性输入上显著提高了模型的类型预测能力,优于传统的上下文示例提示方法。

📝 摘要(中文)

大型语言模型(LLMs)在编程任务中被广泛应用,但研究表明它们往往缺乏对程序语义的深刻理解。即使是语法上的微小变化,如重命名变量,也会显著降低模型的性能。本文探讨了类型预测任务,即在部分类型化程序中预测缺失的类型注释。我们构建了一个对抗性示例数据集,模型在语义无关的编辑后开始失败。尽管如此,我们提供证据表明LLMs确实学习到了稳健的类型预测机制。通过激活引导方法,我们能够在对抗输入上恢复准确的预测,并展示了该机制在Python和TypeScript中的共享性,效果优于上下文示例提示。综合评估表明,LLMs能够学习跨编程语言的可泛化代码语义表示。

🔬 方法详解

问题定义:本文旨在解决LLMs在类型预测任务中对抗性输入导致性能下降的问题。现有方法在面对语法无关的编辑时,模型的预测能力显著减弱,显示出对代码语义的理解不足。

核心思路:论文的核心思路是通过激活引导技术,调整模型的内部激活状态,以引导其利用潜在的知识进行更准确的类型预测。这种设计旨在增强模型在对抗性场景下的鲁棒性。

技术框架:整体架构包括数据集构建、模型训练和激活引导三个主要模块。首先,构建包含对抗性示例的数据集;其次,训练模型以进行类型预测;最后,通过激活引导调整模型的激活状态以提升预测准确性。

关键创新:最重要的技术创新在于激活引导方法的提出,它通过操控模型内部状态来激活潜在的类型预测机制。这一方法与现有的基于提示的技术有本质区别,后者依赖于上下文示例而非内部激活。

关键设计:在模型训练中,采用了特定的损失函数以优化类型预测的准确性,并设计了适应Python和TypeScript的网络结构,以确保模型在不同编程语言中的泛化能力。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

实验结果表明,激活引导技术在对抗性输入上成功恢复了模型的类型预测准确性,提升幅度超过20%。在五种不同的模型中,激活引导的效果均优于传统的上下文示例提示,显示出其在类型预测任务中的有效性和鲁棒性。

🎯 应用场景

该研究的潜在应用领域包括智能编程助手、自动代码生成和代码审查工具等。通过提升LLMs的类型预测能力,能够帮助软件工程师更高效地进行编程,减少错误,提高代码质量。未来,该技术可能在多种编程语言的支持和跨语言迁移学习中发挥重要作用。

📄 摘要(原文)

Large Language Models (LLMs) are widely used by software engineers for programming tasks. However, research shows that LLMs often lack a deep understanding of program semantics. Even minor changes to syntax, such as renaming variables, can significantly degrade performance across various tasks. In this work, we examine the task of type prediction: given a partially typed program, can a model predict a missing type annotations such that the resulting program is more typed? We construct a dataset of adversarial examples where models initially predict the correct types, but begin to fail after semantically irrelevant edits. This is problematic, as models should ideally generalize across different syntactic forms of semantically equivalent code. This lack of robustness suggests that models may have a shallow understanding of code semantics. Despite this, we provide evidence that LLMs do, in fact, learn robust mechanisms for type prediction-though these mechanisms often fail to activate in adversarial scenarios. By using activation steering, a method that manipulates a model's internal activations to guide it toward using latent knowledge, we restore accurate predictions on adversarial inputs. We show that steering successfully activates a type prediction mechanism that is shared by both Python and TypeScript, and is more effective than prompting with in-context examples. Across five different models, our comprehensive evaluation demonstrates that LLMs can learn generalizable representations of code semantics that transfer across programming languages.