r4d_selftest: every push landing after its flag (the push check's regression case)

A test-only mode of the two-rank exact all-reduce (drain 4, set by
R4D_AR_TEST_LATE_PUSH=1 in the launcher, not in any tuning table): every block
publishes its flag before its push, pushes about 20 us later and stores its check
word as a release, since no flag release follows to write the bytes back. The new
case runs the 32-round sweep of ar_oneshot_2rank_exact and its host twin under it.

Without the push check the readers sum their slots' previous contents (16.5 million
of 18.5 million elements wrong on the device row, 12.9 million on the host twin);
with it every sum is exact.

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 0044d8d201
commit fa7a5e4164
2 files changed
+41 -2

No files matched your search

+14 -2
View File
@@ -18,6 +18,7 @@
// * Fine-grained memory (hipDeviceMallocFinegrained) is uncached and posted straight to the
// fabric, so system-scope release/acquire skip the L2 writeback. The drain/acq fences are
// selectable (default drain=1/acq=0 on fine-grained).
#include <cstdlib>
#include "r4d.h"
#include "r4d_ar_seq.h"
#include "r4d_ar_hc.h" // the gather word and the hc write word, shared with the rotated wire
@@ -78,6 +79,14 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel(
int end = start + w; if (end > n16) end = n16;
unsigned int hck = 0u; // the push check (r4d_ar_seq.h)
// drain 4, tests only (R4D_AR_TEST_LATE_PUSH, r4d_selftest): the flag goes out before the push
// and the push ~20 us after it, so the peer sees a flag whose bytes have not landed -- what a
// push over PCIe P2P now and then does on its own -- on every launch.
if (drain == 4) {
if (tid == 0) store_sys_rel(&peer_flags[b], s);
const unsigned long long t0 = wall_clock64();
while (wall_clock64() - t0 < 2000ull) __builtin_amdgcn_s_sleep(8); // 100 MHz: 20 us
}
// Push the chunk to the peer slot, 4x unrolled for memory-level parallelism.
{
int k = start + tid;
@@ -97,13 +106,13 @@ __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);
r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s, drain == 4);
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
if (tid == 0) {
if (drain == 2) __threadfence_system();
store_sys_rel(&peer_flags[b], s);
if (drain != 4) store_sys_rel(&peer_flags[b], s);
unsigned long long z = 0;
// A handshake that cannot complete is a lockstep violation; trap rather than sum stale bytes.
while (r4d_ar_seq_behind(load_sys_acq(&my_flags[b]), s)) {
@@ -550,6 +559,9 @@ void r4d_ar_oneshot_2rank_exact(long peer_scratch, long my_scratch, long peer_fl
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes
// Tests only: every push lands after its flag (drain 4 above). Not in the tuning table, so no
// tuner can pick it.
if (const char* e = std::getenv("R4D_AR_TEST_LATE_PUSH"); e && *e == '1') drain = 4;
#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, \
+27
View File
@@ -23022,6 +23022,32 @@ static void ar_two_rank_sweep(const Plugin& pl, const std::vector<int>& devs, co
if (row->fini) { row->fini(a.inst); row->fini(b.inst); }
}
/* Every peer push landing after its flag (the kernel's test mode, R4D_AR_TEST_LATE_PUSH): a push
* over PCIe P2P sometimes lands a microsecond or more after the release flag that follows it, so a
* reader that trusted the flag would sum the slot's previous contents. The push check
* (r4d_ar_seq.h) makes the reader wait for the bytes; here every launch of the sweep needs it. */
static void ar_two_rank_late(const Plugin& pl, const std::vector<int>& devs, const char* name) {
const RadKernelInfo* row = find(pl, name);
if (!row) { ok(name, false, "row absent"); return; }
if (devs.size() < 2) { skip(name, "fewer than two gfx1201 cards"); return; }
setenv("R4D_AR_TEST_LATE_PUSH", "1", 1);
ArSweep a{ row, 0, devs[0] }, b{ row, 1, devs[1] };
std::thread ta(ar_sweep_worker, &a), tb(ar_sweep_worker, &b);
ta.join();
tb.join();
unsetenv("R4D_AR_TEST_LATE_PUSH");
ok("2-rank all-reduce init, pushes landing after their flags",
a.rc_init == RAD_OK && b.rc_init == RAD_OK,
"rc " + std::to_string(a.rc_init) + "/" + std::to_string(b.rc_init));
if (a.rc_init != RAD_OK || b.rc_init != RAD_OK) return;
ok("2-rank all-reduce, every push landing ~20 us after its flag, 32 rounds, every sum exact on "
"both ranks",
a.rc_launch == RAD_OK && b.rc_launch == RAD_OK && a.bad == 0 && b.bad == 0,
"rc " + std::to_string(a.rc_launch) + "/" + std::to_string(b.rc_launch) + ", " +
std::to_string(a.bad + b.bad) + " elements wrong");
if (row->fini) { row->fini(a.inst); row->fini(b.inst); }
}
/* ------------------------------------------------------------------ the two-rank all-gather */
/* The payload is what the vocab-parallel sampler gathers: (u32 global token id, f32 logit) pairs.
* A token id's bit pattern read as f32 is a denormal, so an all-gather emulated by an all-reduce
@@ -28499,6 +28525,7 @@ static void device_phase(const Plugin& pl) {
if (!std::strcmp(base, "ar_oneshot_2rank_exact")) {
ar_two_rank(pl, devs, n.c_str());
ar_two_rank_sweep(pl, devs, n.c_str());
ar_two_rank_late(pl, devs, n.c_str());
} else {
gather_two_rank(pl, devs, n.c_str());
}