Multi-Head Attention Forward Pass
Updated
Problem statement
Implement the forward computation of a multi-head self-attention layer by hand. The input x is a sequence of n tokens: row i is token i (rows are counted from 0), a vector of width d. The layer has four weight matrices of size d x d: wq, wk and wv make the queries, keys and values, and wo mixes the heads at the end. It splits the width into heads heads, each of width d_k = d / heads. Return the n x d output matrix, computed as follows.
- Compute
Q = x · wq,K = x · wkandV = x · wvwith ordinary matrix products. Each isn x d. - Head
h, counting from0, owns columnsh · d_kthrough(h + 1) · d_k - 1ofQ,KandV. - In each head, token
igets a score for every tokent: the dot product of the head's part ofQ[i]with the head's part ofK[t], divided bysqrt(d_k). - Softmax turns token
i'snscores into weights: the weight of tokentisexp(score[t])divided by the sum ofexpover allnscores. Tokeni's result in that head is the weighted sum, over allt, of the head's part ofV[t], using those weights. - Join the heads' results column by column in head order, head
0on the left, to get ann x dmatrix, and multiply it bywo.
There is no mask and no bias: every token attends to every token, itself included. Scores here can be far above 709, where exp overflows a double, so your softmax must still return the correct weights, with no overflow and no 0 / 0.
Do all the arithmetic in your own code: no library attention, softmax or matrix routine.
Function
multiHeadAttention(x: double[][], wq: double[][], wk: double[][], wv: double[][], wo: double[][], heads: int) → double[][]Examples
Example 1
x = [[1.0,1.0],[0.0,2.0]]wq = [[1.0,0.0],[0.0,1.0]]wk = [[1.0,0.0],[0.0,1.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[1.0,0.0],[0.0,1.0]]heads = 1return = [[0.5,1.5],[0.1955703174930431,1.804429682506957]]One head, so d_k = 2 and every weight matrix is the identity: queries, keys and values all equal x. Token 0 scores [2, 2] / sqrt(2) = [1.4142, 1.4142], a tie, so its weights are [0.5, 0.5] and its output is [0.5, 1.5]. Token 1 scores [2, 4] / sqrt(2) = [1.4142, 2.8284]; softmax gives weights [0.1956, 0.8044], so its output is 0.1956 · [1, 1] + 0.8044 · [0, 2] = [0.1956, 1.8044].
Example 2
x = [[1.0,2.0],[3.0,-1.0]]wq = [[1.0,0.0],[0.0,1.0]]wk = [[1.0,0.0],[0.0,1.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[0.0,1.0],[1.0,0.0]]heads = 2return = [[1.9925821305300957,2.761594155955765],[-0.8577223804672996,2.9950547536867305]]Two heads of width d_k = 1, so the scale is 1. Head 0 uses column 0: values [1, 3]; token 0 scores [1, 3], weights [0.1192, 0.8808], result 2.7616; token 1 scores [3, 9], result 2.9951. Head 1 uses column 1: values [2, -1]; token 0 gets 1.9926, token 1 gets -0.8577. Concatenating gives [[2.7616, 1.9926], [2.9951, -0.8577]], and wo swaps the two columns.
Example 3
x = [[5.0,5.0],[-5.0,5.0]]wq = [[5.0,0.0],[0.0,5.0]]wk = [[5.0,0.0],[0.0,5.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[1.0,0.0],[0.0,1.0]]heads = 1return = [[5.0,5.0],[-5.0,5.0]]Each token's score with itself is 1250 / sqrt(2) = 883.88 and with the other token is 0. exp(883.88) overflows a double, so a softmax that does not subtract the row's largest score first breaks. With the shift the weights are [1, 0] and [0, 1], so each token gets its own value back.
Constraints
1 <= x.length <= 12(the number of tokens,n)1 <= x[i].length <= 12(the model width,d), and every row ofxhas the same length.wq,wk,wvandwoeach havedrows anddcolumns.1 <= heads <= d, anddis a multiple ofheads.- Every entry of
x,wq,wk,wvandwois between-5and5, with at most two digits after the decimal point. - Each returned value is accepted if it is within
10-9of the expected value, or within10-9times the expected value when that value is larger than1in size.
More Waymo Phone Screen questions
- 2D Convolution Layer Forward PassPHONE SCREEN · Seen Dec 2026
- Numerically Stable SoftmaxPHONE SCREEN · Seen Dec 2026
- Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
- Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
- Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
- Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
- Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
- Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
- Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
- Deterministic Depth-First Graph TraversalPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Prefix Matrix Products and AutogradOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Best-Fit Plane Normal from 3D PointsThe Voleon Group · PHONE SCREEN
- Town Temperature Regression AnalysisCitadel · PHONE SCREENCitadel interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Implement Multi-Head AttentionApple · PHONE SCREENApple interview questions
- Multi-Head Attention BlockScale AI · PHONE SCREENScale AI interview questions
- Range of a Third CorrelationTower Research Capital · PHONE SCREEN
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Best Starting Cell for Restaurant DistanceDoorDash · PHONE SCREENDoorDash interview questions