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)