Simulate a warp-per-row matrix-vector kernel

Why it matters Decode-time matvecs in LLM inference are memory bound, and the warp-per-row strided layout is how kernels keep loads coalesced.

A common matvec kernel gives each row to one warp. Lane l sums A[r, k] * x[k] for k = l, l + w, l + 2w, ..., so adjacent lanes read adjacent columns (coalesced). Then the warp adds its lane partials together.

Write warp_matvec(A, x, w=32). A is (R, K), x has length K. Return (y, partials) where partials has shape (R, w) with partials[r, l] = sum over k = l, l + w, ... of A[r, k] * x[k] (lanes with no columns hold 0), and y[r] = sum over lanes of partials[r].

Signaturedef warp_matvec(A, x, w=32)

Example

A [[1, 2, 3, 4, 5]], x [1, 1, 1, 1, 1], w 2 gives y [15] and partials [[9, 6]].

Understand the idea

Picture 32 people reading a long row of numbers together. If each person takes the next number in turn (person 0 takes column 0, person 1 takes column 1, then person 0 takes column 32), the whole group reads one neat block at a time, which is fast for memory. If instead each person took their own long chunk, they would be reading 32 far-apart places at once. Worked example: row [1, 2, 3, 4, 5], x all ones, w = 2. Lane 0 takes columns 0, 2, 4: 1 + 3 + 5 = 9. Lane 1 takes 1, 3: 2 + 4 = 6. y = 15. Rule: partial[l] = sum_{k = l mod w} A[r, k] x[k], y = sum_l partial[l]. LLM decode is mostly matvecs like this, and its speed depends on reading weights with coalesced loads.

Shapes and data flow

A (R, K)  x (K,)  lane l columns l, l+w, ...  partials (R, w)  y (R,)
solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplewarp_matvec([[1, 2, 3, 4, 5]], [1, 1, 1, 1, 1], 2)
  • Sample 2: randomwarp_matvec(_A, _x, 4)