PIXELBANKv9.1.0
Menu

Problem Statement

Modern VLMs RMS-normalize vision features before projecting them into the LM. Implement RMSNorm with a learnable per-feature scale.

Background

RMSNorm (Zhang & Sennrich, 2019) rescales a vector by its root-mean-square, with no mean subtraction:

RMSNorm(x)i=xi1D∑jxj2+ϵ  gi\text{RMSNorm}(x)_i = \frac{x_i}{\sqrt{\tfrac{1}{D}\sum_j x_j^2 + \epsilon}}\; g_i

where g is a learned gain vector (length D) and \epsilon guards against division by zero. Unlike LayerNorm it keeps the direction's sign and is cheaper — which is why LLaMA-family LMs and their VLM wrappers use it.

Your Task

Implement:

def rmsnorm(x, g, eps=1e-6):

Return the normalized vector as a list rounded to 4 decimals.

Input Format

  • x: list of D floats.
  • g: list of D gains.
  • eps (float).

Output Format

  • A list of D floats rounded to 4 decimals.

Sample

print(rmsnorm([1.0, 2.0, 2.0, 4.0], [1.0, 1.0, 1.0, 1.0]))

Output:

[0.4, 0.8, 0.8, 1.6]

Example:

Input:
print(rmsnorm([1.0, 2.0, 2.0, 4.0], [1.0, 1.0, 1.0, 1.0]))
Output:
[0.4, 0.8, 0.8, 1.6]
Reasoning:
  • Compute the mean square of the input vector x=[1.0,2.0,2.0,4.0]x = [1.0, 2.0, 2.0, 4.0] to determine the scaling factor: ms=12+22+22+424=1+4+4+164=254=6.25\text{ms} = \frac{1^2 + 2^2 + 2^2 + 4^2}{4} = \frac{1 + 4 + 4 + 16}{4} = \frac{25}{4} = 6.25.
  • Calculate the normalization denominator by adding the epsilon ϵ=10−6\epsilon = 10^{-6} to the mean square and taking the square root: 6.25+0.000001≈6.250001≈2.5000002\sqrt{6.25 + 0.000001} \approx \sqrt{6.250001} \approx 2.5000002.
  • Divide each element of xx by this denominator to normalize the vector, preserving the original direction: [1.0/2.5,2.0/2.5,2.0/2.5,4.0/2.5]≈[0.4,0.8,0.8,1.6][1.0/2.5, 2.0/2.5, 2.0/2.5, 4.0/2.5] \approx [0.4, 0.8, 0.8, 1.6].
  • Multiply the normalized values by the gain vector g=[1.0,1.0,1.0,1.0]g = [1.0, 1.0, 1.0, 1.0]; since all gains are 1, the values remain unchanged: [0.4â‹…1,0.8â‹…1,0.8â‹…1,1.6â‹…1]=[0.4,0.8,0.8,1.6][0.4 \cdot 1, 0.8 \cdot 1, 0.8 \cdot 1, 1.6 \cdot 1] = [0.4, 0.8, 0.8, 1.6].
  • The final output is [0.4, 0.8, 0.8, 1.6]

Constraints:

  • len(x) == len(g), 1 <= D <= 8192.
  • Divide by sqrt(mean(x^2) + eps); no mean subtraction.
  • Multiply by the gain elementwise; round to 4 decimals; avoid -0.0.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
RMSNorm Before the Projector - Medium | PixelBank