PIXELBANKv9.1.0
Menu

Triton Stable LogSumExp Kernel

Problem Statement

Compute the row-wise log-sum-exp of a (M, N) tensor in a numerically stable way: lse[m] = log(sum_j exp(x[m,j])).

Background

Subtract the row max before exponentiating, then add it back to the log: m + log(sum(exp(x - m))). This prevents overflow for large inputs and underflow for very negative ones.

Your Task

Implement logsumexp_kernel and run(M=64, N=300) comparing to torch.logsumexp(x, 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 M = 64 and N = 300 define the dimensions of the tensor.
  • For each row m in the tensor, we compute the row-wise max m and subtract it from each element x[m,j] to prevent overflow and underflow: xโ€ฒ[m,j]=x[m,j]โˆ’mx'[m,j] = x[m,j] - m.
  • We then calculate the log-sum-exp for each row using the modified values: lse[m]=m+logโก(โˆ‘jexpโก(xโ€ฒ[m,j]))lse[m] = m + \log\left(\sum_j \exp(x'[m,j])\right).
  • The logsumexp_kernel function is compared to the PyTorch reference implementation torch.logsumexp(x, dim=1), and the output True indicates that the results are identical within a certain tolerance, verified by torch.allclose.

Constraints:

  • Subtract row max before exp; add it back after log
  • One program per row; mask invalid lanes with other=-inf
  • Use tl.exp, tl.sum, tl.log
๐Ÿ”’

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.