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
n = 1024
True
- The input
n = 1024is the length of a 1-D vector of random standard-normal floats that lives on the GPU, plus an output vector of the same length waiting to be filled. - Each output element is the square of the matching input: for . For example gives and gives .
- Notice that every output is non-negative whatever the sign of , and each position depends only on its own input, so all positions can be computed independently and in parallel.
- The work is split into fixed-size blocks of positions. When is not a multiple of the block size (for instance ), the last block reaches past index ; those positions must be neither read nor written, or memory outside the vectors gets touched.
- The result
Truemeans every computed value matches the PyTorch reference to within an absolute tolerance of . A single skipped or wrong element would make the check fail.
Constraints
- out = x * x
- Mask the tail block