KatInterview

Softmax-Weighted Value Mixing for Query Rows

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashingmatrixsoftmaxnumerical-stability

Last updated

Implement scaled dot-product attention on nested lists using a numerically stable softmax and a weighted average of value rows.

Softmax-Weighted Value Mixing for Query Rows

You are given three matrices stored as lists of lists of numbers:

  • queries: n rows, each of width d.
  • keys: m rows, each of width d.
  • values: m rows, each of width w. The width w need not equal d.

Write a function that returns, for every query row, a blend of the value rows. The blend weights come from how well the query matches each key.

Procedure

For a query row q:

  1. For every key row k_j, compute the score s_j = (q · k_j) / sqrt(d). Here q · k_j is the dot product and d is the length of the query row.
  2. Convert the scores into weights with a softmax: p_j = exp(s_j) / sum_t exp(s_t).
    • To avoid overflow, subtract the largest score in the row from every score before exponentiating. This does not change the result mathematically.
  3. The output row for q is the weighted sum of the value rows: out[c] = sum_j p_j * values[j][c] for each column c in 0..w-1.

Return the output rows in the same order as the queries, as a list of lists of floats (n rows, w columns each).

Edge-case rules

  • If queries is empty, return [].
  • If all scores in a row are equal, the weights are uniform, so the output is the mean of the value rows.
  • Use the width of the first value row as w. If values is empty, each output row is an empty list.

Constraints

  • 0 <= n <= 200, 1 <= m <= 200, 1 <= d <= 64, 1 <= w <= 64.
  • keys and values have the same number of rows.
  • Every query and key row has the same length d.
  • Entries are finite real numbers, and scores may be large in magnitude, so the softmax must be numerically stable.
  • Results are compared to the expected output within floating-point tolerance.

Examples

Example 1

Input: attend([[1, 0, 1], [0, 1, 0]], [[1, 0, 1], [0, 1, 0]], [[1, 2, 3], [4, 5, 6]])

Output: [[1.718894674425938, 2.718894674425938, 3.718894674425938], [2.9213724270418826, 3.9213724270418826, 4.921372427041883]]

Example 2

Input: attend([[1, 2, 3], [4, 5, 6]], [[1, 0, 0], [0, 1, 0]], [[10, 20, 30], [40, 50, 60]])

Output: [[29.213724270418822, 39.21372427041882, 49.21372427041883], [29.213724270418822, 39.21372427041882, 49.21372427041883]]

Example 3

Input: attend([[0, 0, 0]], [[0, 0, 0]], [[7, 8, 9]])

Output: [[7, 8, 9]]

Hints

Hint 1

Handle each query row independently: first build its list of scores against all keys, then turn that list into weights.

Hint 2

Before calling exp, subtract the maximum score in the row. The weights stay the same, but nothing can overflow and the denominator is at least 1.

Hint 3

Once you have the weights for a row, each output column is the dot product of the weights with that column of the value matrix. Take the output width from the first value row, not from the query width.