Week 05 Companion - Transformer Math, PyTorch Architecture, and Tokenizer Support
<- Companion index · Master Plan · cert plan · build project
Prerequisite Checklist
- You can multiply matrix shapes for projections and attention.
- You understand probability distributions, logits, softmax, and cross-entropy at working level.
- You can write a small
torch.nn.Moduleand know where parameters live. - You know BPE/tokenizer basics: text becomes token IDs before it becomes tensors.
- You can explain train vs generate mode for a language model.
Mini Lesson
A decoder-only transformer is a repeated block that transforms token embeddings into next-token logits. The build week is hard only if the shapes feel mysterious. Make the shapes explicit.
For batch B, sequence length T, model width C, heads H, and head size D = C / H:
x: B x T x C
q, k, v: B x H x T x D
scores = q @ k^T: B x H x T x T
attention = softmax(scores / sqrt(D)) @ v: B x H x T x D
Math Insight
The 1 / sqrt(D) scaling keeps dot products from growing with head dimension. If q and k have roughly unit variance, their dot product has variance proportional to D. Without scaling, large scores push softmax into saturation and gradients become poor.
KV-cache memory is the serving trap:
KV bytes = 2 * layers * kv_heads * head_dim * sequence_length * batch * bytes_per_value
The 2 is K plus V. This one formula explains why inference is often memory-bound.
Playbook Bridge
First, read the Demystifying AI Week 5 bridge: tokens and embeddings, parameters and activations, and self-attention. Use the embedding and self-attention PNGs when the formula feels too compressed.
Before this week, read the
Week 5 Ultra-Scale Playbook bridge.
Add a memory ledger to notes.md: parameters, gradients, optimizer states, activations,
and KV cache. The core habit is to predict memory pressure before PyTorch tells you.
Programming Primer
- PyTorch modules register parameters when assigned as
nn.Parameteror submodules. model.train()andmodel.eval()change behavior for layers like dropout; use them deliberately.- Tokenizers are part of the model contract. Train and inference must use the same tokenizer and chat template.
25-Minute Gate
- For
B=2,T=128,C=768,H=12, computeDand the attention score tensor shape. - Explain why causal masking uses
-infbefore softmax. - Read the week 5 project tests and identify which one protects attention correctness.