Read and refactor a large PyTorch training file
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's onsite coding round, last asked Mar 2026. It tests pytorch, code reading and complexity.
You are handed a few hundred lines of existing PyTorch code that trains a classifier: data loading, the model, the training loop, evaluation and checkpointing, mostly in one file. First answer questions about what it does and what it costs in time and memory. Then make a series of changes, one part at a time, keeping it running and correct after each. Reading unfamiliar code quickly and changing it safely matters more than writing new code.
Part 1: read it
Walk through the file and answer:
- What does one training step do, end to end? Where is the model in
train()versuseval()mode? - Where does time go per step (data loading, forward, backward, optimizer, logging), and what is the cost in terms of batch size, sequence or image size and parameter count?
- What uses memory on the device: parameters, gradients, optimizer state, activations? Which grows with batch size?
- Anything that looks wrong or wasteful.
Part 2 onward: change it
The interviewer asks for changes one at a time. Make each as a small, reviewable edit and show it still trains.
Example
The kind of sequence to expect (yours will differ):
1. Pull the loop apart into train_one_epoch(), evaluate() and main(), with no change in behaviour.
2. The loss is logged with loss.item() every step, forcing a GPU sync: log every k steps instead.
3. Add gradient accumulation so the effective batch is 4x larger on the same memory.
4. Add mixed precision (autocast plus a gradient scaler) and say what memory it saves.
5. Make checkpoints resumable: model, optimizer, scheduler, step and RNG state.
Clarifying questions
- Can I run the code, and is there a small dataset or a smoke test?
- Should behaviour stay bit-for-bit identical after a refactor, or only statistically?
- Which hardware: one GPU, several, or CPU only?
- Are there tests, and may I add some?
What a strong answer covers
- A quick map of the file before editing: entry point, data path, model, loop, side effects.
- Concrete cost reasoning: activations scale with batch size and depth, Adam keeps two extra copies of the parameters,
no_gradandeval()at evaluation, data loader workers and pinned memory. - Spotting common problems: missing
zero_grad, evaluating in training mode, metrics accumulated as tensors that keep the graph alive, needless host–device copies or syncs. - Small, safe edits; a quick check after each (a few steps on a tiny batch, loss before and after compared, overfit one batch).
- Explaining trade-offs of each change (accumulation versus a true larger batch with BatchNorm, mixed-precision stability).
Follow-ups
- Scale it to several GPUs with DistributedDataParallel: what changes in data loading, logging and checkpoints?
- Training got 30% slower after a change. How do you find out why?
- Memory still runs out at the batch size you need: what are your options, in order?
Where it's asked
- OpenAI · Onsite coding · Last asked Mar 202660 min · Live coding; the problem often rebuilds a familiar system in several parts · No AI
What it tests
pytorch · code reading · complexity
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
