arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2503.02296cs.AI
收藏

记忆还是泛化?通过代码重写评估 LLM 代码生成

Evaluating Harmful Memorization in LLM Code Generation

University of California, San Diego(加州大学圣地亚哥分校)

Lizhe Zhang, Wentao Chen, Li Zhong, Letian Peng, Zilong Wang, Jingbo Shang

AI总结:

提出语义代码重写与 MRI 指标,从行为层面区分 LLM 代码生成中的有害记忆和良性重用,并评估模型规模、SFT 与 PPO 对记忆和泛化的影响。

摘要:

大型语言模型(LLM)近期展现出卓越的代码生成能力。然而,关于 LLM 主要是在进行记忆(即复制或重用其训练数据的大部分内容)还是泛化(即超越训练数据),争论正日益增多。现有评估大多以表面/结构相似性来代理记忆,从而将对重复代码的良性重用与有害回忆混为一谈,并忽视语义变化下的任务正确性。我们从行为上将有害记忆定义为在高相似性下的失败,并引入一种语义扰动代码重写:针对给定编程任务,在相似难度水平上重写一个语义不同的答案,然后逆向工程出一个新颖的编程任务。我们进一步提出 Memorization Risk Index(MRI,记忆风险指数),这是一个结合两种信号的归一化分数:(i)模型对重写任务的答案与原始真值解的相似程度,以及(ii)从原始任务到其重写对应任务的性能下降幅度。只有当两个条件同时成立时 MRI 才高——模型输出相似代码但在扰动任务上失败——从而捕捉有害记忆,而非对重复代码的良性重用。在代码生成基准 MBPP+ 和 BigCodeBench 上的实证评估揭示:(1)记忆不会随模型变大而增加,且在许多情况下会随规模扩大而缓解;(2)监督微调(SFT)提高准确率,同时引入记忆;(3)采用近端策略优化(PPO)的强化学习在记忆与泛化之间实现更均衡的权衡。

英文摘要:

Assessing memorization in LLM code generation is challenging because code reuse is not inherently harmful. Correct programs often share APIs, templates, and algorithmic idioms, so code similarity alone may reflect benign reuse rather than problematic memorization. We focus on the harmful case: a model fails to adapt when task semantics change and instead produces code that remains highly similar to the original solution. We propose \emph{code rewriting}, an automated semantic perturbation pipeline that constructs superficially similar but semantically different variants of existing coding tasks by modifying a core logic element of the reference solution and regenerating the corresponding prompt. To summarize this behavior, we introduce the \emph{Harmful Memorization Index} (HMI), an operational diagnostic that combines similarity to the original solution with performance degradation on rewritten tasks. We use harmful memorization as an operational behavioral term for this specific failure mode; HMI does not establish whether an original task or solution appeared in a model's training data. HMI is intended to complement standard accuracy metrics by capturing brittleness under small semantic changes. Across MBPP+ and BigCodeBench, we find that: (1) HMI does not monotonically increase with model scale and often decreases on simpler tasks; (2) high-HMI failures are concentrated in specific logic-edit types, especially constraint changes and data transformations; (3) supervised fine-tuning improves original-task accuracy but can substantially increase HMI on held-out tasks; and (4) across the evaluated post-training settings, PPO yields a more favorable accuracy--HMI trade-off than SFT.

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

↑