v9.1.1
Menu

Triton Row Sum Reduction

mediumFREEReductions & NormalizationTriton Programming

Problem Statement

Given a 2D tensor x of shape (M, N), compute the sum along each row, producing a length-M vector. Assume each row fits in one block.

Background

Launch one program per row (grid = (M,)). Load the whole row with BLOCK_SIZE = triton.next_power_of_2(N), mask columns >= N (load other=0.0), then reduce with tl.sum(row, axis=0).

Your Task

Implement row_sum_kernel and run(M=64, N=300) comparing to x.sum(dim=1).

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 = 64, N = 300
Output
True
Reasoning
  • The input values are M = 64 and N = 300, representing the shape of the 2D tensor x.
  • We launch one program per row, resulting in a total of M programs, each loading a row of length N into a block of size BLOCK_SIZE = triton.next_power_of_2(N).
  • Within each block, we mask columns >= N by loading other=0.0 and then reduce the row using tl.sum(row, axis=0), effectively calculating the sum of each row.
  • The resulting row sums from the Triton kernel are compared to the reference solution x.sum(dim=1) using torch.allclose, yielding True if the outputs match within a certain tolerance.

Constraints

  • One program per row, grid = (M,)
  • BLOCK_SIZE = triton.next_power_of_2(N), pass as constexpr
  • Use tl.sum(row, axis=0); mask invalid columns with other=0.0

Learn this in

More from Triton Programming

solution.py

Test results

Sign in to run your code against the test cases.