Transformer basics
An AI/ML engineer interview on the pieces of a transformer: attention, positions, masks, and the residual stack.
These prompts are original practice. They are not questions from a named employer or a live hiring loop.
1.What does a token embedding do with a token id?
2.What is the shape of the token-embedding matrix?
3.Two positions contain the same token id. Before any position signal is applied, how do their token embeddings compare?
4.How does the original transformer combine the token embedding with positional information?
5.What does the original sinusoidal positional encoding write into the position vector?
6.In the original sinusoidal encoding, which coordinates are sine and which are cosine?
7.In the original sinusoidal encoding, what happens to the oscillation as the dimension index i increases?
8.In the original transformer, what scale is applied to the token embeddings before the position vector is added?
9.How does rotary position encoding inject position?
10.Which of the query, key, and value does RoPE rotate?
11.What does a RoPE rotation do to the length of a query or a key?
12.After RoPE, what does the dot product of a query at position m and a key at position n depend on?
13.How does ALiBi represent position?
14.Where is the ALiBi distance bias inserted?
15.How do ALiBi slopes differ across heads?
16.Where does a relative attention bias enter the block?
17.What limits the indexes a learned absolute position table can represent?
18.Where are absolute position vectors applied, compared with RoPE and ALiBi?
19.With no position signal and no mask that depends on position, what symmetry does self-attention have?
20.Self-attention has no position signal and no mask that depends on position. If the token at a fixed index is swapped with another token, does the output vector at that index stay the same?
21.How are the query, key, and value vectors produced from the attention sublayer input?
22.In self-attention, are the query, key, and value the same vector?
23.Inside the attention sublayer, is a pointwise activation such as ReLU applied between the input and Q, K, and V?
24.In self-attention, where do the query, key, and value come from?
25.In cross-attention, where do the query, key, and value come from?
26.Which stack contains cross-attention in a standard encoder-decoder transformer?
27.How do the score-matrix shapes of self-attention and cross-attention differ?
28.In scaled dot-product attention, the query-key products are divided by which quantity before the softmax?
29.When heads split a fixed d_model, which width does the attention scale use?
30.Why are query-key products scaled before the softmax?
31.Assume query and key components are independent, with mean 0 and variance 1. Why is the divisor the square root of d_k?
32.In the original scaled dot-product attention, is 1/sqrt(d_k) a learned parameter?
33.For one query, what is true of the attention weights after the softmax?
34.Do the queries in one head share a single pool of softmax mass?
35.Can the weights on one key, summed across queries, exceed 1?
36.Must the attention weight from position i to position j equal the weight from j to i?
37.What is one head's output before the output projection mixes heads?
38.Before the output projection, one head's output is a weighted sum of value vectors. Why is that sum a convex combination?
39.A query-key score is negative, and it is not masked. What is true of that key's attention weight?
40.Is a raw query-key score already a probability?
41.What happens to one query's weights if the same finite constant is added to every one of its scores?
42.How does a causal mask stop position i from attending to a later position j greater than i?
43.Under a causal mask, may position i attend to itself?
44.What happens if a forbidden key is given score 0, rather than negative infinity, before the softmax?
45.In a causal attention head, what is the mix at the first position?
46.What does a padding mask do?
47.How is a padding mask different from a masked-language-model mask token?
48.What can a position see in encoder self-attention?
49.What can a position see in decoder self-attention?
50.Does the causal mask hide encoder keys from a decoder query in standard cross-attention?
51.In the original transformer, what does the decoder's masked multi-head attention mask out?
52.What does a decoder-only stack leave out, compared with an encoder-decoder?
53.Does decoder-only mean the model has no attention?
54.Does an encoder-only model use a causal mask for the usual masked-token objective?
55.Which sublayers make one original encoder layer?
56.Which sublayers make one original decoder layer?
57.Which sublayers make one decoder-only layer?
58.Why can a later causal layer not read a future token that earlier layers were forbidden to use?
59.In a causal stack, which tokens can affect the hidden state at position i?
60.A causal prompt is run in one parallel forward pass. Which prompt tokens can position i use?
61.What does a residual connection add?
62.Why do attention and the feed-forward block both return vectors of width d_model?
63.Does the residual add by itself move a vector from one position to another?
64.What is post-norm?
65.What is pre-norm?
66.Which norm placement did the original transformer use, and which placement do many later decoder stacks use?
67.A pre-norm sublayer returns the zero vector. What is the block output?
68.Across which axis does transformer LayerNorm compute its mean and variance?
69.How would BatchNorm differ from LayerNorm on a transformer activation?
70.How does RMSNorm differ from LayerNorm?
71.How is the feed-forward block shared across positions?
72.Does the feed-forward block mix tokens from different positions?
73.What widths does the usual feed-forward block use?
74.Where is the nonlinearity in the original transformer's feed-forward block?
75.What were d_model, the head count, the per-head width, and the feed-forward width in the original base transformer?
76.How deep were the original base encoder and the original base decoder?
77.Ignore small biases, and take the feed-forward inner width as 4 times d_model. How do those feed-forward parameters compare with one self-attention block's Q, K, V, and output projections?
78.How is a SwiGLU-style feed-forward block parameterized, compared with the original two-matrix block?
79.Do layer 1 and layer 2 of a standard transformer share their attention and feed-forward weights?
80.Which attention quantities are learned parameters, and which are computed from the input?
81.When the input sequence gets longer, which shape grows?
82.What is the order of operations in multi-head attention?
83.Why is concatenating heads different from averaging them?
84.Are attention heads combined by a softmax over the head index?
85.With d_model fixed and split evenly, what happens to d_k when the head count goes up?
86.What must match for a query-key dot product, and what value width did the original model use?
87.What is the shape of one head's self-attention score matrix, and how does the cost of building it scale with length?
88.How does feed-forward cost scale with sequence length?
89.After a causal model has processed a prompt, which position's vocabulary distribution is the distribution for the next new token?
90.The network uses two softmaxes. What does each one normalize over?
91.Where is the vocabulary softmax applied?
92.What does sampling temperature divide?
93.How does a positive temperature change the next-token distribution?
94.What does top-k sampling change?
95.Is top-k part of the training loss?
96.What does a KV cache store during decoding, and why?
97.Which past tensors does a standard KV cache need for the next query?
98.Why does each layer keep its own KV cache?
99.Under a causal mask, why do cached keys and values for earlier positions stay valid after a token is appended?
100.What work does a KV cache remove in autoregressive decoding, and what attention work remains?
101.In next-token training with a causal mask, what is the label for the state at index i when a token exists at index i+1?
102.What does masked language modeling predict, and from which directions?
103.What is perplexity for a next-token model?
104.What is teacher forcing, and what changes at generation time?
105.Under a causal mask, what has the last position not attended to?
106.How many tokens can one word be?
107.How does byte-pair encoding build a vocabulary?
108.What sets the context length a transformer can be trusted to use?