← Questions

Write a 2-D convolution, a biased attention step and a training loop

0
NvidiaCodingLast asked Dec 2025

A coding interview question asked in Nvidia's coding round, last asked Dec 2025. It tests numpy, attention and training.

Write three small ML building blocks from scratch in NumPy (and plain PyTorch for the last one). Each part is short; the interviewer cares about correct shapes, no unnecessary Python loops, and knowing what each line does.

Part 1: 2-D convolution

  • conv2d(x: np.ndarray, k: np.ndarray) -> np.ndarray computes a valid convolution (no padding) with stride 1, as deep learning frameworks do it (cross-correlation: the kernel is not flipped). For an H x W input and a kh x kw kernel the output is (H - kh + 1) x (W - kw + 1). Loop at most over the kernel offsets, not over output pixels; better, use slicing or a strided view.
x = [[ 1,  2,  3,  4],
     [ 5,  6,  7,  8],
     [ 9, 10, 11, 12],
     [13, 14, 15, 16]]
k = [[0, 1, 0],
     [1, 1, 1],
     [0, 1, 0]]
conv2d(x, k) -> [[30, 35],
                 [50, 55]]

Part 2: attention with an added bias

  • attention(q, k, v, bias) -> np.ndarray with q of shape (n, d), k of (m, d), v of (m, dv) and bias of (n, m), computing softmax(q @ k.T / sqrt(d) + bias) @ v, with the softmax over each row. A bias of -inf masks a key out.
q = k = [[1, 0], [0, 1]]
v = [[1, 2], [3, 4]]
bias = [[0, -inf], [0, 0]]
attention(q, k, v, bias) -> [[1.0,    2.0   ],
                             [2.3395, 3.3395]]
# row 0 can only see key 0; row 1 weights the keys 0.330 and 0.670

Part 3: finish a training loop

You get a PyTorch loop with pieces missing: zeroing gradients, the forward pass, the loss, the backward pass, the optimizer step, and the evaluation pass. Fill them in and explain what breaks if each one is left out or put in the wrong order.

Constraints

  • NumPy only for Parts 1 and 2.
  • Part 2 must be numerically stable when scores are large.

Clarifying questions

  • Part 1: convolution or cross-correlation? Do we need batches, channels, stride or padding?
  • Part 2: is the bias the same for every head and batch (broadcast), or given per head?
  • Part 2: what should a row whose keys are all masked return?
  • Part 3: is there a learning-rate scheduler or gradient clipping to place as well?

What a strong answer covers

  • Part 1: summing k[i, j] * x[i:i+oh, j:j+ow] over the kernel offsets, or sliding_window_view with einsum; output shape and cost.
  • Part 2: subtracting the row maximum before exp; where the bias is added (after scaling, before softmax); the fully masked row giving NaN unless handled.
  • Part 3: optimizer.zero_grad(), forward, loss, loss.backward(), optimizer.step(), in that order; model.train() and model.eval(); torch.no_grad() for evaluation; moving tensors to the device.
  • Checking each part against a reference (scipy.signal.correlate2d, PyTorch's scaled_dot_product_attention).

Follow-ups

  • Extend conv2d to batches, input and output channels, stride and padding, and give the output shape.
  • Turn the attention into multi-head attention with a causal mask built into the bias.
  • The loss becomes NaN after a few hundred steps. What do you check?

Where it's asked

  • Nvidia · Coding · Last asked Dec 2025Live coding, often C++-leaning; each round usually opens with 10 to 15 minutes on your past work · Assume no AI unless the interviewer offers it

What it tests

numpy · attention · training

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding