From 9d37941a44518b3b2566da1085028a1dbc3d3317 Mon Sep 17 00:00:00 2001 From: orca Date: Tue, 27 Oct 2026 17:18:12 +0100 Subject: [PATCH] fix(gemm): guard the W4A8 prefill tail scale read The epilogue computed $v = acc * Ascale[m]$ before the store guard, so on the last row tile of an M that is not a multiple of the tile height it named Ascale[M] -- a row the caller does not own. The read is under the guard now. inside is STFAST && (m0 + BM <= M), so it implies m < M for every row of the tile, and n < N holds unconditionally because the launch rejects N % BN != 0. No behaviour change is claimed: on gfx1201 at -O3 the compiler already predicates the whole scale-and-store block (s_and_saveexec + s_cbranch_execz around both the global_load_b32 of the scale and the store), so the published binary does not issue the invalid load. This keeps the source honest for compilers and ISA levels that do not. --- libr4d/r4d_gemm_w4a8_prefill.hip | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libr4d/r4d_gemm_w4a8_prefill.hip b/libr4d/r4d_gemm_w4a8_prefill.hip index bcb2985d..79b17db2 100644 --- a/libr4d/r4d_gemm_w4a8_prefill.hip +++ b/libr4d/r4d_gemm_w4a8_prefill.hip @@ -279,9 +279,11 @@ __global__ __launch_bounds__(WM * WN * 32, 8) void r4d_gemm_w4a8_prefill_kernel( for (int e = 0; e < 8; e++) { const int n = n0 + (wn * TN + j) * 16 + (lane & 15); const int m = mb + e; - const __hip_bfloat16 v = - (__hip_bfloat16)(acc[j][i][e] * (ASCVEC ? as[e] : Ascale[m]) * 0.0625f); if (inside || (n < N && m < M)) { + // The scale read stays under the guard: past the last full tile `m >= M`, and the row it + // would read belongs to the next tile or to nothing at all. + const __hip_bfloat16 v = + (__hip_bfloat16)(acc[j][i][e] * (ASCVEC ? as[e] : Ascale[m]) * 0.0625f); // the non-temporal builtin does not take a __hip_bfloat16*, so store its bits if (STNT) __builtin_nontemporal_store(*(const unsigned short*)&v,