Triton Fused Softmax Kernel
Problem Statement
Implement a numerically stable row-wise softmax over a (M, N) tensor, matching the official Triton fused-softmax tutorial. Each row fits in one block.
Background
Subtract the row max before exponentiating (x - tl.max(x)) to avoid overflow, then normalize by the sum of exponentials. Padded lanes load as -inf so exp gives 0.
Your Task
Implement softmax_kernel and run(M=64, N=300) comparing to torch.softmax(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 the softmax operation. - For each row in the tensor, we subtract the row maximum
tl.max(x)from each elementxto avoid overflow, resulting inx - tl.max(x), then apply the exponential function to getexp(x - tl.max(x)). - We normalize these exponentials by dividing by their sum, , to obtain the softmax values for each row.
- The
runfunction compares the resulting Triton softmax outputtriton_outwith the PyTorch reference outputtorch_referenceusingtorch.allclose, returningTrueif the two outputs are close enough, which is the case for the given sample input.
Constraints
- Subtract row max before exp (numerical stability)
- Normalize by tl.sum of the exponentials
- One program per row; mask with other=-inf