TL;DR
RetNet is a language-model architecture that tries to keep the best of two families. A Transformer trains in parallel across a whole sequence but is slow and memory-hungry when it generates text, because it keeps a growing cache of past keys and values. A recurrent network generates cheaply, one step at a time, but cannot train in parallel. RetNet replaces attention with a retention layer that has three equivalent forms: a parallel form for training, a recurrent form for generation, and a chunkwise form for long sequences. The authors report Transformer-level language-modelling scores with much lower inference cost.
Builds on: Transformer, Recurrent Neural Network, Linear Attention · Leads to: State Space Model, Mamba: Linear-Time Sequence Modeling with Selective State Spaces
Why it mattered
For several years the Transformer was the default architecture for large language models, and its main cost is at generation time. Serving a Transformer means storing a key-value cache that grows with every token, so memory use and latency rise with the length of the output. Many groups had tried to find a design that avoids this cost, but the earlier attempts gave up quality or training speed. RetNet was presented as a candidate successor that gives up neither, which is the claim the title makes.
The paper's concrete contribution is a single layer, the retention mechanism, shown to be mathematically the same computation written three ways. That lets one trained model be run in whichever form suits the job. The authors trained language models from 1.3B to 6.7B parameters and reported costs in memory, throughput and latency against Transformers of matching size.
The problem
The authors frame the difficulty as an "impossible triangle" with three goals: training parallelism, good performance, and low-cost inference. Previous approaches met two of the three.
Transformers train in parallel and perform well, but their inference cost per step is in the sequence length and their key-value cache is memory-bound. Linearised attention, which replaces the softmax with a kernel so that generation can run as a recurrence, is efficient but performs worse than standard attention. Recurrent models generate in constant time per step but cannot be trained in parallel across the sequence, and the element-wise simplifications used to speed them up reduce their capacity. State-space models such as S4 are another route; the authors argue that none of these had yet broken the triangle.
RetNet's stated target is a layer that sits in the corner the others miss: parallel training, inference per token, and quality close to the Transformer.
How it works
RetNet is a stack of identical blocks. Each block has a multi-scale retention (MSR) module and a feed-forward module, with residual connections and pre-normalisation, as in a Transformer. The retention layer is where the idea lives. Here is the three-form trick on one tiny instance: a single channel, with the query and key set to and values at positions , , , and decay .
- Input. Each token is projected to a value . The query and key projections and are content-aware, so they depend on the token. In the example both are .
- Recurrent form, step by step. Keep a state that carries the past forward, with , and read out . Start with . At : , so . Check: only one token has been seen.
- Continue the recurrence. At : , so . At : , so . Check: the state is a decayed running sum, so the older value counts half as much at step as at step .
- Parallel form. The same output can be written as one matrix product, , where is a lower-triangular matrix with for and zero above the diagonal. Here has rows , and . Multiplying gives . Check: the same number as step 3, computed without a loop.
- Chunkwise form. For a long sequence, split it into chunks of length . Inside a chunk, use the parallel form. Between chunks, pass a summary state, as in step 2. The result is the same, but memory stays bounded by the chunk size.
- Generation. At inference, use only the recurrent form. Each new token updates with a fixed-size matrix and reads it out. The cost per token does not depend on how many tokens came before.
- Multi-scale decay. The model has several heads, and each head uses its own , so some heads forget quickly and some slowly. The outputs of the heads are normalised with GroupNorm and gated by a swish branch before the output projection.
flowchart LR X["Token inputs"] --> P["Parallel form, training: (QK^T ⊙ D) V"] X --> C["Chunkwise form, long training sequences"] X --> R["Recurrent form, generation: S_n = γ S_(n-1) + K_n^T V_n"] P --> O["Same outputs"] C --> O R --> O
The math
Step 1 in symbols: the recurrence
Let be the scalar value at step , a state vector, and a fixed matrix. The paper begins with the linear recurrence , with output . Unrolling it gives
Making a scalar multiple of a rotation, and then real, turns the power into a decay . This gives the decay used in the walkthrough.
Step 4 in symbols: the parallel form
With , , , and a decay matrix whose entries are for and zero otherwise, the retention layer is
The product is the same kind of object as attention scores, but it is multiplied elementwise by instead of passed through a softmax. Removing the softmax is what makes the recurrent form possible. In the paper, and are also multiplied by a rotation that encodes position, the way rotary embeddings do.
Step 2 in symbols: the recurrent form
The same layer, written as a recurrence over a state matrix , is
The state has fixed size, so generating one token costs the same at step ten and at step ten thousand. This is the inference claim.
Step 5 in symbols: the chunkwise form
Let be the chunk length and let index the -th chunk. The paper writes the output of chunk as the sum of an inner-chunk term and a cross-chunk term,
where the summary state is updated as . The paper sets and to powers of that depend on the position inside the chunk. The inner term is parallel, the cross term is recurrent.
Step 7 in symbols: multi-scale decay and the block
The decay for head is
so the first head has , and each later head has a value closer to and therefore remembers further back. The head outputs are concatenated, normalised by GroupNorm, gated and projected:
The block then adds a feed-forward network with a Gaussian Error Linear Unit (GELU) activation:
The paper also rescales the scores for numerical stability, which does not change the output because GroupNorm is invariant to a scalar factor.
Use a single channel with , decay , and values .
Recurrent form:
- Start with .
- , so .
- , so .
- , so .
Parallel form:
- has entries , , in its last row.
- Since is all ones here, the output is .
- Last row: .
- Second row: .
Interpretation: both forms give and . The recurrent form computes them one at a time with a state of fixed size. The parallel form computes all of them at once. Training uses the second; generation uses the first.
Table 4 of the paper reports, for the 1.3B model trained with an -token sequence on eight A100-80GB GPUs, a memory use of GB for a standard Transformer and GB for RetNet. Throughput is words per second for the Transformer and for RetNet.
- Memory saving: GB.
- Fraction saved: , so about percent.
- Throughput ratio: .
- The introduction summarises training as about a speed-up, which is close to this ratio.
Interpretation: at this size RetNet uses much less memory and trains several times faster than a plain Transformer. The introduction's summary of 25 to 50 percent memory saving is a looser range than this one row, so read the table, not the summary, for the exact figure. The paper's comparison with FlashAttention is narrower, covered in the results below.
Try it: for the 2.7B model the paper reports GB for the Transformer and GB for RetNet. What fraction of memory does RetNet save? Answer: , about percent.
Results
The main comparisons are on language modelling with models of B, B and B parameters, trained on B tokens from The Pile, C4 and The Stack.
Scaling. The authors report that RetNet and Transformer give comparable perplexity at the smaller sizes, and that RetNet starts to outperform the Transformer once the model is larger than about B parameters. The claim rests on three model sizes, so it is an empirical trend, not a law.
Zero-shot and few-shot. For the B models, the average accuracy over seven tasks is for the Transformer and for RetNet in the zero-shot setting, and versus with four shots. RetNet is higher on every listed task in both settings; the gains are largest on Winograd ( versus ) and COPA ( versus ).
Training cost. Table 4 compares a Transformer, a Transformer with FlashAttention, and RetNet, measured on eight A100-80GB GPUs with an -token sequence. At B, RetNet uses GB against GB for FlashAttention, and processes words per second against . At B the gap narrows: GB and words per second for RetNet against GB and for FlashAttention. The authors note that their RetNet implementation uses vanilla PyTorch, so kernel-level optimisation was left for later work.
Inference. The B model's memory is almost entirely the weights: the authors state that the extra state is about percent, while the weights are percent. For an k-token input on a B model, the introduction reports that RetNet decodes faster and saves percent of memory compared with a Transformer using a KV cache. Decoding throughput stays constant as the output grows, and latency barely changes with batch size.
Against other efficient models. On a M-parameter comparison, RetNet has the lowest perplexity on the in-domain set () and on the out-of-domain sets. For example, the Transformer-style Linear Transformer scores in-domain, RWKV , H3 and Hyena .
Ablations. Removing the swish gate raises in-domain perplexity from to . Removing GroupNorm raises it to , and reducing the head dimension from to raises it to . Removing the decay altogether, or using one decay across all heads, also hurts.
Limits. The quality comparison with the Transformer goes up only to B parameters, and the paper's claim that RetNet pulls ahead above about B rests on three sizes. The table comparing properties (Table 1) is qualitative. The training speed numbers come from eight A100-80GB GPUs with a non-optimised PyTorch implementation, so the gap to FlashAttention is closer than the gap to a plain Transformer. The inference numbers are for the B model on one GPU type. The 13B row in Table 4 reports training cost only, with no quality results.
Reading guide
The paper follows a standard research-paper layout. Each section needs the following lessons.
- Section 1 (Introduction). Needs Transformer and KV Cache, to follow the "impossible triangle" argument about inference cost.
- Section 2.1 (Retention). Needs Recurrent Neural Network and Linear Attention. The derivation uses matrix powers and Matrix Multiplication, and the position encoding is the one from Rotary Position Embedding.
- Section 2.2 (Multi-scale retention). Needs Group Normalization and Gated Linear Unit (GLU) (the swish gate is a close relative), plus Gaussian Error Linear Unit (GELU) for the feed-forward part.
- Section 2.3 (Architecture). Needs Layer Normalization and Residual Connection. The residual pattern is the one from the Transformer.
- Section 2.4 (Relation to other methods). Needs Efficiently Modeling Long Sequences with Structured State Spaces for state-space models and Softmax to see what retention removes.
- Section 3.1 and 3.2 (Setup and language modelling). Needs Perplexity and In-Context Learning for the zero-shot and few-shot tables.
- Section 3.3 and 3.4 (Training and inference cost). Needs FlashAttention: Fast and Memory-Efficient Exact Attention for the baseline comparison.
- Section 3.5 and 3.6 (Variants and ablations). Needs State Space Model for context on the other efficient models compared.
Check yourself
QuestionRetNet has a parallel form for training and a recurrent form for generation. What single property of the recurrent form gives it constant cost per generated token?Show answer
The recurrent form keeps a state matrix whose size does not depend on sequence length. Each new token updates and reads it with , so the work per token is the same at any position. A Transformer's key-value cache grows with each token, which is why its cost rises.
QuestionWith one channel, and values at positions 1 and 2, what is the output at position 2 using the recurrence with ?Show answer
. Then , so the output is . Check with the parallel form: and , so the output is .
QuestionWhy does the paper remove the softmax from retention, and what does it give up?Show answer
A softmax normalises each row of scores over all earlier tokens, which cannot be updated one token at a time as a fixed-size state. Removing it, and using a decay matrix instead, lets the same computation run as a recurrence. The price is that the model no longer makes the normalised, competitive weighting over tokens that attention does; the paper compensates with GroupNorm and normalisation of the retention scores, and reports that quality stays comparable.
QuestionA team wants to serve a B model that must generate very long outputs cheaply. Based on the paper's reported numbers, which architecture should they expect to scale better at generation time, and what should they check before deciding?Show answer
RetNet should scale better at generation time: its memory is dominated by weights, with about percent extra state, while a Transformer's cache grows with output length. Before deciding, they should check the paper's limits: its inference numbers come from a single B model on one GPU type, its training speed comparison used a non-optimised implementation, and they should measure quality on their own tasks, since the paper's language-modelling results are on its own data mix.