# Retentive Network: A Successor to Transformer for Large Language Models

Yutao Sun, Li Dong, Shaohan Huang, Shuming Ma, Yuqing Xia, Jilong Xue and 2 more · arXiv · 2023-7-17

- **Problem** Transformers train in parallel, but their decoding cost per step grows with the sequence length, while recurrent models decode cheaply yet are hard to train in parallel.
- **Method** Replaces attention with a retention layer that runs in parallel for training, recurrently for decoding and in chunks for long sequences, with a decay per head, a swish gate and GroupNorm.
- **Results** Reached 69.51 zero-shot average accuracy against 66.07 for Transformer at 6.7B.

## The trade-off between parallel training and cheap decoding

Transformers train well because all positions of a sequence can be computed together, but at inference each new token must attend to the stored keys and values of every earlier token, so per-step cost and memory grow with the sequence. Recurrent models have the opposite profile: a fixed-size state keeps each decoding step cheap, but training cannot be spread across positions in the same way.

The authors call the goal of satisfying all three requirements at once the impossible triangle. RetNet is designed to sit inside that triangle, combining parallel training, low-cost inference and Transformer-comparable quality.

Linearized attention rewrites the exponential of query-key scores as a product of feature maps, \\(\\phi(q)\\cdot\\phi(k)\\), so decoding can run as a recurrence, but its modeling quality falls behind Transformers. Element-wise recurrent variants gain speed by simplifying the state update, which reduces representation capacity, and state-space alternatives such as S4 and its variants have not produced a clear winner over Transformers.

Figure 1 gives the headline comparison, with inference cost reported at an 8k input length.

## From a linear recurrence to a retention form

The starting point is a linear state recurrence. The state \\(s\_n\\) is updated from the previous state and the current projected input, and the output at step n is the query times the state. Unrolling the recurrence gives \\(o\_n=\\sum\_{m=1}^{n}Q\_nA^{n-m}K\_m^{\\top}v\_m\\), so each output is a weighted mix of earlier inputs with weights that depend on the lag n-m.

Query and key projections \\(Q=XW\_Q\\) and \\(K=XW\_K\\) with learnable matrices make the mixing depend on the content of the input rather than on position alone.

The resulting term has the same form as xPos, a relative position embedding proposed for Transformers, which is why the authors connect the two. With \\(\\gamma\\) as a scalar, the output becomes \\(o\_n=\\sum\_{m=1}^{n}\\gamma^{n-m}(Q\_ne^{in\\theta})(K\_me^{im\\theta})^{\\dagger}v\_m\\), where \\(\\dagger\\) is the conjugate transpose. The decay depends only on the distance n-m, while the rotations carry absolute position.

Stacking these terms over positions gives the parallel form, with \\(Q=(XW\_Q)\\odot\\Theta\\), \\(K=(XW\_K)\\odot\\Theta\\), \\(V=XW\_V\\) and \\(\\Theta\_n=e^{in\\theta}\\). A causal decay matrix D with entries \\(D\_{nm}=\\gamma^{n-m}\\) for \\(n\\ge m\\) and 0 otherwise gives \\(\\text{Retention}(X)=(QK^{\\top}\\odot D)V\\). This is a matrix product followed by an elementwise mask, so all positions can be computed together on a GPU, much as in self-attention.

## Recurrent and chunkwise forms of the same layer

The recurrent form keeps one state matrix per layer, with \\(S\_n=\\gamma S\_{n-1}+K\_n^{\\top}V\_n\\) and \\(\\text{Retention}(X\_n)=Q\_nS\_n\\). Each decoded token costs the same however many tokens came before, which is the O(1) inference complexity the authors claim for RetNet. Because the form works without a key-value cache, the implementation is also simpler.

Figure 3 places the parallel and recurrent computations of the same layer side by side.

For long inputs the sequence is split into chunks of length B. Each chunk is computed with the parallel form, and state crosses chunk boundaries through the recurrence. The chunk state is \\(R\_i=K\_{\[i\]}^{\\top}(V\_{\[i\]}\\odot\\zeta)+\\gamma^{B}R\_{i-1}\\), with \\(\\zeta\_{ij}=\\gamma^{B-i-1}\\). The output of chunk i is an inner-chunk term \\((Q\_{\[i\]}K\_{\[i\]}^{\\top}\\odot D)V\_{\[i\]}\\) plus a cross-chunk term \\((Q\_{\[i\]}R\_{i-1})\\odot\\xi\\), where \\(\\xi\_{ij}=\\gamma^{i+1}\\).

