Compute the L2 (Euclidean) norm of a 1D float tensor in Triton. For a tensor x of length N, write the scalar norm into out[0]:
out[0]=i=0∑N−1x[i]2The reduction is a sum-of-squares; the square root is applied once on the host after the kernel returns. Each program reduces its tile in registers, combines partials via a single atomic per program on a scratch scalar, and the launcher takes the square root.
Input: x = [3.0, 4.0]
Output: [5.0]
Input: x = [1.0, 2.0, 2.0]
Output: [3.0]
Square inside the tile, not afterwards. Fusing the multiply into the reduction with tl.sum(x * x) halves the register pressure.
Mask with other=0.0 so the tail lanes contribute zero. The square of zero is still zero, so no special handling at the boundary.
The square root is a single host op after the kernel: out.copy_(torch.sqrt(sumsq_buf)). Keep the per-lane work to the reduction only.
torch.float32 on cuda and contiguous; output has shape (1,).atol=1e-2, rtol=1e-2 to absorb large-N accumulation drift.Accepts: array
Submit a correct solution to see your kernel performance.
Compute the L2 (Euclidean) norm of a 1D float tensor in Triton. For a tensor x of length N, write the scalar norm into out[0]:
out[0]=i=0∑N−1x[i]2The reduction is a sum-of-squares; the square root is applied once on the host after the kernel returns. Each program reduces its tile in registers, combines partials via a single atomic per program on a scratch scalar, and the launcher takes the square root.
Input: x = [3.0, 4.0]
Output: [5.0]
Input: x = [1.0, 2.0, 2.0]
Output: [3.0]
Square inside the tile, not afterwards. Fusing the multiply into the reduction with tl.sum(x * x) halves the register pressure.
Mask with other=0.0 so the tail lanes contribute zero. The square of zero is still zero, so no special handling at the boundary.
The square root is a single host op after the kernel: out.copy_(torch.sqrt(sumsq_buf)). Keep the per-lane work to the reduction only.
torch.float32 on cuda and contiguous; output has shape (1,).atol=1e-2, rtol=1e-2 to absorb large-N accumulation drift.Accepts: array
Submit a correct solution to see your kernel performance.