Triton Fused Add + ReLU Kernel
Problem Statement
Fuse a residual add and a ReLU into one kernel: out = max(x + y, 0).
Background
Fusing avoids materializing the intermediate x + y to global memory — one read of each input, one write of the output.
Your Task
Implement add_relu_kernel and run(n=1024) comparing to torch.relu(x + y).
How it is tested
Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.
Example
n = 1024
True
- The input
n = 1024determines the size of the input tensorsxandy. - The
add_relu_kernelfunction is launched, which computesout = max(x + y, 0)for each element in the tensors, fusing the addition and ReLU operations into a single kernel. - The result
triton_outis compared to the reference outputtorch_referencecomputed usingtorch.relu(x + y). - The
runfunction returnsTrueiftriton_outis close totorch_referenceaccording totorch.allclose, indicating that the custom kernel produces the same result as the PyTorch reference implementation.
Constraints
- Compute x + y then tl.maximum(., 0.0) without a second kernel
- Mask the tail block