The inner term is the parallel form and the cross term reads the carried state, so the chunk length B controls how the work splits between the two.

Training uses the parallel and chunkwise forms, and the authors highlight the chunkwise form for long sequences because it is efficient in both FLOPs and memory. The training cost experiment sets the chunk size to 512.

## Multi-scale retention and the block around it

The head outputs are concatenated, normalized by GroupNorm applied to each head separately, and gated as \\(\\text{MSR}(X)=(\\text{swish}(XW\_G)\\odot Y)W\_O\\). Each head is normalized alone because the different decays give the heads different variance statistics. The swish gate adds non-linearity to the layer.

Several normalization factors keep the numbers in range. The authors rely on GroupNorm producing the same output when a scalar factor multiplies a head's output. The query-key product is scaled as \\(QK^{\\top}/\\sqrt{d}\\), the decay mask becomes \\(\\tilde D\_{nm}=D\_{nm}/\\sqrt{\\sum\_{i=1}^{n}D\_{ni}}\\), and the retention scores become \\(\\tilde R\_{nm}=R\_{nm}/\\max(\|\\sum\_{i=1}^{n}R\_{ni}\|,1)\\).

The authors state that these factors leave the final results unchanged while stabilizing the forward and backward numerics.

An L-layer model stacks blocks with pre-LayerNorm residual sub-layers, \\(Y\_l=\\text{MSR}(LN(X\_l))+X\_l\\) and \\(X\_{l+1}=\\text{FFN}(LN(Y\_l))+Y\_l\\). The FFN is \\(\\text{gelu}(XW\_1)W\_2\\), and the input embeddings are packed into \\(X^0\\in\\mathbb{R}^{\|x\|\\times d\_{model}}\\).

## Where retention sits relative to attention, S4 and linear variants

Compared with attention, retention keeps the query-key structure but removes softmax, and that removal is what makes the recurrent form possible. The closest Transformer variant is the Lex Transformer, which uses xPos as its position embedding, and the authors say the derivation of retention lines up with xPos.

When the query and key projections are content-unaware, retention reduces to an S4-style computation, so S4 appears as a special case of the formulation. Linear attention uses kernels in place of softmax, and the authors argue that it struggles to encode position. Retention is derived from the recurrence rather than from approximating softmax.

Table 1 places the architectures on training parallelization, inference cost, long-sequence memory and performance, and marks RetNet with O(1) inference and O(N) long-sequence memory.

AFT moves softmax onto key vectors and uses element-wise operations, and RWKV replaces position embeddings with exponential decay. Retention keeps high-dimensional states to encode sequence information. Retention also swaps the LayerNorm of Sub-LayerNorm for GroupNorm because multi-scale decay gives the heads different variances. The authors connect the retained states to expressive ability, a point the small-scale comparison in Section 3.5 only partly addresses.

## Scaling against Transformer from 1.3B to 6.7B

The batch size is 4M tokens and the maximal length is 2048. The corpus combines The Pile, C4 and The Stack. To keep parameter counts comparable, RetNet's value projection and gate are twice as wide as its query and key projections, and its FFN intermediate width is 2d. The head dimension is 256 for queries and keys and 512 for values.

Validation perplexity of RetNet is comparable to Transformer across the three sizes, and the authors describe its size scaling as favorable. The authors observe a tendency for RetNet to outperform Transformer for models larger than 2B, a pattern drawn from these three sizes. The authors also report that RetNet training was stable in their runs.

Optimization uses AdamW with 375 warm-up steps, and parameters are initialized following DeepNet. The models were trained on 512 AMD MI200 GPUs with an implementation based on TorchScale.

## Zero-shot and four-shot downstream accuracy

Zero-shot and four-shot accuracy were measured on seven tasks, HellaSwag, BoolQ, COPA, PIQA, Winograd, Winogrande and StoryCloze, using the 6.7B models. The authors state that these accuracy numbers agree with the perplexity trend reported earlier.

Averaged over the tasks, zero-shot accuracy is 69.51 for RetNet against 66.07 for Transformer. The gaps are largest on COPA, 77.0 against 69.0, and on Winograd, 77.2 against 69.5. On BoolQ and PIQA the two are close, at 62.2 against 62.0 and 75.4 against 74.6.

