← Questions

MLP tensor shapes and PyTorch broadcasting

0
NvidiaAI/ML designLast asked Jan 2026

An AI/ML design interview question asked in Nvidia's technical phone screen, last asked Jan 2026. It tests pytorch, tensors and neural networks.

Given a small multi-layer perceptron and a batch of inputs, write down the shape of every tensor in the forward and backward pass, then explain PyTorch's broadcasting rules and the bugs they can hide.

Setup

Unless you agree on a different setup with the interviewer, assume a batch of B = 32 inputs of 784 features, one hidden layer of 256 units with ReLU, and 10 output classes, written with nn.Linear layers.

Part 1: shapes

  • Give the shapes of the input, each layer's weight and bias (in PyTorch's nn.Linear convention), the hidden activations, the logits, the loss, and the gradient of each parameter.
  • Write the forward computation of one linear layer as a matrix product, and say how the bias reaches every row of the batch.

For reference: x is (32, 784), the first weight is (256, 784) and its bias (256,), so x @ W1.T + b1 is (32, 256); the second layer's weight is (10, 256) and the logits are (32, 10).

Part 2: broadcasting

  • State the broadcasting rules: align shapes from the right; each pair of dimensions must be equal or one of them must be 1 (or missing).
  • Explain why (32, 256) + (256,) works, and what (32, 1) - (32,) produces.

What a strong answer covers

  • Every shape correct, including that gradients have the same shapes as their parameters and that the loss is a scalar after mean reduction.
  • The (out_features, in_features) weight layout and why x @ W.T is used.
  • Broadcasting applied to the bias, and the silent bug where predictions of shape (32, 1) minus targets of shape (32,) give a (32, 32) matrix, so the loss is wrong without any error.
  • Habits that catch shape bugs: assertions on shapes, squeeze/unsqueeze used deliberately, and named dimensions or einsum.

Follow-ups

  • Add a second hidden layer of 128 units: what changes?
  • The input is now a sequence of shape (B, T, 784): how does nn.Linear handle the extra dimension?
  • Count the parameters in the model.
  • Derive the gradient of the first weight by hand and check its shape.

Where it's asked

What it tests

pytorch · tensors · neural networks

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing