rmsNorm function
void
rmsNorm(
- Float32List x,
- Float32List weight,
- Float32List out,
- int xOffset,
- int outOffset,
- int n,
- double eps,
RMSNorm of x[xOffset, xOffset + n) into out[outOffset, ...). The reference rounds x * rsqrt(...) to float32 before multiplying by the weight, so this does too.
Implementation
void rmsNorm(
Float32List x,
Float32List weight,
Float32List out,
int xOffset,
int outOffset,
int n,
double eps,
) {
var sumSquares = 0.0;
for (var j = 0; j < n; j++) {
final v = x[xOffset + j];
sumSquares += v * v;
}
final r = 1 / math.sqrt(sumSquares / n + eps);
for (var j = 0; j < n; j++) {
out[outOffset + j] = x[xOffset + j] * r;
out[outOffset + j] = weight[j] * out[outOffset + j];
}
}