
Memory Caching: RNNs with Growing Memory
Key Points
- 1Recurrent Neural Networks (RNNs) face limitations in recall-intensive tasks due to their fixed-size memory, contrasting with Transformers that offer growing memory but at the cost of quadratic complexity.
- 2This paper proposes Memory Caching (MC), a technique that enables RNNs' effective memory capacity to grow by caching checkpoints of their hidden states, thereby providing a flexible complexity trade-off between O(L) of RNNs and O(L^2) of Transformers.
- 3Experimental results demonstrate that MC significantly enhances the performance of recurrent models, extending their effective context length and achieving competitive performance against Transformers in in-context recall tasks.
The paper introduces Memory Caching (MC), a technique designed to bridge the gap between memory-bounded recurrent neural networks (RNNs) and memory-intensive Transformers. While Transformers offer a growing memory capacity that scales quadratically with sequence length (), RNNs compress information into a fixed-size memory, leading to complexity but often underperforming in recall-intensive, long-context tasks due to forgetting. MC allows the effective memory capacity of RNNs to grow with sequence length, providing a flexible trade-off with complexity, where is the number of cached segments.
The core idea of MC is to segment the input sequence into segments, , each with length . For each segment , a memory module compresses the segment's information. The memory update within a segment at time is defined as , where , , and are key, value, and query projections of the input token . The last state of each segment's memory, , is cached.
The key innovation lies in how the model computes its output . Instead of relying solely on the current "online memory" , MC augments this with access to all previously cached segment memories. The output is computed as:
where is an aggregation function combining the online memory with the cached memories for the query . The paper proposes four variants for this aggregation:
- Residual Memory: This is the simplest aggregation, summing the online memory's output with the outputs of all cached memories when queried by .
For strictly linear memory modules (e.g., linear attention), this formulation can mathematically collapse into a standard fixed-size memory if segments are trained sequentially from the previous state, as cached memories can be pre-summed. However, even this simple form shows empirical benefits, acting as a retention mechanism.
- Gated Residual Memory (GRM): To introduce query-dependent selectivity, GRM incorporates input-dependent gating parameters for each segment.
The gates are typically parametrized as similarities between a linear projection of the current input and a mean-pooled representation of segment , . Specifically, , often followed by a softmax normalization. This prevents the collapse observed in the linear residual case, as the gates are token- and segment-dependent.
- Memory Soup: Inspired by model souping, this variant combines the *parameters* of the cached memory modules into a single, data-dependent memory before retrieval. If is parameterized by , then the aggregated memory's parameters are . The output is then .
- Sparse Selective Caching (SSC): To mitigate memory overhead for very long sequences, SSC selectively chooses a subset of cached memories. A router computes relevance scores for each segment . It then selects the top- most relevant segments to form a set , which are used in the aggregation.
This reduces the computational cost of retrieval by operating only on a sparse subset of cached memories.
The paper discusses two main approaches for managing memory states across segments:
- Checkpointing a single memory: In this view, the model trains an associative memory, and the cached states are checkpoints of this optimization process. This implies , meaning the memory for a new segment starts from the last state of the previous segment.
- Independent compressors: Each segment is processed by an independent memory module that starts from an initial point unrelated to . This views each cached memory as a compressed representative of its specific segment, avoiding interference between segments.
As a proof of concept, MC is empirically validated on architectures like Linear Attention (LA), deep memory module Titans, Sliding Window Linear Attention (SWLA), and Deep Linear Attention (DLA) across language modeling, long-context, and retrieval tasks. The results indicate that MC enhances recurrent model performance, extends their effective context length, and performs competitively against Transformers in in-context recall tasks, narrowing the performance gap with state-of-the-art recurrent models.