Unveiling Memorization in Code Models

Zhou Yang, Zhipeng Zhao, Chenyu Wang, Jieke Shi, Dongsun Kim, DongGyun Han, David Lo

TL;DR

Large Language Models (LLMs) for code don't just learn programming patterns; they frequently "copy-paste" verbatim snippets from their training data. This study reveals that popular models like CodeParrot and StarCoder memorize thousands of lines of code, including sensitive API keys, personal emails, and restrictive licenses, posing significant security and legal risks.

What Problem Does This Paper Address?

As AI-powered coding assistants become ubiquitous, a critical question arises: are these models truly "understanding" code, or are they simply regurgitating their training data? This phenomenon, known as memorization, is a double-edged sword. While it helps models recall common boilerplate, it also leads to:

  1. Intellectual Property (IP) Issues: Generating licensed code (e.g., GPL) without the user's knowledge.
  2. Security Vulnerabilities: Leaking hardcoded secrets like passwords or private keys found in public repositories.
  3. Privacy Breaches: Exposing PII (Personally Identifiable Information) like developer emails or internal IP addresses.

The authors set out to systematically quantify how much code models memorize, what types of content they leak, and whether we can detect these leaks without having access to the original training data.

Methods

The researchers employed a multi-pronged approach to "unveil" the memory of these models:

  • Generation Strategies: They used four ways to prompt the models, ranging from "Non-Prompt Generation" (starting from a blank slate) to "Prompt-Conditioned Generation" (giving the model a function header to complete).
  • Memorization Detection: They used Simian, a Type-1 clone detection tool, to find exact verbatim matches (at least 6 lines long) between the model's output and its training set.
  • Taxonomy Building: Through a manual "card sorting" study of 381 samples, they categorized memorized content into 14 subcategories across three main pillars: Documentation, Code Logic, and Others.
  • Inference Metrics: They tested four statistical metrics—including Perplexity and the PPL-zlib ratio (which compares model confidence against data compression)—to see if they could predict if a snippet was memorized without looking at the training data.

Datasets

  • Primary Models: CodeParrot (1.5B parameters) and CodeParrot-small (110M parameters). These were chosen because their training data (a 180GB GitHub Python dataset) is publicly available for verification.
  • Deployed Models: To test real-world impact, they analyzed InCoder (6B) and StarCoder (15.5B), using GitHub's search API as a proxy for their training data.
  • Benchmark: The models were also evaluated on OpenAI’s HumanEval to ensure they were high-performing models, not just "stochastic parrots."

Key Insights and Ideas

  • Bigger Models are Leakier: Given the same architecture and data, larger models memorize significantly more content and longer snippets than smaller ones.
  • The Power of Duplication: There is a massive correlation (0.804 Spearman rank) between how often a snippet appears in the training data and how likely the model is to memorize it. If a piece of code appears multiple times in GitHub, the model is almost certain to "hardcode" it.
  • Logic vs. Boilerplate: While simple prompts often trigger memorized licenses and headers, prompting with specific code context (like a function name) successfully drives the model to regurgitate complex code logic.
  • Detecting Memorization is Feasible: You don't need the training data to know if a model is "cheating." Metrics like the PPL-zlib ratio are incredibly effective at flagging memorized content, as memorized strings typically have much lower perplexity than original "creative" generations.

Key Results

  • Massive Extraction: From just 20,000 generated outputs, the authors extracted over 40,125 unique memorized code snippets.
  • Secrets Uncovered: The study successfully extracted 25 private keys, 914 emails, and 25 IP addresses that were verbatim matches from the training data.
  • Real-World Leakage: In the top 100 most "confident" outputs from deployed models, 81% of InCoder's and 75% of StarCoder's snippets were found verbatim on GitHub.
  • Logic Leakage: Over 90% of the memorized content in InCoder was related to actual code logic rather than just comments or licenses.

Limitations and Future Work

The study primarily focuses on Type-1 clones (exact matches). This likely represents the "tip of the iceberg," as it misses memorization where the model changes variable names (Type-2) or slightly modifies the structure (Type-3).

The authors suggest that future work should explore:

  • De-duplication as Defense: Since duplicates drive memorization, cleaning training sets is the first line of defense.
  • Differential Privacy: Implementing training techniques that mathematically limit the model's ability to "remember" specific samples.
  • User Controls: Platforms like GitHub should allow users to explicitly opt-out of having their repositories used for AI training to mitigate IP and privacy risks.

Summary updated

BibTeX
@inproceedings{yang2024unveiling,
  author = {Zhou Yang and Zhipeng Zhao and Chenyu Wang and Jieke Shi and Dongsun Kim and DongGyun Han and David Lo},
  title = {{Unveiling Memorization in Code Models}},
  booktitle = {ICSE},
  year = {2024},
  doi = {10.1145/3597503.3639074},
}
Figure