Linear algebra for LLMsPart 6: The rest of the transformer

Logits

6 min

From the last hidden state to scores

logits=Norm⁡(h) WU\displaystyle \text{logits} = \operatorname{Norm}(h)\, W_U
→

After the last block, a final norm and the unembedding WUW_U (d×Vd \times V) give every position one score per vocabulary token. Llama 3’s V=128,256V = 128{,}256, so this is one of the biggest matrices in the model.

Write

Logits = Norm⁡(h) WU\operatorname{Norm}(h)\,W_U, WU ⁣:d×VW_U\!: d \times V, output (B,T,V)(B, T, V).

Weight tying: reuse the embedding table

WU=E⊤  ⇒  logitj=h⋅Ej,:\displaystyle W_U = E^\top \;\Rightarrow\; \text{logit}_j = h \cdot E_{j,:}

Some models use the transposed embedding table as the unembedding. Then the logit for token jj is the dot product of the hidden state with token jj’s own embedding: “how much does hh look like this token?” GPT-2 ties its weights; Llama 3 8B keeps two separate matrices.

Write

Tied: WU=E⊤W_U = E^\top, logitj=h⋅Ej,:_j = h \cdot E_{j,:}.

✏️ Your turn

Score the toy vocabulary

h=[1,0,1,0]h = [1, 0, 1, 0]

With tied weights (and skipping the norm), write the five logits EhEh.

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

Answer

Each logit is hh dotted with a row: “the” scores 1+2=31 + 2 = 3, the highest.

Softmax to probabilities

Softmax (Part 5) turns the logits into a distribution over the next token: “the” gets about 69%. Sampling picks from it; greedy decoding takes the top one.

The training loss

L=−log⁡ptarget\displaystyle \mathcal{L} = -\log p_{\text{target}}

Cross-entropy: the negative log-probability the model gave to the token that actually came next, averaged over every position. If the target here were “the”, the loss would be −ln⁡0.693≈0.37-\ln 0.693 \approx 0.37.

Write

Cross-entropy: L=−log⁡ptarget\mathcal{L} = -\log p_{\text{target}}, averaged over positions.

What it costs

2 d V=2×4096×128,256≈1.05×109 FLOPs per token\displaystyle 2\,d\,V = 2 \times 4096 \times 128{,}256 \approx 1.05 \times 10^9 \text{ FLOPs per token}

In a transformer

At generation time only the last position’s logits are needed, so code computes logits = lm_head(h[:, -1]). In training every position predicts its next token, so the full (B,T,V)(B, T, V) tensor is built: at T=8192T = 8192 and V=128,256V = 128{,}256 that’s over a billion logits per sequence.

📌 Recap

Logits in one card

  • Logits = Norm⁡(h) WU\operatorname{Norm}(h)\,W_U, one score per vocabulary token.
  • Tied weights: WU=E⊤W_U = E^\top, so each logit is h⋅h \cdot a token embedding.
  • Softmax gives next-token probabilities; the loss is −log⁡ptarget-\log p_{\text{target}}.
  • Cost: 2dV2dV FLOPs per token.

Write

The four lines above.