beetlebug▾
/

ztcl::tensor rmsnorm

Gives the root-mean-square norm of each row, times a weight row.

ztcl::tensor rmsnorm X W EPS

Description

Gives each row x of X times r, then times W, the row of weights with the size of the last axis of X. r is 1 / sqrt(m + EPS) with m the mean of the squares of x. The bits are those of mul [mul $x $r] $w with r from expr {1.0 / sqrt([ztcl::tensor item [ztcl::tensor mean [ztcl::tensor square $x]]] + $eps)}: each square in the dtype, the mean in f64 rounded to the dtype, r in f64 rounded to the dtype.

Returns

A tensor of the shape of X.

Examples

Each example of this page runs after package require ztcl::tensor.

% ztcl::tensor rmsnorm [ztcl::tensor of {{3 4}}] [ztcl::tensor of {1 2}] 0
tensor f32 {1 2} {0.84852815 2.2627418}

Errors

% ztcl::tensor rmsnorm [ztcl::tensor zeros {1 3}] [ztcl::tensor zeros 2] 1e-5
tensor rmsnorm: the shapes {1 3} and {2} do not match: W is one row of X
-errorcode ZTCL TENSOR

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.4
Compute cost
A cost for each element operation by its kind (a copy, an add, a division, a math function, a multiply-add of a matmul), set so that one compute unit takes about the time of one unit of plain Tcl. docs/modules/tensor.md has the table.