BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Debug and fix9 of 16 in topic

864. Fix attention scores without the transpose

MediumDebug and fix

Why it matters Shape bugs that happen to run on square inputs are the worst kind: they pass a quick smoke test and fail in production.

The starter code below runs, but it has a realistic bug. Read the spec, find the bug and fix it. Keep the function name and signature.

attention(q, k, v) should compute scaled dot-product attention without a mask: softmax(q @ k.T / sqrt(d)) @ v, where the softmax runs over keys (the last axis) and d is the size of the last axis of q.

Inputs: q is a list or ndarray of shape (Tq, d). k and v have shape (Tk, d) and (Tk, dv). Tq and Tk can differ.

Return an ndarray of floats with shape (Tq, dv). No rounding.

Functiondef attention(q, k, v)

Example 1

Input: attention([[1.0, 0.0]], [[1.0, 0.0], [1.0, 1.0]], [[10.0], [20.0]])

Output: [[15.0]]

Example 2

Input: q = [[1.0, 2.0], [0.0, 1.0]], k = [[1.0, 0.0], [3.0, 1.0]], v = [[1.0, 0.0], [0.0, 1.0]]

Output: [[0.055807, 0.944193], [0.330238, 0.669762]]

Example 3

Input: q = [[0.5, 0.5, 0.0]], k = [[1.0, 2.0, 0.0], [0.0, 1.0, 0.0], [3.0, 0.0, 1.0]], v = [[1.0, 1.0], [2.0, 0.0], [0.0, 3.0]]

Output: [[0.828758, 1.561656]]

Constraints

  • v has shape (Tk, d)
  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Write down the shape of every intermediate.

Hint 2

Each score is a dot product of a query row and a key row.

Hint 3

Transpose k before the matmul.

Common mistakes
  • Transposing q instead.
  • Softmax over the query axis.
  • Scaling by sqrt(Tk) instead of sqrt(d).
Understand the idea

q @ k multiplies a (Tq, d) matrix by a (Tk, d) matrix. That only runs when d equals Tk, and even then it computes the wrong thing. Each query must be dotted with each key, which is q @ k.T with shape (Tq, Tk). Tests with Tq != Tk or non-symmetric k catch the bug.

Shapes and data flow
q: (Tq, d). k: (Tk, d). v: (Tk, dv). scores: (Tq, Tk). Output: (Tq, dv).
Topics
Debug and fix
Similar questions
  • Fix the causal mask off-by-oneMedium
  • Fix Adam missing bias correctionMedium
  • Fix the scaler that leaks test dataMedium
  • Fix the KV cache that grows the wrong axisMedium
All Debug and fix problems
Report
864 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
8
9
10
import numpy as np
​
def attention(q, k, v):
q, k, v = (np.asarray(a, dtype=float) for a in (q, k, v))
s = q @ k / np.sqrt(q.shape[-1])
s = s - s.max(axis=-1, keepdims=True)
w = np.exp(s)
w = w / w.sum(axis=-1, keepdims=True)
return w @ v
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS