Triton Fused Multiply-Add Kernel
Problem Statement
Implement a fused multiply-add kernel: out = a * x + b * y for two 1D tensors and two runtime scalars a, b.
Background
Fusing multiple elementwise operations into one kernel means each element is read and written exactly once, saving memory bandwidth versus separate multiply and add passes.
Your Task
Implement fma_kernel and run(n=1024, a=2.0, b=-1.0).
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, a = 2.0, b = -1.0
True
- Inputs:
n = 1024is the length of two random vectors and ;a = 2.0andb = -1.0are plain scalars that weight them. - Math: every position is independent, . With the sample scalars that is , so if and then .
- Fusion: each and is read once and each written once, with no intermediate vectors for or .
- Tiling: if each program instance handles a block of elements, you need blocks. For and that is 2 blocks, and the second has only real elements; the other 548 positions must not be read or written.
- Output:
Truemeans your matches to within an absolute tolerance of . Notice the scalars can be negative or fractional, so they are runtime values, not constants.
Constraints
- Single kernel computing ax + by
- Two tensor pointers, two scalar args
- Mask the tail block