Expand description
CPU forward-pass primitives: RMS-norm, SiLU, sparse SwiGLU FFN.
Functions§
- add_
rmsnorm_ fused_ into - Fused residual addition and RMSNorm into a caller-owned buffer:
h += delta, thenout = rms_norm(h, weight). This avoids a separate pass overhfor the residual addition and sum of squares. - erf_f32
- erf(x) by Abramowitz–Stegun 7.1.26, |error| ≤ 1.5e-7.
- gelu_
erf - SiLU activation function.
tanh-approximated GELU (Gemma’s GeGLU gate; HF
gelu_pytorch_tanh). Exact (erf) GELU,0.5·x·(1 + erf(x/√2))— HFhidden_act = "gelu". erf by Abramowitz–Stegun 7.1.26 (|error| ≤ 1.5e-7), the same formula the GPU kernels use (WGSL and MSL have no erf). - gelu_
tanh - rms_
norm - RMS normalization with explicit weight semantics.
- rms_
norm_ into rms_normwriting into a caller-owned buffer — the decode hot path calls this twice per layer; the returning variant allocated each time.- silu
- sparse_
ffn_ forward - Sparse SwiGLU FFN: compute only
active_indicesneurons. - sparse_
ffn_ forward_ pair - Fused two-position sparse FFN: gate/up/down weight rows are streamed from memory ONCE for both positions. Bit-identical to two single calls (per-position accumulation order is unchanged).