Archived
ops: rmsnorm_p, gelu_mul, logit_softcap, scale in lockstep across libref/libavx/libr4d + OPS.md rows
Elementwise ops with numeric selftest cases against the f64 oracle (both legs of logit_softcap: bf16 and the f32 band the sampling chain carries). libr4d gains env-gated verification probes alongside them (R4D_VERIFY_OFF recompute-on-host checks; default off — instruments, not features).
This commit is contained in:
1 parent
73620ef00e
commit
c37ce73d72
20 files changed
+2260
-51
No files matched your search
+43
-2
@@ -159,6 +159,7 @@ adding a kernel plugin; "refused at load" names one op and leaves you to find th
|
||||
| op | params | operands |
|
||||
|---|---|---|
|
||||
| `rmsnorm` | `M`(range) `n` `eps`:f64 `dtype` `wadd`:f64/opt `mult`:f64/opt | `x`, `w`w? → `y` (no gain operand: a gain of one) |
|
||||
| `rmsnorm_p` | `M`(range) `n` `eps`:f64 `dtype` | `x` → `y` |
|
||||
| `rmsnorm_add` | `M`(range) `n` `eps`:f64 `dtype` `wadd`:f64/opt | `x`, `residual`, `w`w → `y`, `residual_out` |
|
||||
| `hc_enter` | `M`(range) `n` `hc` `dtype` | `x` → `h` |
|
||||
| `hc_read` | `M`(range) `n` `hc` `lowrank` `eps`:f64 `dtype` `wadd`:f64/opt `inject`/opt `group`/opt `rotate`/opt `mix`/opt | `h`, `w`w, `mix_down`w, `mix_up`w, `inject_w`w? → `x`, `inj`?, `q`?, `scale`?, `rq`?, `rscale`? |
|
||||
@@ -177,10 +178,13 @@ adding a kernel plugin; "refused at load" names one op and leaves you to find th
|
||||
| `conv2d` | `M`(range) `width` `cin` `cout` `kh` `kw` `stride` `pad` `cu_div`/opt `valid_stride`/opt `dtype` | `x`, `w`w, `cu_seqlens`, `valid`? → `y` |
|
||||
| `conv1d_dw` | `M`(range) `n` `k` `cu_div`/opt `dtype` | `x`, `w`w, `cu_seqlens` → `y` |
|
||||
| `rel_sinusoid` | `M`(range) `n` `max_timescale`:f64/opt `dtype` | → `y` |
|
||||
| `gelu_mul` | `M`(range) `n` `dtype` | `gate_up` → `y` (splits `[M,2n]` into gelu·up, tanh approx) |
|
||||
| `gelu` / `silu` | `M`(range) `n` `dtype` | `x` → `y` |
|
||||
| `cast` | `M`(range) `n` `from` `to` | `x` → `y` |
|
||||
| `softmax` | `M`(range) `n` `dtype` | `x` → `y` |
|
||||
| `sigmoid` | `M`(range) `n` `dtype` | `x` → `y` |
|
||||
| `logit_softcap` | `M`(range) `n` `cap`:f64 `dtype` | `x` → `y` |
|
||||
| `scale` | `M`(range) `n` `dtype` | `x`, `s`w → `y` |
|
||||
| `silu` | `M` `n` `dtype` | `x` → `y` |
|
||||
| `fill` | `M`(range) `n` `dtype` `value`:f64/opt | → `y` |
|
||||
|
||||
@@ -190,6 +194,15 @@ names one op.
|
||||
`rmsnorm_add` exists because the residual add and the next norm are one pass in every real
|
||||
implementation. A plugin asks for it first and falls back to `add` + `rmsnorm` (`spec.md` §2.3).
|
||||
|
||||
`rmsnorm_p` is `rmsnorm` with the gain removed: `x * rsqrt(mean(x^2) + eps)` and no `w` at all.
|
||||
Gemma's attention normalises the value with a bare RMS (a weightless norm) while normalising the
|
||||
key with the `k_norm` gain in the same layer, and there is no checkpoint plane to feed the value
|
||||
through a gain -- `rmsnorm` would need a phantom all-zero weight to say the same thing.
|
||||
|
||||
`gelu_mul` is `silu_mul` with the tanh GELU instead of silu -- Gemma's gated FFN is trained with
|
||||
`gelu_pytorch_tanh`, and gated feed-forwards ask for the tanh form rather than the unary `gelu`'s
|
||||
exact erf one. Both split the fused `gate_up` with the gate in the FIRST half.
|
||||
|
||||
`hc_read` / `hc_write` are the GATED RESIDUAL pair, which is what a hyper-connection model has
|
||||
instead of a pre-norm and a residual add. The stream is `hc` times the model width and every block
|
||||
reads a mix of the `hc` sub-streams and writes back into all of them, both gated by data:
|
||||
@@ -429,8 +442,8 @@ that folds dequantisation into its epilogue still receives them.
|
||||
| `attn_dense` | `M`(range) `head_dim` `gqa` `causal` `dtype` `scale`:f64/opt `max_seqlen`/opt `window`/opt | `q`, `k`, `v`, `cu_seqlens` → `out` |
|
||||
| `attn_local_rel` | `M`(range) `head_dim` `n_head` `left` `right` `softcap`:f64 `cu_div`/opt `dtype` | `q`, `k`, `v`, `relk`, `pds`w, `cu_seqlens` → `out` |
|
||||
| `kv_store` | `M`(range) `head_dim` `n_head_kv` `block_size` `kv_dtype` `k_scale`:f64/opt `v_scale`:f64/opt | `k`, `v`, `slot_mapping`, `kv_cache`:inout |
|
||||
| `rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `scale`:f64 `mode` `rotary_dim`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `qkv`:inout, `positions` |
|
||||
| `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `positions` → `cos_sin` |
|
||||
| `rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `scale`:f64 `mode` `rotary_dim`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt `ladder`/opt | `qkv`:inout, `positions` |
|
||||
| `rope_table` | `M`(range) `rot` `theta`:f64 `scale`:f64 `dtype` `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt `head_dim`/opt `ladder`/opt | `positions` → `cos_sin` |
|
||||
| `qk_norm_rope` | `M`(range) `head_dim` `n_head` `n_head_kv` `theta`:f64 `eps`:f64 `scale`:f64/opt `mode`/opt `rotary_dim`/opt `wadd`:f64/opt `v_norm`/opt `sections`/opt `comps`/opt | `qkv`:inout, `positions`, `q_w`w, `k_w`w |
|
||||
|
||||
**`attn_local_rel`** is Gemma 4's audio attention: local, with a relative-position term and a
|
||||
@@ -484,6 +497,16 @@ restarts the frequency ladder in each, `theta^(-2j / (2 s_c))` for the run's `j`
|
||||
vision tower's 2-D rotary over row and column). With `[M]` positions every component is that one
|
||||
position, so `mrope` and `imrope` over a text batch are `neox` exactly.
|
||||
|
||||
**`ladder` names the width the base frequency ladder is measured in.** Absent, or `rotary`, the
|
||||
exponent divides by `rotary_dim`: a partial rotary is a complete rotation of its own width, and
|
||||
every existing model is bit-for-bit unchanged. `head` divides by `head_dim` instead -- the
|
||||
**proportional** variant, where the `rotary_dim / 2` frequencies are a prefix of the full-width
|
||||
table rather than a table rescaled to the rotated width: at `head_dim 512, rotary_dim 128`,
|
||||
frequency `i = 1` wants `theta^(-1 / 256)`, not `theta^(-1 / 64)`. Only the base ladder moves
|
||||
(`axial` keeps its per-section ladders), and the tail of the head past `rotary_dim` is untouched
|
||||
either way. `rope_table` carries a `head_dim` param for the same reason: its angles must stay
|
||||
`rope`'s to the last operation.
|
||||
|
||||
**`gemm_nt_bias`'s epilogue** is a linear layer's: the biased product rounded to `y`'s dtype, then
|
||||
`act` of it (`none`, `gelu` exact, `gelu_tanh`) rounded, then `res` added and rounded -- each step
|
||||
what a separate tensor would hold, so the fused form is the unfused chain to the bit.
|
||||
@@ -992,6 +1015,24 @@ holds only where the rank holds the WHOLE vocabulary -- `world_size` 1, not mere
|
||||
on most steps, so a check keyed on the offset alone refuses correct calls. Bounding ids is the
|
||||
caller's, and the tokeniser and the sampler both already are.
|
||||
|
||||
### `embed_lookup`'s runtime `mult`
|
||||
|
||||
`mult` (optional f64, default 1) multiplies the rows that are FOUND: `x[m] = wte[id] * mult`.
|
||||
A missed row — negative padding or another rank's token — writes zero, not zero times the scale.
|
||||
Absent it is the plain gather the op has always been, which is what keeps every other caller in
|
||||
the tree bit-identical.
|
||||
|
||||
**Not `wscale`.** `wscale` is the scale `embed_lookup_q`'s LAYOUT quantises with — a property of
|
||||
the stored table, read back as the dequant. `mult` is applied AFTER the gather, to the rows that
|
||||
come out of it; `embed_lookup_q` takes no `mult` at all, because its rows arrive already scaled by
|
||||
the table's own dequant.
|
||||
|
||||
**Why it exists**: Gemma 4 scales its embedding by `sqrt(n_embd)` at the lookup — the reference's
|
||||
`build_inp_embd` and HF's `Gemma4TextScaledWordEmbedding` both do it there, and the checkpoint's
|
||||
table is plain. Baking it into the table instead would untie it from the head, which the model
|
||||
ties and which the checkpoint stores twice; scaling the lookup leaves the table and the head the
|
||||
same tensor the weights loader already maps.
|
||||
|
||||
### `embed_lookup_q`, the n-gram gather
|
||||
|
||||
**A gather from a table quantised with ONE scale for all of it**: `x[m] = wte[id] * scale[0]`.
|
||||
|
||||
Reference in new issue
Block a user