Skip to content
fn

layer_norm_forward_f32

void layer_norm_forward_f32(const float * x, const float * gamma, const float * beta, float * y, float * saved_mean, float * saved_rstd, int outer, int N, double eps)

Single-precision LayerNorm forward — normalises every row of length N using its empirical mean and variance, then applies a per-feature affine transform γx^+β\gamma \hat{x} + \beta in the same pass.

The f32 path is a vDSP fast path: it computes the row mean with vmean_f32, centres the row into a scratch buffer with vsadd_f32, derives the variance as a self-dot-product via vdotpr_f32, then fuses the affine into a vmadd_f32 call so the data is touched twice rather than three times. Per-row mean and rstd = 1/sqrt(var + eps) are stashed so the backward kernel can skip the recomputation.

Math

y_i = \gamma_i \hat{x}_i + \beta_i,$$ with $\mu, \sigma^2$ the row mean and variance of `x`. **References** [Ba, Kiros & Hinton, "Layer Normalization" ([arXiv:1607.06450](https://arxiv.org/abs/1607.06450), 2016)](https://arxiv.org/abs/1607.06450).

Parameters

xconst float*
Input buffer of shape (outer, N) in row-major order.
gamma, betaconst float*
Per-feature affine parameters of shape (N,). Both required.
yfloat*
Output buffer of shape (outer, N); written densely.
saved_mean, saved_rstdfloat*
Per-row scratch outputs of shape (outer,). Consumed by layer_norm_backward_f32.
outerstd::size_t
Number of independent rows (product of all leading dimensions).
Nstd::size_t
Length of the normalised axis.
epsdouble
Variance stabiliser; cast to float before being added to var.