Tensor and numerical foundations · Hard

RMSNorm Backward Pass

Problem 9 of 50 · Python and NumPy

Problem

Given upstream derivative dy for y = x * weight / sqrt(mean(x²)+eps), return {"dx": gradient with respect to x, "dweight": gradient with respect to weight}. Sum the shared weight gradient across all rows. Do not numerically differentiate in your submission.

The derivative with respect to x includes the dependence of the denominator on every feature in its row.

def rms_norm_backward(x, weight, dy, eps):

Constraints

  • x and dy are N × D; weight has D entries; 1 ≤ N,D ≤ 8.
  • eps ≥ 0 and every row denominator is positive.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      2
    ]
  ],
  [
    1
  ],
  [
    [
      1
    ]
  ],
  0
]
Expected result: {
  "dx": [
    [
      0
    ]
  ],
  "dweight": [
    1.000000000001
  ]
}

For positive scalar x and eps=0, x/|x| is locally constant, so dx=0 and dweight=1.

Example 2

Arguments: [
  [
    [
      1,
      0
    ]
  ],
  [
    1,
    1
  ],
  [
    [
      0,
      1
    ]
  ],
  0
]
Expected result: {
  "dx": [
    [
      0,
      1.4142135623023844
    ]
  ],
  "dweight": [
    0,
    0
  ]
}

The upstream signal is perpendicular to [1,0], giving dx=[0,sqrt(2)] and zero weight gradients.

Sign in to solve this problem

Browse all coding problems · Learn the foundations