From 167595a9b4017aa134c8a3d124a2cfb2ac6e0475 Mon Sep 17 00:00:00 2001 From: mici81 Date: Fri, 30 Oct 2026 11:39:08 +0100 Subject: [PATCH] libr4d: the split-KV row sum read with fetch-inactive (#328) The prefill KV split (6a7824a) stores each segment's row sum under `if (h == 0)`, so only half the wave consumes `l_i += swap16(l_i)`. A compiler may sink the exchange into that half, where v_permlanex16 with fi = 0 reads the lane's own value for the inactive partner, and the stored sum is then twice one half's instead of the pair's: the merge normalises by the wrong total. The decode kernel's f32-partial path has the same shape (lrow read under `if (h == 0)` only). swap16_fi sets fetch-inactive, so the partner's half is read whatever exec says. With every lane active it is the same exchange. Found by sabi-noema and posted by mordae in #328; this is their helper and prefill change, plus the decode path. The compilers I have do not sink it: in this tree's build (ROCm 10.1) and in the published 1.14.0 libr4d.so, every split instance of the h256/gqa6 prefill kernel exchanges before the `h == 0` mask. So the fix changes the code (op_sel:[1,0] on those exchanges, a register shuffle around them) but not the results here, and no test here can fail without it. r4d_selftest: the h256/gqa6 prefill and decode rows, every cache layout, 453 checks, 0 failures. Co-Authored-By: Claude Opus 5.5 --- libr4d/r4d_attn_decode_h256_gqa6.hip | 4 +++- libr4d/r4d_attn_prefill_h256_gqa6.hip | 4 +++- libr4d/r4d_common.h | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/libr4d/r4d_attn_decode_h256_gqa6.hip b/libr4d/r4d_attn_decode_h256_gqa6.hip index ff3f161c..8355aff4 100644 --- a/libr4d/r4d_attn_decode_h256_gqa6.hip +++ b/libr4d/r4d_attn_decode_h256_gqa6.hip @@ -608,7 +608,9 @@ void r4d_attn_decode_body(const R4DArgs& a, int splits) } if (!live) return; - const float lrow = l_i + swap16(l_i); + // fetch-inactive: the f32 partials read lrow under `if (h == 0)` only (see the prefill kernel's + // epilogue, #328). + const float lrow = l_i + swap16_fi(l_i); const int tok = qoff + qrow; const size_t idx = (size_t)(tok * a.q_heads + qhead) * splits + sp; const size_t nrow = (size_t)r4d_q_total(a) * a.q_heads * splits; diff --git a/libr4d/r4d_attn_prefill_h256_gqa6.hip b/libr4d/r4d_attn_prefill_h256_gqa6.hip index cda34f40..03f1a64d 100644 --- a/libr4d/r4d_attn_prefill_h256_gqa6.hip +++ b/libr4d/r4d_attn_prefill_h256_gqa6.hip @@ -679,7 +679,9 @@ void r4d_attn_prefill_kernel(const R4DArgs a) } if (!live) return; - l_i += swap16(l_i); + // fetch-inactive: the split store reads l_i under `if (h == 0)` only, so the exchange may be sunk + // into that half; swap16_fi still reads the partner's half of the row sum (#328). + l_i += swap16_fi(l_i); if constexpr (SPLIT) { // Unnormalised: the merge weighs segments by 2^(m - max m) and applies v_descale itself. r4d_prefill_store_partial(a, qoff + qpos, qhead, sp, acc, m_ref, l_i, h); diff --git a/libr4d/r4d_common.h b/libr4d/r4d_common.h index 447f828d..03ce76e3 100644 --- a/libr4d/r4d_common.h +++ b/libr4d/r4d_common.h @@ -150,6 +150,16 @@ __device__ __forceinline__ unsigned long long r4d_xor_lane_u64(unsigned long lon // broadcast lane 0. __device__ __forceinline__ float swap16(float x) { return r4d_xor_lane<16>(x); } +// The same exchange with fetch-inactive set. swap16 assumes every lane is active; where only one half +// consumes the result (a store under `if (h == 0)`), a compiler may sink the exchange into that half, +// and with fi = 0 a lane then reads its own value for the inactive partner's. fi = 1 reads the +// partner's register whatever exec says, so a pair sum stays a pair sum. Identical to swap16 with +// every lane active. +__device__ __forceinline__ float swap16_fi(float x) { + return __uint_as_float((uint32_t)__builtin_amdgcn_permlanex16( + (int)__float_as_uint(x), (int)__float_as_uint(x), 0x76543210u, 0xFEDCBA98u, true, false)); +} + // A wave32 sum on DPP instead of the LDS crossbar. `__shfl_xor` is a `ds_bpermute_b32` on gfx1201 // at any offset, queued behind other LDS traffic; where little arithmetic separates reductions, a // five-stage shuffle butterfly dominates the run. Offsets 8..1 use row_xmask (dpp_ctrl 0x160 | N)