Triton Matrix-Vector Product (GEMV)
Problem Statement
Compute y = A @ x where A is (M, K) and x is a length-K vector, using one program per output row (a reduction, not tl.dot).
Background
Each program loads row m of A and the full vector x, multiplies elementwise, and reduces with tl.sum. This shows that small inner dimensions are often better as a reduction than a matmul.
Your Task
Implement gemv_kernel and run(M=128, K=256) comparing to A @ x.
How it is tested
Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.
Example
M = 128, K = 256
True
- The input values
M = 128andK = 256define the dimensions of matrixAand vectorx, whereAis a matrix andxis a length- vector. - The
gemv_kernelfunction is implemented to compute the matrix-vector producty = A @ xusing a reduction, where each program loads a row ofAand the full vectorx, multiplies elementwise, and reduces withtl.sum. - The
runfunction allocates inputs on the GPU, launches thegemv_kernel, and computes the reference result usingA @ x. - The final output is
Trueif the result from thegemv_kernelis close to the reference result, i.e.,torch.allclose(triton_out, torch_reference, ...)returnsTrue.
Constraints
- One program per row, grid = (M,)
- BLOCK_K = triton.next_power_of_2(K); mask columns
- y[m] = tl.sum(row * x, axis=0)