Triton Autotuned Vector Add
Problem Statement
Add two vectors but let Triton pick the best BLOCK_SIZE using @triton.autotune.
Background
@triton.autotune benchmarks a list of triton.Configs (keyed on input sizes) and caches the fastest. The grid becomes a lambda over meta so it can read the chosen BLOCK_SIZE.
Your Task
Decorate add_kernel with @triton.autotune over at least two block sizes and implement run(n=4096) that launches with a meta-aware grid.
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 = 4096
True
n = 4096is the length of two random float vectors and ; the target is for every index from to .- Each kernel instance handles one contiguous block of =
BLOCK_SIZEelements (instance covers to ). So the launch needs instances, and that count depends on which wins the benchmark. - With there are instances; with there are . Since , each index is covered once.
- The second test, , is not a multiple of either size: gives instances and gives , both spanning slots, so the last slots lie past the end of the vectors and must contribute nothing.
- The output
Truesays every equals to within . Notice that changes speed, never the values.
Constraints
- Use @triton.autotune with configs of differing BLOCK_SIZE, key=['n']
- grid = lambda meta: (triton.cdiv(n, meta['BLOCK_SIZE']),)
- BLOCK_SIZE is supplied by the config, not passed explicitly