TensorTonic
Problems
Cross Entropy Loss (Mean Reduction) in Triton
Loss FunctionsGPU Programming
Medium

Implement the categorical cross-entropy loss with mean reduction in Triton. Given logits of shape (B,C)(B, C)(B,C) and integer targets of shape (B,)(B,)(B,):

loss=−1B∑i=0B−1(logits[i,ti]−log⁡∑c=0C−1elogits[i,c])\text{loss} = -\frac{1}{B} \sum_{i=0}^{B-1} \left( \text{logits}[i, t_i] - \log \sum_{c=0}^{C-1} e^{\text{logits}[i, c]} \right)loss=−B1​i=0∑B−1​(logits[i,ti​]−logc=0∑C−1​elogits[i,c])

The log-sum-exp must be computed in the numerically stable form, subtracting the row maximum before the exponentials. This is a row-parallel reduction with a cross-program accumulation: one program per row computes the per-row loss and atomic-adds it into a single-element output buffer that the launcher pre-zeros and finalizes by dividing by BBB.

Loading visualization...

Examples

Input: logits = [[2.0, 1.0, 0.1]], target = [0]

Output: [0.4170]

Input: logits = [[1.0, 2.0], [2.0, 1.0]], target = [1, 0]

Output: [0.3133]

Hint 1

Each program is one batch row. Load the row of logits with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’) on the column mask so padded lanes never affect the row max or the sum.

Hint 2

Compute row_max\text{row\_max}row_max first, then lse=log⁡∑exp⁡(row−row_max)+row_max\text{lse} = \log \sum \exp(\text{row} - \text{row\_max}) + \text{row\_max}lse=log∑exp(row−row_max)+row_max. The target logit is a scalar load at index tit_iti​.

Hint 3

Pre-zero the loss buffer on the host. The kernel adds (lse−target_logit)(\text{lse} - \text{target\_logit})(lse−target_logit) per row via one atomic per program; divide by BBB from the launcher after the kernel returns.

Requirements

  • Use one Triton program per row, holding the full row of logits in registers
  • Compute log-sum-exp in the stable form by subtracting the row max before exponentiating
  • Atomic-add each row contribution into a single-element loss buffer, then divide by BBB on the host
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤B≤2561 \le B \le 2561≤B≤256, 2≤C≤10242 \le C \le 10242≤C≤1024.
  • logits and loss_out are torch.float32; target is torch.int32 with each value in [0,C)[0, C)[0,C).
  • Use BLOCK_SIZE as a tl.constexpr chosen as triton.next_power_of_2(C) in the launcher.
  • Comparisons use atol = 1e-2 with rtol = 1e-2 because the final value sums a log-sum-exp across all rows and is sensitive to accumulation order.
Try Similar Problems
LayerNorm Forward in TritonMediumRMSNorm Forward in TritonMediumCross-Entropy Loss as a CUDA KernelMediumLayerNorm Backward in TritonHardDropout (Inverted Scaling) in TritonEasyRow-Wise LogSumExp in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumRotary Position Embedding in TritonMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Cross Entropy Loss (Mean Reduction) in Triton

Cross Entropy Loss (Mean Reduction) in Triton

Loss FunctionsGPU Programming
Medium

Implement the categorical cross-entropy loss with mean reduction in Triton. Given logits of shape (B,C)(B, C)(B,C) and integer targets of shape (B,)(B,)(B,):

loss=−1B∑i=0B−1(logits[i,ti]−log⁡∑c=0C−1elogits[i,c])\text{loss} = -\frac{1}{B} \sum_{i=0}^{B-1} \left( \text{logits}[i, t_i] - \log \sum_{c=0}^{C-1} e^{\text{logits}[i, c]} \right)loss=−B1​i=0∑B−1​(logits[i,ti​]−logc=0∑C−1​elogits[i,c])

The log-sum-exp must be computed in the numerically stable form, subtracting the row maximum before the exponentials. This is a row-parallel reduction with a cross-program accumulation: one program per row computes the per-row loss and atomic-adds it into a single-element output buffer that the launcher pre-zeros and finalizes by dividing by BBB.

Loading visualization...

Examples

Input: logits = [[2.0, 1.0, 0.1]], target = [0]

Output: [0.4170]

Input: logits = [[1.0, 2.0], [2.0, 1.0]], target = [1, 0]

Output: [0.3133]

Hint 1

Each program is one batch row. Load the row of logits with other=-float(’inf’)\texttt{other=-float('inf')}other=-float(’inf’) on the column mask so padded lanes never affect the row max or the sum.

Hint 2

Compute row_max\text{row\_max}row_max first, then lse=log⁡∑exp⁡(row−row_max)+row_max\text{lse} = \log \sum \exp(\text{row} - \text{row\_max}) + \text{row\_max}lse=log∑exp(row−row_max)+row_max. The target logit is a scalar load at index tit_iti​.

Hint 3

Pre-zero the loss buffer on the host. The kernel adds (lse−target_logit)(\text{lse} - \text{target\_logit})(lse−target_logit) per row via one atomic per program; divide by BBB from the launcher after the kernel returns.

Requirements

  • Use one Triton program per row, holding the full row of logits in registers
  • Compute log-sum-exp in the stable form by subtracting the row max before exponentiating
  • Atomic-add each row contribution into a single-element loss buffer, then divide by BBB on the host
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤B≤2561 \le B \le 2561≤B≤256, 2≤C≤10242 \le C \le 10242≤C≤1024.
  • logits and loss_out are torch.float32; target is torch.int32 with each value in [0,C)[0, C)[0,C).
  • Use BLOCK_SIZE as a tl.constexpr chosen as triton.next_power_of_2(C) in the launcher.
  • Comparisons use atol = 1e-2 with rtol = 1e-2 because the final value sums a log-sum-exp across all rows and is sensitive to accumulation order.
Try Similar Problems
LayerNorm Forward in TritonMediumRMSNorm Forward in TritonMediumCross-Entropy Loss as a CUDA KernelMediumLayerNorm Backward in TritonHardDropout (Inverted Scaling) in TritonEasyRow-Wise LogSumExp in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumRotary Position Embedding in TritonMedium

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1