Transformer Attention and SwiGLU Forward Pass
Updated
Problem statement
Implement a Transformer attention and SwiGLU forward pass for one sequence of token vectors.
For this exercise, assume the pass consists exactly of bias-free multi-head self-attention followed by a bias-free SwiGLU feed-forward layer. The formulas below define the whole computation.
Inputs and shapes
xhas shapen by d.wq,wk,wvandwoeach have shaped by d.wgandwueach have shaped by f;wdhas shapef by d.headsdividesd. Each head useswidth = d / headsconsecutive feature coordinates.
Multi-head self-attention
- Project the tokens:
Q = x * wq,K = x * wkandV = x * wv. - For head
h, use coordinatesh * widththrough(h + 1) * width - 1. For query tokeniand key tokenj, its score is the dot product of their head slices divided bysqrt(width). - Apply softmax across all
nkey tokens independently for each query and head:p[j] = exp(score[j]) / sum(exp(score)). Form the weighted sum of that head's value slices. - Concatenate heads in increasing head order to obtain
Cof shapen by d, then computeA = C * wo.
Parallel SwiGLU branches
Compute G = A * wg and U = A * wu from the same A. For every coordinate, define SiLU(z) = z / (1 + exp(-z)). Multiply element by element to obtain B = SiLU(G) elementwise-multiplied-by U. Return B * wd, an n by d matrix.
For this exercise, assume there is no batch dimension, causal or padding mask, bias, residual addition, normalization, positional encoding, dropout, training or backward pass. Use ordinary matrix loops and standard mathematical functions without a machine-learning or numerical-array library. A stable softmax may subtract the row maximum before exponentiation.
Function
transformerForward(x: double[][], wq: double[][], wk: double[][], wv: double[][], wo: double[][], wg: double[][], wu: double[][], wd: double[][], heads: int) → double[][]Examples
Example 1
x = [[1]]wq = [[1]]wk = [[1]]wv = [[1]]wo = [[1]]wg = [[1]]wu = [[1]]wd = [[1]]heads = 1return = [[0.7310585786300049]]There is one token and one head, so its attention probability is one. All projections preserve 1. The gate and up branches are both 1, giving SiLU(1) times 1 = approximately 0.7310585786.
Example 2
x = [[1,0],[0,1]]wq = [[1,0],[0,1]]wk = [[1,0],[0,1]]wv = [[1,0],[0,1]]wo = [[1,0],[0,1]]wg = [[1,0],[0,1]]wu = [[1,0],[0,1]]wd = [[1,0],[0,1]]heads = 2return = [[0.3607715420179067,0.15561483280046365],[0.15561483280046365,0.3607715420179067]]Each one-coordinate head has its own softmax. The concatenated attention rows are approximately [0.7310585786,0.5] and [0.5,0.7310585786]; applying the two parallel identity branches and SiLU gives the displayed output.
Example 3
x = [[1,2],[-1,0]]wq = [[0,0],[0,0]]wk = [[1,0],[0,1]]wv = [[1,0],[0,1]]wo = [[1,0],[0,1]]wg = [[1,0],[0,1]]wu = [[1,0],[0,1]]wd = [[1,0],[0,1]]heads = 1return = [[0.0,0.7310585786300049],[0.0,0.7310585786300049]]Every projected query is zero, so both tokens receive weight 0.5. Their average value is [0,1], and the identity output projection preserves it. Both output rows are [0,SiLU(1)].
Constraints
- For this exercise, assume
1 <= n <= 16,1 <= d <= 8,1 <= f <= 16, and1 <= heads <= dwithd % heads = 0. - Every matrix is nonempty, rectangular and has the stated compatible shape.
- Token coordinates are finite values in
[-8,8]; weight coordinates are finite values in[-2,2]. - Compute in double precision. Each returned coordinate is accepted when its absolute error is at most
10^-7 * max(1, abs(expected)).
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Vanilla RNN Hidden StatesNVIDIA · PHONE SCREENNVIDIA interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
- Deterministic K-Means AssignmentsMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions