TL;DR
Infini-attention is a change to the attention layer of a transformer. Ordinary attention forgets everything outside its current window, and keeping every past key and value costs memory that grows with the input. Infini-attention keeps attending inside each segment as usual, but it also stores the keys and values of past segments in a small fixed-size matrix. Later segments read from that matrix with their queries and mix the result with local attention through a learned gate. The paper reports 1M-token passkey retrieval after light fine-tuning and a new best result on 500K-token book summarisation with an 8B model.
Builds on: Multi-Head Attention, Linear Attention, Recurrent Neural Network, Associative Memory · Leads to: later long-context models that keep a fixed-size memory inside attention (no lesson yet)
Why it mattered
Long documents, codebases and books are longer than the context windows that most transformers were trained with. Extending a window directly costs memory and compute that grow quickly with length. The paper's pitch is a change small enough to slot into an existing model: it reuses the query, key and value states the layer already computes, and it can be added by continued pre-training rather than training from scratch.
The paper reports that a 1B model with Infini-attention, trained on 4K-token inputs and then fine-tuned on 5K-token passkey examples, retrieves a hidden passkey at lengths up to 1M tokens. It also reports that an 8B model, continually pre-trained, scores 18.5 overall on 500K-token BookSum, above the baselines in its Table 4. Fixed-size memory inside attention is now a common research direction for long contexts, and this paper is one of the clearest early examples.
The problem
Attention compares every token with every other token, so its cost grows with the square of the length. The paper cites a key-value cache of about 3 TB for a 500B-parameter model at batch size 512 and context length 2048 (from Pope et al., 2023), which shows why serving long contexts is expensive.
Earlier memory fixes each trade something away. Transformer-XL caches the keys and values of the previous segment only, so older context is discarded. Compressive Transformers compress old segments, but they still drop the oldest memory to make room. Memorizing Transformers keep every key and value and search them with nearest-neighbour lookup, but only in one layer and at a large storage cost. RMT and AutoCompressors pass summary vectors forward, but they need many of them to perform well. The paper asks for a memory that is bounded, updated incrementally, and usable in every layer.
How it works
The layer processes the input one segment at a time. Here is one step-by-step pass, with a toy memory of two-dimensional keys and values. The toy sets the feature map σ to the identity so the numbers stay small; the real method uses ELU plus 1, which keeps values positive.
- Split the input into segments of N tokens. The paper uses N = 2048. Check: a 10,000-token input becomes five segments.
- For the current segment, compute queries Q, keys K and values V as usual. Check: nothing new has been computed yet.
- Compute ordinary causal attention inside the segment, giving the local output A_dot. Check: this is the same attention as before, restricted to the current segment.
- Read memory. Suppose earlier segments wrote two bindings. Segment 1 wrote key (1, 0) with value (3, 5), which made M = [[3, 5], [0, 0]] and z = (1, 0). Segment 2 wrote key (0, 1) with value (2, 4), which adds the outer product and gives M = [[3, 5], [2, 4]] and z = (1, 1). Now the current segment sends the query (1, 0). Check: the read returns (3, 5), the value stored under that key.
- Mix the two outputs with a gate. A = sigmoid(β) times A_mem plus (1 minus sigmoid(β)) times A_dot, where β is one learned scalar per head. Check: with β = 0, the gate is 0.5, so a local output of (1, 1) and a memory output of (3, 5) combine to (2, 3).
- Update the memory with the current segment's keys and values, so the next segment can read them. Check: the update only adds to M and z, so its size stays d_key by d_value however many segments have been seen. A query (1, 1) now reads (5, 9) divided by 2, which is (2.5, 4.5), the average of the two stored values.
- Concatenate the outputs of all heads and project them with W_O to get the layer's output. Check: the output shape matches an ordinary attention layer, so the rest of the network is unchanged.
The whole layer as a flow:
flowchart LR X[Segment s input] --> QKV[Compute Q K V] QKV --> LOC[Local causal attention A_dot] QKV --> RET[Read memory with Q: A_mem] MEM[Memory M and z from earlier segments] --> RET LOC --> GATE[Gate with learned beta] RET --> GATE QKV --> UPD[Update memory with K and V] UPD --> MEM GATE --> OUT[Output of segment s]
The math
Step 1 is standard multi-head scaled dot-product attention on the segment:
Step 4 reads the memory. The memory M has shape d_key by d_value and z has shape d_key. With σ the element-wise ELU plus 1:
Step 6 writes the memory. The memory update adds the outer product of the keys and values, and the normalisation term adds the keys:
The paper also uses a delta-rule variant, which first retrieves what is already stored under each key and writes only the difference:
If the key-value binding is already in memory, this update leaves M unchanged. The authors report that it helps slightly.
Step 5 gates the two outputs with one learned scalar per head:
Step 7 concatenates the heads and projects them. The memory size per head per layer is d_key times (d_value + 1), which does not depend on how many segments have been seen. For the paper's dimensions, with d_key = d_value = 128 and H = 8 heads, one layer's memory is 8 times (128 times 128 + 128), which is 132,096 numbers. A Transformer-XL cache of one segment of length 2048 in the same layer holds 8 times 2048 times 256, which is 4,194,304 numbers, about 32 times more, and that cache grows if you want a longer window.
Results
Long-context language modelling (Table 2, average token-level perplexity, lower is better, small models trained from scratch with 12 layers and segment length 2048):
- PG19: Transformer-XL 11.88 with 50M memory, Memorizing Transformers 11.37 with 183M, RMT 13.27 with 2.5M, Infini-Transformer (Linear) 9.65 and Linear plus Delta 9.67, both with 1.6M.
- Arxiv-math: Transformer-XL 2.42, Memorizing Transformers 2.26, RMT 2.55, Infini-Transformer Linear 2.24 and Linear plus Delta 2.23.
- With 100K-token training, Arxiv-math perplexity falls to 2.21 and 2.20 for Linear and Linear plus Delta.
The paper's compression claim is 183M divided by 1.6M, about 114 times less memory than Memorizing Transformers.
Passkey retrieval (Table 3): a 1B model is continually pre-trained on 4K inputs, then fine-tuned for 400 steps on 5K inputs. After fine-tuning, the Linear plus Delta model scores 99% to 100% for start, middle and end passkey positions at every length from 32K to 1M. The Linear variant scores 94% to 100% at 1M. Without fine-tuning, zero-shot accuracy is high only when the passkey sits near the end of the input, and it is in single digits to low teens for start and middle positions, so fine-tuning does most of the work.
Book summarisation (Table 4, 500K-token BookSum, Rouge scores): BART 16.2 overall, PRIMERA with Unlimiformer 17.2, Infini-Transformers Linear 18.0 and Linear plus Delta 18.5, with Rouge-1 of 40.0 for the best model.
Limits the paper shows or implies.
- The language-modelling experiments use small models trained from scratch. The large-model results are continual pre-training of existing models.
- The passkey task is a retrieval test with a synthetic hidden number. It does not test reasoning over long text.
- The memory is a fixed matrix, so it must compress everything it has seen. The paper does not measure how much detail from far back is lost, and a reader should weigh that when judging the results.
- Gate analysis shows some heads specialise into local or memory use, and others mix both. Each layer keeps at least one short-range head, which the authors say keeps the input signal flowing to the output.
Reading guide
- Section 1 (Introduction): needs Attention Is All You Need and Attention Mechanism for the motivation.
- Section 2 (Background): needs Recurrent Neural Network and Associative Memory for the memory idea.
- Section 3.1.1 (scaled dot-product attention): needs Multi-Head Attention and Softmax.
- Section 3.1.2 (compressive memory): needs Linear Attention and Sigmoid Function. The delta rule is an extension; read it after the basic update.
- Section 3.2 (memory and effective context): compare with Transformer-XL: Attentive Language Models beyond a Fixed-Length Context and KV Cache.
- Section 4 (experiments): the implementation details use Positional Encoding and Sliding Window Attention as background.