Measuring memorization in RLHF for code completion

Jamie Hayes, Ilia Shumailov, William P. Porter, Aneesh Pappu

Large Language Models (LLMs) are notorious for their "eidetic memory"—the tendency to regurgitate snippets of their training data verbatim. While the machine learning community has a solid understanding of how this happens during pre-training and supervised fine-tuning (SFT), the "alignment" phase—where models are tuned to follow human preferences—has remained a black box for privacy researchers.

In a recent study, researchers from Google DeepMind take a deep dive into how Reinforcement Learning from Human Feedback (RLHF) and its newer alternatives, like Identity Preference Optimization (IPO), affect data memorization in code completion models.

The Privacy Risks of Alignment

As LLMs become ubiquitous in code editors, they are increasingly aligned using real-world user interactions. This raises a massive privacy red flag: if a developer provides feedback on a snippet containing a sensitive internal file path or a proprietary API key, could the model later "regurgitate" that information to another user?

While it is well-documented that SFT leads to memorization, the RLHF pipeline is more complex, involving a Reward Model (RM) and a Reinforcement Learning (RL) phase. The authors set out to determine if and when training data surfaces and propagates through these different stages of alignment.

Methodology: Detecting Counterfactual Memorization

The researchers employed a rigorous "Counterfactual Memorization" framework. To prove a model has memorized a specific example $x$, they compare the model in question against a "control model" that was never exposed to $x$. If the target model can reproduce $x$ but the control model cannot, it is a clear case of memorization rather than general coding ability.

Because code is highly structured and repetitive, they used a sliding window normalized edit distance. This allows them to detect if a model has memorized a specific line of code even if it is buried within a long, non-memorized file.

They compared two primary alignment paths:

  1. Standard RLHF: SFT $\rightarrow$ Reward Model Training $\rightarrow$ RL Fine-Tuning (RLFT).
  2. Direct Preference Learning (IPO): A variant of $\Psi$PO that skips the reward model and optimizes the policy directly from preference pairs.

Key Insights: RLHF vs. Direct Preference Learning

The "Low Information" Signal of RLHF

One of the most surprising findings is that the RL phase is quite "safe" from a privacy perspective. Because the model is optimized against a scalar reward (a single number) rather than a sequence of tokens, it is much harder for the model to "absorb" the verbatim text of the reward model's training data.

Fine-Tuned (FT) model memorization vs. Control
RL Fine-Tuned (RLFT) model memorization vs. Control

As illustrated in the scatter plots above, a standard Fine-Tuned (FT) model frequently memorizes target examples (shown by points clustering near the bottom axis, regardless of the control model's performance). In stark contrast, the RL Fine-Tuned (RLFT) model rarely deviates from the control model's baseline (points cluster tightly along the diagonal), indicating that the RL phase introduces almost no new memorization.

SFT Memorization is Sticky

However, RLHF is not a cure-all. If a model memorizes a snippet during the initial SFT phase, RLHF will not "unlearn" it. In most cases, those examples remain memorized even after extensive RL alignment.

The IPO Privacy Tax

Direct preference algorithms like IPO are gaining popularity because they are simpler to implement, removing the need for a separate reward model. However, this paper shows they come with a significant "privacy tax." By optimizing directly on preference pairs, the model treats the "preferred" text much like an SFT target, leading to drastically higher rates of regurgitation.

Quantitative Results

The study's experiments spanned multiple models (including Gemini Nano-1, Gemini Pro, and Gemma 2B/7B) and datasets, yielding striking comparisons:

  • RLHF vs. SFT: When a dataset was used for SFT, 17.6% of examples were memorized. When that same data was used only for Reward Modeling in an RLHF pipeline, memorization dropped to a negligible 0.9%.
  • IPO Leakage: Aligning with IPO led to 18.2% (Gemma 2B) and 19.5% (Gemma 7B) memorization rates on the preference data—nearly 20x higher than the risk posed by RLHF.
  • PII Regurgitation: In a test using fictitious PII-like file paths, the SFT model leaked the sensitive paths in 50% of cases. The RLHF model (where the paths were only in the RM training data) leaked 0%.

Conclusion and Future Work

The authors acknowledge that their definition of memorization is restricted to whether a model can be induced to generate a copy, not whether the data exists in the weights in some abstract form.

Ultimately, while RLHF is computationally more expensive and complex than direct preference methods like IPO, it acts as a much safer mechanism for mitigating the risk of regurgitating sensitive preference data. Future work will need to investigate if there are ways to regularize IPO-style algorithms to match the privacy profile of RLHF without sacrificing their simplicity.

Summary updated

BibTeX
@inproceedings{hayes2025measuring,
  author = {Jamie Hayes and Ilia Shumailov and William P. Porter and Aneesh Pappu},
  title = {{Measuring memorization in RLHF for code completion}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=Tg8RLxpMDu},
}
Figure