AI
New Metric Reveals When LLMs Rigidly Memorize Code Instead of Learning Flexibly
Researchers propose a diagnostic tool to identify when language models fail to adapt code solutions to changed task requirements, revealing brittleness that standard accuracy metrics miss.
Distinguishing between helpful code reuse and harmful memorization in large language models presents a nuanced challenge. While it's natural for programs to share common APIs and algorithmic patterns, models that have memorized solutions tend to reproduce nearly identical code even when task requirements fundamentally change—a failure mode that conventional performance benchmarks often overlook.
The Problem with Traditional Evaluation#
Measuring memorization in LLM code generation is complicated because similarity between generated and reference code doesn't necessarily signal a problem. Programmers routinely reuse idiomatic approaches and standard libraries, so high code similarity can simply reflect best practices. The real concern emerges when a model mechanically repeats a memorized solution despite the underlying task semantics shifting, indicating it has failed to understand the underlying logic.
Introducing the Harmful Memorization Index#
To capture this specific brittleness, researchers introduced the Harmful Memorization Index (HMI)—an operational diagnostic that combines two measurements: how closely the generated code mirrors the original solution and how badly performance degrades when task semantics are deliberately altered. The approach relies on code rewriting, an automated pipeline that creates subtle but semantically distinct variants of existing tasks by modifying core logic elements while regenerating corresponding prompts.
Importantly, HMI diagnoses a particular failure pattern in model behavior; it does not determine whether an original task appeared in training data. Instead, it captures whether models remain brittle under small semantic changes—complementing standard accuracy metrics with a measure of adaptability.
Key Findings Across Benchmarks#
Testing on MBPP+ and BigCodeBench revealed several patterns:
- HMI does not scale predictably: Harmful memorization doesn't necessarily increase with model size and often drops on simpler tasks
- Failure concentration: High-HMI breakdowns cluster around specific logic modifications, particularly constraint adjustments and data transformations
- Fine-tuning trade-offs: Supervised fine-tuning (SFT) boosts accuracy on original tasks but can substantially raise HMI on held-out variants

