Transformers and Attention Questions

The transformer architecture behind modern language models. Covers queries, keys and values, scaled dot-product and multi-head attention (including why scores are scaled and worked numeric examples), masking, positional encoding (sinusoidal, learned, relative with bucketed biases, rotary, and 2D schemes for image patches), encoder-only, decoder-only and encoder-decoder variants, residual connections, layer normalization placement and the feed-forward block, parameter, memory and compute accounting, why transformers scale better than recurrent models, efficient and long-context attention variants (sparse, local window with global tokens, hashing-based, linear, low-rank, FlashAttention, multi-query), and what attention heads learn and what raw attention weights do and do not explain. Includes implementing attention and its components from scratch. Distributed training, serving and quantization, fine-tuning, pretraining objectives, and general optimizer practice are covered elsewhere.

MediumTechnical
31 practiced

Implement batched scaled dot-product attention from scratch (PyTorch or NumPy) that accepts an optional mask broadcastable to the scores and returns both the output and the attention weights. State the shapes and what happens to a fully masked row.

EasyTechnical
37 practiced

Explain self-attention in a Transformer: what queries, keys and values are, how the output is computed from them, why the scores are scaled, and what shapes flow through the computation for a batch of sequences.

MediumTechnical
34 practiced

Implement a multi-head attention module in PyTorch with a single fused QKV projection, support for padding and causal masks, dropout on the attention weights, and an output of shape (batch, seq_len, d_model) alongside the attention weights. Avoid Python loops over heads.

MediumTechnical
37 practiced

Walk through the Transformer encoder architecture end to end: multi-head self-attention, positional encoding, residual connections and layer normalization. What are the time and memory costs of self-attention, and what do they imply in practice?

EasyTechnical
29 practiced

Compute single-head attention by hand for a tiny example (two tokens, d_k = 2, given Q, K and V). Show the score matrix, the scaling, the row-wise softmax and the final output, and say what each attention row means.

Unlock Full Question Bank

Get access to all 25 Transformers and Attention interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.