Train a Tiny GPT
🤖 What is a tiny GPT? A GPT is a stack of causal transformer blocks trained to predict the next token. A tiny one works on characters: the vocabulary is the set of characters in the text, the model reads a window of them and predicts the next at every position, and generation feeds its own predictions back in. Everything a large language model does is here at small scale.
Mercor's machine learning engineer onsites have included this as a timed mini-project: implement an autoregressive language model and train it on Shakespeare end to end, with a coding agent allowed, followed by Transformer fundamentals such as pre- and post-norm, self-attention, position embeddings and decoding methods (the question); another report describes an hour of coding a nanoGPT plus 30 minutes of review, and stresses that communication was weighted heavily (the question).
The Prompt
This round is a small project, not a puzzle, so the interviewer grades whether everything works end to end, how you check each piece, and how clearly you explain the model while you build it.
"Implement a small autoregressive transformer language model, train it on this Shakespeare text at the character level, and generate a sample. You have an hour."
Clarifying Questions
You: Character-level, so the vocabulary is the set of characters in the file? No tokenizer library?
Interviewer: Yes, characters.
You: Can I use nn.Linear, nn.Embedding, nn.LayerNorm, but write attention myself?
Interviewer: Yes. That's what we want to see.
You: We're on a CPU, so I'll keep the model to a few hundred thousand parameters and train for seconds to minutes. Is that fine?
Interviewer: Fine. Show it learns; we're not chasing a number.
You: I'll hold out the last 10% of the text for validation and report both losses.
Interviewer: Good.
What You'll Build
encode(s) -> list[int],decode(ids) -> str, andget_batch(split) -> (x, y), each(B, T).Bigram: a lookup-table baseline,forward(idx, targets) -> (logits, loss).GPT(vocab, T, D, H, L): embeddings,Lpre-norm blocks, a final norm and an output head.train(model, steps, ...) -> historyandgenerate(model, idx, n, temperature, top_k) -> (B, T + n).