Deep architectures
Efficient Attention and Long-Context Architectures
Compare architectural strategies for reducing attention cost, preserving useful memory, and testing whether long context actually helps.
By the end you can
- Describe why dense self-attention becomes expensive as sequence length grows
- Compare local, sparse, low-rank, kernelized, recurrent-memory, and compressed-context strategies
- Distinguish nominal context length from effective use of distant evidence
- Design evaluations that expose retrieval, ordering, and long-range reasoning failures
Long context creates two separate problems
Dense self-attention compares every query position with every key position. The score matrix therefore grows quadratically with sequence length. That is before feed-forward activations and cached states are counted. The FlashAttention abstract opens with the flat version of the same sentence: “Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length.”
The 2022 result then complicates the usual account of where the cost sits. The binding constraint is not arithmetic alone. It is the reads and writes between GPU high bandwidth memory and on-chip SRAM. A tiled, IO-aware attention that is mathematically exact — nothing approximated, no pair of positions dropped — gave a 15% end-to-end wall-clock speedup on BERT-large at sequence length 512, measured against the MLPerf 1.1 training speed record. It gave 3x on GPT-2 at sequence length 1K and 2.4x on Long Range Arena at sequence lengths of 1K–4K. It also produced the first Transformers to beat chance on Path-X (sequence length 16K, 61.4% accuracy) and on Path-256 (sequence length 64K, 63.1% accuracy). Two lessons follow. An exact method can outrun the approximations on the clock, so approximation is a choice to be justified rather than an inevitability. And before changing what attention computes, find out whether what it moves was the problem.
The second problem is not computational but epistemic. A model may accept a long prompt. It can still fail to retrieve, combine, or prioritize the evidence that matters. The rest of this lesson is mostly about measuring that failure rather than the first one.
A large context window is a capacity limit, not proof of reliable long-range use.
Visual
Six ways to reduce or reorganize long-range interaction
Each strategy changes both cost and the information paths available to the model. Three of the six rows below have named instances that were measured rather than merely proposed. Read them as evidence about the family, not as product recommendations.
Sparse global links. BigBird is “a sparse attention mechanism that reduces this quadratic dependency to linear”: local windows, random links, and a handful of global tokens. Eleven authors published it in 2020. They proved that it “is a universal approximator of sequence functions and is Turing complete”, thereby preserving these properties of the quadratic, full attention model. Their analysis singles out the value of “O(1) global tokens (such as CLS), that attend to the entire sequence”. The pattern, they report, “can handle sequences of length up to 8x of what was previously possible using similar hardware”. Sparsity did not cost expressive power here. It cost a direct path. The global tokens buy that back.
Sparsity can also be chosen by the data instead of fixed in the architecture. Reformer replaces dot-product attention with locality-sensitive hashing, “changing its complexity from O(L^2) to O(L log L), where L is the length of the sequence”. It adds reversible residual layers too, “which allows storing activations only once in the training process instead of N times, where N is the number of layers”. Both halves arrived in one 2020 paper. They are two different budgets — the attention computation and the stored activations — and only attacking both turns into a hardware claim. Google Research stated the combined result in exactly those terms: “The novel application of these two approaches in Reformer makes it highly efficient, enabling it to process text sequences of lengths up to 1 million words on a single accelerator using only 16GB of memory.”
Recurrent or cached memory has its own measured instance. Transformer-XL carries state across segment boundaries, using a segment-level recurrence mechanism and a relative positional encoding scheme. It was published in 2019, and it quantified both halves of what that buys: “As a result, Transformer-XL learns dependency that is 80% longer than RNNs and 450% longer than vanilla Transformers, achieves better performance on both short and long sequences, and is up to 1,800+ times faster than vanilla Transformers during evaluation.” The 450% and the 1,800+ are different kinds of win. Carrying state lets the model see further. It also stops the model recomputing the same prefix at every step.
Local windows
Tokens attend within nearby neighborhoods, sometimes with shifted or dilated patterns.
Sparse global links
Selected landmarks, blocks, or global tokens connect distant regions.
Low-rank or projected attention
Keys and values are compressed before interaction.
Kernel or linearized attention
The computation is rearranged to avoid an explicit dense score matrix.
Recurrent or cached memory
Past information is summarized and carried across segments.
Retrieval and external context
A separate system selects evidence instead of loading everything into one prompt.
Comparison
Efficiency methods alter the model’s evidence graph
No approximation is free. Each protects some relationships and weakens others.
The linearized column is the one where the algebra is explicit enough to inspect. Express self-attention as a linear dot-product of kernel feature maps, then exploit the associativity of matrix products, and complexity falls from O(N^2) to O(N) in the sequence length N. Four authors did that in 2020. Reassociating the product means the dense score matrix is never formed at all. That is why the “avoids materializing dense scores” line below is a literal statement about the computation rather than a figure of speech. The same rearrangement changes what the model is: “We show that this formulation permits an iterative implementation that dramatically accelerates autoregressive transformers and reveals their relationship to recurrent neural networks.” On autoregressive prediction of very long sequences they report linear transformers up to 4000x faster than vanilla transformers.
That number is also where the trade sits. The speed comes from stepping through the sequence while carrying a running state of fixed size. A fixed-size state is a compression decision made in advance, before anyone knows which distant token the task will turn out to need. The comparison below is between evidence graphs, not between speeds.
Local and block-sparse
Preserves nearby detail while limiting arbitrary distant interaction.
- Predictable memory use
- Strong fit for local structure
- Needs bridges for remote evidence
- Sensitive to window layout
Projected or low-rank
Compresses the key-value sequence before matching.
- Reduces pairwise cost
- Can preserve dominant structure
- May discard rare evidence
- Projection quality becomes critical
Linearized attention
Uses feature maps or algebraic reassociation.
- Avoids materializing dense scores
- Can stream summaries
- Changes normalization behavior
- Numerical stability needs care
Memory or retrieval
Moves long-term evidence outside the current dense graph.
- Scales beyond one sequence
- Supports reusable stores
- Introduces selection errors
- Needs provenance and freshness controls
Example
Tests that reveal fake long-context competence
A model can pass short benchmarks while failing these operational cases.
RULER turned that list into a benchmark in 2024. It extends needle-in-a-haystack with “variations with diverse types and quantities of needles”. It adds “multi-hop tracing and aggregation to test behaviors beyond searching from context”. It then ran 17 long-context models over 13 tasks. The result is the reason the first bullet is not enough on its own. “Despite achieving nearly perfect accuracy in the vanilla NIAH test, almost all models exhibit large performance drops as the context length increases.” And: “while these models all claim context sizes of 32K tokens or greater, only half of them can maintain satisfactory performance at the length of 32K.”
RULER is not the only such measurement, and the second one was built on a different principle. LongBench collects real bilingual tasks instead of synthesising haystacks whose contents it controls: “LongBench comprises 21 datasets across 6 task categories in both English and Chinese, with an average length of 6,711 words (English) and 13,386 characters (Chinese).” It appeared in 2024, and 8 LLMs were evaluated on it. It also tested the alternative that the next section proposes, and the finding is a qualified one worth carrying. Retrieval-style context compression helps models with weak long-context ability. It still lags models that are genuinely strong at long context. Two benchmarks, two constructions, one conclusion about advertised windows.
- Needle placement: insert one decisive fact at the beginning, middle, and end, then compare retrieval accuracy by position — the published curve is U-shaped, so the middle is where the damage will be found.
- Competing evidence: include a recent correction beside an older statement and test whether timestamps control the answer.
- Cross-document join: require two distant passages to support one conclusion, rather than asking for isolated lookup — this is the multi-hop tracing and aggregation that RULER added on top of needle retrieval.
- Order sensitivity: permute sections whose sequence changes meaning and verify that the model does not treat them as a bag of text.
- Distractor scaling: add plausible but irrelevant documents and measure whether unsupported confidence rises.
Figure
Steps
Choose a long-context strategy from the evidence pattern
Do not begin with a target token count. Begin with the access pattern the product must support.
Step five has a named baseline. Retrieval-augmented generation dates to 2020. The paper defines “RAG models where the parametric memory is a pre-trained seq2seq model and the non-parametric memory is a dense vector index of Wikipedia, accessed with a pre-trained neural retriever”. It set the state of the art on three open-domain question-answering tasks, “outperforming parametric seq2seq models and task-specific retrieve-and-extract architectures”. On generation tasks, RAG models “generate more specific, diverse and factual language than a state-of-the-art parametric-only seq2seq baseline”. The reason given for the work is the governance one this lesson keeps returning to. For a model that stores everything in its weights, “providing provenance for their decisions and updating their world knowledge remain open research problems”.
Step five also has a boundary condition, from the LongBench results above. Retrieval-style context compression helped the models with weak long-context ability, and still lagged the models that were genuinely strong at long context. Read together, the two findings say something narrow. Retrieval is a substitute for long-context competence you do not have, and a complement to the provenance you need either way. So run it as a baseline in both directions. Measure the cost and the grounded accuracy of selecting less evidence. Then measure what a genuinely strong long-context model does with the same task, before assuming the retriever is the cheaper answer.
1. Classify dependencies
Estimate local, periodic, global, and cross-document relationships.
2. Separate storage from reasoning
Decide what belongs in weights, current context, cache, summary, or retrieval.
3. Model peak memory
Include attention states, activations, cache growth, and batch concurrency.
4. Build adversarial context tests
Vary distance, order, conflicts, and distractor density.
5. Compare a retrieval baseline
Check whether selecting less evidence improves cost and grounded accuracy.
Analogy
A research desk with shelves, index cards, and an archive
A researcher keeps a few pages open, pins important notes, summarizes older chapters, and requests files from an archive. Each mechanism preserves a different kind of access.
The summarizing arm is the one with a named model behind it. Compress past memories rather than discarding them, and you reach 17.1 perplexity on WikiText-103 and 0.97 bits per character on enwik8. Four authors reported that in 2019. The instructive part is what they had to build in order to test the idea at all. The available benchmarks were too short to exercise long-range memory. So they released PG-19: an open-vocabulary benchmark of Project Gutenberg books published before 1919, with 28,602 training books totalling 1,973,136,207 tokens, 50 validation books (3,007,061 tokens) and 100 test books (6,966,499 tokens). DeepMind states the comparison in the repository README: “PG-19 is over double the size of the Billion Word benchmark [2] and contains documents that are 20X longer, on average, than the WikiText long-range language modelling benchmark [3].” Books, not synthetic haystacks. A real long-range dependency is a plot, not a planted needle.
Pinned notes and archive slips stay readable to the researcher. Neural summaries are lossy vectors, and retrieval scores are learned estimates. One giant desk is not the only route to long-range work.
Efficient context design is a memory hierarchy, not merely a larger input box.
Key idea
Nominal length and effective context are different measurements
A model may technically accept hundreds of thousands of tokens while relying mostly on recent or highly salient regions. Position, formatting, distractors, and task type all influence effective use.
That dependence on position has a published shape. Move the relevant information around inside the context, on multi-document question answering and key-value retrieval, and accuracy traces a U. Seven authors measured it and reported the result in 2024: “In particular, we observe that performance is often highest when relevant information occurs at the beginning or end of the input context, and significantly degrades when models must access relevant information in the middle of long contexts, even for explicitly long-context models.” The clause that matters for procurement is the last one. Being sold as long-context did not exempt a model from the curve.
Report performance as a function of distance and context composition. A single maximum-length example hides the behavior that users will encounter, and it hides it in a specific place. An evaluator who plants the decisive fact at the top or the bottom of the prompt measures the best case twice and the middle never.
Evaluate where evidence appears, how much competes with it, and whether multiple pieces must be combined.
Long context should earn its operational cost
A longer window can simplify interfaces and preserve raw evidence, but it increases memory pressure and can invite irrelevant context. External retrieval adds system complexity and offers selective, inspectable evidence in return.
The measured results in this lesson do not converge on one architecture. That is the finding. An exact, IO-aware attention bought 15%, 3x and 2.4x on different workloads without approximating anything. Hashing-based sparsity bought O(L log L), and a million words on one accelerator with 16GB. Segment-level recurrence bought dependencies 450% longer and evaluation up to 1,800+ times faster. Kernel feature maps bought O(N) and up to 4000x on autoregressive prediction. Retrieval bought provenance. Each was measured on its own workload, against its own baseline. None of them is a default.
The best architecture may therefore combine local attention, compressed memory, and retrieval. Document which layer owns freshness, access control, citations, and deletion. Then test the assembled system at the positions and distractor levels where the published failures actually appear, not only at its maximum length.
The winning design is the smallest evidence path that reliably supports the task.
Key takeaways
- Dense attention offers flexible global interaction, but its score matrix grows quadratically with sequence length. FlashAttention showed that much of that cost is memory traffic between GPU high bandwidth memory and on-chip SRAM rather than arithmetic. An exact method beat the approximations by 15%, 3x and 2.4x on different workloads.
- Local, sparse, projected, linearized, memory-based, and retrieval designs preserve different evidence paths. BigBird reported 8x the previously possible sequence length. Reformer reported O(L log L) attention and 1 million words on a single accelerator with 16GB. Transformer-XL reported dependencies 450% longer than vanilla Transformers.
- Nominal context capacity does not establish effective retrieval, ordering, or multi-passage integration. Across 17 long-context models and 13 tasks, RULER found that only half of those claiming 32K tokens or more maintained satisfactory performance at 32K.
- Long-context tests should vary evidence location, contradiction, distractor density, and dependency distance. Accuracy follows a U-shaped curve that degrades significantly in the middle of long contexts, even for explicitly long-context models.
- Memory hierarchies can combine current context, cache, compressed state, and external retrieval. Compressing past memories rather than discarding them reached 17.1 perplexity on WikiText-103 and 0.97 bits per character on enwik8, and required PG-19 to be built because existing benchmarks were too short.
- Architecture selection should compare grounded quality, peak memory, throughput, and evidence governance together. LongBench found that retrieval-style context compression helps models with weak long-context ability but still lags models genuinely strong at long context.