Phase 10: LLMs from Scratch

Differential Attention (V2)

Softmax attention spreads a small amount of probability over every non-matching token. Over 100k tokens that noise adds up and drowns the signal. Differential Transformer (Ye et al., ICLR 2025) fixes it by computing attention as the difference of two softmaxes, subtracting the shared noise floor. DIFF V2 (Microsoft, January 2026) is the production-stack rewrite: matching decode latency to baseline Transformer, no custom kernels, FlashAttention-compatible. This lesson is V1 to V2 end-to-end, with a working toy implementation of the difference operation you can run in stdlib Python. State precisely why softmax attention has a noise floor and why it grows with context length. Derive the differential attention formula and explain why the subtraction cancels the shared noise component while preserving signal. Walk the V1-to-V2 diff: what got faster, what got simpler, what got more stable, and why each change was necessary for production pre-training. Implement differential attention from scratch in pure Python and empirically verify the noise-cancellation property on a synthetic signal-plus-noise query. Standard softmax attention has a mathematical property that turns into an operational headache at scale. For a query q, the attention weights are softmax(qK^T / sqrt(d)). Softmax can never produce exact zeros — every non-matching token gets some positive mass. That residual mass is noise, and it scales with context length. At 128k tokens, even if each…

Differential Attention (V2): Softmax attention spreads a small amount of probability over every non-matching token. Over 100k tokens that noise adds up and…

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.