Exercises — Long-context architectures
This page tests everything in Long-context architectures: how attention scales, why sparse/linear/state-space/memory tricks each cut the cost, and how to compute the numbers by hand. Work each problem before opening its solution.
Every symbol used here is built in the parent note. Quick reminders as we go:
- = number of tokens in the sequence.
- = "grows no faster than", a way to say how the cost scales as gets huge — see Computational Complexity.
- A comparison = one dot product between two -dimensional vectors (one token looking at one other token). Each comparison itself hides an multiply-add cost — we count comparisons and keep the factor in mind separately.
- Per-head, per-layer. Every cost below (, , , …) is for one attention head. Real models run heads in parallel, so the true layer cost is the per-head count. We hold fixed and count one head unless a problem says otherwise; multiply by for the full picture.
Figure 1 below is reused throughout — you'll count its lit cells directly in Exercises 2.1 and 3.1:

Figure 1. Left panel = full attention: every token (row) looks at every token (column), so all cells are lit. Right panel = a sliding window of width : each row only lights the cells near the diagonal. Count the lit cells and you have counted the cost.
Level 1 — Recognition
Recall Solution 1.1
Full attention compares every token with every token, so the count is . The scaling class is — quadratic. Double the tokens and the cost quadruples. With heads the layer does times this many.
Recall Solution 1.2
- (a) full attention → (every pair).
- (b) Longformer → ( = window, = number of global tokens).
- (c) linear attention → ( = feature-map dim, = value dim).
- (d) S4 → ( = fixed state dimension).
Notice: (b),(c),(d) are all linear in because their non- factors (, , ) are constants that do not grow with the sequence. All four are per head; multiply by the head count for the whole layer.
Level 2 — Application
Recall Solution 2.1
Think of the right panel of Figure 1: regular tokens light only near-diagonal cells, global tokens light a whole row.
Regular tokens: there are of them, each costing : Global tokens: of them, each costing : Total comparisons per head.
Full attention (the left panel of Figure 1, all cells lit) would cost . So the sparse scheme uses of the work. (Both counts scale by the same head factor , so the ratio is unchanged.)
Recall Solution 2.2
Quick recall of what and are (built once, in the parent): is the value-weighted sum of key feature-maps, and is the plain sum of key feature-maps (the normalizer). Both are computed one time over all keys, then reused by every query.
First map the query: (that's with ).
Numerator = . Denominator = . Because and were built once for all queries, this query cost only two length-3 dot products — no scan over the keys at all. That is the whole point of kernel-style linear attention.
Level 3 — Analysis
Recall Solution 3.1
Picture the right panel of Figure 1 widening: as grows the lit band thickens until it fills the whole left panel. Set the costs equal: So the window only reaches full-attention cost when it is as wide as the whole sequence — i.e. when the band fills the grid and it stops being a window. For any the sliding window is strictly cheaper, and the savings ratio is . At you pay of full attention.
Recall Solution 3.2
Count in "comparisons", where each comparison is one -dimensional dot product (the same unit as full attention), so the hidden factor is common to every term and cancels in the ratio.
Per token the cost is: local comparisons, plus memory attention over the retrieved values (64 comparisons), plus ANN search (each a -dim similarity): Per-token cost comparisons. Over tokens: Full attention: . Ratio .
Dominant term: the local window () dwarfs both retrieval () and search (). The retrieval machinery is almost free; the sliding window is where the money goes — which tells you that is the knob to shrink first.
Level 4 — Synthesis
Recall Solution 4.1
Where does enter? The attention score matrix stores one scalar similarity per token pair — the -dimensional vectors are dotted down to a single number, so the score-matrix size is and is independent of . Likewise the S4 running state is a length- vector of numbers, also independent of . So for "numbers stored as running attention/state", does not appear — it would only matter if we counted the activation tensors ( each), which both methods share and which are linear, not quadratic. That's why the headline comparison drops .
(a) Full attention stores an score matrix (per head): (b) An S4 layer carries a fixed-size hidden state of dimension — it does not grow with . So it holds on the order of numbers as the running state. That's a ~39-million-fold shrink in the attention-matrix memory.
Bookkeeping caveat: S4 also stores fixed model parameters — the state matrices and the convolution kernel (order to a few numbers). These are constant in : negligible next to the scores and unchanged whether the sequence is 1k or 100k tokens, but they are still part of the true footprint and shouldn't be forgotten. The dominant, -dependent term is what the reduction factor captures.
The catch the parent flags: that fixed 256-vector must compress all history — anything it can't hold is gone, unlike attention which keeps every token addressable. Compare with the RNN hidden state (same fixed-size idea) and the Information Bottleneck view (a fixed state is a bottleneck).
Recall Solution 4.2
Linear beats full when : So the break-even point is . Reading it: below ~16k tokens, full attention is actually cheaper here — the feature-map constants outweigh the quadratic term. Linear attention only wins once the sequence is long enough that overtakes the constant . This is why "linear = always faster" is false at short context. (Both sides scale by the same head count , so cancels and does not move the break-even.)
Level 5 — Mastery
Recall Solution 5.1
Path (A) — score then weight, term by term:
- , times .
- , times .
- Sum .
Path (B) — precompute first: Then
Both give . They must — it's just the distributive/associative law of the dot product. Path (B) is the whole efficiency win: build once, then every query is a single fixed-size dot product.
Recall Solution 5.2
A defensible choice is memory-augmented + local window (Memorizing-Transformer style). Quantitative backing (all per-head; multiply by head count uniformly):
- Full attention is out: scores/layer — infeasible.
- Sparse (Longformer): with , globals, cost — feasible, but purely-positional windows can miss a clause cited 150k tokens earlier.
- S4/SSM: cheapest memory (fixed 256-state, ~ smaller matrix from Ex 4.1), but that fixed state compresses away rare specific facts — bad for "quote clause 14.3 exactly".
- Memory-augmented: local for fluency + ANN retrieval () that addresses individual earlier facts by content, cost from the Ex 3.2 logic — same ballpark as sparse but with content-based recall of exact clauses.
Decision: memory-augmented + local window wins for this task, because a legal assistant must quote exact clauses from anywhere in the document. It costs comparisons/head — essentially the same as sparse () and vastly below full attention (, a ~336× saving) — yet, unlike sparse's fixed positional window or S4's lossy fixed state, its content-addressed retrieval can pull a clause cited 150k tokens earlier.
Biggest risk: retrieval quality. If the ANN index returns the wrong neighbors (stale or poorly-embedded memory), the model confidently attends to irrelevant clauses — a failure sparse attention cannot have because its pattern is fixed and positional. Mitigation: periodically re-embed the memory bank and evict stale entries so retrieved keys stay faithful to the current model.
(There is no single "correct" family — the mastery skill is matching the cost structure and failure mode to the task: exact-recall legal work rewards addressable memory; smooth long-range reasoning rewards SSMs.)
Recall Self-test recap (cloze)
Full attention scales as ::: per head Longformer total cost with window and globals ::: Linear-attention break-even sequence length ::: The linear-attention denominator plays the role of ::: softmax normalization ANN memory search costs ::: , not An S4 hidden state has ::: fixed dimension, independent of Every per-head cost must be multiplied by ::: the number of heads