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

+39
View File
@@ -1009,6 +1009,45 @@ int ref_gemm_nt_q_bias_shape(const RadParam* p, int n_p, int operand, RadOpdDesc
return gemm_q_shape(p, n_p, operand, true, out);
}
/* RUNTIME LoRA. `slot` is drawn in [0, S] -- S ITSELF IS A BASE ROW, one past the pool -- so a cold
* draw exercises the rows neither op may touch as well as the adapter rows; a draw never yields
* -1, which is the other spelling of a base row, so that one is a hand case (tests/lora_test.cpp,
* ref_test). A single row is drawn in [0, S): a one-row call whose row drew S would write nothing,
* and a comparison of nothing proves nothing (rad-avx-check refuses it as vacuous). The scale stream exists only beside a coded activation: a bf16, f16 or f32 `a` is
* read as it stands, the way gemm_nt_q's fp8a16 form reads it, and an fp8 or i8 one without its
* scale is refused by the launch. `group` defaults to the 128 every block-scaled checkpoint and
* quantiser in this project uses when the geometry does not state one. */
int ref_lora_shrink_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), K = gi(p, n_p, "K", 0);
const int64_t R = gi(p, n_p, "R", 0), S = gi(p, n_p, "S", 0);
int64_t group = gi(p, n_p, "group", 0);
const bool coded = D == (uint32_t)RAD_F8E4M3 || D == (uint32_t)RAD_F8E5M2 ||
D == (uint32_t)RAD_I8;
if (coded && group <= 0) group = 128;
Opd o;
o.t(D, { M, K }); /* a */
if (coded) o.t((uint32_t)RAD_F32, { M, (K + group - 1) / group }); /* a_scale */
else o.skip(); /* a_scale */
o.t_idx({ M }, M > 1 ? S + 1 : S, -1, 0); /* slot, S = base */
o.t((uint32_t)RAD_BF16, { S, R, K }); /* lora_a */
o.t((uint32_t)RAD_F32, { M, R }); /* t */
return o.finish("lora_shrink", operand, out);
}
int ref_lora_expand_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0);
const int64_t R = gi(p, n_p, "R", 0), S = gi(p, n_p, "S", 0);
Opd o;
o.t((uint32_t)RAD_F32, { M, R }); /* t */
o.t_idx({ M }, M > 1 ? S + 1 : S, -1, 0); /* slot, S = base */
o.t((uint32_t)RAD_BF16, { S, N, R }); /* lora_b */
o.t((uint32_t)RAD_F32, { S, N }); /* scale */
o.t(D, { M, N }); /* y */
return o.finish("lora_expand", operand, out);
}
/* ================================================================== attention
*
* The paged layout (ref_ops.h):