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.

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.

MediumTechnical
42 practiced

Compare pre-LN and post-LN Transformers. How do residuals and normalization affect gradient flow and training stability, and when would you prefer each at scale?

MediumTechnical
29 practiced

Implement the sinusoidal positional encoding from the original Transformer paper so it returns a (max_len, d_model) array, and explain why the frequencies are arranged the way they are.

EasyTechnical
35 practiced

Why do Transformers need positional information at all? Explain sinusoidal and learned positional embeddings and the idea behind relative positional encodings.

HardTechnical
29 practiced

Implement causal multi-head attention in PyTorch that applies rotary embeddings to Q and K. Support batching, state the expected shapes, and give the computational complexity.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.