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
M = 64, N = 300
True
- The input values
M = 64andN = 300define the dimensions of the tensor. - For each row
min the tensor, we compute the row-wise maxmand subtract it from each elementx[m,j]to prevent overflow and underflow: . - We then calculate the log-sum-exp for each row using the modified values: .
- The
logsumexp_kernelfunction is compared to the PyTorch reference implementationtorch.logsumexp(x, dim=1), and the outputTrueindicates that the results are identical within a certain tolerance, verified bytorch.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