With four shots, the averages are 66.44 for Transformer and 69.76 for RetNet. RetNet's score is higher than Transformer's on each of the seven tasks in both settings, although the authors describe the two as comparable.

## Training speed and memory against Transformer and FlashAttention

The training sequence length is 8192, the runs use eight Nvidia A100-80GB GPUs, and tensor parallelism is enabled for the 6.7B and 13B models. RetNet is implemented in vanilla PyTorch and uses the chunkwise form with chunk size 512, without FlashAttention-like kernels.

The authors summarize the training gains as 7× acceleration and 25-50% memory saving relative to standard Transformer.

The authors describe RetNet as still competitive with FlashAttention without specific kernels, and they note that kernel fusion could reduce its cost further. The authors also ran training on an AMD MI200 cluster.

## Memory, throughput and latency during decoding

The introduction's headline is 8.4× faster decoding and 70% memory saving for a 7B model at an 8k sequence length, measured against Transformer with key-value caches. The detailed inference study in Section 3.4 runs the 6.7B model on an A100-80GB GPU, with Transformers reusing their KV caches and RetNet using its recurrent form. The two statements therefore refer to different model sizes, 7B in the introduction and 6.7B in the detailed study.

Transformer memory grows linearly with sequence length because of the KV cache, while RetNet's memory stays consistent even for long sequences. At long sequences RetNet's additional memory is about 3%, with model weights occupying 97%. Figure 6 shows GPU memory and throughput against sequence length, and latency against batch size, for Transformer and RetNet at 6.7B.

Transformer's throughput falls as the decoded length grows, while RetNet's throughput is higher and does not change with length. Transformer latency rises with batch size and input length, so keeping latency acceptable forces smaller batches and lower overall throughput. RetNet's decoding latency stays almost the same across batch sizes and input lengths.

## Variant comparison and ablation results

The variant comparison uses models with 200M parameters, 16 layers and hidden dimension 1024, trained for 10k steps with 0.5M-token batches. The compared models are Linear Transformer, RWKV, H3 and Hyena, with the TimeMix module substituting for self-attention in RWKV while FFN layers stay the same.

On the in-domain set RetNet reaches 26.05 perplexity, against 30.92 for RWKV, 29.97 for H3, 32.08 for Hyena and 40.24 for Linear Transformer. RetNet also has the lowest perplexity on all four out-of-domain corpora, for example 45.27 on PG22 against 49.17 for H3.

For training, the authors compare token-mixing costs: RWKV's is O(dn), Hyena's is O(dn log n) with Fast Fourier Transform acceleration, and the chunkwise form of retention is O(dn(b+h)). Here b is the chunk size and h the head dimension, usually set to 512 and 256, and the authors argue that b+h has negligible effect at large d or long n.

For inference, Hyena has the same O(n) cost per step as Transformer, while the other compared architectures decode in O(1).

The ablations use the same evaluation settings as the variant comparison and remove one design choice at a time. Removing the swish gate raises in-domain perplexity from 26.05 to 27.84, and removing GroupNorm raises it to 27.54. Removing γ decay, which amounts to γ = 1, gives 27.86. Replacing the multi-scale decay with γ = 127/128 for all heads gives 27.02.

Every ablated row is higher than the full model on all five columns.

Reducing the head dimension from 256 to 64 gives 27.68 in-domain perplexity, against 26.05 for the full model. Queries and keys use 64 and values use 128, while the hidden dimension is kept, so the number of heads increases. The authors tie this to head dimension setting the memory capacity of the recurrent hidden states.

## Limitations of the evidence

The training cost numbers come from a vanilla PyTorch implementation of RetNet, and the authors leave kernel fusion and FlashAttention-like kernels for future work. Those training measurements use a sequence length of 8192 on eight A100-80GB GPUs.

The detailed inference measurements cover the 6.7B model on an A100-80GB GPU. The 7B headline figures in the introduction come from a separate statement and are not repeated in the detailed inference study.

The advantage over Transformer is observed across three sizes, and the authors describe a tendency beyond 2B, while at smaller sizes the results are described as comparable.

The authors plan to scale RetNet further in model size and training steps.

## Paper info

