📘
Triton Scalar Multiply Kernel
Problem Statement
Write a Triton kernel that multiplies every element of a 1D tensor by a runtime scalar: out = x * scale.
Background
Scalars can be passed as ordinary kernel arguments (no pointer needed) and used directly inside the kernel.
Your Task
Implement mul_kernel and run(n=1024, scale=3.0) that returns whether the output equals x * scale.
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, scale = 3.0
Output:
True
Reasoning:
- The input values are
n = 1024andscale = 3.0, which are used to allocate a 1D tensor of lengthnand a scalar valuescale. - The
mul_kernelfunction is launched, which multiplies every element of the input tensorxby thescalevalue, resulting in an output tensoroutwhere each element is calculated as outi=xi⋅scale. - The output tensor
outis then compared to a reference tensor calculated using PyTorch, where each element is also xi⋅scale, using thetorch.allclosefunction with a small tolerance. - The comparison returns
Trueif all elements of the output tensor are close to the corresponding elements of the reference tensor, indicating that the Triton kernel produced the correct result.
Constraints:
- Pass scale as a plain kernel argument
- Mask out-of-bounds lanes
- out = x * scale
Editor
Python 3.13.1
GPU · T4
Test Results
0/0Run code to see test results.