Skip to content

Capstone 4: CS336 Assignment 1

ready≈ 50 hoursNeeds: 4.1–4.6

Part 4 built every piece of a small language model one module at a time, with Glassbox’s tests beside you. This capstone takes away the scaffolding. You do Assignment 1 of Stanford CS336, Language Modeling from Scratch, from its official handout and against its official tests. You write the same components again, in their interfaces, and train the result. Read the handout’s policy on AI tools before you start and follow it: writing the code yourself is the point.

What changes. The handout says only what each function must do, not how to structure your code. It also rules out the shortcuts 4.6 allowed: from torch.nn, torch.nn.functional and torch.optim you may use only nn.Parameter, the container classes such as nn.Module, and the torch.optim.Optimizer base class. So no nn.Linear, no F.scaled_dot_product_attention and no torch.optim.AdamW. The tokenizer is byte-level BPE with special tokens, trained on a real corpus, so its speed matters. Read the handout’s rules on what is allowed before you start.

Follow the repository’s README: clone it, install uv, and run the test suite. Every test fails at first. You connect your code to the tests through the adapter functions in tests/adapters.py, then work through the handout section by section until uv run pytest passes. The README also explains how to download TinyStories and the OpenWebText sample.

Terminal window
git clone https://github.com/stanford-cs336/assignment1-basics
cd assignment1-basics
uv run pytest
Handout section What you build Glassbox module
BPE tokenizer training Pre-tokenization with the GPT-2 regex, merge counting, special tokens, a serialised vocabulary and merge list 4.1 train_bpe, get_stats, merge
Tokenizer encode, decode and a streaming encode_iterable 4.1 encode, decode
Linear and embedding layers Weights as parameters with a truncated-normal initialisation 3.4, 4.6
RMSNorm, SwiGLU, RoPE Pre-norm components of a LLaMA-style block 4.5 rmsnorm, swiglu, rope
Softmax and attention Stable softmax, scaled dot-product attention with a mask, causal multi-head self-attention 4.4, 4.5
Transformer LM Blocks stacked, a final norm, an output layer 4.6 GPT
Loss and optimizer Stable cross-entropy, AdamW, cosine schedule with warmup, gradient clipping 2.4, 3.3, 3.5
Training loop Batches of random windows, checkpointing, logging 4.6 get_batch, train
Decoding Temperature and top-p sampling 4.6 top_k_top_p, generate
Experiments Learning-rate sweeps, ablations (no norm, post-norm, no RoPE, SiLU MLP), TinyStories and OpenWebText runs 3.5, the 4.6 notebook

The handout’s experiments are budgeted in data-centre GPU hours, and its TinyStories target is a validation loss of at most 1.45 per token. For low-resource runs it allows about 40 million training tokens and a target of 2.00 instead. A free Kaggle GPU sits in between. Start from the 4.6 notebook: replace its model and optimizer with yours and its character data with your tokenizer’s output, saved as a NumPy array and read with np.memmap, as the handout suggests. Report the validation loss you reach and your learning-rate sweep next to the handout’s targets, and write up the results as the handout asks; the write-up is the part that teaches the most.

Check one function at a time with the test that names it, for example uv run pytest -k test_rope. Compare shapes and conventions with your Glassbox version first: most failures are a transposed weight, a different pairing of RoPE features, or a missing special token. Glassbox does not publish solutions to the CS336 assignments, and you should not look for others’ code: as with the course’s own students, the value is in doing it yourself.

Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.