Linear algebra for LLMsPart 5: Attention, one operation at a time

Softmax

7 min

Scores in, weights out

softmax⁡(z)i=ezi∑jezj\displaystyle \operatorname{softmax}(z)_i = \frac{e^{z_i}}{\sum_j e^{z_j}}

Raise ee to each score, then divide by the total. Every weight comes out positive, and they add up to 1.

Write

softmax⁡(z)i=ezi/∑jezj\operatorname{softmax}(z)_i = e^{z_i} / \sum_j e^{z_j}

Step 1: exponentiate

→

e2≈7.389e^2 \approx 7.389, e1≈2.718e^1 \approx 2.718, e0=1e^0 = 1. Bigger scores grow much faster: a gap of 1 in score becomes a factor of e≈2.7e \approx 2.7.

Step 2: divide by the total

÷11.10711.107=

The total is 7.389+2.718+1=11.1077.389 + 2.718 + 1 = 11.107. The weights keep the order of the scores, and the biggest score gets most of the weight.

Write

softmax⁡([2,1,0])≈[0.665,0.245,0.090]\operatorname{softmax}([2, 1, 0]) \approx [0.665, 0.245, 0.090]

Adding a constant changes nothing

softmax⁡(z+c)=softmax⁡(z)\displaystyle \operatorname{softmax}(z + c) = \operatorname{softmax}(z)
ezi+c∑jezj+c=ec eziec∑jezj\displaystyle \frac{e^{z_i + c}}{\sum_j e^{z_j + c}} = \frac{e^c\, e^{z_i}}{e^c \sum_j e^{z_j}}

The factor ece^c cancels. So [102,101,100][102, 101, 100] gives the same weights as [2,1,0][2, 1, 0]. Code uses this: it subtracts the largest score first, so e1000e^{1000} never has to be computed (it would overflow to infinity).

Write

Shift-invariant: softmax⁡(z−max⁡z)=softmax⁡(z)\operatorname{softmax}(z - \max z) = \operatorname{softmax}(z). Always subtract the max.

Temperature

τ=0.5 ⁣:\tau = 0.5\!: sharperτ=1 ⁣:\tau = 1\!:τ=2 ⁣:\tau = 2\!: flatter

softmax⁡(z/τ)\operatorname{softmax}(z / \tau). Dividing by a small τ\tau stretches the gaps, so the top score takes nearly everything; a large τ\tau shrinks them toward a uniform spread. Dividing attention scores by dk\sqrt{d_k} is a temperature of dk\sqrt{d_k}.

Write

softmax⁡(z/τ)\operatorname{softmax}(z/\tau): τ<1\tau < 1 sharper, τ>1\tau > 1 flatter.

−∞-\infty means exactly zero weight

softmax⁡\operatorname{softmax}=

e−∞=0e^{-\infty} = 0, so that entry gets no weight, and the others are normalized among themselves. This is how the causal mask works.

On a matrix: one softmax per row

weights = scores.softmax(dim=-1)    # each row sums to 1

For attention scores (one row per query, one column per key), each query gets its own distribution over the keys.

✏️ Your turn

Softmax by hand

softmax⁡\operatorname{softmax}

Use eln⁡2=2e^{\ln 2} = 2. Write the three weights.

Work it out on paper, then go to the next card to check.

Answer

[2,1,2]5=\frac{[2, 1, 2]}{5} =

Exponentiate to [2,1,2][2, 1, 2], total 5, divide.

Softmax at the output too

In a transformer

The model’s last layer produces a score (a logit) for every token in the vocabulary; softmax⁡(logits/τ)\operatorname{softmax}(\text{logits}/\tau) turns them into next-token probabilities. The temperature setting when you sample from an LLM is this τ\tau.

📌 Recap

Softmax in one card

  • softmax⁡(z)i=ezi/∑jezj\operatorname{softmax}(z)_i = e^{z_i}/\sum_j e^{z_j}: positive, sums to 1, keeps the order.
  • Shift-invariant, so subtract the max for safety.
  • Temperature: softmax⁡(z/τ)\operatorname{softmax}(z/\tau).
  • A score of −∞-\infty gets weight exactly 0.

Write

The four lines above.