Foundations to Frontier
Tests
E. Deep Learning › E17 State-Space and Recurrent-Revival Models

RetNet

The Retentive Network (Sun and colleagues, Microsoft, 2023) replaces softmax attention with retention: attention without the softmax, multiplied by an exponential decay γ^(n−m) that fades older tokens. The same operator has three equivalent forms: parallel (like attention, for training), recurrent (a matrix-valued state, for O(1) decoding) and chunkwise (parallel inside chunks, recurrent across them, for long sequences). Different heads use different decays, giving multi-scale memory. This page covers retention, its three forms, multi-scale decay and its relation to linear attention. In the exercises you implement the decay mask and the recurrent step, verify the three forms agree, and measure associative recall with non-orthogonal keys. The forms agreed to 3 × 10⁻¹⁵. With 8 stored pairs, the oldest pair was recalled perfectly at γ = 1, 96% of the time at γ = 0.9, 24% at 0.7 and 6% at 0.5.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Attention that fades, computed three ways

Retention works like attention: each new token asks how strongly it matches each earlier token and blends their values. There are two differences: no softmax, and older tokens are gradually discounted by a factor γ per step. That discount is what allows a cheap running memory. The same computation can be done all at once for training, step by step for generation, or in chunks for long inputs. The demo shows how γ sets how far back retention reaches.

State-space lab
Past tokens weighted by γ^k; effective memory about 1/(1 − γ) = 10 tokens. RetNet uses different γ per head to mix short and long memories.
Why engineers care

Retention gives Transformer-like training with RNN-like inference: low latency and fixed memory per generated token, useful for serving large models and for real-time systems.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Retention differs from attention by…
  2. 2. γ < 1 makes older tokens…
  3. 3. Retention can be computed…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Parallel form: Retention(X) = (QKᵀ ⊙ D)V with D_nm = γ^(n−m) for n ≥ m, else 0. For γ = 0.5 and 3 tokens, D has rows (1, 0, 0), (0.5, 1, 0), (0.25, 0.5, 1).
  • Recurrent form: S_n = γS_(n−1) + k_nᵀv_n, o_n = q_n S_n, with a d_k × d_v state per head (256 × 512 = 131,072 values).
  • Chunkwise form: within a chunk of size B, use the parallel form; across chunks, carry S scaled by γ^B. 13 tokens in chunks of 4 give 4 chunks.
  • Multi-scale decay: head h uses γ_h = 1 − 2^(−5−h), from 0.96875 (memory about 32 tokens) to 0.996 (256).
  • Rotation: RetNet also applies xPos-style rotations to q and k for relative position.
  • Decay of old information: after 7 steps, γ = 0.5 leaves 0.5⁷ ≈ 0.008 of a pair’s weight.
📝 Quick check (Level 1)
0/3 answered
  1. 1. D₃₁ for γ = 0.5 (rows/cols from 1):
  2. 2. Effective memory for γ = 0.96875:
  3. 3. Recurrent state per head:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

RetNet in code

Exercise 1.

  • The decay mask matched γ^(n−m).
  • One recurrent step stored k ⊗ v and read it with q.
  • The multi-scale γ_h values were correct.

Practice.

  • Use TorchScale (Microsoft) or community RetNet implementations; Group norm per head replaces softmax normalisation.
  • Use the parallel form for short training sequences, chunkwise (chunk 512) for long ones, and the recurrent form for decoding.
  • Consider data-dependent decay (as in GLA or RWKV-6) if content-based forgetting is needed.
D = decay_mask(L, gamma)                                  # (L, L), gamma^(n-m) below the diagonal
out = ((Q @ K.transpose(-1, -2)) * D) @ V                 # parallel retention (training)
# decoding, one token at a time:
S = gamma * S + K_t.unsqueeze(-1) * V_t.unsqueeze(-2)     # (d_k, d_v) state
o_t = Q_t @ S
🐍 Exercise 1: retention
Implement the decay mask, one recurrent retention step and RetNet's multi-scale decays.
📝 Quick check (Level 2)
0/3 answered
  1. 1. RetNet normalises heads with…
  2. 2. Long sequences use…
  3. 3. A library with RetNet:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Equivalence of the forms.

  • Unrolling S_n = γS_(n−1) + k_nᵀv_n gives S_n = Σ_(m≤n) γ^(n−m) k_mᵀv_m, so o_n = Σ_m γ^(n−m)(q_n·k_m)v_m. That is row n of (QKᵀ ⊙ D)V.
  • The chunkwise form splits this sum into within-chunk and earlier-chunk parts.

Retention as linear attention with decay.

  • Without the softmax, q·k scores are linear in the keys, so the state can accumulate key–value outer products: an associative memory that answers a query by q·S. This is why retention can do associative recall, unlike time-invariant SSMs with scalar states.
  • The decay γ trades recency against capacity. It limits interference from old pairs but also erases them; multi-scale heads mix short and long memories.

Limits. With a fixed γ, forgetting depends only on age, not on content. Very long-range exact recall competes with interference among many stored pairs in a finite d_k × d_v state.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Unrolled retention state is…
  2. 2. Retention can do associative recall because…
  3. 3. With fixed γ, forgetting depends on…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Are the three forms equivalent, and what does decay cost?

History.

  • Y. Sun et al., “Retentive network: a successor to Transformer for large language models” (2023).
  • Related: linear attention (Katharopoulos et al., 2020), Gated Linear Attention (Yang et al., 2024).

Exercise 2.

  • Equivalence: the parallel, recurrent and chunkwise (chunk 4) forms are computed on random Q, K, V (13 tokens, γ = 0.9).
  • Recall: 8 key–value pairs with random unit keys (dimension 16, so not orthogonal) and one-hot values are stored with decay γ. Each key is then queried, and accuracy is recorded by pair age (3000 trials).

The three forms agreed to 3 × 10⁻¹⁵.

γ newest pair 4th newest oldest (8th)
1.0 1.000 1.000 1.000
0.9 1.000 1.000 0.955
0.7 1.000 0.881 0.241
0.5 1.000 0.566 0.057
  • The forms are exactly equivalent, so training can use the parallel or chunkwise form and decoding the cheap recurrence.
  • Retention’s state is an associative memory: without decay it recalled all 8 pairs perfectly despite non-orthogonal keys.
  • Decay trades memory for recency: with γ = 0.7 the oldest pair is mostly lost (0.24), because older entries shrink below the interference from newer ones. This is why RetNet gives each head a different γ, close to 1 for long memory.
🐍 Exercise 2 (research): are the three forms equivalent, and what does decay cost?
Verify parallel, recurrent and chunkwise retention agree, and measure associative recall by pair age for several decays.

RetNet in AI, robotics and aerospace

  • Low-latency serving: constant-time decoding per token for assistants and real-time control interfaces.
  • Long streams: chunkwise processing of long sensor logs with bounded memory.
  • Design lesson: if an operator has parallel and recurrent forms, use each where it is cheapest, and tune decay to the memory each head needs.

References

  1. Y. Sun et al. (2023). “Retentive network: a successor to Transformer for large language models.” arXiv:2307.08621.
  2. A. Katharopoulos, A. Vyas, N. Pappas and F. Fleuret (2020). “Transformers are RNNs: fast autoregressive Transformers with linear attention.” Proc. ICML.
  3. S. Yang et al. (2024). “Gated linear attention Transformers with hardware-efficient training.” Proc. ICML.