Trace one token through GPT-2 small
CPU · your Macnotebook90 minrequiredGoal · Print the shape of every intermediate tensor for one forward pass of GPT-2 small and reproduce the parameter count from the config.
- 1pip install torch transformers; load gpt2 with AutoModelForCausalLM and print model.config.
- 2Register forward hooks on every nn.Module and log (qualified name, output shape) in order.
- 3Run the prompt 'The GPU is' and collect the log into a table.
- 4Hand-derive the expected shapes of wte, wpe, ln_1, c_attn, c_proj, c_fc, mlp.c_proj, ln_f and lm_head from config.json and compare.
- 5Write params(config) implementing the formula and compare with sum(p.numel() for p in model.parameters()).
- 6Implement a 20-step greedy decoding loop with argmax and confirm it matches model.generate(do_sample=False).
- Deliverable
- A notebook with the shape table, the formula and a markdown cell explaining any surprises (such as Conv1D weight layout).
- Check
- Your formula returns 124,439,808 exactly; every hooked shape matches your prediction; your greedy loop and generate produce identical token ids.