Tensara Logo

tensara

L2 Normalization

EASY

Implement L2 Normalization for a 2D tensor. L2 normalization is a technique where each row of the input tensor is normalized by the Euclidean (L2) norm of its elements.

The formula for L2 Normalization is:

y=xxi2\text{y} = \frac{x}{\sqrt{\sum x_i^2}}

where the sum of squared values is computed across the second dimension (D) for each element in the first dimension (B).

Input:

  • Tensor X\text{X} of shape (B,D)(\text{B}, \text{D}) (input data)

Output:

  • Tensor Y\text{Y} of shape (B,D)(\text{B}, \text{D}) (normalized data)

Notes:

  • For numerical stability, you may need to add a small epsilon ϵ=1010\epsilon = 10^{-10} to the denominator to avoid division by zero.
  • After normalization, the L2 norm of each row should be approximately 1.0.

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.