RMSNorm Before the Projector
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:
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
print(rmsnorm([1.0, 2.0, 2.0, 4.0], [1.0, 1.0, 1.0, 1.0]))
[0.4, 0.8, 0.8, 1.6]
- Compute the mean square of the input vector to determine the scaling factor: .
- Calculate the normalization denominator by adding the epsilon to the mean square and taking the square root: .
- Divide each element of by this denominator to normalize the vector, preserving the original direction: .
- Multiply the normalized values by the gain vector ; since all gains are 1, the values remain unchanged: .
- 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.