From 28939a99df702eb48cafa17de973fe9cdef535a5 Mon Sep 17 00:00:00 2001 From: Asim Siddiqui Date: Thu, 5 Nov 2026 21:49:30 +1100 Subject: [PATCH] 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. --- libref/ref_moe.cpp | 2 +- libref/ref_shapes.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libref/ref_moe.cpp b/libref/ref_moe.cpp index 9684bf86..742d0064 100644 --- a/libref/ref_moe.cpp +++ b/libref/ref_moe.cpp @@ -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 diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index 27fecdd6..c7720727 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -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);