TensorTonic
Problems
GEMV: Matrix Vector Product in Triton
Linear AlgebraGPU Programming
Medium

Implement a matrix-vector product (GEMV) kernel in Triton. Given AAA of shape (M,N)(M, N)(M,N) and xxx of shape (N,)(N,)(N,), compute out\texttt{out}out of shape (M,)(M,)(M,) as a row-by-vector dot product:

out[i]=∑j=0N−1A[i,j]⋅x[j]\texttt{out}[i] = \sum_{j=0}^{N-1} A[i, j] \cdot x[j]out[i]=j=0∑N−1​A[i,j]⋅x[j]

This is a row-block reduction: each program owns a contiguous slab of output rows and walks the NNN dimension in chunks, accumulating the per-row dot products in an fp32 register vector. The pattern is the simplest non-trivial inner reduction in Triton.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], x = [5, 6]

Output: [17, 39]

Input: A = [[1, 0, 1], [0, 1, 0], [1, 1, 1]], x = [2, 3, 4]

Output: [6, 3, 9]

Hint 1

Use a 1D grid over the row blocks. Inside each program the row indices are a vector and the column chunk is another vector.

Hint 2

Initialize an fp32 accumulator of shape (BLOCK_M,)(\text{BLOCK\_M},)(BLOCK_M,) and loop along NNN, broadcasting the xxx chunk against the row tile before summing along the column axis.

Hint 3

On the final store, only the MMM tail can be partial. Reuse the row mask you built once at the top of the kernel.

Requirements

  • Use one Triton program per row block; each program reduces its rows along NNN
  • Accumulate the per-row dot product in fp32
  • Mask the loads on both the MMM and NNN tails and the store on the MMM tail
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤5121 \le M, N \le 5121≤M,N≤512.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-2 + rtol=1e-2 * abs(expected) for N≥128N \ge 128N≥128 to absorb reduction drift.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

GEMV: Matrix Vector Product in Triton

GEMV: Matrix Vector Product in Triton

Linear AlgebraGPU Programming
Medium

Implement a matrix-vector product (GEMV) kernel in Triton. Given AAA of shape (M,N)(M, N)(M,N) and xxx of shape (N,)(N,)(N,), compute out\texttt{out}out of shape (M,)(M,)(M,) as a row-by-vector dot product:

out[i]=∑j=0N−1A[i,j]⋅x[j]\texttt{out}[i] = \sum_{j=0}^{N-1} A[i, j] \cdot x[j]out[i]=j=0∑N−1​A[i,j]⋅x[j]

This is a row-block reduction: each program owns a contiguous slab of output rows and walks the NNN dimension in chunks, accumulating the per-row dot products in an fp32 register vector. The pattern is the simplest non-trivial inner reduction in Triton.

Loading visualization...

Examples

Input: A = [[1, 2], [3, 4]], x = [5, 6]

Output: [17, 39]

Input: A = [[1, 0, 1], [0, 1, 0], [1, 1, 1]], x = [2, 3, 4]

Output: [6, 3, 9]

Hint 1

Use a 1D grid over the row blocks. Inside each program the row indices are a vector and the column chunk is another vector.

Hint 2

Initialize an fp32 accumulator of shape (BLOCK_M,)(\text{BLOCK\_M},)(BLOCK_M,) and loop along NNN, broadcasting the xxx chunk against the row tile before summing along the column axis.

Hint 3

On the final store, only the MMM tail can be partial. Reuse the row mask you built once at the top of the kernel.

Requirements

  • Use one Triton program per row block; each program reduces its rows along NNN
  • Accumulate the per-row dot product in fp32
  • Mask the loads on both the MMM and NNN tails and the store on the MMM tail
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤5121 \le M, N \le 5121≤M,N≤512.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-2 + rtol=1e-2 * abs(expected) for N≥128N \ge 128N≥128 to absorb reduction drift.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1