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

Detecting Functional Memorization in Code Language Models

Matthieu Meeus, Anil Ramakrishna, Matthew Grange, Zheng Xu, Luca Melis

TL;DR

Large Language Models (LLMs) don't just memorize code word-for-word; they memorize the underlying logic. This paper introduces the concept of functional memorization, demonstrating that models can reproduce proprietary algorithms and hard-coded logic even when the code structure is completely transformed, rendering traditional text-based "verbatim" audits obsolete.

What Problem Does This Paper Address?

As LLMs become the backbone of coding assistants, concerns about training data leakage—specifically the reproduction of licensed or proprietary code—have skyrocketed. Traditionally, researchers have audited this by looking for "textual overlap" (using metrics like BLEU or edit distance). If the model outputs a sequence of tokens that matches the training data, it’s flagged as memorized.

However, code is unique because it is malleable. You can rename variables, restructure loops, or swap a for loop for a list comprehension without changing what the code actually does. The authors argue that current auditing methods suffer from a massive blind spot: they fail to detect when a model has internalized the "functional logic" of a training sample and reproduces it in a different surface form. If a model leaks a secret trading algorithm but changes the variable names, a text-based audit would give it a clean bill of health.

Methods

The authors employ a counterfactual setup to isolate memorization from general coding ability. They compare two versions of the Olmo-3-32B model:

  1. The Target Model ($M_T$): A "midtrained" checkpoint that has been exposed to a specific dataset (CraneCode).
  2. The Reference Model ($M_R$): A pretrained checkpoint that has not seen that specific data.

By prompting both models with the same function signatures and comparing their outputs to the ground truth training data, the authors can determine if a specific piece of logic was "learned" specifically from the training set.

To measure similarity, they move beyond simple text matching and use a multi-tiered suite of metrics:

  • Textual: BLEU and Edit Distance.
  • Structural: CodeBLEU (which looks at Abstract Syntax Trees and data flow) and TSED (Tree-edit distance).
  • LLM-as-a-judge: Using LLaMA-3.1-70B to evaluate if two code snippets are logically equivalent.
  • Execution-based: Using HyClone, which actually runs the generated code against the ground truth using various inputs to see if the outputs match perfectly.

Datasets

  • CraneCode: A filtered and rewritten version of the Python subset of The Stack v2.
  • Evaluation Sample: From an initial 100k functions, the authors used an LLM judge to filter for 7,422 functions that contained "meaningful functional logic" (e.g., business rules, decision logic) rather than boilerplate code.

Key Insights and Ideas

  • The "Upper-Left Quadrant": The most striking finding is the existence of generations with very low textual similarity (BLEU < 0.75) but high functional similarity. These are the "stealth leaks"—cases where the model reproduces the logic but not the text.
  • Logic Internalization: Models aren't just "stochastic parrots" of tokens; they internalize domain-specific constants and rules. For example, one model reproduced exact, hard-coded commission rates for specific Bulgarian cities (Sofia, Varna, Plovdiv) found in the training data, even though it completely restructured the code from a dictionary-loop to a flat tuple-index access.
  • Generalization vs. Memorization: The counterfactual setup proved essential. Some "memorized" looking code was actually just the model arriving at a natural solution that the reference model (which hadn't seen the data) also produced.
  • Severity Invariance: Surprisingly, the "sensitivity" or "severity" of the code (how proprietary it seemed) didn't significantly change how much the model memorized it. Private logic is just as vulnerable to leakage as common utility functions.

Key Results

  • Systematic Shift: Exposure to midtraining data increased similarity across all metrics, confirming that models move toward training data in both form and function.
  • Functional Leakage Scale: While only 0.58% of functions were memorized near-verbatim, a much larger 3.9% were flagged as functionally memorized by the authors' conservative LLM judge.
  • Execution Verification: Even under the strictest possible test—running the code—0.28% of generations were confirmed as functional clones of training data that the reference model could not produce.
  • Metric Correlation: Text-based metrics and structural metrics correlate well with each other, but LLM-judges and execution-based tests capture a "distinct and particularly stringent dimension" of leakage that others miss.

Limitations and Future Work

  • Execution Coverage: The execution-based tool (HyClone) could only successfully run about 15% of the functions. Real-world code often depends on complex class states (self), external APIs, or specific file structures that are difficult to simulate in a sandbox.
  • Judge Sensitivity: LLM-as-a-judge results varied significantly depending on the prompt used, highlighting the need for more robust, standardized evaluation prompts.
  • Computational Cost: While text metrics are cheap, execution-based and LLM-based auditing is computationally expensive, making it difficult to scale to massive datasets.
  • Future Direction: The authors suggest that future work should focus on improving the "executability" of code snippets to provide more ground-truth functional comparisons.

Summary updated

BibTeX
@inproceedings{meeus2026detecting,
  author = {Matthieu Meeus and Anil Ramakrishna and Matthew Grange and Zheng Xu and Luca Melis},
  title = {{Detecting Functional Memorization in Code Language Models}},
  booktitle = {DL4C @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=yWBrNBcRLj},
}
Figure