Tensara Logo

tensara

Layer Normalization

MEDIUM

Implement Layer Normalization over the last 3 dimensions (F, D1, D2) of a 4D tensor.

The formula for Layer Normalization is:

y=xE[x]Var[x]+ϵγ+β\text{y} = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta

where the mean E[x]\mathrm{E}[x] and variance Var[x]\mathrm{Var}[x] are computed over the normalization dimensions (F, D1, D2) for each element in the first dimension (B). γ\gamma and β\beta are learnable affine parameters (elementwise scale and shift), and ϵ\epsilon is a small value added to the variance for numerical stability.

Input:

  • Tensor X\text{X} of shape (B,F,D1,D2)(\text{B}, \text{F}, \text{D1}, \text{D2}) (input data)
  • Vector gamma\text{gamma} of shape (F,D1,D2)(\text{F}, \text{D1}, \text{D2}) (scale parameters)
  • Vector beta\text{beta} of shape (F,D1,D2)(\text{F}, \text{D1}, \text{D2}) (shift parameters)
  • Epsilon ϵ\epsilon (a small float, typically 1e-5)

Output:

  • Tensor Y\text{Y} of shape (B,F,D1,D2)(\text{B}, \text{F}, \text{D1}, \text{D2}) (normalized data)

Notes:

  • Compute the mean and variance across the last three dimensions (F,D1,D2)(\text{F}, \text{D1}, \text{D2}) independently for each sample in the batch B\text{B}.
  • Apply the normalization using the computed mean/variance and the provided γ\gamma and β\beta.
  • Use ϵ=105\epsilon = 10^{-5}
  • This problem is adapted from KernelBench

GPU Type

Language

Data Type

Loading...

Loading editor...

CUDA C++ environment

Sample Run Results

Hit "Run" to test your code with sample inputs

Desktop Required for Code Submission

For the best coding experience, please switch to a desktop device to write and submit your solution.