Linear algebra for LLMsPart 6: The rest of the transformer
Logits
6 min
From the last hidden state to scores
After the last block, a final norm and the unembedding () give every position one score per vocabulary token. Llama 3’s , so this is one of the biggest matrices in the model.
Write
Weight tying: reuse the embedding table
Some models use the transposed embedding table as the unembedding. Then the logit for token is the dot product of the hidden state with token ’s own embedding: “how much does look like this token?” GPT-2 ties its weights; Llama 3 8B keeps two separate matrices.
Write
✏️ Your turn
Score the toy vocabulary
With tied weights (and skipping the norm), write the five logits .
Work it out on paper, then go to the next card to check.
Answer
Each logit is dotted with a row: “the” scores , 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
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 .
Write
What it costs
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 tensor is built: at and that’s over a billion logits per sequence.
📌 Recap
Logits in one card
- Logits = , one score per vocabulary token.
- Tied weights: , so each logit is a token embedding.
- Softmax gives next-token probabilities; the loss is .
- Cost: FLOPs per token.
Write