rmsNorm function

void rmsNorm(
  1. Float32List x,
  2. Float32List weight,
  3. Float32List out,
  4. int xOffset,
  5. int outOffset,
  6. int n,
  7. 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];
  }
}