Attention With Absolute Index Bias
Updated
Problem statement
Implement the attention formula softmax(Q * K + B) * V, where B[i][j] = abs(i-j). Return the attention output matrix.
Q has shape n by d, K has shape d by m, and V has shape m by p. Indices start at zero. Each query row gets one score per key. Apply softmax to each query row on its own, across its m keys: softmax(s)[j] = exp(s[j]) / sum(exp(s)).
Add the bias exactly as written: it is positive and grows with the distance between i and j. There is no causal mask and no extra scale factor. Compute in double precision. An answer within relative or absolute error 10^-6 is accepted. You may subtract the largest score in a row before taking exp. This gives the same result and avoids overflow.
Function
biasedAttention(q: double[][], k: double[][], v: double[][]) → double[][]Examples
Example 1
q = [[0]]k = [[0,0]]v = [[0],[10]]return = [[7.310585786300049]]There is one query row and two keys. Both dot products are zero, so the scores are just the bias values, 0 and 1. Softmax gives the second key e/(1+e) of the weight. The first value row is zero, so the output is that share of the second value row.
Example 2
q = [[1,0],[0,1]]k = [[2,0],[0,2]]v = [[10,0],[0,20]]return = [[7.310585786300049,5.3788284273999025],[2.6894142136999513,14.621171572600097]]Each query row gets its own softmax. In the first row, the first key has the higher score, so the output leans toward the first value row. In the second row, the second key has the higher score, so the output leans toward the second value row. The output has two rows with two values each.
Example 3
q = [[10,-10]]k = [[10],[-10]]v = [[3,-4]]return = [[3.0,-4.0]]There is only one key, so softmax gives it probability one, even though its dot product is large. The output is that key's value row, unchanged.
Constraints
1 <= n,m,d,p <= 64.- Each matrix is a full rectangle with the shape given above. Every entry is a finite number in
[-20,20].
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Cumulative No-Intercept Regression BetasCitadel · PHONE SCREENCitadel interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Automation Pipeline, Part 2: Stage WorkersPlaid · PHONE SCREENPlaid interview questions
- Reservoir Sampling with Recorded DrawsGoogle · PHONE SCREENGoogle interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Token Bucket Request DecisionsDatadog · PHONE SCREENDatadog interview questions
- Gradient Descent for Linear RegressionMicrosoft · PHONE SCREENMicrosoft interview questions