PyTorch interview questions

9 questions across 6 sections, in the order you would learn them. Every answer here is one that works — but an answer you have read is not an answer you can give, so use Test yourself and say it out loud first.

If this comes up in an interview

What does model.eval() do?

Switches layers that behave differently at inference, notably dropout and batch normalisation, into evaluation mode. Forgetting it makes validation numbers noisy and pessimistic, and forgetting to switch back disables dropout for the rest of training.

Why wrap validation in torch.no_grad()?

It stops the framework recording operations for gradient computation, which is unnecessary during evaluation. It saves memory and time, and without it a large validation pass can run out of memory.

Your GPU utilisation is 20 percent during training. What is wrong?

Almost certainly data loading, not the model. Raise num_workers, enable pin_memory, and check for expensive per-item preprocessing that should be precomputed or moved onto the GPU.

Autograd, custom pieces and performance

Read the section that teaches this

If this comes up in an interview

Why might a tensor's gradient be None after backward?

The operation that produced it was not recorded in the autograd graph, usually because it was detached, created from a Python number, or computed inside no_grad. Autograd can only backpropagate through what it recorded.

How would you speed up a training run?

Profile first, because the bottleneck is usually data loading or synchronisation rather than arithmetic. Then mixed precision, more data loader workers, removing per-step .item() calls, and torch.compile.

Shapes, broadcasting and the bugs they cause

Read the section that teaches this

If this comes up in an interview

A regression model's loss decreases slowly but predictions are nonsense. What would you check first?

Shapes. A prediction of shape [N, 1] subtracted from targets of shape [N] broadcasts to [N, N] and computes a meaningless loss without error. Assert shapes where the loss is computed, and squeeze or reshape explicitly.

Then they push further

Why does view sometimes fail after transpose?

Transpose only changes strides, so the data is no longer laid out contiguously for the new shape. view cannot reinterpret non-contiguous memory. Call contiguous() first, or use reshape, which copies if it has to.

What gets marked up. Marked up for naming the silent broadcast bug specifically. It is one of the most common real training bugs.

Build and train a small transformer from scratch

Read the section that teaches this

If this comes up in an interview

Walk me through a minimal GPT-style model.

Token ids go through token and position embeddings and are added. A stack of blocks follows, each with layer norm, causal multi-head self-attention added back to the residual, then layer norm and an MLP added back. A final norm and a linear layer produce logits over the vocabulary. Training minimises cross-entropy between logits and the next token at every position.

Then they push further

Why is the loss at initialisation a useful check?

An untrained model should predict roughly uniformly, so its loss should be close to the log of the vocabulary size. Much higher means a bad initialisation or a bug, such as targets not shifted correctly.

Why is the causal mask needed during training?

Training predicts every position at once. Without the mask, each position could attend to the tokens it is supposed to predict, and the model would learn to copy rather than to predict.

What gets marked up. Strong candidates can write this from memory and explain residual adds, pre-norm and the mask. Many applicants for research roles are asked to implement part of it live.

When the loss will not go down

Read the section that teaches this

If this comes up in an interview

Your model's training loss is flat from the first step. How do you debug it?

Check the initial loss against the expected value for uniform guessing, try to overfit one small batch, decode and inspect a batch with its labels, sweep the learning rate by factors of ten, and log gradient norms to see whether gradients reach the parameters at all.

Then they push further

It cannot overfit a single batch. What does that tell you?

The problem is in the model, the loss or the optimiser setup, not the data volume. Common causes are parameters not passed to the optimiser, a detached tensor, a wrong loss for the output format, or labels that do not match the inputs.

What gets marked up. Marked up for 'overfit one batch' as an early step and for a systematic order rather than random changes.

Checkpoints and reproducibility

Read the section that teaches this

If this comes up in an interview

What do you need to save to resume a training run exactly?

Model weights, optimiser state, scheduler state, the step or epoch counter, random number generator states, and the position in the data. Missing the optimiser state is the common mistake: Adam restarts with empty moment estimates and training behaves differently.

These are the questions for one topic. The whole question bank covers every topic on the site.