libref: fix the w4a4 activation row pitch and the dequant_w4 wq extent

Two layout bugs found in pre-PR review:

1. ref_moe_gemm_w4a4 treated A->stride[0] as a byte pitch. Strides are in
   elements (abi/rad_types.h), and an int4 row of K nibbles is K/2 bytes, so
   the reference read row 2*src instead of row src (and ran past the plane
   for M > ~4). The weight side already converts (w_ld / 2); the activation
   now does too. The kernel has the same bug (separate libr3d fix).

2. ref_dequant_w4_shape declared wq as {N*K/8} I4 elements = N*K/16 bytes,
   but the packed plane is N*K nibbles = N*K/2 bytes (the kernel and the
   reference both read N*K/8 dwords). Declared extent is now {N*K}.

Both were invisible to the harness: ref_test checks status only, and the
64-byte poison margin absorbed the over-read at sweep sizes.
This commit is contained in:
Asim Siddiqui authored and StillDeadcode committed 2026-11-05 14:59:08 +01:00
1 parent df4e2ea889
commit 28939a99df
2 files changed
+2 -2

No files matched your search

+1 -1
View File
@@ -884,7 +884,7 @@ int ref_moe_gemm_w4a4(const RadArgs* a, RadStream) {
if (M <= 0 || numel(AS) < ar * (K / 128) || numel(ST) < T || numel(OF) < n_expert + 1)
return RAD_E_SHAPE;
const int64_t a_ld = A->rank >= 2 ? A->stride[0] : K / 2; // BYTES per activation row (K int4)
const int64_t a_ld = A->rank >= 2 ? A->stride[0] / 2 : K / 2; // BYTES per activation row (K int4 = K/2 bytes)
const int64_t y_ld = Y->rank >= 2 ? Y->stride[0] : N;
const int64_t w_ld = W->stride[1]; // nibbles per weight row
const int64_t ws_ld = WS->stride[1]; // scales per weight row
+1 -1
View File
@@ -1341,7 +1341,7 @@ int ref_dequant_w4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
const int64_t group = gi(p, n_p, "group", 128);
if (N < 1 || K < 1 || N % 16 || K % 64 || group < 1 || K % group) return RAD_E_UNSUPPORTED;
Opd o;
o.t(RAD_I4, { N * K / 8 }); /* wq -- packed int4 */
o.t(RAD_I4, { N * K }); /* wq -- packed int4: N*K nibbles = N*K/2 bytes = N*K/8 dwords */
o.t(RAD_F32, { N * K / group }); /* ws -- scale (f16 in a dword, but declared f32) */
o.t(RAD_BF16, { N, K }); /* y */
return o.finish("dequant_w4", operand, out);