NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Large Language Model Unlearning for Source Code

Xue Jiang, Yihong Dong, Huangzhao Zhang, Tangxinyu Wang, Zheng Fang, Yingwei Ma, Rongyu Cao, Binhua Li, Zhi Jin, Wenpin Jiao, Yongbin Li, Ge Li

TL;DR

Large Language Models (LLMs) trained on vast code repositories can inadvertently memorize and reproduce copyrighted, insecure, or outdated code. This paper introduces PROD, a novel unlearning method that surgically removes specific undesirable code snippets from LLMs without degrading their general coding abilities. PROD achieves this by precisely manipulating token prediction probabilities, offering a robust solution for safer and more reliable code generation.

What Problem Does This Paper Address?

LLMs have become incredibly adept at generating code, but their training on massive, often uncurated, datasets scraped from the internet poses significant risks. These models can memorize and regurgitate copyrighted code, leading to legal issues. They can also reproduce insecure code patterns, introducing vulnerabilities into software. Furthermore, they might generate code using deprecated APIs, leading to fragile and unmaintainable systems. While "unlearning" techniques exist to remove specific data influences from models, applying them to source code has proven challenging. Existing methods often cause "spillover" effects, damaging the LLM's fundamental understanding of programming language syntax and semantics, rendering it practically useless for general code generation. The core problem is how to precisely erase specific undesirable code knowledge without harming the LLM's overall coding competence.

Methods

The paper proposes PROD (Probabilistic Redistribution for Output Distribution), a surgical approach to source code unlearning. Unlike previous methods that operate at the sample level, PROD works at the token level to achieve fine-grained control. The process involves three key steps:

  1. Target Distribution Sculpting:
    • Output Distribution Collection: For a given prompt and its undesirable code completion, PROD first captures the LLM's raw next-token probability distribution.
  2. Forget Data Elimination: The probability of the specific "forbidden" token (the next token in the undesirable snippet) is surgically set to negative infinity, effectively zeroing out its prediction.
  3. Noise Elimination: To prevent the model from amplifying less probable, irrelevant tokens (noise) after the forbidden token is suppressed, PROD uses nucleus sampling to prune the long tail of the distribution.
  4. Probabilistic Redistribution: The remaining probability mass is then redistributed across the surviving vocabulary in a way that mirrors the original statistical patterns of the programming language. This sculpted distribution serves as the new supervisory signal.
  5. Post-Training Optimization: The LLM is then fine-tuned using a standard cross-entropy loss to match this new, sculpted target distribution. This process aims to align the LLM's output with the desired "forgetting" behavior while preserving its general coding knowledge. PROD uses a modified loss function that incorporates an "alpha-suppression" trick to control the "forget strength" and ensure training stability by freezing the original distribution.

Datasets

The paper introduces a new benchmark for code unlearning, comprising three specific tasks:

  • Copyrighted Code Unlearning: Uses 100 files randomly drawn from the Stack corpus, simulating the need to remove copyrighted code. Forget quality is measured by the complement of BLEU score against the original snippet.
  • Insecure Code Unlearning: Leverages the CyberSecEval benchmark, containing code snippets with known Common Weakness Enumerations (CWEs). Forget quality is assessed by exact-match accuracy and static analyzer results.
  • Deprecated API Unlearning: Curated from the VersiCode dataset, focusing on identifying and removing code that uses deprecated APIs. Forget quality is measured by exact-match accuracy.

Model utility is evaluated using the HumanEval benchmark, a standard for assessing general code generation capabilities.

Key Insights and Ideas

  • Granularity is Key: The paper strongly argues that the failure of previous unlearning methods on code stems from their coarse granularity. Erasing entire code snippets, rather than specific tokens, damages the underlying language structure.
  • Code is Different: Programming languages have rigid syntax and semantics, making them less tolerant to the "spillover" effects seen in natural language unlearning. This necessitates specialized approaches.
  • PROD's Surgical Precision: By directly manipulating token probabilities and redistributing them, PROD achieves precise forgetting without the collateral damage that cripples model utility.
  • The Trade-off is Real: Forget quality and model utility are often in tension. The proposed Pareto Dominance Ratio (PDR) metric effectively captures this trade-off, providing a more holistic evaluation than individual metrics.
  • Robustness Matters: Unlearning methods need to be robust against adversarial attacks that try to "recover" forgotten information. PROD demonstrates superior resilience in this regard.

Key Results

  • PROD consistently outperforms existing unlearning baselines (GA, DPO, NPO, FLAT) across all three unlearning tasks (copyrighted, insecure, deprecated API).
  • The proposed Pareto Dominance Ratio (PDR) metric shows PROD achieving an average relative gain of 124% over the strongest competitor, indicating superior overall performance in balancing forget quality and model utility.
  • PROD successfully unlearns undesirable code snippets while maintaining high model utility, as evidenced by its performance on the HumanEval benchmark, often outperforming the original model's utility after unlearning.
  • The method demonstrates broad applicability, showing consistent improvements across four different LLMs (CodeLlama-7B, Qwen2.5-Coder-7B, Deepseek-coder-6.7B, Starcoder-7B).
  • PROD exhibits significantly higher robustness against prefix injection attacks compared to baselines, with forgotten content remaining suppressed.

Limitations and Future Work

  • Training Stability: While PROD aims for stability, the paper notes that short forget sequences can require more training steps due to weaker supervisory signals. The alpha-suppression trick is introduced to mitigate this.
  • Hyperparameter Tuning: The paper explores the impact of hyperparameters p (noise elimination) and a (forget strength), suggesting that optimal values are crucial for balancing performance. The optimal p was found to be 0.8, and a=0 (zeroing out forget data probability) yielded balanced performance.
  • Future Work: The paper implicitly suggests that further exploration of different divergence measures for the optimization objective and more sophisticated noise elimination strategies could yield further improvements. The robustness against various adversarial attacks could also be a direction for future research.

Summary updated

BibTeX
@inproceedings{jiang2026large,
  author = {Xue Jiang and Yihong Dong and Huangzhao Zhang and Tangxinyu Wang and Zheng Fang and Yingwei Ma and Rongyu Cao and Binhua Li and Zhi Jin and Wenpin Jiao and Yongbin Li and Ge Li},
  title = {{Large Language Model Unlearning for Source Code}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2026},
  doi = {10.1609/aaai.v40i37.40398},
}
Figure