libref, libavx: lora_shrink and lora_expand, the per-row runtime LoRA ops

The two halves of y += scale * (B . (A . x)) at a trunk GEMM, with each row's adapter named by an
i32 `slot`:

    lora_shrink  t[m, j] = sum_k x(m, k) * lora_a[s, j, k]
    lora_expand  y[m, n] = y[m, n] + scale[s, n] * sum_j t[m, j] * lora_b[s, n, j]

A row whose slot is outside [0, S) is a base row and neither op touches it: shrink writes no `t`,
expand neither reads nor writes `y`, so a base row stays bit for bit what the GEMM wrote (y + 0
would already turn a -0 into +0). x(m, k) is gemm_nt_q's activation value, read through the same
make_scales; an fp8/i8 activation without its scale stream is refused. The dtype parameters carry
the tensor dtype names (rad_dtype_name). Sums are per row in a fixed order, so a row's bytes do
not depend on M or on its neighbours.

libref is the oracle (straight f32 loops, OpenMP over whole output elements); libavx covers both
at every ISA level with vectorised dots. ref_test gains hand-computed cases (column slices, -1 and
S as base slots, a -0 that must survive, the fp8 scale fold, the missing-scale and wrong-group
refusals); avx_check gains a case table; docs/OPS.md the two rows; rad-kbench and avx_check a
tolerance row for the shrink's f32 reduction.
This commit is contained in:
baKon committed 2026-10-16 20:52:09 -05:00
1 parent 48f7b8eb6d
commit 7864acf50a
13 files changed
+569

No files matched your search

+34
View File
@@ -621,6 +621,40 @@ what a separate tensor would hold, so the fused form equals the unfused chain bi
may be `y` itself, the residual stream updated in place (the encoder towers' layers do this), so an
implementation reads `res` before it stores the product over it.
### Runtime LoRA: `lora_shrink` and `lora_expand`
| op | params | operands |
|---|---|---|
| `lora_shrink` | `M`(range) `K` `R` `S` `group`/opt `dtype` | `a`, `a_scale`?, `slot`, `lora_a` → `t` |
| `lora_expand` | `M`(range) `N` `R` `S` `dtype` | `t`, `slot`, `lora_b`, `scale`, `y`:inout |
The two halves of `y += scale ⊙ (B · (A · x))` at a trunk GEMM, issued by the core right after the
GEMM in the GEMM's own row slice, with several adapters and no adapter mixed in one step. `slot` is
an i32 per row naming its adapter's pool slot; **a row with `slot < 0 || slot >= S` is a base row
and neither op touches it**: `lora_shrink` writes nothing into its `t` row, and `lora_expand` does
not read or write its `y` row at all — so a base row stays bit for bit what the GEMM wrote, where
`y + 0` would already have turned a -0 into +0.
- `lora_shrink`: `t[m, j] = Σ_k x(m, k) · lora_a[s, j, k]`, `s = slot[m]`, f32 accumulate.
`a`/`a_scale` are the GEMM's own first two operands, and `x(m, k)` is the value `gemm_nt_q` uses
for that element: the code times its group's scale, read through the same scale-stream rule
(`[rows, groups]` or `[rows]`, the group derived from the stream), or a `bf16`/`f16`/`f32`
element as it stands. An `fp8_e4m3`/`fp8_e5m2`/`i8` `a` without `a_scale` is refused. `group`
is a cross-check on the stream and 0 means none; `dtype` is `a`'s tensor dtype name
(`rad_dtype_name`: `bf16`, `f16`, `f32`, `fp8_e4m3`, `i8`, ...), not a combined quant string.
`lora_a` is `[S, R, K]` bf16, `t` is `[M, R]` f32.
- `lora_expand`: `y[m, n] = y[m, n] + scale[s, n] · Σ_j t[m, j] · lora_b[s, n, j]`, f32
accumulate, one rounding to `y`'s dtype (`bf16`/`f16`/`f32`, named by `dtype`). `lora_b` is
`[S, N, R]` bf16 and `scale` `[S, N]` f32 — per output column, so a fused projection whose parts
carry different `alpha / r` is one launch.
`a`, `a_scale` and `y` may be row and column slices of wider buffers; every row stride is the
tensor's. The pools are ordinary inputs and not weights: the engine owns them as program buffers
and copies an adapter into a slot between steps, so a recorded pass replays the same pointers and
reads whatever adapters the slots hold. A row's result does not depend on `M` or on any other row
(no split of a reduction that moves with the batch), which is what lets a request's bytes be the
same alone and in a mixed batch. Implementations: libref, libavx, libr4d (scalar f32 rows).
## Attention
| op | params | operands |