
After studying this material, you should be able to:
Imagine reading a very long book and having to remember every single word perfectly before answering a question. That is essentially what a Transformer does.
Token 1 → Token 2 → Token 3 → ... → Token 100,000
↑ ↑ ↑
remembered remembered remembered
perfectly perfectly perfectly
The KV Cache Explained:
When a Transformer processes text, for each token it creates three vectors:
| Vector | Role | Analogy |
|---|---|---|
| Query (Q) | What this token is looking for | A search question |
| Key (K) | What this token offers | A file label |
| Value (V) | The actual information | The file contents |
The model stores every Key and Value in a KV Cache to avoid recomputation.
The problem: As context grows, the KV cache grows linearly. More tokens = more memory = slower processing.
AI agents are being asked to:
Each of these demands more tokens, which means more memory cost.
This is the conceptual foundation of the entire article.
Definition: Perfect, verbatim recall of every past token
Past tokens: [T1][T2][T3][T4]...[T10,000]
↓ ↓ ↓ ↓ ↓
ALL stored perfectly in KV cache
Cache size grows with every new token
Advantages:
Disadvantages:
Definition: A compressed, fixed-size summary of the past
Past tokens: [T1][T2][T3][T4]...[T10,000]
↓
Compressed into fixed-size STATE
State size stays CONSTANT regardless of input length
Advantages:
Disadvantages:
Not all attention layers need eidetic memory.
Some layers perform long-range retrieval (need eidetic memory). Other layers perform local operations like tracking the current clause (fading memory is sufficient).
Layer Type | Memory Needed | Example Task
--------------------|---------------|---------------------------
Long-range attention| Eidetic | "What did the user say 50,000 tokens ago?"
Local attention | Fading | "What word comes next in this sentence?"
Think of sequence model architectures as points on a map:
PURE TRANSFORMER ←————————————————→ PURE SSM
(all eidetic) hybrid zone (all fading)
↑
Best balance of:
- Memory efficiency
- Speed
- Quality
Before hybrids, researchers tried:
| Method | How It Works | Limitation |
|---|---|---|
| Sparse Attention | Only looks at a subset of past tokens | May miss important tokens |
| Sliding-Window Attention (SWA) | Only keeps the most recent N tokens | Forgets everything before the window |
| State Space Models (SSMs) | Compresses all history into fixed state | Lossy compression |
Hybrid Model Architecture:
Layer 1: [ATTENTION] ← eidetic, for long-range retrieval
Layer 2: [SSM] ← fading, for local operations
Layer 3: [SSM] ← fading, for local operations
Layer 4: [ATTENTION] ← eidetic, for long-range retrieval
Layer 5: [SSM] ← fading, for local operations
...
Result: Smaller memory footprint + preserved recall where it actually matters
Previously, testing each hybrid configuration required:
This made systematic comparison practically impossible.
A Transformer is actually a special case of a state space model.
Both attention layers and SSM layers can be expressed as:
Output = (Mixing Matrix) × (Value Sequence)
Attention: Mixing Matrix = Q × K (computed dynamically)
SSM: Mixing Matrix = recurrent state (fixed dimension)
Because of this structural similarity, the learned knowledge in a Transformer's weights can be transferred to an SSM.
STAGE 1: LAYER SELECTION
┌─────────────────────────────────────────┐
│ Test each layer: replace with SWA │
│ If quality drops → needs eidetic memory │
│ If quality holds → candidate for SSM │
└─────────────────────────────────────────┘
↓
STAGE 2: WEIGHT TRANSFER & ALIGNMENT
┌─────────────────────────────────────────┐
│ Copy Q, K, V projections from attention │
│ to SSM input projections │
│ Run short distillation to align behavior│
└─────────────────────────────────────────┘
↓
STAGE 3: END-TO-END ADAPTATION
┌─────────────────────────────────────────┐
│ Fine-tune the full hybrid model │
│ All layers coadapt together │
│ Uses <0.5% of original training budget │
└─────────────────────────────────────────┘
To formally determine if a layer can be converted, researchers use Hankel rank:
Hankel rank measures how much information must pass through the state bottleneck — essentially, the minimum SSM state size needed to perfectly replicate what an attention layer does.
Three SSM types were tested. They differ in how they update their fixed-size state.
State update:
New State = (decay_factor × Old State) + New Token
Example:
Old State: [0.8, 0.6, 0.9, 0.7] ← all past information
Decay: × 0.9 applied to ALL
New State: [0.72, 0.54, 0.81, 0.63] + new token contribution
Analogy: Like a camera with a uniform fade — everything gets dimmer at the same rate.
Limitation: Cannot selectively remember important things and forget unimportant ones.
State update:
1. Find the slot associated with current key
2. ERASE that specific slot
3. Write new information there
4. Rest of state is unchanged (no uniform decay)
Analogy: Like a whiteboard where you can erase and rewrite specific sections without affecting others.
Advantage over Mamba-2: Can revise specific associations without degrading everything else.
State update:
1. Consider statistics of ALL past keys
2. Use this cumulative history to determine what to erase
3. Write new information with history-informed precision
Analogy: Like an expert editor who understands the entire document's themes before deciding what to revise.
Unique feature: Variable computation at inference time (adjustable iteration count).
Quality: GKA > GDN > Mamba-2
The gap WIDENS on long-context tasks
The gap NARROWS on short-context tasks
This matches the theoretical expressiveness hierarchy — more sophisticated state management = better performance when compression quality matters most.
"Surely keeping recent tokens perfectly (SWA) is better than compressing everything (SSM)?"
Test: Long-context benchmarks
Memory budget: SWA given 2-4× MORE memory than SSM state
Result:
SSM hybrid: ████████████████████ (better)
SWA hybrid: ████████████ (worse, despite more memory)
SWA's problem: It has a hard cutoff. Anything outside the window is completely gone.
SWA with window=1000:
Tokens 1-9000: [COMPLETELY FORGOTTEN]
Tokens 9001-10000: [PERFECTLY REMEMBERED]
SSM's advantage: It compresses everything, even if imperfectly.
SSM with equivalent memory:
Tokens 1-9000: [DIMLY REMEMBERED - some signal preserved]
Tokens 9001-10000: [CLEARLY REMEMBERED - recent = stronger signal]
Key principle: A good lossy summary of everything beats a perfect recording of only the recent past.
Context Length | Speedup (32B model) | Speedup (8B model)
16K tokens | 1.50× | 1.94×
128K tokens | 2.06× | 2.29×
Why does speedup grow with context length?
At long context, reading the KV cache dominates computation time. Halving the cache size roughly doubles how many sequences can be processed simultaneously.
On AIME 2025 math competition problems:
Standard Transformer: ████████████████████ (baseline)
GKA Hybrid: ████████████████████ (same accuracy)
but reaches accuracy thresholds 1.2-1.6× faster
Both models generate similarly long reasoning traces (~14,000 tokens median), meaning the hybrid isn't cutting corners — it's genuinely faster at the same quality level.
Iterations | Accuracy Loss | Speed Gain
30 (full) | 0% | baseline
10 (fast) | ~1% | +10% faster decoding
Practical implication: One trained model can serve multiple use cases:
High-accuracy deployment: run 30 iterations
Balanced deployment: run 20 iterations
Low-latency deployment: run 10 iterations
No need to maintain separate model variants.
Primed hybrids natively support 128K tokens. But real-world use cases often exceed this:
Long document (256K tokens):
┌──────────────────────────────────────────────────┐
│ Full Document │
└──────────────────────────────────────────────────┘
↓ Split into chunks
┌─────────────┐ ┌─────────────┐
│ Chunk 1 │ │ Chunk 2 │
│ (128K) │ │ (128K) │
└─────────────┘ └─────────────┘
↓ Process independently in parallel
┌─────────────┐ ┌─────────────┐
│ KV Cache 1 │ │ KV Cache 2 │
│ SSM State 1 │ │ SSM State 2 │
└─────────────┘ └─────────────┘
↓ Combine
┌──────────────────────────────┐
│ Concatenated KV Caches │
│ Weighted-averaged SSM States │
└──────────────────────────────┘
Result: 128K → 256K context with no additional training, while preserving the GKA > GDN > Mamba-2 quality hierarchy.
PROBLEM: Transformers have growing memory cost
↓
INSIGHT: Not all layers need perfect recall
↓
SOLUTION: Hybrid architectures (eidetic + fading)
↓
BARRIER: Testing hybrids requires full retraining
↓
BREAKTHROUGH: Priming (reuse Transformer weights)
↓
FINDING 1: GKA > GDN > Mamba-2 for SSM layers
FINDING 2: SSM fading memory > SWA windowed memory
FINDING 3: 2× throughput at 128K context, same quality
FINDING 4: GKA offers variable speed-accuracy trade-off
FINDING 5: Context can extend to 256K without retraining