TensorTonic
Problems
Fused Row-Wise Softmax in Triton
Activation FunctionsGPU Programming
Medium

Implement a numerically stable, fused row-wise softmax in Triton. For each row iii and column jjj of the input matrix:

out[i,j]=exp⁡(x[i,j]−max⁡kx[i,k])∑k=0N−1exp⁡(x[i,k]−max⁡kx[i,k])\texttt{out}[i, j] = \frac{\exp(x[i, j] - \max_k x[i, k])}{\sum_{k=0}^{N-1} \exp(x[i, k] - \max_k x[i, k])}out[i,j]=∑k=0N−1​exp(x[i,k]−maxk​x[i,k])exp(x[i,j]−maxk​x[i,k])​

The pattern is one program per row: each program loads its full row into registers, takes the row max, subtracts it for numerical stability, exponentiates, sums, and divides. The whole row pipeline happens in one kernel with one read of xxx and one write of out\texttt{out}out.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]]

Output: [[0.0900, 0.2447, 0.6652], [0.6652, 0.2447, 0.0900]]

Input: x = [[0.0, 0.0, 0.0, 0.0]]

Output: [[0.25, 0.25, 0.25, 0.25]]

Hint 1

One program per row. Use \texttt{tl.program_id} as the row index and address the row by row⋅stride\text{row} \cdot \text{stride}row⋅stride.

Hint 2

Always max-subtract before exponentiating. Without it large positive logits overflow float32; with it the largest exponent argument is exactly 000.

Hint 3

For the row mask, load with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’) so masked lanes lose the max and contribute zero to the sum after exp⁡(−∞)=0\exp(-\infty)=0exp(−∞)=0.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Subtract the row max before exponentiating for numerical stability
  • Mask the column tail so non-power-of-two row widths are correct
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M≤1281 \le M \le 1281≤M≤128, 1≤N≤40961 \le N \le 40961≤N≤4096.
  • Input is torch.float32 on cuda and row-contiguous; output has the same shape.
  • Tolerance: atol=1e-3. Max-subtract keeps the exponent in [−∞,0][-\infty, 0][−∞,0], so float32 holds.
Try Similar Problems
Online Softmax Kernel in TritonMediumFused Matmul + Bias + ReLU in TritonMediumRow-Wise LogSumExp in TritonMediumSingle-Pass Mean and Variance in TritonMediumGELU in TritonEasyReLU in TritonEasySiLU in TritonEasySoftmax as a CUDA KernelMediumTriton Row-Wise SoftmaxMediumVector Sum Reduction in TritonEasyVector Max Reduction in TritonEasyL2 Vector Norm in TritonEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Fused Row-Wise Softmax in Triton

Fused Row-Wise Softmax in Triton

Activation FunctionsGPU Programming
Medium

Implement a numerically stable, fused row-wise softmax in Triton. For each row iii and column jjj of the input matrix:

out[i,j]=exp⁡(x[i,j]−max⁡kx[i,k])∑k=0N−1exp⁡(x[i,k]−max⁡kx[i,k])\texttt{out}[i, j] = \frac{\exp(x[i, j] - \max_k x[i, k])}{\sum_{k=0}^{N-1} \exp(x[i, k] - \max_k x[i, k])}out[i,j]=∑k=0N−1​exp(x[i,k]−maxk​x[i,k])exp(x[i,j]−maxk​x[i,k])​

The pattern is one program per row: each program loads its full row into registers, takes the row max, subtracts it for numerical stability, exponentiates, sums, and divides. The whole row pipeline happens in one kernel with one read of xxx and one write of out\texttt{out}out.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]]

Output: [[0.0900, 0.2447, 0.6652], [0.6652, 0.2447, 0.0900]]

Input: x = [[0.0, 0.0, 0.0, 0.0]]

Output: [[0.25, 0.25, 0.25, 0.25]]

Hint 1

One program per row. Use \texttt{tl.program_id} as the row index and address the row by row⋅stride\text{row} \cdot \text{stride}row⋅stride.

Hint 2

Always max-subtract before exponentiating. Without it large positive logits overflow float32; with it the largest exponent argument is exactly 000.

Hint 3

For the row mask, load with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’) so masked lanes lose the max and contribute zero to the sum after exp⁡(−∞)=0\exp(-\infty)=0exp(−∞)=0.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Subtract the row max before exponentiating for numerical stability
  • Mask the column tail so non-power-of-two row widths are correct
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M≤1281 \le M \le 1281≤M≤128, 1≤N≤40961 \le N \le 40961≤N≤4096.
  • Input is torch.float32 on cuda and row-contiguous; output has the same shape.
  • Tolerance: atol=1e-3. Max-subtract keeps the exponent in [−∞,0][-\infty, 0][−∞,0], so float32 holds.
Try Similar Problems
Online Softmax Kernel in TritonMediumFused Matmul + Bias + ReLU in TritonMediumRow-Wise LogSumExp in TritonMediumSingle-Pass Mean and Variance in TritonMediumGELU in TritonEasyReLU in TritonEasySiLU in TritonEasySoftmax as a CUDA KernelMediumTriton Row-Wise SoftmaxMediumVector Sum Reduction in TritonEasyVector Max Reduction in TritonEasyL2 Vector Norm in TritonEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1