Module 4 · Lesson 2 of 5
Softmax without keeping every score
Preserve exact attention mathematically by combining stable summaries across tiles.
Terms from Module 3 you need
This lesson uses these GPU terms; each link opens the Module 3 lesson that teaches it.
- Global memory
- The GPU’s large main memory (HBM on data-centre GPUs), reachable by every thread. It is the slow boundary: K/V, weights and any full score matrix normally live here. Module 3.2 →
- Shared memory
- Small, fast memory shared by the threads of one thread block; a kernel stages a tile of operands here on purpose so the block can reuse them. Module 3.2 →
- Register
- Per-thread working storage, the fastest and smallest level; running sums (accumulators) live here. Too many per thread can spill to slower memory. Module 3.2 →
- Bandwidth
- Bytes per second a memory path can deliver. When a step needs more bytes than the path supplies, memory, not arithmetic, sets its speed. Module 3.4 →
Why subtract the maximum?
Softmax turns scores into weights. Exponentials can overflow, so subtract the row maximum before exponentiating; normalization cancels the common scale.
What you will be able to explain
Calculate an online merge; explain both rescale corrections; compare streamed output against dense attention.
All weights use the same denominator over every allowed key. Masked keys contribute exactly zero; small positive unmasked weights are not mask zeros.
Numerical fixture: four positions, Q = [1, 0], Kⱼ = [√2 × scoreⱼ, 0], scores = [1, 2, 3, 4], V = [[1, 0], [0, 1], [1, 1], [2, 0]]. Thus Q·Kⱼ / √2 produces the stated score. These invented Q/K/V are not changed embeddings from Module 2.
For this row all four keys are allowed. The final weights are approximately [0.03206, 0.08714, 0.23688, 0.64391]. The streaming path below conceals its final output until normalization.
Carry a summary, not all previous scores
Keep the largest score so far, the denominator at that scale, and an unnormalized weighted-value sum. New tiles update this summary.
- m
- Running maximum among allowed scores seen so far.
- ℓ
- Sum of exp(score − m), not a probability.
- u
- Vector Σ exp(score − m) × V.
- α
- exp(old m − new m); corrects earlier contributions to the new scale.
Start m = −∞, ℓ = 0, u = [0, 0]. When a larger maximum arrives, rescale both ℓ and every component of u before adding the new tile. Only after all keys: output = u / ℓ.
Predict: The maximum rises from 2 to 4. Keep the old weighted sum unchanged?
First tile: m = 2, ℓ ≈ 1.36788, u ≈ [0.36788, 1]. Second tile raises the maximum to 4; multiply earlier contributions by exp(−2).
Open this same model as a focused walkthrough. Explore changes tile width or score order; those settings reset the walkthrough and remain consistent across its views.
Read each update in the code
The numerator is deliberately unnormalized. Adding independent tile averages loses their different denominators.
α = exp(m − m′)
pⱼ = exp(scoreⱼ − m′)
ℓ′ = αℓ + Σpⱼ
u′ = αu + ΣpⱼVⱼ
o = u / ℓ after the final tile
for tile in key_value_tiles:
new_m = max(m, max(tile.scores))
alpha = exp(m - new_m) if denominator > 0 else 0
p = exp(tile.scores - new_m)
denominator = alpha * denominator + sum(p)
numerator = alpha * numerator + p @ tile.values
m = new_m
output = numerator / denominatorExample trap: two equal-size tiles can still have different softmax mass. Averaging their independently normalized outputs is generally wrong. Merge unnormalized summaries instead.
Numerical and masking boundaries
An entirely masked tile leaves the existing summary unchanged. This model explicitly handles an empty summary rather than evaluating −∞ − (−∞). A completely masked row returns zero by declared teaching convention. Production APIs may define this differently. Large finite scores remain stable; rounding and accumulation order can produce tiny output differences.
Causal chunk with prefix length p: query row i may read keys j ≤ p + i, using absolute zero-based positions. A single decode query over a cache is rectangular; a top-left square mask can wrongly hide earlier keys.
Online normalizer §3 · Accumulator code correspondence
Takeaway: Carrying a small summary (m, ℓ, u) finishes a softmax row tile by tile without ever holding all its scores, and gives the same result as the dense calculation.