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

+27 -4
View File
@@ -76,6 +76,7 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
const int w = (n16 + nb - 1) / nb; // 16B words per block
const int start = b * w;
int end = start + w; if (end > n16) end = n16;
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
// Push the chunk to the peer slot, 4x unrolled for memory-level parallelism.
{
@@ -83,8 +84,10 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
for (; k + 3 * nt < end; k += 4 * nt) {
int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt];
ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3;
hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) +
r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt));
}
for (; k < end; k += nt) ps[k] = in4[k];
for (; k < end; k += nt) { const int4 v = in4[k]; ps[k] = v; hck += r4d_archk_u4(v, 4u * (unsigned)k); }
}
// Drain modes, ordering the pushes before the release flag so the peer sees data first:
// 0 = none (relies on PCIe posted-write ordering and the release flag)
@@ -94,6 +97,7 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
// 3 = all-thread s_wait_storecnt 0: every wave drains its posted writes with no L2
// writeback-invalidate; PCIe keeps posted writes to one peer ordered, so the release flag
// lands after the data. Correct only for fine-grained memory (coarse leaves data in L2).
r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
@@ -108,6 +112,9 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
}
__syncthreads();
if (acq) __threadfence_system();
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
});
// Reduce the chunk, 4x unrolled: out = local input + peer input (staged in local scratch).
{
int k = start + tid;
@@ -147,15 +154,19 @@ __global__ void r4d_ar_hc_write_kernel(
const int w = (n16 + nb - 1) / nb; // 16B words per block
const int start = b * w;
int end = start + w; if (end > n16) end = n16;
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
{
int k = start + tid;
for (; k + 3 * nt < end; k += 4 * nt) {
int4 v0 = y4[k], v1 = y4[k + nt], v2 = y4[k + 2 * nt], v3 = y4[k + 3 * nt];
ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3;
hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) +
r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt));
}
for (; k < end; k += nt) ps[k] = y4[k];
for (; k < end; k += nt) { const int4 v = y4[k]; ps[k] = v; hck += r4d_archk_u4(v, 4u * (unsigned)k); }
}
r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
@@ -168,8 +179,11 @@ __global__ void r4d_ar_hc_write_kernel(
}
}
__syncthreads();
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
if (acq) __threadfence_system();
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
});
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
const int wpr = n >> 3; // 16B words a row
for (int k = start + tid; k < end; k += nt) {
@@ -231,6 +245,7 @@ __global__ void r4d_ar_gather_hc_write_kernel(
const int w = (n16 + nb - 1) / nb; // 16B words per block
const int start = b * w;
int end = start + w; if (end > n16) end = n16;
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
const int wpr = n >> 3; // 16B words a row
const int row_lo = start / wpr;
const int nrow = end > start ? (end - 1) / wpr - row_lo + 1 : 0;
@@ -264,7 +279,9 @@ __global__ void r4d_ar_gather_hc_write_kernel(
const int4 out = r4d_ar_gather_word<INV>(G, inv, s_i, s_w, s_g, row, row - row_lo, i0);
y4[k] = out;
ps[k] = out;
hck += r4d_archk_u4(out, 4u * (unsigned)k);
}
r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
@@ -277,8 +294,11 @@ __global__ void r4d_ar_gather_hc_write_kernel(
}
}
__syncthreads();
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
if (acq) __threadfence_system();
r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) {
for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k);
});
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
for (int k = start + tid; k < end; k += nt) {
const int4 r = vadd<__hip_bfloat16, 8>(y4[k], ms[k]);
@@ -398,6 +418,7 @@ void r4d_ar_hc_write(long peer_scratch, long my_scratch, long peer_flags, long m
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes
r4d_ar_hc_write_kernel<<<nb, nt, 0, st>>>(
(int4*)y, (int4*)peer_scratch, (const int4*)my_scratch, (unsigned*)peer_flags,
(unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, (unsigned short*)h,
@@ -436,6 +457,7 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags,
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes
/* Zeroed including padding: the struct is passed by value, so its tail padding is part of the
* kernel-argument bytes a recorded pass's audit compares. */
R4dArGather G;
@@ -527,6 +549,7 @@ void r4d_ar_oneshot_2rank_exact(long peer_scratch, long my_scratch, long peer_fl
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes
#define L(T, LN) r4d_ar_oneshot_2rank_exact_kernel<T, LN><<<nb, nt, 0, st>>>( \
(const int4*)inp, (int4*)peer_scratch, (const int4*)my_scratch, (int4*)out, \
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n16, \