PIXELBANKv9.1.0
Menu

Problem Statement

Implement the forward pass of LayerNorm over the last dimension of a (M, N) tensor: subtract the row mean, divide by the row standard deviation, then scale by weight and shift by bias.

Background

Per row: mean = sum(x)/N, var = sum((x-mean)^2)/N, rstd = 1/sqrt(var + eps), out = (x-mean)rstdweight + bias. Mask invalid columns to 0 before the variance sum.

Your Task

Implement layernorm_kernel and run(M=64, N=256, eps=1e-5) comparing to torch.nn.functional.layer_norm.

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:
M = 64, N = 256, eps = 1e-5
Output:
True
Reasoning:
  • The input values are used to launch the layernorm_kernel function on a GPU, which implements the forward pass of LayerNorm over the last dimension of a (M, N) tensor.
  • For each row, the mean is calculated as mean=โˆ‘(x)/Nmean = \sum(x)/N, and the var is calculated as var=โˆ‘((xโˆ’mean)2)/Nvar = \sum((x-mean)^2)/N, masking invalid columns to 0 before the variance sum.
  • The row standard deviation rstd is then calculated as rstd=1/var+epsrstd = 1/\sqrt{var + eps}, and the output out is computed as out=(xโˆ’mean)โˆ—rstdโˆ—weight+biasout = (x-mean)*rstd*weight + bias.
  • The output of the layernorm_kernel function is compared to the output of torch.nn.functional.layer_norm using torch.allclose, which returns True if the two outputs are close enough, indicating that the implementation is correct.

Constraints:

  • Compute mean and variance per row using tl.sum
  • rstd = 1 / tl.sqrt(var + eps)
  • out = (x - mean) * rstd * weight + bias
๐Ÿ”’

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

0/0
Run code to see test results.