[ICLR 2025] GradMem: Transforming Memory into an Optimization Problem
GradMem: Learning to Write Context into Memory with Test-Time Gradient Descent
GradMem is a novel memory mechanism that compresses long contexts into a fixed set of memory tokens using test-time gradient descent. Unlike forward-only encoders, it treats context writing as an iterative optimization task, achieving SOTA performance in associative retrieval and competitive results on NLP benchmarks like bAbI and SQuAD.
TL;DR
GradMem shifts the paradigm of context compression from static "forward-only" encoding to dynamic "test-time optimization." By taking a few gradient descent steps on a small set of memory tokens, it "writes" information into a compact state that can be reused across multiple queries. This method significantly increases the information density of memory, outperforming standard recurrent transformers in both synthetic retrieval and natural language reasoning.
Academic Positioning: This work bridges the gap between Test-Time Training (TTT) and Recurrent Memory Transformers (RMT). It is a refinement of the "Memory as Parameters" philosophy, utilizing meta-learning to make gradient-based memory updates efficient enough for real-time inference.
Problem & Motivation: The Limits of Forward Passes
The dominant "KV-cache" approach in Transformers is computationally "expensive and heavy." While the community has explored compressive memory (like RMT), these methods typically use a forward-only write rule. Once the context is processed, the memory is fixed. There is no feedback loop to check if the memory actually captured the nuances of the context.
The authors' key insight is that optimization is a powerful compression algorithm. If we treat memory tokens as trainable parameters and minimize a reconstruction loss on the current context, the gradient naturally "pushes" the most relevant and difficult-to-predict information into the memory state.
Methodology: The WRITE/READ Cycle
GradMem operates in two distinct phases:
- WRITE Phase (The Core Innovation): Given a context , the model initializes memory tokens . It then performs steps () of gradient descent to minimize (autoregressive reconstruction loss). Crucially, the model weights are frozen; only the memory embeddings are updated.
- READ Phase: The original context is removed. The optimized memory tokens are prepended to the query , and the model generates the target .

The Meta-Learning Twist
To make this work in just 5 steps (rather than thousands), the authors use Model-Agnostic Meta-Learning (MAML). They train the model parameters and the initial memory state so that the "inner loop" gradient descent is maximally productive for the "outer loop" task performance.
Experiments: How Much Can It Store?
The most striking results come from Associative KV-Retrieval, a stress test for memory capacity.
- Capacity Scaling: With just 8 memory tokens, GradMem (K=5) can retrieve 64 KV-pairs with 99.1% accuracy. In contrast, RMT (forward-only) collapses to 19% accuracy at the same context length.
- Compute-Accuracy Trade-off: A unique feature of GradMem is that you can allocate more "thinking time" (more gradient steps ) at test time to get better results without retraining the model.

Transfer to NLP
Beyond synthetic tasks, GradMem was tested on bAbI (reasoning) and SQuAD (QA). While Mamba remains a strong baseline due to its extensive pretraining, GradMem consistently outperforms forward-only recurrent transformers (RMT), proving that the reconstruction objective translates well to natural language understanding.
Critical Analysis & Future Outlook
The Cost of Iteration
The main bottleneck for GradMem is the "double-backward" required during training (differentiating through a gradient). The authors mitigated this with a custom Flash HVP implementation to reduce GPU memory by 50%.
Efficiency vs. Accuracy
GradMem is most efficient in "one-writer, many-readers" scenarios. If you need to answer 100 questions about a single 10,000-token document, pay the high WRITE cost once, and enjoy ultra-fast READ passes.
Conclusion
GradMem proves that test-time gradient descent is not just for fine-tuning; it is a viable, high-capacity mechanism for "writing" into memory. It opens the door for models that can "meditate" on a context to form deep, internal representations before answering queries.
Takeaway: The future of long-context LLMs might not be larger KV-caches, but smarter, optimization-driven memory managers.
