PIXELBANKv9.1.0
Menu

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:

Input:
M = 128, K = 256
Output:
True
Reasoning:
  • The input values M = 128 and K = 256 define the dimensions of matrix A and vector x, where A is a (M,K)(M, K) matrix and x is a length-KK vector.
  • The gemv_kernel function is implemented to compute the matrix-vector product y = A @ x using a reduction, where each program loads a row of A and the full vector x, multiplies elementwise, and reduces with tl.sum.
  • The run function allocates inputs on the GPU, launches the gemv_kernel, and computes the reference result using A @ x.
  • The final output is True if the result from the gemv_kernel is close to the reference result, i.e., torch.allclose(triton_out, torch_reference, ...) returns True.

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)
๐Ÿ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Triton Matrix-Vector Product (GEMV) - Medium | PixelBank