v9.1.1
Menu

Triton Fused Add + ReLU Kernel

mediumPROElementwise & Fused OpsTriton Programming

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

Input
n = 1024
Output
True
Reasoning
  • The input n = 1024 determines the size of the input tensors x and y.
  • The add_relu_kernel function is launched, which computes out = max(x + y, 0) for each element in the tensors, fusing the addition and ReLU operations into a single kernel.
  • The result triton_out is compared to the reference output torch_reference computed using torch.relu(x + y).
  • The run function returns True if triton_out is close to torch_reference according to torch.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

Learn this in

More from Triton Programming

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.