v9.1.1
Menu

Triton Square Kernel

easyPROTriton FundamentalsTriton Programming

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 n = 1024 is the length of a 1-D vector xx 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: outi=xi⋅xi=xi2\text{out}_i = x_i \cdot x_i = x_i^2 for i=0,…,n−1i = 0, \dots, n-1. For example xi=−1.5x_i = -1.5 gives 2.252.25 and xi=0.3x_i = 0.3 gives 0.090.09.
  • Notice that every output is non-negative whatever the sign of xix_i, and each position depends only on its own input, so all nn positions can be computed independently and in parallel.
  • The work is split into fixed-size blocks of positions. When nn is not a multiple of the block size (for instance n=333n = 333), the last block reaches past index n−1n-1; those positions must be neither read nor written, or memory outside the vectors gets touched.
  • The result True means every computed value matches the PyTorch reference xi2x_i^2 to within an absolute tolerance of 10−510^{-5}. A single skipped or wrong element would make the check fail.

Constraints

  • out = x * x
  • Mask the tail block

Learn this in

More from Triton Programming

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.