Level 5 project
Build a Mini Transformer
Start from the canonical project files, validate the result, and keep the evidence you need to explain what you built.
There is no upload or grading service. Run the documented validator or checks where applicable, keep the results, and score your own evidence against the rubric below. A passing check by itself does not mean the project is complete. Suggested strong-evidence target: 16/20. Critical-criterion rule: A 16/20 total is not sufficient if any criterion the rubric marks as critical scores zero.
Start here
Launch lesson: Mini Transformer Integration Workshop
Prerequisite project: Tokenizer Workbench
Prerequisites
Python help for this project:
Complete Level 5 through L5.16 — Mini Transformer Integration Workshop. You should be able to explain Q/K/V roles, dot-product scaling, softmax attention weights, causal masks, multiple heads, residual connections, LayerNorm, feed-forward networks, and stacked Transformer blocks.
The canonical cumulative dependency is the Level 4 project Tokenizer Workbench. This Level 5 package accepts integer token IDs directly so it remains independently reproducible.
Goal
Build a deterministic decoder-style mini Transformer from understandable pieces and prove the invariants that make it causal and composable.
Task
Complete the TODOs in projects/starters/l05/mini_transformer.py.
Your mini Transformer must:
- implement numerically stable softmax;
- normalize each token's feature vector;
- implement scaled causal multi-head self-attention with future probability exactly zero;
- reject an embedding width that is not divisible by the head count;
- preserve residual-stream shape through attention, feed-forward, and stacked blocks;
- assemble token + position embeddings, stacked blocks, final normalization, and vocabulary logits;
- keep earlier logits unchanged when only future token IDs change;
- expose attention weights so causal behavior can be inspected.
Stuck on a TODO?
Choose one function at a time. Hint 1 offers direction, hint 2 offers pseudocode, and hint 3 reveals only that function's reference implementation after confirmation.
mini_transformer.py: softmax()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: layer_norm()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: feed_forward()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: causal_multi_head_attention()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: residual_add()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: transformer_block()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: stack_blocks()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
mini_transformer.py: mini_transformer()
Try the starter function and run its validator before revealing the reference.
Hints for this TODO
Validation
Run these commands from the downloaded Project folder or the public materials repository root.
python projects/tests/l05/validate_submission.py projects/starters/l05/mini_transformer.pyCompare with a reference
First complete your own TODOs and run the submission validator above. Then open the published reference solution. Reference acceptance tests check the completed reference, not your submission:
python projects/tests/l05/test_reference.pyRubric
Project ID: p05-mini-transformer Launch: after l05-16-mini-transformer-integration-workshop Canonical dependency: p04-tokenizer-workbench
Score each criterion 0–4. A strong submission earns at least 16/20 and has no zero in Causality or Reproducibility.
| Criterion | Level 5 exit-skill mapping | 4 — Strong evidence | 2 — Partial evidence | 0 — Missing/incorrect |
|---|---|---|---|---|
| Scaled causal multi-head attention | Implement scaled causal MHA | Scores are scaled, heads split/rejoin correctly, rows normalize, future mass is zero, divisibility checked | Attention mostly works but one invariant is weak or untested | Future leakage or no multi-head implementation |
| Residual/norm/MLP block | Explain and implement residual/norm/MLP roles | Pre-norm residual paths preserve shape and MLP is position-wise with explicit checks | Components exist but ordering/role explanation is incomplete | Block contract is absent or shape-invalid |
| Stacking and full architecture | Assemble and stack Transformer blocks | Embeddings → stack → final norm → vocabulary logits are traced and depth preserves (T,C) | Complete path exists but shape reasoning is weak | No complete mini Transformer |
| Debugging and evaluation | Debug shape/mask errors | Intentional divisibility and future-leakage failures are reproduced; evidence → hypothesis → focused fix is clear | One failure is shown without strong causal evidence | Only happy-path output |
| Reproducibility and communication | Reproduce and explain | Standard validator passes from clean Python 3.11+, no network/secrets, architecture and causal reasoning are clear | Runs with undocumented assumptions or weak explanation | Cannot reproduce or relies on unrecorded external state |
Required evidence
- objective validator output;
- architecture shape note;
- causal prefix-invariance evidence;
- future-attention-mass evidence;
- intentional failure/debug note;
- short explanation of why attention mixes positions while the feed-forward network acts position-wise.
Score reasoning and observable evidence, not matching wording: every shape, causality, and debugging claim should point to a focused test or result.
Self-check before you call it done
- Can you show a passing objective validator and the shapes from embeddings through the Transformer stack to logits?
- Can you point to prefix-invariance and zero-future-attention-mass measurements rather than only assert causality?
- Can you demonstrate the divisibility and future-leakage failures, explain the focused fixes, and rerun the checks?
- Can you explain from observable evidence how attention mixes positions while the feed-forward path acts position-wise?