arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2502.18499cs.SEcs.AIcs.CL
收藏

语言模型在语法代码补全中的机制理解

Mechanistic Understanding of Language Models in Syntactic Code Completion

George Mason University(乔治梅森大学)

Samuel Miller, Daking Rai, Ziyu Yao

AI总结:

本研究通过机制可解释性方法,在CodeLlama-7b模型上分析语法代码补全中的闭合括号任务,发现中间至后期层及多头注意力至关重要,并识别出跟踪括号数量的注意力头对性能有正负影响。

摘要:

近年来,语言模型(LMs)在代码生成任务中展现出惊人的熟练度,尤其是在针对代码特定数据集进行微调后,这类模型通常被称为代码语言模型(Code LMs)。然而,我们对代码语言模型内部决策过程的理解仍然有限,例如它们如何利用其(语法或语义)知识,这可能导致随着它们在现实生活中的日益普及而产生意想不到的危害。这促使我们开展了首批机制可解释性(Mechanistic Interpretability)工作之一,以理解代码语言模型如何在CodeLlama-7b模型(Roziere等人,2023)上执行语法补全任务,具体来说是闭合括号任务。我们的研究结果表明,该模型需要中间到后期层才能自信地预测闭合括号任务的正确标签。此外,我们确定虽然多头注意力(MHA)和前馈(FF)子层都发挥重要作用,但多头注意力尤为关键。进一步地,我们还发现了能够精确跟踪已闭合括号数量的注意力头,但它们可能促进也可能不促进正确数量的尚缺失闭合括号,从而对模型性能产生正面或负面影响。

英文摘要:

Recently, language models (LMs) have shown impressive proficiency in code generation tasks, especially when fine-tuned on code-specific datasets, commonly known as Code LMs. However, our understanding of the internal decision-making processes of Code LMs, such as how they use their (syntactic or semantic) knowledge, remains limited, which could lead to unintended harm as they are increasingly used in real life. This motivates us to conduct one of the first Mechanistic Interpretability works to understand how Code LMs perform a syntactic completion task, specifically the closing parenthesis task, on the CodeLlama-7b model (Roziere et al. 2023). Our findings reveal that the model requires middle-later layers until it can confidently predict the correct label for the closing parenthesis task. Additionally, we identify that while both multi-head attention (MHA) and feed-forward (FF) sub-layers play essential roles, MHA is particularly crucial. Furthermore, we also discover attention heads that keep track of the number of already closed parentheses precisely but may or may not promote a correct number of closing parentheses that are still missing, leading to a positive or negative impact on the model's performance.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