Course · Transformers & language modelsPhase 07 · Study guide

Can you prove that a predicted token cannot see its future?

Training Systems · Local CPULaptop CPU4 lessons (~6.5h)

Revision v0.18.1 ·

Attention lets each query mix values according to its relationship with the keys. Its matrix looks abstract until you follow one row: which positions are allowed, what probabilities do they receive, and which values are combined? Shapes, masks and target shifts determine the meaning of that calculation. Build the causal model from those local contracts. Change a future token and check an earlier output, trace the prompt-to-answer target boundary, and keep padding or packed documents out of valid comparisons. Read generation as the result of both a model and a declared decoding policy.

Preparation: Data & checkpoint recovery

Runtime: CPU for small blocks; TPU or GPU for training experiments

Check your starting point

A query has equal scores for three allowed keys. What are the attention weights, and what happens if one key is masked?

Compare your reasoning

The weights are equal thirds. Masking one key leaves equal halves on the two allowed keys and zero on the masked key. Normalize over keys, not queries.

If this is unfamiliar, work through Attention from small pieces before attempting the project.

Build in stages

1. Audit attention and targets

Check one attention row by hand, token/target shifts, padding denominators and segment boundaries. Perturb future tokens and inspect earlier outputs.

2. Train, resume and decode one small model

Preserve residual shapes and sensitive arithmetic, compare the next resumed update and state the decoding policy. Continue into the text harness for cache/full-prefix parity.

Try a changed condition

Make a prediction before opening the reasoning. Keep your answer and the evidence you would collect.

Pack document A at positions 0 and 1 and document B at positions 2 and 3. Which keys may B’s first query use? What if A changes?

Compare an approach

With causal and same-segment masking, query 2 may use key 2 only, assuming it is a valid input. A pure causal mask incorrectly permits keys 0 and 1. Change A while keeping B fixed and require B outputs to remain unchanged. Align shifted targets so an end-of-document position does not learn the next document’s first token.

Diagnose a likely failure

Symptom: Padding produces NaNs in attention.

Check next: Count permitted keys in every query row. A fully masked softmax needs an explicit invalid-query policy; its output and loss must not become valid training evidence.

Decide what is ready

Use text-harness stages 1–4. Keep independent attention arithmetic, future-token invariance, resumed Adam state and full-prefix/cache parity with declared precision.

Where to put your project code

Write your code in: projects/text-harness/my_model.py (copy projects/text-harness/starter/model.py first: cp projects/text-harness/starter/model.py projects/text-harness/my_model.py).

Verify your implementation: python3 projects/text-harness/tests/check.py --stage all --impl projects/text-harness/my_model.py

Run the staged project checks on your implementation in projects/text-harness/my_model.py and record your transfer experiment results in your portfolio.

Open A checkpointed causal language model

Open Trace a tiny bidirectional encoder from pretraining to retrieval

What still needs further work

Real-corpus curation, tokenizer tradeoff studies, long-context evaluation and live generative quality need further labs beyond the tiny byte-model harness.