The 60-second answer
Define tensor shapes first: batch, sequence, hidden size, number of heads, and per-head dimension; assert hidden_size % heads == 0. Implement normalized attention → residual and normalized MLP → residual (or clearly state post-norm if using that variant), including dropout/masks where required.
Build the answer in this order
Define tensor shapes first: batch, sequence, hidden size, number of heads, and per-head dimension; assert hidden_size % heads == 0.
Implement normalized attention → residual and normalized MLP → residual (or clearly state post-norm if using that variant), including dropout/masks where required.
Reshape/transposes must preserve batch/head/sequence semantics and return the original [B,T,D] shape.
Test with tiny deterministic tensors, causal/padding masks, train/eval modes, and a backward pass that produces finite gradients.
A useful interview mental model
This is the shape of a strong answer—not a script to memorize.
Senior-level signal
- Senior answers discuss pre-norm stability, fused attention kernels, and avoiding unnecessary contiguous copies/transposes.
- Call out KV caching as a serving concern, separate from the training forward pass.
What the interviewer is really testing
Likely follow-up questions
Common weak-answer patterns
- Ignoring shape, dtype, device, masking, or broadcasting assumptions.
- Using a framework call without explaining the underlying operation.
- Skipping gradient, numerical-stability, and batching checks.