📘
Triton Square Kernel
Problem Statement
Implement an elementwise square kernel: out = x * x.
Background
A warm-up for elementwise math in registers. Compute the product of the loaded value with itself and store it.
Your Task
Implement square_kernel and run(n=1024).
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
Output:
True
Reasoning:
- The input value
n = 1024determines the size of the input arrayx. - The
square_kernelfunction computes the elementwise square ofx, resulting in an output arrayoutwhere each element is xi⋅xi=xi2. - The
runfunction allocates the input arrayxon the GPU, launches thesquare_kernel, and computes a reference solution using PyTorch. - The final output is
Trueif the Triton outputtriton_outis close to the PyTorch reference solutiontorch_reference, as determined bytorch.allclose.
Constraints:
- out = x * x
- Mask the tail block
Editor
Python 3.13.1
GPU · T4
Test Results
0/0Run code to see test results.