From e3d65f3a7c0f81adb0afd77b2ec7c34caafbc674 Mon Sep 17 00:00:00 2001 From: Asim Siddiqui Date: Thu, 5 Nov 2026 21:49:54 +1100 Subject: [PATCH] libr3d: fix the w4a4 activation row pitch (strides are elements, not bytes) The launcher passed a->stride[0] straight as the byte pitch, but strides are in elements (abi/rad_types.h): an int4 row of K nibbles is K/2 bytes. The kernel read row 2*src instead of row src (row 0 correct, every other row from the wrong source, and past the plane for larger M). The weight side already converts (r3d_tab_of's s0 = stride*bits/8, the kernel uses w_ld/2); the activation now does too. The libref reference had the same bug and is fixed on the w4a4-libref-wip branch, so the two now agree on the correct row. Missed because r3d_selftest checks finiteness only and ref_test checks status only on the libref sweep geometries. --- libr3d/r3d_moe_gemm_w4a4.hip | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libr3d/r3d_moe_gemm_w4a4.hip b/libr3d/r3d_moe_gemm_w4a4.hip index b20f997f..baf2cc09 100644 --- a/libr3d/r3d_moe_gemm_w4a4.hip +++ b/libr3d/r3d_moe_gemm_w4a4.hip @@ -14,7 +14,7 @@ * scale_w[e][n][b] in f32 -- the exact order the libref reference runs, so it agrees bit for bit. * * Decode band: at most 16 sorted rows per block (M * top_k <= 16); prefill finds no row and falls - * back. The activation stride a_ld is in BYTES (K/2 per row: K int4 nibbles). + * back. The activation stride a_ld is in BYTES (K/2 per row: stride[0] is K int4 nibbles). */ #include @@ -197,7 +197,8 @@ int r3d_rad_moe_gemm_w4a4(const RadArgs* a, RadStream s) R3dMoeTabs tw; rc = r3d_moe_q_tabs(c, RAD_I4, 4, RAD_BF16, 16, c.N, c.K / kGroup, &tw); if (rc != RAD_OK) return rc; - const int a_ld = (int)c.a->stride[0], as_ld = (int)c.a_scale->stride[0]; + /* Strides are in elements (abi/rad_types.h): an int4 row of K nibbles is K/2 bytes. */ + const int a_ld = (int)c.a->stride[0] / 2, as_ld = (int)c.a_scale->stride[0]; const int y_ld = (int)c.y->stride[0]; const int mrows = r3d_geti_or(a, "mrows", 16);