PIXELBANKv9.1.0
Menu

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:

Input:
M = 64, N = 300
Output:
True
Reasoning:
  • The input values M = 64 and N = 300 define 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 element x to avoid overflow, resulting in x - tl.max(x), then apply the exponential function to get exp(x - tl.max(x)).
  • We normalize these exponentials by dividing by their sum, exp(xโˆ’tl.max(x))โˆ‘exp(xโˆ’tl.max(x))\frac{exp(x - tl.max(x))}{\sum exp(x - tl.max(x))}, to obtain the softmax values for each row.
  • The run function compares the resulting Triton softmax output triton_out with the PyTorch reference output torch_reference using torch.allclose, returning True if 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
๐Ÿ”’

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.
Triton Fused Softmax Kernel - Hard | PixelBank