← Back to Table of Contents

Chapter 30 β€” Beyond Transformers β€” SSMs & Alternatives

β€œAttention is all you need β€” until your sequence is a million tokens long and the O(TΒ²) cost makes you need something else.”

The Quadratic Attention Problem

Standard attention computes a TΓ—T score matrix:

\[\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V\]
  • Compute: O(TΒ² Β· d)
  • Memory: O(TΒ²) for the attention matrix (O(T) with Flash Attention, but compute remains O(TΒ²))

At T = 1M tokens, even with Flash Attention, the FLOPs for attention alone become prohibitive. This motivates architectures with linear or sub-quadratic complexity.

State Space Models (SSMs)

SSMs map an input sequence to an output sequence through a latent state:

\(h_t = \bar{A} h_{t-1} + \bar{B} x_t \quad \text{(state update)}\) \(y_t = C h_t + D x_t \quad \text{(output)}\)

Where:

  • $h_t \in \mathbb{R}^N$ β€” hidden state (per channel)
  • $\bar{A}, \bar{B}$ β€” discretized system matrices
  • $C, D$ β€” output projection matrices
SSM vs Attention
Attention
  • Each token can attend to all previous tokens
  • KV-cache grows with sequence length
  • O(TΒ²d) compute per layer
  • Excellent at precise retrieval from context
SSM (Mamba)
  • Information compressed into fixed-size state
  • Constant memory during generation (no KV-cache)
  • O(Td) compute per layer
  • Better at summarizing long sequences, weaker at precise recall

S4: Structured State Spaces (2022)

The breakthrough that made SSMs competitive:

  • HiPPO initialization for matrix A β€” enables remembering long-range dependencies
  • Parallel scan β€” the recurrence can be computed as a convolution during training (parallelizable on GPUs)
  • During training: O(T log T) via FFT-based convolution
  • During inference: O(1) per step via the recurrence

Mamba: Selective State Spaces (2023)

Mamba (Gu & Dao) made SSMs competitive with transformers by making the state matrices input-dependent (selective):

Mamba Block
Input: [B, T, d]
↓
Linear projection β†’ expand to [B, T, 2Β·d_inner]
↓
1D convolution (causal, short kernel)
↓
Selective SSM: B, C, Ξ” are functions of the input
↓
Gate Γ— SSM output β†’ linear projection β†’ [B, T, d]

Key innovation: standard SSMs have fixed A, B, C matrices (same for every input). Mamba makes B, C, and the discretization step Ξ” input-dependent, allowing the model to selectively focus on or ignore different parts of the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Simplified selective SSM (conceptual)
def selective_ssm(x, A, D, dt_proj, B_proj, C_proj, conv1d):
    """
    x: [B, T, d_inner] β€” input after expansion
    """
    # Input-dependent parameters (SELECTIVE)
    delta = F.softplus(dt_proj(x))    # [B, T, d_inner] β€” controls forgetting
    B = B_proj(x)                      # [B, T, N] β€” input-to-state
    C = C_proj(x)                      # [B, T, N] β€” state-to-output
    
    # Discretize
    A_bar = torch.exp(delta.unsqueeze(-1) * A)  # [B, T, d_inner, N]
    B_bar = delta.unsqueeze(-1) * B.unsqueeze(2) # [B, T, d_inner, N]
    
    # Parallel selective scan (efficient CUDA kernel)
    y = selective_scan(x, A_bar, B_bar, C, D)  # [B, T, d_inner]
    return y

Mamba-1 3B matches Transformer 3B on language modeling, with 5Γ— higher throughput on long sequences.

Mamba-2 (2024)

Mamba-2 showed that selective SSMs can be viewed as a form of structured linear attention:

  • 2–8Γ— faster than Mamba-1 via SSD (Structured State Space Duality) algorithm
  • Connections to linear attention make it easier to reason about theoretically
  • Still O(Td) but with better hardware utilization

Linear Attention

Replace the softmax attention kernel with a linear kernel:

\[\text{LinearAttn}(Q, K, V) = \phi(Q)(\phi(K)^T V)\]

By computing $\phi(K)^T V$ first (an NΓ—d matrix), we avoid the TΓ—T attention matrix:

  • Compute: O(T Β· d Β· N) where N is the feature dimension
  • Memory: O(T Β· d) β€” no attention matrix stored

Variants: RetNet, HGRN2, GLA (Gated Linear Attention).

RWKV: RNN-Transformer Hybrid

RWKV combines the parallelizable training of transformers with the constant-memory inference of RNNs:

  • Training: attention-like parallelism using WKV operator
  • Inference: recurrent β€” process one token at a time with fixed state
  • No KV-cache β€” state size is fixed regardless of context length
RWKV Version Key Innovation
RWKV-4 Initial architecture, WKV attention
RWKV-5 (Eagle) Multi-head attention variant
RWKV-6 (Finch) Data-dependent linear recurrence
RWKV-7 (Goose) Latest, improved expressiveness

Hybrid Architectures

The most promising direction may be combining attention and SSMs:

Jamba (AI21, 2024)

Jamba Architecture β€” Hybrid Layers
Attention Layer β€” precise retrieval, in-context learning
Mamba Layer β€” efficient sequence mixing
Mamba Layer β€” most layers are Mamba (cheaper)
Attention Layer β€” every few layers for recall tasks
Mamba Layer
Mamba Layer
MoE FFN β€” some layers use MoE for capacity
Jamba: Mamba + Attention + MoE β€” ratio of ~7:1 Mamba-to-Attention layers

Jamba 52B supports 256K context with much less memory than a pure attention model, because most layers don’t have a KV-cache.

Other Hybrids

Model Architecture Context
Zamba (Zyphra) Mamba + shared attention layers 4K–16K
RecurrentGemma (Google) Griffin: local attention + linear recurrence 8K
Samba (Microsoft) Mamba + sliding window attention up to 256K

Complexity Comparison

Architecture Training Compute (per layer) Inference (per token) Memory (generation)
Standard Attention O(TΒ²d) O(Td) O(T Β· n_kv_heads Β· d_head) β€” KV-cache
Flash Attention O(TΒ²d) O(Td) O(T Β· n_kv_heads Β· d_head) β€” KV-cache
Sliding Window O(T Β· w Β· d) O(w Β· d) O(w Β· n_kv_heads Β· d_head)
Linear Attention O(T Β· dΒ²) O(dΒ²) O(dΒ²) β€” fixed state
SSM (Mamba) O(T Β· d Β· N) O(d Β· N) O(d Β· N) β€” fixed state
Hybrid Mixed Mixed Reduced KV-cache (attention layers only)

Current State

As of early 2026:

  • Transformers dominate: the ecosystem, tooling, and proven scaling make them the default
  • Mamba/SSMs: competitive at small scale, promising at long context, but haven’t dethroned transformers at frontier scale
  • Hybrids win on efficiency: Jamba, Samba show that mixing architectures gives the best memory-quality tradeoff
  • The jury is still out: no alternative has beaten a well-trained transformer at the same compute budget on standard benchmarks

What’s Next

Architecture innovations help, but there’s another way to improve LLM output quality: spend more compute at inference time. The next chapter covers reasoning models β€” from chain-of-thought prompting to o1-style test-time compute scaling.

← Previous: Chapter 33 β€” Mixture of Experts Β· Next: Chapter 35 β€” Reasoning Models β†’


Last updated: April 2026