Phase 7 · Transformers/
Lesson 7.12frontier2 h 30 min

Beyond vanilla attention: state space models, long context and hybrids

Sliding windows, linear attention, S4 and Mamba, and why 2026 models are hybrids

Attention's quadratic cost and ever-growing KV cache are the limits that million-token contexts run into. This lesson maps the alternatives — windowed and linear attention, state space models such as S4 and Mamba, and the hybrid architectures that most efficient 2026 models use — and explains the one thing attention still does better than any of them: exact recall. That trade-off is why long context and retrieval (9.1 Why RAG, and the canonical pipeline) are complements, and why agent memory design (10.4 Memory and context engineering) matters even with huge context windows.

You'll be able to

  • Quantify the quadratic-attention problem and explain how sliding-window and linear attention reduce it
  • Explain the state space recurrence, its convolutional training form, and what Mamba's selectivity adds
  • Compare transformers, linear attention, SSMs and hybrids on training parallelism, inference cost and exact recall
  • Describe the main long-context techniques for transformers and how to evaluate long-context ability

Key ideas

  1. Attention costs O(n²d) per layer with a KV cache that grows with context; at long contexts attention dominates compute and memory.
  2. Sliding-window attention costs O(nwd) with a fixed rolling cache; receptive field grows by w per layer but exact long-range lookup degrades.
  3. Linear attention drops the softmax: φ(Q)(φ(K)ᵀV) costs O(nd²) and becomes a fixed-size recurrent state S_t = S_{t−1} + φ(k_t)v_tᵀ.
Show 4 more key ideas
  1. An SSM is a discretized linear system h_t = Āh_{t−1} + B̄x_t, y_t = Ch_t; linearity makes it a convolution (parallel training) and a recurrence (O(1) inference).
  2. Mamba makes B, C and Δ input-dependent (selective) and computes the recurrence with a parallel scan; Mamba-2 shows SSMs are structured masked attention.
  3. Fixed-size states summarize; attention stores verbatim — so pure SSMs lag on exact-recall tasks, and hybrids (a few attention layers among many SSM layers) are the practical answer.
  4. Long context in transformers comes from RoPE scaling, ring attention and sparse/compressive variants; evaluate with needle-in-a-haystack and remember lost-in-the-middle.

Suggested path through this lesson