FastPrepMulti-Head Attention Forward Pass

Multi-Head Attention Forward Pass

Meta logoMeta● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a projection-free multi-head scaled dot-product attention forward pass. query, key, and value are row-major integer matrices with the same feature width d; key and value have the same number of rows. heads divides d.

Split each row into contiguous head slices. For each query row and head, compute dot products against every key slice, divide by sqrt(d / heads), apply a numerically stable softmax, and use the weights to combine the matching value slices. Concatenate the head outputs.

Return one comma-separated string per query row with every coordinate formatted to exactly six decimal places. There are no learned projections or masks.

Function

multiHeadAttention(query: int[][], key: int[][], value: int[][], heads: int) → String[]

Examples

Example 1

query = [[1]]key = [[0],[1]]value = [[10],[20]]heads = 1return = ["17.310586"]

Softmax over scores 0 and 1 weights the second value more heavily.

Example 2

query = [[1,0]]key = [[1,0],[0,1]]value = [[4,8],[6,2]]heads = 2return = ["4.537883,5.000000"]

Each one-dimensional head computes its own attention distribution.

Constraints

  • 1 <= query rows, key rows, d <= 40.
  • All rows have width d; 1 <= heads <= d and divides d.
  • Entries are between -100 and 100.

More Meta problems

See Meta hiring insights
public String[] multiHeadAttention(int[][] query, int[][] key, int[][] value, int heads) {
  // write your code here
}
query[[1]]
key[[0],[1]]
value[[10],[20]]
heads1
expected["17.310586"]
Checking account…