Transformers and Attention Questions
The transformer architecture that underlies modern language and multimodal models. Covers self-attention and multi-head attention, positional encoding, encoder/decoder structures, and why transformers scale better than recurrent alternatives. Focuses on the architectural intuition behind contemporary foundation models.
Explain scaled dot-product attention and multi-head attention algorithmically and mathematically. Provide the equations for Attention(Q,K,V), explain the sqrt(d_k) scaling factor, compute time and memory complexity in terms of sequence length L and embedding dim d, and discuss why multiple heads can be beneficial.
Explain the transformer encoder architecture: multi-head self-attention, scaled dot-product attention, positional encodings, residual connections, and layer normalization. Describe the O(n^2) time and memory complexity of self-attention and the practical implications for long sequences. Explain when Transformers are preferred over RNNs/CNNs for sequence tasks.
Implement sinusoidal positional encodings from the Transformer paper in Python/NumPy. Function signature: positional_encoding(seq_len, d_model) -> ndarray of shape (seq_len, d_model). Ensure even and odd dimensions use sin and cos of different frequencies respectively, and show a short example of adding encodings to token embeddings.
Implement the scaled dot-product attention mechanism in NumPy. Function signature should accept Q, K, V arrays of shape (batch, seq_len, d_k) and an optional mask for padding or causal masking. Compute attention weights as softmax(Q @ K.T / sqrt(d_k)), apply the mask correctly, return the attended output and the attention matrix. Include numerical stability measures for softmax.
What is the basic intuition behind transformer self-attention, and why did transformers become the dominant architecture for modern language models?
Unlock Full Question Bank
Get access to all 6 Transformers and Attention interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.