src/features/rmsnorm.c file

Functions

static void rmsnorm_row(vm_float_t* y, const vm_float_t* x, const vm_float_t* weight, const vm_float_t* residual, vm_float_t* residual_out, const int cols, const vm_float_t epsilon)
One row.
auto vm_rmsnorm(vm_float_t* y, const vm_float_t* x, const vm_float_t* weight, const vm_float_t* residual, vm_float_t* residual_out, int rows, int cols, vm_float_t epsilon) -> bool
RMSNorm of each row, with an optional weight and pre-add.

Function documentation

static void rmsnorm_row(vm_float_t* y, const vm_float_t* x, const vm_float_t* weight, const vm_float_t* residual, vm_float_t* residual_out, const int cols, const vm_float_t epsilon)

One row.

The sum of squares is finished before y is written.

y may exactly alias x. Each residual element is read before that slot of residual_out is stored, so residual_out may exactly alias residual.

bool vm_rmsnorm(vm_float_t* y, const vm_float_t* x, const vm_float_t* weight, const vm_float_t* residual, vm_float_t* residual_out, int rows, int cols, vm_float_t epsilon)

RMSNorm of each row, with an optional weight and pre-add.

Parameters
y Output; length rows * cols. May exactly alias x. Unchanged on failure.
x
weight Scale; length cols, shared by every row. NULL skips it.
residual Pre-add; length rows * cols. NULL skips it.
residual_out Sum x + residual when this and residual are both set. May exactly alias residual. Unchanged on failure.
rows Row count. Must be positive.
cols Row length. Must be positive.
epsilon Added inside the square root. Must be greater than 0.
Returns false when a pointer, a size, or epsilon is invalid.

x and y are row-major, rows by cols, and cols is contiguous. Each row is

y = (v / sqrt(mean(v^2) + epsilon)) * weight

where v is x, or x + residual when residual is set. A null weight skips the scale. A null residual skips the add. When both residual and residual_out are set, that sum is written through residual_out. When residual is null, residual_out is not read or written.

weight has length cols and is the same on every row. residual and residual_out have length rows * cols. y may exactly alias x. residual_out may exactly alias residual. Any other overlap is caller error. No allocation. Forward only.

A null y or x, rows <= 0, cols <= 0, or an epsilon that is not greater than 0 (zero, negative, or NaN) returns false and does not write y or residual_out.

On finite inputs whose squares, their sum, and the root stay finite, each output differs from the same steps in IEEE-754 double by less than 32 * cols * VECMAT_EPSILON * (1 + |y|) absolute. A written residual_out stays within that bound of x + residual.