PIXELBANKv9.1.0
Menu

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:

Input:
n = 4096
Output:
True
Reasoning:
  • The run function is called with the input n = 4096, which represents the size of the vectors to be added.
  • The add_kernel function is decorated with @triton.autotune to benchmark different block sizes and determine the fastest configuration for the given input size.
  • The Triton kernel is launched with the chosen block size, and the vector addition is performed on the GPU, producing the triton_out result.
  • The triton_out result is compared to the torch_reference result using torch.allclose, which checks for element-wise equality within a tolerance, yielding the output True if the results match.

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
solution.py

Test Results

0/0
Run code to see test results.
Triton Autotuned Vector Add - Medium | PixelBank