Linear Attention and Retentive Networks: How Recurrent Duals and Chunkwise Tiling Eliminate the Quadratic Bottleneck
Autoregressive large language models built on standard multi-head self-attention face two fundamental scaling ceilings: quadratic compute and memory complexity during pre-training, and linearly expanding key-value (KV) cache memory footprints during autoregressive generation. While optimizations such as FlashAttention reduce memory access overheads and Grouped-Query Attention (GQA) compresses KV projection channels, the core mechanics of softmax attention remain fundamentally bound to sequence length.
Linear attention mechanisms and modern recurrent architectures, exemplified by Retentive Networks (RetNet) and Gated Linear Attention (GLA), resolve this bottleneck by exploiting the mathematical duality between attention and recurrence. By replacing non-linear softmax normalization with kernel feature maps, exponential decay matrices, and data-dependent gating, these architectures achieve the "impossible triangle" of sequence modeling: parallel training at GPU scale, constant memory and latency during inference, and competitive language modeling perplexity.
The Quadratic Dilemma of Softmax Attention
Standard scaled dot-product attention computes interactions between queries , keys , and values across sequence length :
The intermediate matrix requires materializing or evaluating pairwise interactions. During training, backpropagating through this matrix requires memory (or recomputation in IO-aware kernels).
Standard Softmax Attention (Autoregressive Generation):
Token 1 ──► [K1, V1] ───┐
Token 2 ──► [K2, V2] ───┼──► KV Cache Grows O(N) ──► Softmax(q_t @ K^T) @ V
Token 3 ──► [K3, V3] ───┤ (O(N) latency & memory per step)
Token t ──► [Kt, Vt] ───┘
Linear Attention / Retention (Autoregressive Generation):
Token t ──► k_t^T @ v_t ──► [ State Matrix S_t ] ──► o_t = q_t @ S_t
(Fixed d x d size) (O(1) latency & memory per step)During autoregressive inference, generating token requires retrieving all previous key-value pairs stored in GPU High-Bandwidth Memory (HBM). For a model with layers, hidden dimension , batch size , and sequence length , the KV cache memory scales strictly as:
At sequence lengths of 32k or 128k tokens, KV cache allocation eclipses the model parameter weights, bounding inference concurrency by memory bandwidth rather than compute.
The Associative Property and Linear Kernel Formulation
The root constraint preventing efficient reordering in standard self-attention is the row-wise softmax denominator. The -th token output is defined as:
Because the exponentiation binds and non-linearly, the outer product cannot be decomposed.
As demonstrated by Katharopoulos et al. (2020) in "Transformers are RNNs", replacing the exponential kernel with a decomposable feature representation (such as or ) removes the non-linear coupling:
Substituting this into the causal attention equation yields:
By applying the associative property of matrix multiplication:
The order of operations shifts from to . This simple algebraic identity enables a dual recurrent formulation.
The Recurrent Dual
Instead of storing sequence histories in a growing cache, the model maintains a fixed-size state matrix and normalizer vector :
During generation, updating and computing requires only basic matrix-vector operations. Memory complexity drops from to per sequence, and per-token step latency becomes entirely invariant to sequence length.
The Historical Pitfalls of Vanilla Linear Attention
Despite its theoretical appeal, early linear attention suffered from severe practical shortcomings:
- Unbounded Memory Accumulation: In vanilla linear attention, every historical token contributes equally to . Over long sequences, activations in grow monotonically, causing numerical overflow and gradient instability in low-precision (FP16/BF16) formats.
- Associative Recall Degradation: Softmax attention acts as a non-parametric memory lookup, retrieving precise key-value matches with high confidence. Compressing an entire sequence into a fixed matrix creates an informational bottleneck, causing linear attention to underperform standard Transformers on multi-hop reasoning, code syntax tracking, and needle-in-a-haystack retrieval tasks.
- Slow Wall-Clock Training: Without I/O-aware kernels tailored to the associative formulation, naive linear attention implementations required frequent memory round-trips to GPU global memory, rendering training slower than optimized FlashAttention routines.
Retentive Networks (RetNet): Multi-Scale Retention and Tri-Representation
To resolve the stability and expressive deficits of linear attention, Sun et al. (2023) from Microsoft Research introduced the Retentive Network (RetNet). RetNet eliminates the softmax normalization layer entirely and introduces an explicit exponential relative position decay into the linear formulation, termed Multi-Scale Retention (MSR).

