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

+5
View File
@@ -118,6 +118,9 @@ int ref_gemm_nt (const RadArgs*, RadStream);
int ref_gemm_nt_bias (const RadArgs*, RadStream);
int ref_gemm_nt_q (const RadArgs*, RadStream);
int ref_gemm_nt_q_bias (const RadArgs*, RadStream);
/* Runtime LoRA's two halves, per-row adapter slot (ref_lora.cpp). */
int ref_lora_shrink (const RadArgs*, RadStream);
int ref_lora_expand (const RadArgs*, RadStream);
int ref_embed_lookup (const RadArgs*, RadStream);
int ref_embed_lookup_q (const RadArgs*, RadStream);
int ref_logits_gemm (const RadArgs*, RadStream);
@@ -283,6 +286,8 @@ REF_SHAPE_FN(ref_gemm_nt);
REF_SHAPE_FN(ref_gemm_nt_bias);
REF_SHAPE_FN(ref_gemm_nt_q);
REF_SHAPE_FN(ref_gemm_nt_q_bias);
REF_SHAPE_FN(ref_lora_shrink);
REF_SHAPE_FN(ref_lora_expand);
REF_SHAPE_FN(ref_embed_lookup);
REF_SHAPE_FN(ref_embed_lookup_q);
REF_SHAPE_FN(ref_logits_gemm);