Neural Garbage Collection: Learning to Forget while Learning to Reason

Michael Y. Li, Jubayer Ibn Hamid, Emily Fox, Noah GoodmanNeurIPS 2026

TL;DR

When language models "think" out loud to solve hard problems, their memory footprint balloons with every generated word, creating a massive hardware bottleneck. Instead of relying on hand-crafted rules to clean up this memory, Neural Garbage Collection (NGC) uses reinforcement learning to let the model decide for itself what to forget—using nothing more than a simple reward for getting the final answer right. By treating efficiency as a learned skill rather than an engineering patch, NGC slashes peak memory usage by 2–3x while keeping reasoning sharp.

What Problem Does This Paper Address?

Allowing language models to generate long, step-by-step chains of thought has unlocked incredible problem-solving abilities. However, Transformers remember their past thoughts by storing internal representations of every token in a KV (Key-Value) cache. As a model ponders a complex math puzzle over thousands of steps, this cache grows linearly, rapidly eating up GPU memory.

Humans don't do this. When you multiply two large numbers on scratchpaper, you remember the final product and crumple up the intermediate carry-overs. Much of a model's KV cache is similarly transient scratch work. Until now, researchers have tried to prune the cache on the model's behalf using fixed heuristics (like dropping older tokens or those with low attention scores) or auxiliary compression losses. The problem? These hand-designed proxies only guess at what the model actually needs downstream to solve the task. The authors ask: If a model can learn to reason from scratch, why can't it learn to forget?

Methods

NGC extends standard reinforcement learning for reasoning—where generating a token is treated as a discrete action—by making cache eviction a discrete action, too.

Figure 1: Neural Garbage Collection: Learning to Forget while Learning to Reason. As the language model (LM) generates its chain-of-thought, it periodically enters an eviction round: (1) the LM scores KV cache entries via softmax, (2) samples cache evictions using those scores via Gumbel top-k, and (3) samples the next token conditioned only on the pruned cache. Since both tokens and cache evictions are discrete actions sampled from the LM, we can jointly train the LM to reason and manage its memory via end-to-end reinforcement learning from outcome-based task reward alone (e.g., binary correctness) — no proxy objectives, no supervised finetuning.

Here is how the "grow-then-evict" cycle works (applied to a 1.5B-parameter model):

  • Zero-Parameter Scoring: Every $\delta$ tokens, the model pauses for an eviction round. Rather than adding new neural network layers to judge memory utility, NGC repurposes the Transformer's native attention mechanism. It averages how much the 5 most recent tokens attend to past keys, grouping keys into contiguous 32-token blocks to simplify credit assignment and align with GPU memory pages.
  • Stochastic Forgetting: Instead of deterministically keeping the highest-scored blocks, NGC adds Gumbel noise to sample a subset of blocks to keep (the Gumbel-top-$k$ trick). This makes the discrete choice mathematically compatible with policy gradients, allowing a single binary task reward (correct vs. incorrect final answer) to jointly update both how the model reasons and what it chooses to remember.
  • Replay Attention Masks: In standard RL training, you re-evaluate a generated sequence in one parallel forward pass using a standard causal mask. Because NGC permanently deletes memories mid-thought, standard masks would let later tokens "cheat" by seeing evicted tokens during training. NGC records exact token visibility into a custom replay mask per layer, keeping training strictly on-policy with zero extra forward-pass overhead.
  • Staircase Curriculum: To prevent early training collapse, the model starts with gentle eviction rates that gradually step up to aggressive memory cuts.
Figure 2: Replay masks enable efficient end-to-end RL training. The model evicts cache entries at fixed intervals (round 1: t1, t3; round 2: t2, t5), and we encode the resulting visibility patterns into a replay mask: row i records which keys were visible when ti was generated. A single forward pass over the sampled tokens with this mask reproduces the next-token distributions in parallel.

Datasets

No new benchmarks are introduced; the authors use established reasoning datasets to test learning from scratch and out-of-distribution generalization:

  • Countdown: A combinatorial arithmetic game (combining 3–4 numbers via basic operations to hit a target). Used for controlled training (327,680 problems) and testing (1,024 problems).
  • DAPO-17k: A large-scale, diverse mathematical reasoning dataset used for larger-scale RL training.
  • AMC 2023, AMC 2025, and AIME 2025: Standard, challenging high-school math competition benchmarks used purely for held-out evaluation.

