Work through the memory consumers in order: batch size and sequence length, activations retained for the backward pass, optimizer state, and fragmentation from variable shapes. Quick fixes are a smaller batch with gradient accumulation to preserve the effective batch, gradient checkpointing, bf16, and a memory efficient optimizer. Also check you are not accumulating tensors that still carry a computation graph.
Why interviewers ask this
This is a daily reality for anyone training models, so the interviewer is checking for a systematic method rather than random flag flipping. The detail that identifies experience is knowing activations usually dominate rather than weights, plus the classic bug of appending a loss tensor to a list every step, which retains the whole graph and leaks memory across iterations.
How to structure your answer
- List the memory consumers in rough order of size.
- Distinguish a capacity problem from a leak that grows over steps.
- Give the cheap fixes: accumulation, checkpointing, bf16.
- Name the retained graph bug and how you spot it.
Example answer
First thing I check is whether it dies immediately or after a few hundred steps, because those are different bugs. Immediate is a capacity problem, growing is a leak. For capacity I break down what is actually resident: weights, gradients, optimizer state (with Adam that is two extra copies), and activations saved for the backward pass. On a transformer the activations usually dominate, and they scale with batch size times sequence length, so those are the first knobs. I drop the batch and use gradient accumulation to keep the effective batch identical, which costs wall clock but nothing else. Then gradient checkpointing, which recomputes activations during backward and trades roughly thirty percent extra compute for a large memory saving. bf16 halves activation size on top of that. If it grows over steps, something is holding a graph. The classic is appending the raw loss tensor to a list for logging, which keeps every step's computation graph alive, and the fix is calling item on it. I confirm by printing allocated and reserved memory every hundred steps, because a clean staircase upward is the tell.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What is the difference between allocated and reserved memory in the allocator?
- How does memory fragmentation happen with variable length inputs?
- When would you use CPU offloading instead of shrinking the batch?
Related machine learning engineer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions