↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Layer 1 of 12 Head 1: γ = 0.968750 · half-life ≈ 22 tokens Formula demo on GPT-2's recorded Q, K, V · no softmax · γh = 1 − 2−5−h · Θ rotation · keys ÷ √64
Loading the RetNet demo…

What retention is

Retention (Sun et al., 2023) is attention without the softmax, with a fixed decay in its place. Dropping the softmax lets the layer be rewritten as a recurrence over a fixed-size state, and the decay keeps that state from growing without bound. The RetNet paper gives three equivalent ways to compute it: in parallel over the whole sequence for training, recurrently token by token for decoding at cost per token, and chunk by chunk for long sequences.

This tab is a formula demo on GPT-2 small's recorded queries, keys and values at layers 1, 3, 6, 9 and 12. GPT-2 was trained with softmax attention, so the page shows what retention's formula does with real vectors, not what a trained RetNet computes. Every constant comes from the paper and its torchscale code; nothing is learned for the demo.

From recurrence to matrix

Start from a state , a matrix that decays by and absorbs each new key-value pair, and read it with the query:

The right-hand side is a weighted sum of values, so for the whole sequence at once

The Parallel retention card draws for one head (the QKᵀ ⊙ D view), with the decay alone (Decay D) and the raw products (QKᵀ) as the other two views. The weights are signed and need not sum to one; RetNet normalises each head's output afterwards instead.

Positions: the Θ rotation

Before the product, queries and keys are rotated by angles proportional to their positions, and , turning each pair of dimensions by . The product then depends only on the offset , as with rotary embeddings (RoFormer). RetNet takes the rotation from xPos (Sun et al., 2022). The demo uses torchscale's angles for and divides keys by ; the Θ boxes on the card open the rotation.

Multi-scale decay

Each head gets its own decay, fixed by the paper rather than learned:

Head 1 has , so its weights halve about every 22 tokens; head 12 has and halves only after about 45,000. The toolbar gives each head's half-life. Short-memory heads concentrate on recent tokens, long-memory heads keep nearly undecayed sums over the whole prompt, and between them the heads cover many time scales.

The decay is a cousin of ALiBi (the ALiBi tab). ALiBi subtracts a penalty proportional to distance from each score before the softmax, which multiplies the exponentiated scores by a geometric factor ; retention multiplies the raw products by and does not normalise.

The recurrent form

The Recurrent form panel runs the same computation token by token. It draws the head's 64 × 64 state after the selected token, the size of the state at every token (it shrinks by each step and grows by ), and the selected token's output from both forms. The state has the same size however long the prompt is, which is the point: a decoding step reads one matrix instead of a growing key-value cache. Over every token, head and recorded layer, the two float32 results differ by at most , or of the largest output.

For long sequences RetNet uses the chunkwise form between the two: the parallel product inside each chunk, and a state passed from chunk to chunk as in the recurrence.

The full layer and where it came from

A RetNet layer does more than this page shows: each head's output is normalised with GroupNorm, multiplied by a swish gate and projected by . GPT-2 has no gate weights , so the demo stops at the heads.

Retention belongs to the linear-attention family. Katharopoulos et al. (2020) showed that attention without the softmax is a recurrent network, and Schlag et al. (2021) read its state as a fast-weight memory written by outer products. RetNet added the fixed per-head decay and the rotation. Later layers made the decay depend on the input: Mamba-2, whose scalar-decay form contains retention as the case of a constant step (the Mamba-2 tab), and RWKV-7 (the Linear tab).

Papers

  1. Retentive Network: A Successor to Transformer for Large Language Models Yutao Sun et al. · arXiv 2023 Retention: parallel, recurrent and chunkwise forms, the multi-scale decay and the full RetNet layer.
  2. A Length-Extrapolatable Transformer Yutao Sun et al. · ACL 2023 xPos, the relative-position rotation that RetNet's Θ follows.
  3. Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention Angelos Katharopoulos et al. · ICML 2020 Linear attention and its recurrent form, retention without the decay.
  4. Linear Transformers Are Secretly Fast Weight Programmers Imanol Schlag, Kazuki Irie, Jürgen Schmidhuber · ICML 2021 Reads the linear-attention state as a fast-weight memory written by outer products, the state drawn on the page.
  5. RoFormer: Enhanced Transformer with Rotary Position Embedding Jianlin Su et al. · arXiv 2021 Rotary position embeddings, the rotation that xPos and RetNet build on.
  6. Language Models are Unsupervised Multitask Learners Alec Radford et al. · 2019 GPT-2, whose recorded queries, keys and values this demo runs through retention.

See also