Build a Transformer from Scratch
The capstone: assemble a working transformer from first principles, one component at a time. Tokenization and embeddings feed scaled attention, attention assembles into a block, masks and a stable loss train it, and caches plus FLOPs let you decode and measure it. Several problems intentionally revisit Deep Learning Essentials and LLM Engineering; that repetition is spaced review, not filler, and it is how the pieces finally lock together. Take it after Deep Learning Essentials.
Your progress
0/32 · 0%Path readiness 0%Easy 0% · Medium 0% · Hard 0%
Continue path · Byte-Level BPE Token CountWhat you will learn
- Tokenize, embed, and position tokens for a transformer
- Build scaled and multi-head attention from first principles
- Assemble a pre-norm block with LayerNorm, GELU, and residuals
- Train with masking and a stable loss, then decode with a KV cache
- Measure attention cost and forward FLOPs per token
Before you start
1Tokenize and Embed
Everything starts as text: byte-level BPE token counts and merge steps, embedding lookups and parameter budgets, the backward scatter-add that trains them, and sinusoidal positional encoding.
- Easy
- BPE Merge StepMediumnlp-020NLP
- Easy
- Easy
- Medium
- Medium
Stage checkpoint
0/2 to passA checkpoint is a short retrieval test drawn from this stage: solve 2 of 3 from memory to mark the stage complete. The stage stays open either way.
2Attention from First Principles
The core mechanism: scaled attention scores, softmax weights, and the weighted sum, then multi-head split and merge, a parameter count with bias, and cross-attention shapes.
- Medium
- Medium
- Medium
- Medium
- Medium
- Hard
- Easy
Stage checkpoint
0/2 to passA checkpoint is a short retrieval test drawn from this stage: solve 2 of 3 from memory to mark the stage complete. The stage stays open either way.
3The Block
Wrap attention in the pieces that make it trainable: LayerNorm and its RMSNorm comparison, GELU in both tanh and exact forms, pre- versus post-norm residual scaling, and a full encoder block with its parameter count.
- Medium
- Medium
- Hard
- Medium
- Hard
- Hard
- Hard
Stage checkpoint
0/2 to passA checkpoint is a short retrieval test drawn from this stage: solve 2 of 3 from memory to mark the stage complete. The stage stays open either way.
- Residual Gradient Scale Pre Vs Post NormHard
- Transformer Encoder Block ForwardHard
- Transformer Encoder Block Parameter CountHard
4Training and Masking
Teach the stack to predict: numerically stable softmax, the softmax cross-entropy backward pass, padding and sliding-window attention masks, seeded attention dropout, and a mixed-precision loss-scaling step.
- Medium
- Hard
- Medium
- Medium
- Medium
- Medium
Stage checkpoint
0/2 to passA checkpoint is a short retrieval test drawn from this stage: solve 2 of 3 from memory to mark the stage complete. The stage stays open either way.
5Decode and Measure
Run it and count the cost: KV-cache sizing and appends, online-softmax rescaling and logsumexp tracking, quadratic attention FLOPs, and forward FLOPs per token.
- Medium
- Easy
- Medium
- Medium
- Easy
- Medium
Stage checkpoint
0/2 to passA checkpoint is a short retrieval test drawn from this stage: solve 2 of 3 from memory to mark the stage complete. The stage stays open either way.
Capstone
ProjectClose the path with one real artifact from the catalog.
The full decoder-only transformer, exactly the stack this path assembles piece by piece.