Mitigating Sensitive Information Leakage in LLMs4Code through Machine Unlearning

Ruotong Geng, Mingyang Geng, Shangwen Wang, Haotian Wang, Zhipeng Lin, Dezun Dong · DOI · OpenReview

TL;DR

Large Language Models for Code (LLMs4Code) are powerful tools, but they can inadvertently leak sensitive information from their training data. This paper explores machine unlearning as a solution, demonstrating that it can significantly reduce direct leakage while preserving code generation capabilities. However, unlearning can shift leakage from direct to indirect forms, highlighting a new challenge for future research.

What Problem Does This Paper Address?

LLMs4Code, like their natural language counterparts, are trained on vast datasets. While this enables impressive code generation, it also means they can "memorize" and subsequently leak sensitive information present in the training data. This poses a significant privacy risk, especially when dealing with personal data, confidential code snippets, or proprietary information. Traditional methods like retraining are computationally expensive, and data cleaning during pre-processing is inflexible. This paper investigates machine unlearning as a more efficient and adaptable approach to mitigate this privacy leakage in LLMs4Code.

Methods

The core of this research lies in applying and evaluating machine unlearning techniques on LLMs4Code. The authors focus on three specific unlearning algorithms:

  • Forget Set Gradient Ascent (GA): This method uses gradients from privacy-related queries to update the model in a way that encourages it to "forget" the patterns associated with answering those queries.
  • Forget Set Gradient Ascent and Retain Set Gradient Descent (GA+GD): This approach combines the forgetting mechanism of GA with a gradient descent on a "retain set" of general data. The goal is to forget sensitive information while simultaneously preserving the model's general code generation capabilities.
  • Forget Set Gradient Ascent and Kullback-Leibler Divergence Minimization (GA+KL): This method also uses GA for forgetting but employs KL divergence to align the unlearned model's output distribution with that of a reference model trained only on the retain set. This aims to maintain similar generative behavior for non-sensitive inputs.

The study also introduces a novel benchmark for evaluating LLMs4Code unlearning, comprising a "forget set" for privacy leakage assessment and a "retain set" for evaluating code generation performance.

Datasets

The research utilizes a custom-built benchmark consisting of two key components:

  • Forget Set: This set comprises 5,000 synthetic fictional character resumes, each containing sensitive attributes like name, address, education, phone number, and email. Ten privacy-related questions were generated for each resume, transformed into code completion prompts using GPT-4. This set is split into training (90%) and testing (10%) subsets.
  • Retain Set: This set consists of 5,000 samples from established code generation datasets: HumanEval, MBXP, and LiveBenchWhite. This set is used to evaluate the models' general code generation capabilities after unlearning. It is also split into training (90%) and testing (10%) subsets.

Key Insights and Ideas

  • Unlearning is a viable privacy defense: Machine unlearning can significantly reduce direct privacy leakage from LLMs4Code without substantially harming their code generation abilities.
  • Leakage shifts from direct to indirect: After unlearning, models are less likely to reveal sensitive information when directly asked. However, they become more prone to leaking information indirectly, meaning contextually related but not explicitly queried sensitive data might be exposed.
  • Larger models are more prone to leakage: Models with more parameters tend to memorize and reproduce more sensitive data, making them initially more vulnerable.
  • Gradient-based methods are effective: Techniques like GA and GA+GD show strong performance in reducing leakage, particularly for structured data like emails.
  • KL divergence has limitations: While useful, KL regularization might not be as effective as gradient-based methods for mitigating leaks of more diverse and context-dependent data types like addresses.

Key Results

  • Leakage Reduction: Unlearning techniques, especially GA+GD, reduced the leak rate of AIXCoder by over 50% and achieved reductions of up to 84.7% for CodeLlama-34B. Across all models, an average leakage reduction of over 70% was observed with GA+GD.
  • Performance Preservation: The functional correctness of the models, measured by Pass@1, remained largely stable, with only minor decreases (e.g., within 1.5% for larger models).
  • Indirect Leakage Increase: While direct leakage decreased significantly, indirect leakage generally increased after unlearning. For example, in AIXCoder-7B, direct leakage dropped from 0.49 to 0.38, while indirect leakage rose from 0.51 to 0.62.
  • Data Type Effectiveness: Gradient-based methods were particularly effective for structured data like emails. For semi-structured data like passwords and addresses, GA+GD showed more consistent results.

Limitations and Future Work

The authors acknowledge several limitations:

  • Human Evaluation Bias: The reliance on human evaluators for assessing privacy leakage and its nature (direct vs. indirect) introduces potential subjectivity and bias. Future work could incorporate automated evaluation methods.
  • Synthetic Dataset: The use of a synthetic dataset, while controlled, might not fully capture the complexity of real-world code development scenarios and the privacy concerns therein. Future work should explore using real-world, anonymized datasets.
  • Prompt Engineering: The methods used to trigger leakage might not cover all possible adversarial queries. Broader prompt engineering and red-teaming strategies are needed.

The paper strongly suggests that future research should focus on:

  • Mitigating Indirect Leakage: Developing unlearning techniques that can effectively address both direct memorization and indirect semantic associations is a critical open challenge.
  • Automated Evaluation: Incorporating automated evaluation methods to complement human judgments and improve reproducibility.
  • Real-World Data: Extending the benchmark to include real-world, anonymized datasets to validate findings at scale.
  • Broader Prompting Strategies: Exploring more diverse and adversarial prompting techniques to stress-test unlearning methods.

Summary updated

BibTeX
@inproceedings{geng2025mitigating,
  author = {Ruotong Geng and Mingyang Geng and Shangwen Wang and Haotian Wang and Zhipeng Lin and Dezun Dong},
  title = {{Mitigating Sensitive Information Leakage in LLMs4Code through Machine Unlearning}},
  booktitle = {Neural},
  year = {2025},
  doi = {10.1016/j.neunet.2026.108606},
}
Figure