Linear Attention and Retentive Networks: How Recurrent Duals and Chunkwise Tiling Eliminate the Quadratic Bottleneck

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 (GQ

8 min
Linear Attention and Retentive Networks: How Recurrent Duals and Chunkwise Tiling Eliminate the Quadratic Bottleneck

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 O(1)O(1) 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 QRN×dQ \in \mathbb{R}^{N \times d}, keys KRN×dK \in \mathbb{R}^{N \times d}, and values VRN×dV \in \mathbb{R}^{N \times d} across sequence length NN:

Attention(Q,K,V)=softmax(QKTd)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{Q K^T}{\sqrt{d}}\right) V

The intermediate matrix A=QKTRN×NA = Q K^T \in \mathbb{R}^{N \times N} requires materializing or evaluating N2N^2 pairwise interactions. During training, backpropagating through this N×NN \times N matrix requires O(N2)O(N^2) memory (or O(N2)O(N^2) 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 tt requires retrieving all previous key-value pairs stored in GPU High-Bandwidth Memory (HBM). For a model with LL layers, hidden dimension dmodeld_{\text{model}}, batch size BB, and sequence length NN, the KV cache memory scales strictly as:

KV Cache Size=2×B×L×N×dmodel×bytes per element\text{KV Cache Size} = 2 \times B \times L \times N \times d_{\text{model}} \times \text{bytes per element}

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 ii-th token output is defined as:

oi=j=1iexp(qikjTd)vjj=1iexp(qikjTd)o_i = \frac{\sum_{j=1}^i \exp\left(\frac{q_i k_j^T}{\sqrt{d}}\right) v_j}{\sum_{j=1}^i \exp\left(\frac{q_i k_j^T}{\sqrt{d}}\right)}

Because the exponentiation exp(qikjT)\exp(q_i k_j^T) binds qiq_i and kjk_j non-linearly, the outer product QKTQ K^T cannot be decomposed.

As demonstrated by Katharopoulos et al. (2020) in "Transformers are RNNs", replacing the exponential kernel with a decomposable feature representation ϕ(x)\phi(x) (such as ELU(x)+1\text{ELU}(x) + 1 or ReLU(x)\text{ReLU}(x)) removes the non-linear coupling:

sim(qi,kj)=ϕ(qi)ϕ(kj)T\text{sim}(q_i, k_j) = \phi(q_i) \phi(k_j)^T

Substituting this into the causal attention equation yields:

oi=j=1i(ϕ(qi)ϕ(kj)T)vjj=1iϕ(qi)ϕ(kj)T=ϕ(qi)j=1i(ϕ(kj)Tvj)ϕ(qi)j=1iϕ(kj)To_i = \frac{\sum_{j=1}^i \left(\phi(q_i) \phi(k_j)^T\right) v_j}{\sum_{j=1}^i \phi(q_i) \phi(k_j)^T} = \frac{\phi(q_i) \sum_{j=1}^i \left(\phi(k_j)^T v_j\right)}{\phi(q_i) \sum_{j=1}^i \phi(k_j)^T}

By applying the associative property of matrix multiplication:

(QKT)V=Q(KTV)(Q K^T) V = Q (K^T V)

The order of operations shifts from (N×N)×(N×d)(N \times N) \times (N \times d) to (N×d)×(d×d)(N \times d) \times (d \times d). 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 StRd×dS_t \in \mathbb{R}^{d \times d} and normalizer vector ztRdz_t \in \mathbb{R}^d:

St=St1+ϕ(kt)TvtS_t = S_{t-1} + \phi(k_t)^T v_t

zt=zt1+ϕ(kt)Tz_t = z_{t-1} + \phi(k_t)^T

ot=ϕ(qt)Stϕ(qt)zto_t = \frac{\phi(q_t) S_t}{\phi(q_t) z_t}

During generation, updating StS_t and computing oto_t requires only basic matrix-vector operations. Memory complexity drops from O(N)O(N) to O(1)O(1) 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:

  1. Unbounded Memory Accumulation: In vanilla linear attention, every historical token contributes equally to StS_t. Over long sequences, activations in StS_t grow monotonically, causing numerical overflow and gradient instability in low-precision (FP16/BF16) formats.
  2. 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 d×dd \times d 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.
  3. 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).

Chunkwise Retention and Recurrent Dual Architecture

1. Multi-Scale Decay Formulation

RetNet constructs a decay matrix DRN×ND \in \mathbb{R}^{N \times N} where:

