Log inStart free
LESSON 28.02 · 5 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Online softmax

Watch softmax finish a row of scores in one pass over the keys.


Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 28.02 · SOFTMAX BAYTOKEN 7 · MAX 0.387 · 3 PASSES · 528 B
01
Stage 1 of 5 · Three passes
Safe softmax needs three passes: the max m, the sum ℓ, then the output. Each pass reads K again.
Explain it simply

Softmax turns scores into weights that sum to 1. Safe softmax subtracts the largest score first, so every power of e is 1 or less. Here the scores are small, yet e to the 12 is 162,755, past FP16's 65,504. The max comes before the sum, and the sum before the output, so K is read three times.

Sixteen score bars for query token 7. A dashed line marks the max, 0.387. Three pass arrows read K. The bar shows 528 bytes.

scores s, max m, sum ℓoutput o, O and byte countstile of keys in SRAMsum before the rescale
s012345678910111213141516max m 0.387pass 1: find the max m · reads Kpass 2: add up ℓ · reads Kpass 3: divide, add up V · reads K and VO, three passes−0.1170.3140.0030.178KKKVHBM bytes, three passes · 528 B+ q, oquery token 7 · its 16 scoress012345678910111213141516max m 0.387pass 1: find the max m · reads Kpass 2: add up ℓ · reads Kpass 3: divide, add up V · reads K and VO, three passes−0.1170.3140.0030.178KKKVHBM bytes, three passes · 528 B+ q, oquery token 7 · its 16 scores
Toy: 1 head, 16 tokens × 4 values, seeded random, not trained; one query row, keys in tiles
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
pj=esj−m/ℓ,ℓ=∑jesj−m\sy{p}{grad}{p_j} = e^{\sy{s}{grad}{s_j} - \sy{m}{grad}{m}} / \sy{l}{grad}{\ell},\quad \sy{l}{grad}{\ell} = \sum_j e^{\sy{s}{grad}{s_j} - \sy{m}{grad}{m}}
m=0.387, ℓ=11.330, p14=1/11.330=0.088m = 0.387,\ \ell = 11.330,\ p_{14} = 1 / 11.330 = 0.088

Each weight is e to the score minus the max, over the sum.

Common mix-ups

MYTHOnline softmax is an approximation of the real softmax.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With one tile of 16 keys, how many times does the running max rise?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.