Triton Vector Addition Kernel
Problem Statement
Write a Triton kernel that adds two 1D float tensors element-wise: out = x + y.
Background
A Triton program instance handles one block of BLOCK_SIZE elements. Use tl.program_id(0) to get the block index, build per-element offsets with tl.arange, guard out-of-bounds lanes with a mask, then tl.load / tl.store.
Your Task
Implement add_kernel and a run(n=1024) that launches it over a 1D grid and returns whether the result matches 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
n = 1024is the length of the two random float vectors and ; the target is for every index .- The vector is cut into blocks of
BLOCK_SIZEelements and each program instance owns one block: program covers indices . - With , the number of programs is , and program covers indices to — exactly the whole vector, so every lane is a real element.
- For there is still program, but its last lanes (indices –) point past the end and must neither read nor write; for there are programs and no spare lanes.
- The output
Truemeans every computed agrees with to within a tiny tolerance (about ). Notice that a single wrongly covered or out-of-range index would make the comparison fail.
Constraints
- Use @triton.jit and tl.program_id(0)
- offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
- Mask out-of-bounds lanes with mask = offsets < n
- Launch grid = (triton.cdiv(n, BLOCK_SIZE),)