Skip to content
mentoor

Build a tiny GPT

For engineers

Write a small character-level GPT in PyTorch, from tokenizer and causal self-attention to a training loop and sampling, to see what every LLM does inside

See where you stand

110 questions across four stages: Understand, Apply, Solve and Judge.

Try 5 questions

Practice items, not counted towards your mastery. No account needed.

Check yourself · 1 of 5

A character tokenizer is built for a corpus that contains 65 different characters. What is vocab_size?

Choose one answer

Check yourself · 2 of 5

In causal self-attention, when is the lower-triangular mask applied?

Choose one answer

Check yourself · 3 of 5

Why does a GPT add position embeddings to token embeddings?

Choose one answer

Check yourself · 4 of 5

A new character-level model with 65 characters has not been trained at all. About what cross-entropy loss should the first step print?

Choose one answer

Check yourself · 5 of 5

Which logits does a GPT use to choose the next token during generation?

Choose one answer

Topics

  1. Character tokenizer and training batches4 min read · Map characters to ids, split train and validation text, and cut random fixed-length blocks with targets shifted by one token
  2. Causal self-attention in code5 min read · Queries, keys and values, scaled dot products, the lower-triangular mask and multi-head reshaping in a few dozen lines of PyTorch
  3. Stacking blocks into a GPT model5 min read · Token and position embeddings, residual blocks with layer norm and an MLP, and a final projection to vocabulary logits
  4. Training loop and reading loss curves5 min read · Cross-entropy loss, AdamW, learning-rate warmup and decay, and how train versus validation loss reveals learning, overfitting or a broken run
  5. Sampling text from your trained model5 min read · Autoregressive generation with temperature and top-k, cropping the context window, and why a small model writes fluent nonsense
  6. From toy GPT to production models4 min read · What separates a tiny model from frontier models: BPE tokenizers, data scale, compute, post-training, and reference projects such as nanoGPT and nanochat

Start here

110 questions · last verified 2026-12