From 94ae86f6fdc00d940208cce9629251e55571bbe1 Mon Sep 17 00:00:00 2001 From: baKon Date: Fri, 16 Oct 2026 03:06:38 -0500 Subject: [PATCH] libref, libavx: lora_shrink checks `group` against the stream's grid The stream states the group through its extent (make_scales' grid_block), and a K no wider than one group has one group whatever `group` says: a 64-wide row under group 128 read as group 64 and was refused. Compare the grid -- groups along K -- instead of the number. An a_scale with no dtype is absent, like one with no data. --- docs/OPS.md | 5 +++-- libavx/avx_lora.cpp | 3 ++- libref/ref_lora.cpp | 13 ++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/OPS.md b/docs/OPS.md index 5461dda3..70836c43 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -639,8 +639,9 @@ not read or write its `y` row at all — so a base row stays bit for bit what th `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 + element as it stands. An `fp8_e4m3`/`fp8_e5m2`/`i8` `a` without `a_scale` (null data) is refused. + `group` is a cross-check on the stream's grid -- `ceil(K / group)` groups along K -- 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 diff --git a/libavx/avx_lora.cpp b/libavx/avx_lora.cpp index e43c5adb..da3d028b 100644 --- a/libavx/avx_lora.cpp +++ b/libavx/avx_lora.cpp @@ -105,6 +105,7 @@ AVX_KERNEL(lora_shrink) { return RAD_E_INVAL; const RadTensor *X = &a->t[0], *SL = &a->t[2], *LA = &a->t[3], *T = &a->t[4]; const RadTensor* XS = rad_arg_in(a, 1); + if (XS && XS->dtype == RAD_DT_INVALID) XS = nullptr; /* absent, as libref */ const int64_t K = p_int(a, "K", 0), R = p_int(a, "R", 0), S = p_int(a, "S", 0); if (K <= 0 || R <= 0 || S <= 0) return RAD_E_SHAPE; const int64_t M = band_rows(numel(T) / R, p_int(a, "M", 0)); @@ -115,7 +116,7 @@ AVX_KERNEL(lora_shrink) { if (T->dtype != RAD_F32) return RAD_E_DTYPE; const Scales SA = make_scales(XS, M, K); const int64_t gp = p_int(a, "group", 0); - if (gp > 0 && SA.p && SA.group != gp) return RAD_E_SHAPE; + if (gp > 0 && SA.p && (K + gp - 1) / gp != (K + SA.group - 1) / SA.group) return RAD_E_SHAPE; const Mat mx = as_mat(X, K), mt = as_mat(T, R); const Pool3 pa = pool3(LA, R, K); diff --git a/libref/ref_lora.cpp b/libref/ref_lora.cpp index 14f8c05e..90bd3ea4 100644 --- a/libref/ref_lora.cpp +++ b/libref/ref_lora.cpp @@ -67,6 +67,7 @@ int ref_lora_shrink(const RadArgs* a, RadStream) { return RAD_E_INVAL; const RadTensor *X = &a->t[0], *SL = &a->t[2], *LA = &a->t[3], *T = &a->t[4]; const RadTensor* XS = t_in(a, 1); + if (XS && XS->dtype == RAD_DT_INVALID) XS = nullptr; /* absent: null data or no dtype */ const int64_t K = p_int(a, "K", 0), R = p_int(a, "R", 0), S = p_int(a, "S", 0); if (K <= 0 || R <= 0 || S <= 0) return RAD_E_SHAPE; const int64_t M = band_rows(numel(T) / R, p_int(a, "M", 0)); @@ -76,13 +77,15 @@ int ref_lora_shrink(const RadArgs* a, RadStream) { if (coded_act(X->dtype) && !XS) return RAD_E_INVAL; /* a code without its scale */ if (T->dtype != RAD_F32) return RAD_E_DTYPE; - /* gemm_nt_q's reading of the scale stream, and its `group` cross-check: the stream states the - * group, and a parameter that disagrees with it is a caller describing other bytes. Absent - * the stream the parameter says nothing -- a bf16 activation has no scale whatever `group` - * holds. */ + /* gemm_nt_q's reading of the scale stream, and a `group` cross-check: the stream states the + * group, and a parameter whose GRID disagrees with it is a caller describing other bytes. The + * grid -- groups along K -- and not the number, because a K no wider than one group has one + * group whatever `group` says (a 64-wide row under 128), and from two groups up one power of + * two gives each count. Absent the stream the parameter says nothing: a bf16 activation has no + * scale whatever `group` holds. */ const Scales SA = make_scales(XS, M, K, false); const int64_t gp = p_int(a, "group", 0); - if (gp > 0 && SA.p && SA.group != gp) return RAD_E_SHAPE; + if (gp > 0 && SA.p && (K + gp - 1) / gp != (K + SA.group - 1) / SA.group) return RAD_E_SHAPE; const Mat mx = as_mat(X, K), mt = as_mat(T, R); const Pool3 pa = pool3(LA, R, K);