libr4d: check that a peer's push has landed before reducing it (2-rank family)

A two-rank all-reduce or gather pushes its bytes into the peer's slot over PCIe P2P,
drains its stores and then publishes a release flag; the peer reads the slot once it
sees the flag. On 2x R9700 the bytes do not always land before the flag. With reads
streaming beside the all-reduce (prefetch), a greedy reply differed from one server
start to the next.

A probe in ar_rmsnorm_quant_fp8 compared, right after the handshake, a plain load of
the slot with a system-scope load and with the same word 100 us later: in about 1
launch in 1000 the reader saw the slot's previous contents, and the new bytes arrived
about 1 us later, sometimes more. Pushed bytes wait in the writer's L2 until a
system-scope release writes them back: with the flag sent before the push (a test
mode) and nothing after it, they stayed invisible for the whole kernel (49 ms). The
flag's release does write them back, but the writeback and the flag store reach the
peer by different paths, so the flag can arrive first. A stronger drain on the writer
(drain=1), PCIe relaxed ordering off on both cards and an HDP flush on the reader did
not change that; acq=1 hides it only by delaying the reads (and costs 12-33 % of
decode).

The writer now sums a per-dword hash (MurmurHash3's finalizer over value and index)
over what it pushes and stores the block's sum, salted with its handshake value, into
the peer's check word for (slot, block): two parity slots of fine-grained words after
the flags. The flag's release that follows writes the pushed bytes back. The reader
reads the check word, sums the same over the bytes it is about to read and, until the
two agree, takes an acquire fence, sleeps briefly and reads again; a push that never
lands traps after 2 s. A stale element and a stale check word both disagree, so the
order in which the two land does not matter. No value changes.

Kernels: ar_rmsnorm_quant_fp8 (exact and 6-bit wire), r4d_ar_oneshot_2rank_exact,
r4d_ar_hc_write, r4d_ar_gather_hc_write, r4d_ar_gather_2rank,
r4d_ar_oneshot_2rank_wht6, r4d_ar_hc_write_wht6, ar_rmsnorm_quant_i8,
r4d_ar_ln_had_quant_i8. Also: the link gate is released after the check, the exact
and gather launchers round their width to whole waves (the check's wave sum reads all
32 lanes), and ln_had's dynamic LDS ceiling leaves room for the check's static words.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-25 10:11:27 +01:00
1 parent 2721f7c951
commit 0044d8d201
9 files changed
+317 -24

No files matched your search

+42 -3
View File
@@ -129,7 +129,8 @@ struct Wht6Chunk {
template <int CG = CHUNK_GROUPS, typename Ld>
__device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_groups, int lane,
int lig, int gw, uint4* p_pay,
unsigned short* p_sc, uint4* l_pay, float* l_sc) {
unsigned short* p_sc, uint4* l_pay, float* l_sc,
unsigned int& hck) {
using Ch = Wht6Chunk<CG>;
const int qmin = -(1 << (BITS - 1)), qmax = (1 << (BITS - 1)) - 1;
unsigned int acc[Ch::NW];
@@ -157,8 +158,10 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group
float sc = (a > 0.f) ? bf16_round(a * (1.0f / (float)qmax)) : 1.0f;
if (!(sc > 0.f)) sc = 1.0f;
if (lig == 0 && live) {
p_sc[g] = (unsigned short)(__float_as_uint(sc) >> 16);
const unsigned short sc16 = (unsigned short)(__float_as_uint(sc) >> 16);
p_sc[g] = sc16;
l_sc[g] = sc;
hck += r4d_archk_mix(sc16, R4D_ARCHK_U16 + (unsigned)g);
}
const float inv = 1.0f / sc;
#pragma unroll
@@ -177,6 +180,7 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group
const long o = (long)c * (Ch::NW / 4 * WAVE) + (long)u * WAVE + lane;
p_pay[o] = q;
l_pay[o] = q;
hck += r4d_archk_u4(q, 4u * (unsigned)o);
}
} else {
unsigned int* pp = reinterpret_cast<unsigned int*>(p_pay);
@@ -186,6 +190,7 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group
const long o = (long)c * (Ch::NW * WAVE) + (long)u * WAVE + lane;
pp[o] = acc[u];
lp[o] = acc[u];
hck += r4d_archk_mix(acc[u], (unsigned)o);
}
}
}
@@ -209,6 +214,34 @@ __device__ __forceinline__ void wht6_handshake(unsigned int* peer_flags, unsigne
if (acq) __threadfence_system();
}
// The push check's re-hash of one chunk (r4d_ar_seq.h): the payload words and live scales the
// reduce below reads, at the indices wht6_push_chunk hashed them.
template <int CG = CHUNK_GROUPS>
__device__ __forceinline__ void wht6_rehash_chunk(unsigned int& h, int c, int n_groups, int lane,
int lig, int gw, const uint4* m_pay,
const unsigned short* m_sc) {
using Ch = Wht6Chunk<CG>;
if constexpr (Ch::U4) {
#pragma unroll
for (int u = 0; u < Ch::NW / 4; ++u) {
const long o = (long)c * (Ch::NW / 4 * WAVE) + (long)u * WAVE + lane;
h += r4d_archk_u4(m_pay[o], 4u * (unsigned)o);
}
} else {
const unsigned int* mp = reinterpret_cast<const unsigned int*>(m_pay);
#pragma unroll
for (int u = 0; u < Ch::NW; ++u) {
const long o = (long)c * (Ch::NW * WAVE) + (long)u * WAVE + lane;
h += r4d_archk_mix(mp[o], (unsigned)o);
}
}
#pragma unroll
for (int t = 0; t < Ch::ITERS; ++t) {
const int g = c * CG + t * GPW + gw;
if (lig == 0 && g < n_groups) h += r4d_archk_mix(m_sc[g], R4D_ARCHK_U16 + (unsigned)g);
}
}
// Reduce: unpack both halves, add in fp32, inverse-rotate, pass each live group's EPL elements to st.
template <int CG = CHUNK_GROUPS, typename St>
__device__ __forceinline__ void wht6_reduce_chunk(const St& st, int c, int n_groups, int lane,
@@ -300,10 +333,16 @@ __global__ void r4d_ar_oneshot_2rank_wht6_kernel(
#pragma unroll
for (int i = 0; i < EPL; ++i) v[i] = rd_to_f(in[off + i]);
};
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
for (int c = c0 + wid; c < c1; c += nwarp)
wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc);
wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc, hck);
r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, s);
wht6_handshake(peer_flags, my_flags, b, s, tid, drain, acq);
r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, s, [&](unsigned int& h) {
for (int c = c0 + wid; c < c1; c += nwarp)
wht6_rehash_chunk(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc);
});
const auto st = [&](long off, const float* v) {
#pragma unroll