- **Authors**: Yutao Sun, Li Dong, Shaohan Huang, Shuming Ma, Yuqing Xia, Jilong Xue, Jianyong Wang, Furu Wei
- **Venue**: arXiv · Preprint
- **First published**: 2023-7-17
- **Added**: 2026-10-20
- **Updated**: 2023-8-9
- **Version**: v4
- **arXiv**: 2307.08621
- **Category**: cs.CL · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2307.08621v4) · [PDF ↗](https://arxiv.org/pdf/2307.08621v4)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Yutao Sun, Li Dong, Shaohan Huang, Shuming Ma, Yuqing Xia, Jilong Xue, Jianyong Wang, Furu Wei (2023). Retentive Network: A Successor to Transformer for Large Language Models. arXiv:2307.08621v4. https://arxiv.org/abs/2307.08621v4

**BibTeX**

```
@misc{sun2023retentive,
  title = {{Retentive Network: A Successor to Transformer for Large Language Models}},
  author = {Yutao Sun and Li Dong and Shaohan Huang and Shuming Ma and Yuqing Xia and Jilong Xue and Jianyong Wang and Furu Wei},
  year = {2023},
  eprint = {2307.08621},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.08621v4}
}
```

## Other paper commentary

- [OLMo: Accelerating the Science of Language Models](https://refivu.com/en/papers/674dc7cc-bbc8-47b3-a55b-69c562d96047-olmo-accelerating-the-science-of-language-models.md)
- [Tree of Thoughts: Deliberate Problem Solving with Large Language Models](https://refivu.com/en/papers/708b6d99-e4dc-4582-abc4-e2de5a72d5d3-tree-of-thoughts-deliberate-problem-solving-with-large.md)
- [Self-Instruct: Aligning Language Models with Self-Generated Instructions](https://refivu.com/en/papers/b7e9c5f5-b8d4-4382-9c03-d006766b8f88-self-instruct-aligning-language-models-with-self-generated.md)
- [DeepSeek-V3 Technical Report](https://refivu.com/en/papers/761895a4-08f3-446f-98de-60152a184c97-deepseek-v3-technical-report.md)
- [The Llama 3 Herd of Models](https://refivu.com/en/papers/27985e84-42ca-486e-8e0e-2fa739789ab4-the-llama-3-herd-of-models.md)
- [Gemma: Open Models Based on Gemini Research and Technology](https://refivu.com/en/papers/e2b5810b-65da-4a36-9bd2-a17a9a6f8512-gemma-open-models-based-on-gemini-research-and.md)
- [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://refivu.com/en/papers/569bb9ab-8345-4766-a539-e7ffb7b1754e-retrieval-augmented-generation-for-knowledge-intensive-nlp.md)
- [Weight Normalization: A Simple Reparameterization to Accelerate Training of Deep Neural Networks](https://refivu.com/en/papers/c1050ccf-ffa6-4158-8fff-24b3a8223df6-weight-normalization-a-simple-reparameterization-to.md)
- [RWKV: Reinventing RNNs for the Transformer Era](https://refivu.com/en/papers/f86c302a-2e46-481a-b0a7-850579f6f3c8-rwkv-reinventing-rnns-for-the-transformer-era.md)
- [Spectral Normalization for Generative Adversarial Networks](https://refivu.com/en/papers/a69a23bf-3dd6-4b32-9fb1-444f7f67a6e8-spectral-normalization-for-generative-adversarial-networks.md)
- [Highway Networks](https://refivu.com/en/papers/78251486-9138-48d8-a03c-8447a3433591-highway-networks.md)

## Terms in this paper

- [Transformer](https://refivu.com/en/terms/48e8be2570bc-transformer)
- [perplexity](https://refivu.com/en/terms/2b204e513801-perplexity)
- [Latency](https://refivu.com/en/terms/884c3275b80a-latency)
- [RoPE](https://refivu.com/en/terms/e4cc5cfc6ebf-rope)
- [throughput](https://refivu.com/en/terms/f064ae564aec-throughput)
- [LayerNorm](https://refivu.com/en/terms/71749b6cdbf4-layernorm)
- [RWKV](https://refivu.com/en/terms/c1422018176f-rwkv)
- [Attention Free Transformer (AFT)](https://refivu.com/en/terms/6fdeafffb594-attention-free-transformer-aft)
- [GPU Memory](https://refivu.com/en/terms/6012aa2bffa6-gpu-memory)
- [H3](https://refivu.com/en/terms/90ee5b1e86ed-h3)
- [linear transformer](https://refivu.com/en/terms/d8bd1026d78c-linear-transformer)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/ca4ee5c6-87cf-427b-ae91-b593c10e0b48-retentive-network-a-successor-to-transformer-for-large)
