Skip to main content

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.

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:

  1. implement numerically stable softmax;
  2. normalize each token's feature vector;
  3. implement scaled causal multi-head self-attention with future probability exactly zero;
  4. reject an embedding width that is not divisible by the head count;
  5. preserve residual-stream shape through attention, feed-forward, and stacked blocks;
  6. assemble token + position embeddings, stacked blocks, final normalization, and vocabulary logits;
  7. keep earlier logits unchanged when only future token IDs change;
  8. 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.py

Compare 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.py

Rubric

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.

CriterionLevel 5 exit-skill mapping4 — Strong evidence2 — Partial evidence0 — Missing/incorrect
Scaled causal multi-head attentionImplement scaled causal MHAScores are scaled, heads split/rejoin correctly, rows normalize, future mass is zero, divisibility checkedAttention mostly works but one invariant is weak or untestedFuture leakage or no multi-head implementation
Residual/norm/MLP blockExplain and implement residual/norm/MLP rolesPre-norm residual paths preserve shape and MLP is position-wise with explicit checksComponents exist but ordering/role explanation is incompleteBlock contract is absent or shape-invalid
Stacking and full architectureAssemble and stack Transformer blocksEmbeddings → stack → final norm → vocabulary logits are traced and depth preserves (T,C)Complete path exists but shape reasoning is weakNo complete mini Transformer
Debugging and evaluationDebug shape/mask errorsIntentional divisibility and future-leakage failures are reproduced; evidence → hypothesis → focused fix is clearOne failure is shown without strong causal evidenceOnly happy-path output
Reproducibility and communicationReproduce and explainStandard validator passes from clean Python 3.11+, no network/secrets, architecture and causal reasoning are clearRuns with undocumented assumptions or weak explanationCannot 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?

← Back to all projects