TensorTonic
Problems
Row-Wise LogSumExp in Triton
GPU Programming
Medium

Compute a numerically stable row-wise log-sum-exp in Triton. For each row iii of the input:

out[i]=log⁡∑j=0N−1exp⁡(x[i,j])\texttt{out}[i] = \log \sum_{j=0}^{N-1} \exp(x[i, j])out[i]=logj=0∑N−1​exp(x[i,j])

Direct evaluation overflows for large logits. The stable form subtracts the row max before exponentiating and adds it back after the log:

out[i]=mi+log⁡∑j=0N−1exp⁡(x[i,j]−mi),mi=max⁡jx[i,j]\texttt{out}[i] = m_i + \log \sum_{j=0}^{N-1} \exp(x[i, j] - m_i), \qquad m_i = \max_j x[i, j]out[i]=mi​+logj=0∑N−1​exp(x[i,j]−mi​),mi​=jmax​x[i,j]

Pattern: one program per row, structurally identical to fused softmax but with m+log⁡(∑exp⁡(⋅))m + \log(\sum \exp(\cdot))m+log(∑exp(⋅)) instead of dividing by the sum.

Loading visualization...

Examples

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

Output: [1.3863] (which is log⁡4\log 4log4)

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

Output: [3.4076, 1.0986]

Hint 1

Same structure as fused softmax for the reduction half: one program per row, load with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’), compute row_max\text{row\_max}row_max then ∑exp⁡(row−row_max)\sum \exp(\text{row} - \text{row\_max})∑exp(row−row_max).

Hint 2

The final scalar is lse=row_max+log⁡(s)\text{lse} = \text{row\_max} + \log(s)lse=row_max+log(s). Adding back the max is what restores the value lost during the stability shift.

Hint 3

The output is one scalar per row, so the store is a single scalar per program; no column mask needed on the write.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Compute the stable form by subtracting the row max before exponentiating and adding it back after the log
  • Mask the column tail so masked lanes do not affect the row max or the sum
  • 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 shape (M,)(M,)(M,).
  • Tolerance: atol=1e-3. The max-subtract identity is exact up to float32; no large-NNN widening is required for these shapes.
Try Similar Problems
Single-Pass Mean and Variance in TritonMediumFused Row-Wise Softmax in TritonMediumVector Sum Reduction in TritonEasyVector Max Reduction in TritonEasyL2 Vector Norm in TritonEasyScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumRotary Position Embedding in TritonMediumLayerNorm Forward in TritonMediumOnline Softmax Kernel in TritonMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Row-Wise LogSumExp in Triton

Row-Wise LogSumExp in Triton

GPU Programming
Medium

Compute a numerically stable row-wise log-sum-exp in Triton. For each row iii of the input:

out[i]=log⁡∑j=0N−1exp⁡(x[i,j])\texttt{out}[i] = \log \sum_{j=0}^{N-1} \exp(x[i, j])out[i]=logj=0∑N−1​exp(x[i,j])

Direct evaluation overflows for large logits. The stable form subtracts the row max before exponentiating and adds it back after the log:

out[i]=mi+log⁡∑j=0N−1exp⁡(x[i,j]−mi),mi=max⁡jx[i,j]\texttt{out}[i] = m_i + \log \sum_{j=0}^{N-1} \exp(x[i, j] - m_i), \qquad m_i = \max_j x[i, j]out[i]=mi​+logj=0∑N−1​exp(x[i,j]−mi​),mi​=jmax​x[i,j]

Pattern: one program per row, structurally identical to fused softmax but with m+log⁡(∑exp⁡(⋅))m + \log(\sum \exp(\cdot))m+log(∑exp(⋅)) instead of dividing by the sum.

Loading visualization...

Examples

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

Output: [1.3863] (which is log⁡4\log 4log4)

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

Output: [3.4076, 1.0986]

Hint 1

Same structure as fused softmax for the reduction half: one program per row, load with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’), compute row_max\text{row\_max}row_max then ∑exp⁡(row−row_max)\sum \exp(\text{row} - \text{row\_max})∑exp(row−row_max).

Hint 2

The final scalar is lse=row_max+log⁡(s)\text{lse} = \text{row\_max} + \log(s)lse=row_max+log(s). Adding back the max is what restores the value lost during the stability shift.

Hint 3

The output is one scalar per row, so the store is a single scalar per program; no column mask needed on the write.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Compute the stable form by subtracting the row max before exponentiating and adding it back after the log
  • Mask the column tail so masked lanes do not affect the row max or the sum
  • 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 shape (M,)(M,)(M,).
  • Tolerance: atol=1e-3. The max-subtract identity is exact up to float32; no large-NNN widening is required for these shapes.
Try Similar Problems
Single-Pass Mean and Variance in TritonMediumFused Row-Wise Softmax in TritonMediumVector Sum Reduction in TritonEasyVector Max Reduction in TritonEasyL2 Vector Norm in TritonEasyScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumRotary Position Embedding in TritonMediumLayerNorm Forward in TritonMediumOnline Softmax Kernel in TritonMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1