1. Multi-Scale Decay Formulation
RetNet constructs a decay matrix where:
Here, is a fixed scalar decay factor. In Multi-Scale Retention, each attention head is assigned a distinct value (e.g., for head index ). Heads with high values retain long-range context, while heads with low focus strictly on local token interactions.
2. The Three Computation Paradigms
RetNet is uniquely defined by three mathematically equivalent representations:
A. Parallel Representation (Training)
For parallel execution across the entire sequence during pre-training, the retention layer operates as:
where denotes element-wise Hadamard multiplication. This formulation maps directly onto GPU Tensor Cores as a sequence of matrix multiplications, matching the parallel scaling of standard Transformer pre-training.
B. Recurrent Representation (Autoregressive Inference)
During token generation, retention converts to a state-space recurrent step:
Here, serves as the entire memory state. The autoregressive step requires no historical token lookups, reducing inference latency to a constant and decoding throughput up to 8.4x faster than KV-cached Transformers at 8k sequence lengths.
C. Chunkwise Recurrent Representation (Long-Context Pre-Training)
For training on massive sequence lengths without unbounded activation memory, RetNet segments sequences into discrete chunks of length (typically ).
Inside each chunk , intra-chunk retention is computed in parallel across the chunk tokens:
Between chunk boundaries, the recurrent state is updated and propagated forward:
where and are intra-chunk decay projection vectors. Chunkwise tiling reduces training compute to linear time while maintaining high Tensor Core arithmetic intensity.
Gated Linear Attention (GLA) and Hardware-Software Co-Design
While RetNet employs fixed, position-based decay factors , Yang et al. (2024) expanded the paradigm by introducing Gated Linear Attention (GLA). GLA replaces static decay with data-dependent forgetting gates, closing the expressivity gap between linear attention and softmax attention.
Linear Recurrent State Updates Compared:
1. Vanilla Linear Attention:
S_t = S_{t-1} + k_t^T @ v_t (Uniform accumulation, unstable)
2. RetNet (Multi-Scale Retention):
S_t = γ * S_{t-1} + k_t^T @ v_t (Static decay factor γ per head)
3. Gated Linear Attention (GLA):
S_t = G_t ⊙ S_{t-1} + k_t^T @ v_t (Data-dependent gate G_t = σ(x_t W))
4. Mamba-2 (State Space Duality):
h_t = A_t * h_{t-1} + B_t * x_t (Semi-separable 1D/scalar state updates)Data-Dependent Gating Mechanism
In GLA, the recurrent update is modulated by a dynamic 2D forget gate :
By parameterizing where , the model dynamically suppresses or preserves specific memory dimensions based on input semantics (such as wiping state memory at sentence or document boundaries).
FlashLinearAttention: I/O-Aware GPU Kernels
A core contribution of GLA is FlashLinearAttention (FLA), a specialized Triton library designed around GPU memory hierarchy:
- SRAM Chunk Tiling: Chunk-level state transitions and matrix updates are computed entirely within high-speed GPU on-chip Shared Memory (SRAM), eliminating expensive global HBM round-trips.
- Fused Inter-Chunk Reductions: By fusing the intra-chunk tensor contraction and inter-chunk state propagation into a single kernel launch, FLA achieves faster pre-training wall-clock speed than FlashAttention-2 on long sequences.
Architectural Comparison: Attention, Retention, and SSMs
Modern linear sequence models share deep mathematical connections, formalizing what Dao and Gu (2024) defined as Structured State Space Duality (SSD):
| Feature | Standard Transformer | RetNet (MSR) | Gated Linear Attention (GLA) | Mamba-2 (SSD) | RWKV-6 | | :--- | :--- | :--- | :--- | :--- | :--- | | Attention Kernel | Softmax | Linear | Gated Linear | Semiseparable 1-SSM | Time-decayed | | Decay Structure | None (Softmax Causal) | Static Multi-Scale | Data-Dependent Gate | Structured Matrix | Data-Dependent Vector | | Inference Step Memory | (Expanding Cache) | ( Matrix) | ( Matrix) | (1D/2D Hidden State) | ( Matrix) | | Inference Step Latency | Compute | Constant | Constant | Constant | Constant | | Training Complexity | (or Tiled IO) | (Chunkwise) | (Chunkwise FLA) | (SSD Matrix Multiply)| (WKV Kernel) | | Associative Recall | High | Moderate | High | High | High |
Practical Deployment Realities and Hybrid Systems
While linear attention architectures deliver massive inference throughput gains, production deployments highlight several architectural trade-offs:
1. The Fixed-State Capacity Bound
A standard Transformer storing full KV caches possesses non-parametric capacity: every token retains an uncompressed slot in memory. In contrast, linear attention models compress arbitrary context into fixed-size matrix states. While sufficient for general language understanding and long-context synthesis, complex synthetic retrieval tasks (such as tracing dense dependency graphs across millions of tokens) can experience capacity saturation.
2. The Hybrid Standard
To capture the benefits of both paradigms, frontier production architectures increasingly adopt hybrid designs:
- Linear-Attention Dominant Stacks: Interleaving 80-90% GLA or RetNet layers with 10-20% standard Softmax Attention layers (or Sliding Window Attention layers).
- Recurrent Prefill + Local Window: Retaining linear state updates for global context while reserving dense attention for local syntax and immediate token context.
This hybrid configuration preserves sub-quadratic pre-training efficiency and slashes inference KV cache memory footprints by up to 80%, while maintaining full retrieval precision on complex reasoning benchmarks.
Sources
- Retentive Network: A Successor to Transformer for Large Language Models (Sun et al., Microsoft Research, 2023)
- Gated Linear Attention Transformers with Hardware-Efficient Training (Yang et al., ICML 2024)
- Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention (Katharopoulos et al., ICML 2020)
- Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality (Dao & Gu, ICML 2024)
- RWKV: Reinventing RNNs for the Transformer Era (Peng et al., EMNLP 2023)
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Dao et al., NeurIPS 2022)