Key Insights and Ideas

  • Efficiency is a learnable capability: You don't need auxiliary distillation losses or human-written summarization prompts to manage memory. Pure end-to-end pressure from an outcome reward forces the model to figure out what is scratch work and what is vital context.
  • Heuristics are brittle across tasks: A fixed rule like SnapKV (which evicts based on raw attention weights) works decently on competition math yet fails miserably on Countdown. Learned garbage collection adapts its memory strategy to the task at hand.
  • "Budget-Aware Interoception" helps models plan: Simply appending a tag like <eviction_rate>50%</eviction_rate> to the prompt lets the model "sense" its memory constraints ahead of time, leading to much more graceful performance degradation—and an 8–13% accuracy boost on Countdown—even under extreme memory budgets never seen during training.
  • Both halves of the gradient matter: Ablations show that merely dropping KV entries during training ("KV dropout") fails (2.5% accuracy), and fixing the replay mask without sending gradients through the eviction decisions themselves falls short (35.7% vs. NGC's 49.6%).

Key Results

Across all experiments, NGC substantially outperforms heuristic eviction baselines (SnapKV, KeyDiff, KNorm, and StreamingLLM):

  • Countdown: At a 2.4x reduction in peak KV cache size (evicting 50% of the cache each round), NGC achieves 49.6% pass@1 accuracy—more than doubling the next-best baseline, KeyDiff (21.2%), while SnapKV hits just 7.8%.
  • Competition Math: Trained on DAPO-17k and evaluated at 3–5x peak cache reductions, NGC consistently beats all baselines on AMC 2023, AMC 2025, and AIME 2025 across pass@$k$ metrics ($k=1$ to $32$). At moderate 2–3x cache reductions, NGC closely approaches the upper-bound accuracy of an unconstrained, full-cache model.

Limitations and Future Work

Because of academic compute constraints, the study focuses exclusively on a single 1.5B-parameter model (DeepSeek-R1-Distill-Qwen-1.5B) and relies on cleanly verifiable math and logic tasks with unambiguous binary rewards. Furthermore, the authors evaluate algorithmic efficiency via peak KV cache size rather than wall-clock inference speedups, noting that timing is easily confounded by system-level batching effects. Natural next steps include scaling NGC up to frontier model sizes and extending the framework to domains with softer, non-binary reward signals.

Summary updated

Abstract

Chain-of-thought reasoning has driven striking advances in language model capability, yet every reasoning step grows the KV cache, creating a bottleneck to scaling this paradigm further. Current approaches manage these constraints on the model’s behalf using hand-designed criteria. A more scalable approach would let end-to-end learning subsume this design choice entirely, following a broader pattern in deep learning. After all, if a model can learn to reason, why can't it learn to forget? We introduce \textbf{Neural Garbage Collection (NGC)}, in which a language model learns to forget while learning to reason, trained \emph{end-to-end} from outcome-based task reward alone. As the model reasons, it periodically pauses, decides which KV cache entries to evict, and continues to reason conditioned on the remaining cache. By treating tokens in a chain-of-thought and cache eviction decisions as discrete actions sampled from the language model, we can use reinforcement learning to jointly optimize how the model reasons and how it manages its own memory: what the model evicts shapes what it remembers, what it remembers shapes its reasoning, and the correctness of that reasoning determines its reward. Crucially, the model learns this behavior entirely from a \emph{single learning signal} — the outcome-based task reward — without supervised fine-tuning or proxy objectives. On Countdown, AMC, and AIME tasks, NGC maintains strong accuracy relative to the full-cache upper bound at a 2–3x compression in peak KV cache size and substantially outperforms eviction baselines. Our results are a first step towards a broader vision where end-to-end optimization drives both capability and efficiency in language models.
BibTeX
@inproceedings{li2026neural,
  author = {Michael Y. Li and Jubayer Ibn Hamid and Emily Fox and Noah Goodman},
  title = {{Neural Garbage Collection: Learning to Forget while Learning to Reason}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=WibVOPxZMf},
}
Figure