TensorTonic
Problems
Vector Sum Reduction in Triton
GPU Programming
Easy

Implement a 1D sum reduction in Triton. For a float tensor xxx of length NNN, write the scalar sum into out[0]\texttt{out}[0]out[0]:

out[0]=∑i=0N−1x[i]\texttt{out}[0] = \sum_{i=0}^{N-1} x[i]out[0]=i=0∑N−1​x[i]

This is the canonical Triton reduction. Each program covers one tile, reduces its lanes in registers, and combines partial sums across programs with a single atomic add on out[0]\texttt{out}[0]out[0]. Because atomics accumulate, the host must zero out\texttt{out}out before launch.

Loading visualization...

Examples

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

Output: [10.0]

Input: x = [0.0, -1.0, 2.5, 1.5, -3.0]

Output: [0.0]

Hint 1

Each program reduces its lanes with tl.sum\texttt{tl.sum}tl.sum along the tile axis. The masked tail must load other=0.0\texttt{other=0.0}other=0.0 so it does not poison the partial.

Hint 2

Combine partials with \texttt{tl.atomic_add} to a single-element output. One atomic per program is far cheaper than one atomic per lane.

Hint 3

Remember to call out.zero_()\texttt{out.zero\_()}out.zero_() in the launcher before the kernel; atomics accumulate into whatever value is already there.

Requirements

  • Use one Triton program per tile and reduce each tile in registers
  • Combine partial sums across programs with a single atomic add per program
  • Pre-zero out\texttt{out}out on the host so atomics start from a clean slate
  • Mask the tail so out-of-range lanes contribute zero
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1061 \le N \le 10^61≤N≤106.
  • Input tensor is torch.float32 on cuda and contiguous; output has shape (1,).
  • Tolerance for large NNN: combined atol=1e-2, rtol=1e-2 to absorb float32 accumulation drift.
Try Similar Problems
Vector Max Reduction in TritonEasyL2 Vector Norm in TritonEasyRow-Wise LogSumExp in TritonMediumSingle-Pass Mean and Variance in TritonMediumFused Row-Wise Softmax in TritonMediumDropout (Inverted Scaling) in TritonEasyVector Addition in TritonEasyKV Cache Append in TritonEasyFused Multiply-Add in TritonEasyGELU in TritonEasyReLU in TritonEasySiLU in TritonEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Vector Sum Reduction in Triton

Vector Sum Reduction in Triton

GPU Programming
Easy

Implement a 1D sum reduction in Triton. For a float tensor xxx of length NNN, write the scalar sum into out[0]\texttt{out}[0]out[0]:

out[0]=∑i=0N−1x[i]\texttt{out}[0] = \sum_{i=0}^{N-1} x[i]out[0]=i=0∑N−1​x[i]

This is the canonical Triton reduction. Each program covers one tile, reduces its lanes in registers, and combines partial sums across programs with a single atomic add on out[0]\texttt{out}[0]out[0]. Because atomics accumulate, the host must zero out\texttt{out}out before launch.

Loading visualization...

Examples

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

Output: [10.0]

Input: x = [0.0, -1.0, 2.5, 1.5, -3.0]

Output: [0.0]

Hint 1

Each program reduces its lanes with tl.sum\texttt{tl.sum}tl.sum along the tile axis. The masked tail must load other=0.0\texttt{other=0.0}other=0.0 so it does not poison the partial.

Hint 2

Combine partials with \texttt{tl.atomic_add} to a single-element output. One atomic per program is far cheaper than one atomic per lane.

Hint 3

Remember to call out.zero_()\texttt{out.zero\_()}out.zero_() in the launcher before the kernel; atomics accumulate into whatever value is already there.

Requirements

  • Use one Triton program per tile and reduce each tile in registers
  • Combine partial sums across programs with a single atomic add per program
  • Pre-zero out\texttt{out}out on the host so atomics start from a clean slate
  • Mask the tail so out-of-range lanes contribute zero
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1061 \le N \le 10^61≤N≤106.
  • Input tensor is torch.float32 on cuda and contiguous; output has shape (1,).
  • Tolerance for large NNN: combined atol=1e-2, rtol=1e-2 to absorb float32 accumulation drift.
Try Similar Problems
Vector Max Reduction in TritonEasyL2 Vector Norm in TritonEasyRow-Wise LogSumExp in TritonMediumSingle-Pass Mean and Variance in TritonMediumFused Row-Wise Softmax in TritonMediumDropout (Inverted Scaling) in TritonEasyVector Addition in TritonEasyKV Cache Append in TritonEasyFused Multiply-Add in TritonEasyGELU in TritonEasyReLU in TritonEasySiLU 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