Tensor and numerical foundations · Medium

RMSNorm

Problem 4 of 50 · Python and NumPy

Problem

Implement RMSNorm over the last dimension of a two-dimensional activation matrix: y = x * weight / sqrt(mean(x²) + eps). Do not subtract the mean. Return the same shape as x.

def rms_norm(x, weight, eps):

Constraints

  • x has shape N × D and weight has D values; 1 ≤ N,D ≤ 16.
  • eps ≥ 0; each row has positive mean(x²)+eps; all inputs are finite.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1,
      -1
    ]
  ],
  [
    1,
    1
  ],
  0
]
Expected result: [
  [
    1,
    -1
  ]
]

Each row has mean square 1, so [1,-1] is unchanged.

Example 2

Arguments: [
  [
    [
      0,
      0
    ]
  ],
  [
    2,
    3
  ],
  0.25
]
Expected result: [
  [
    0,
    0
  ]
]

The denominator is sqrt(0.25), but multiplying a zero row still returns zeros.

Sign in to solve this problem

Browse all coding problems · Learn the foundations