Dn,m={γnm,nm0,n<mD_{n, m} = \begin{cases} \gamma^{n - m}, & n \ge m \\ 0, & n < m \end{cases}

Here, γ(0,1)\gamma \in (0, 1) is a fixed scalar decay factor. In Multi-Scale Retention, each attention head is assigned a distinct γ\gamma value (e.g., γ=125h\gamma = 1 - 2^{-5 - h} for head index hh). Heads with high γ\gamma values retain long-range context, while heads with low γ\gamma 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:

Retention(X)=((QKT)D)V\text{Retention}(X) = \left( (Q K^T) \odot D \right) V

where \odot 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:

St=γSt1+ktTvtS_t = \gamma S_{t-1} + k_t^T v_t

ot=qtSto_t = q_t S_t

Here, StRd×dS_t \in \mathbb{R}^{d \times d} serves as the entire memory state. The autoregressive step requires no historical token lookups, reducing inference latency to a constant O(1)O(1) 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 BB (typically B=512B = 512).

Inside each chunk ii, intra-chunk retention is computed in parallel across the chunk tokens:

R[i]intra=((Q[i]K[i]T)Dintra)V[i]R_{[i]}^{\text{intra}} = \left( (Q_{[i]} K_{[i]}^T) \odot D_{\text{intra}} \right) V_{[i]}

Between chunk boundaries, the recurrent state SiS_i is updated and propagated forward:

Si=γBSi1+(K[i]TΞ)V[i]S_i = \gamma^B S_{i-1} + (K_{[i]}^T \odot \Xi) V_{[i]}

R[i]=R[i]intra+(Q[i]Θ)Si1R_{[i]} = R_{[i]}^{\text{intra}} + (Q_{[i]} \odot \Theta) S_{i-1}

where Ξ\Xi and Θ\Theta are intra-chunk decay projection vectors. Chunkwise tiling reduces training compute to linear O(N)O(N) 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 γ\gamma, 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 GtRd×dG_t \in \mathbb{R}^{d \times d}:

St=GtSt1+ktTvtS_t = G_t \odot S_{t-1} + k_t^T v_t

By parameterizing Gt=αt1TG_t = \alpha_t \mathbf{1}^T where αt=σ(xtWα)\alpha_t = \sigma(x_t W_\alpha), 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:

  1. 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.
  2. 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 (QKT/d)(Q K^T / \sqrt{d}) | Linear (QKTD)(Q K^T \odot D) | Gated Linear (QKTG)(Q K^T \odot G) | Semiseparable 1-SSM | Time-decayed (KTV)(K^T V) | | Decay Structure | None (Softmax Causal) | Static Multi-Scale γ\gamma | Data-Dependent Gate αt\alpha_t | Structured AA Matrix | Data-Dependent Vector | | Inference Step Memory | O(N)O(N) (Expanding Cache) | O(1)O(1) (d×dd \times d Matrix) | O(1)O(1) (d×dd \times d Matrix) | O(1)O(1) (1D/2D Hidden State) | O(1)O(1) (d×dd \times d Matrix) | | Inference Step Latency | O(N)O(N) Compute | O(1)O(1) Constant | O(1)O(1) Constant | O(1)O(1) Constant | O(1)O(1) Constant | | Training Complexity | O(N2)O(N^2) (or Tiled IO) | O(N)O(N) (Chunkwise) | O(N)O(N) (Chunkwise FLA) | O(N)O(N) (SSD Matrix Multiply)| O(N)O(N) (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 d×dd \times d 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

Written by

More to read

  • On-Device LLM Inference in Production: Architecture, Runtimes, and Hardware Constraints

    Deploying generative language models directly onto edge devices such as smartphones, laptops, embedded systems, and browser sandboxes marks a fundamental shift in AI systems engineering. Moving inference from centralized GPU clusters to client silicon eliminates cloud API costs, cuts network latency to zero, guarantees data privacy by keeping user inputs local, and enables offline functionality. However, executing modern autoregressive models on resource-constrained client hardware presents str

    1 min
  • Kahneman-Tversky Optimization: Aligning LLMs with Prospect Theory and Binary Feedback

    Alignment of large language models has traditionally centered on preference learning. Methods such as Reinforcement Learning from Human Feedback (Christiano et al., 2017), Direct Preference Optimization (Rafailov et al., 2023), and Identity Preference Optimization (Azar et al., 2023) require training data formatted as pairs of candidate responses $(x, y_w, y_l)$ generated for the exact same prompt $x$, where $y_w$ is preferred over $y_l$. In real-world production environments, paired preference

    1 min
  • SK Hynix Announces 9 Billion Share Buyback to Calm AI Spending Worries

    SK Hynix announced Wednesday it will buy back and cancel 40 trillion won ($28.61 billion) worth of treasury shares, allocating more than 50 percent of free cash flow generated between 2025 and 2027 to shareholder returns. The buyback, to be executed between August 20 and November 19, represents roughly 24 million shares. The company also said it would pursue an expansion of its total shareholder return target from the previous "within 50 percent of cumulative FCF" to "over 50 percent of cumulat

    1 min