From ffec4241bd7316408f61bf07dfdcab3ffd945d40 Mon Sep 17 00:00:00 2001 From: Saatvik Viniak Date: Fri, 4 Dec 2026 01:17:59 -0600 Subject: [PATCH] gfx12 prompt attention: pin the q.k WMMA before the trimmed store With q4_lite's G = 8 the trimmed store guard passes only lanes 0-15 (halves differ for Flash-Next's G = 12), and the compiler could sink the whole-wave WMMA into that half-wave branch; the R9700 then stored -nan and every prompt past a few thousand tokens ended in non-finite logits (#879). Pinning s with an empty asm before the store keeps the arithmetic unchanged and makes TRIM 2/1/0 bit-identical; Flash-Next checksums are unchanged. The hip parity test now counts non-finite outputs before comparing: NaN comparisons are false, so the broken kernel used to pass with a bogus FP64 error of 0. --- engine/src/kernels/cuda/qsa_prompt_attn.cu | 4 ++++ engine/src/kernels/qsa_prompt_attn_parity.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/engine/src/kernels/cuda/qsa_prompt_attn.cu b/engine/src/kernels/cuda/qsa_prompt_attn.cu index 40fa3cb3fa..ba2de7454d 100644 --- a/engine/src/kernels/cuda/qsa_prompt_attn.cu +++ b/engine/src/kernels/cuda/qsa_prompt_attn.cu @@ -2101,6 +2101,10 @@ __device__ __forceinline__ void prompt_attn_wmma_body(const float* __restrict__ s = wmma_f16(qh[kk], b, s); if constexpr (SPLIT) s = wmma_f16(ql[kk], b, s); } + // the WMMA needs the whole wave: pin s here, so a store guard that only one half of the wave passes + // (G <= 8: rows 8..15 are all padding) cannot pull the WMMAs into that branch +#pragma unroll + for (int i = 0; i < 8; ++i) asm volatile("" : "+v"(s[i])); #pragma unroll for (int i = 0; i < 8; ++i) if (!TR || PA_ROW(i, half) < G) S.part[warp][PA_ROW(i, half)][nt * 16 + col] = s[i] * cd.ksc[nt]; } diff --git a/engine/src/kernels/qsa_prompt_attn_parity.cpp b/engine/src/kernels/qsa_prompt_attn_parity.cpp index 0a7b8b0976..2ec95a8056 100644 --- a/engine/src/kernels/qsa_prompt_attn_parity.cpp +++ b/engine/src/kernels/qsa_prompt_attn_parity.cpp @@ -238,6 +238,12 @@ int run(int fmt, int64_t ctx, int64_t nq, int reps) { // fmt 1 int8, 0 fp16, 2 for (float x : nw) { uint32_t u; std::memcpy(&u, &x, 4); hh = (hh ^ u) * 1099511628211ull; } std::printf("NEWHASH %016llx\n", (unsigned long long) hh); } + { // a non-finite output fails every comparison below silently (NaN > x is false): count them first + long long bad = 0; + for (float x : nw) bad += !std::isfinite(x); + for (float x : im) bad += !std::isfinite(x); + if (bad) { std::printf("FAIL ctx %lld, %lld queries: %lld non-finite outputs\n", (long long) ctx, (long long) nq, bad); return 1; } + } // 1. FP64 reference on a sample of queries double err_old = 0, err_new = 0, err_im = 0, ref_scale = 0; for (int64_t i = 0; i < nq; i += std::max(1, nq / 16)) {