Tensara Logo

tensara

Batch Normalization

MEDIUM

Implement Batch Normalization over the batch dimension (B) for each feature channel in a 4D tensor.

The formula for Batch Normalization is:

y=xE[x]Var[x]+ϵ\text{y} = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}}

where the mean E[x]\mathrm{E}[x] and variance Var[x]\mathrm{Var}[x] are computed over the batch dimension (B) for each feature channel independently. ϵ\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)
  • 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 batch dimension B\text{B} independently for each feature channel F\text{F}.
  • The statistics (mean and variance) are computed independently for each spatial location (D1,D2)(D1, D2) in each feature channel.
  • Use ϵ=105\epsilon = 10^{-5}
  • For simplicity, this implementation focuses on the core normalization without learnable parameters (gamma and beta) and without tracking running statistics.
  • 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.