From 104c81f005afd55a77c9648725b2dde414091840 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Tue, 20 Oct 2026 11:45:36 -0500 Subject: [PATCH] libr4d, core: two first-launch races that could change a fresh server's first reply Counter blocks: the per-(stream, output) counter blocks of the fp8a8 split-K finisher (split_ctr), the gated delta-net conv (ru_conv_ctr), the mxfp4a8 decode GEMM and the hc mix are zeroed with hipMemset, which queues on HIP's null stream and returns before it lands. The engine launches on queues of its own that need not wait for that stream, so the first launch on a new block could run beside its own zeroing: a split-K finisher counting arrivals while the memset reset the count gives that one launch a wrong output. r4d_moe.hip's router counters and the all-reduce's IPC allocations already wait; these four now do too (hipDeviceSynchronize, once per block). Seen as a greedy reply that differed on the first request of a freshly started 31B DFlash2 server (2 of 13 fresh starts on 4x R9700, 2026-10-17; every later request matched): the drafter's fp8a8 GEMMs take split_ctr blocks on their first launch, and a draft that differs moves the verify blocks, whose rounding the target's greedy choice can feel at a near-tie. No value changes otherwise. Weight transfers: resolve_weight puts the mover's `ready` wait on the issuing lane's stream, but remembered the generation it waited on per weight for the whole context. A weight resolved first on lane 1 then had its wait on lane 1 only, and the lane-0 GEMM resolving it next skipped its own and could read the slot before the copy landed. The Gemma 4 26B-A4B's dense arm is such a weight: a decode step issues it on lane 1, beside the routed arm, and an overlapped prefill on lane 0. waited_gen_ is now per lane, one record for each of the context's two lanes (set_lane admits 0 and 1 only), indexed by lane_ like the other per-lane state: at worst one more stream wait. Inert where no transfer is in flight (static weights, the 31B on four cards); it matters wherever weights stream. Co-Authored-By: Claude Opus 5.5 --- core/runtime/ctx.cpp | 2 +- core/runtime/ctx.h | 7 ++++++- core/runtime/issue.cpp | 10 ++++++---- ...d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip | 5 ++++- libr4d/r4d_gemm_fp8a8.hip | 5 ++++- libr4d/r4d_gemm_mxfp4a8_decode.hip | 7 ++++++- libr4d/r4d_hc_bf16.hip | 5 ++++- 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index da0d20ce..64487a86 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -271,7 +271,7 @@ int Ctx::bind_weights() { Program& P = *program_; w_tmpl_.assign(P.weights.size(), RadTensor{}); w_bits_.assign(P.weights.size(), 0); - waited_gen_.assign(P.weights.size(), UINT32_MAX); + for (auto& g : waited_gen_) g.assign(P.weights.size(), UINT32_MAX); w_direct_.assign(P.weights.size(), 0); for (size_t i = 1; i < P.weights.size(); ++i) { diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index 95ea0caf..36f4057b 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -685,7 +685,12 @@ private: int level_ = 0; std::vector w_tmpl_; std::vector w_bits_; - std::vector waited_gen_; /* the generation we last made the stream wait on */ + /* The generation each lane's stream was last made to wait on, a weight. Per lane: the wait + * orders only the stream it is put on, so a weight resolved on lane 1 (the Gemma 4 26B-A4B's + * dense arm, which a decode step issues on lane 1 and an overlapped prefill on lane 0) must + * still be waited on when lane 0 resolves it. Indexed by lane_, like the other per-lane state + * here: set_lane admits lanes 0 and 1 only. */ + std::vector waited_gen_[2]; std::vector plans_; /* Ops not yet seen issued. flush_issued walks only these and drops each once it fires. */ diff --git a/core/runtime/issue.cpp b/core/runtime/issue.cpp index 086dc7b7..84228622 100644 --- a/core/runtime/issue.cpp +++ b/core/runtime/issue.cpp @@ -82,15 +82,17 @@ void* Ctx::resolve_weight(rad_weight w) { const WeightSlot* s = residency_->lookup(w); /* never blocks -- residency.h */ if (s) { p = s->ptr; - /* Wait once per generation, not per issue; a stream wait orders everything issued after - * it. Sound because the mover bumps `generation` per transfer (residency.h). */ - if (s->ready && waited_gen_[w] != s->generation) { + /* Wait once per generation and lane, not per issue; a stream wait orders everything + * issued after it on that stream, and nothing on the other lane. Sound because the mover + * bumps `generation` per transfer (residency.h). */ + std::vector& waited = waited_gen_[lane_]; + if (s->ready && waited[w] != s->generation) { /* Kept out of a recording: it orders every later play too, and moving a weight a * pass names directly re-keys the pass. */ if (rec_) tape_pause(true); rad_event_wait(cur_stream(), s->ready); /* the stream waits, not the host */ if (rec_) tape_pause(false); - waited_gen_[w] = s->generation; + waited[w] = s->generation; ++n_stream_waits_; } } diff --git a/libr4d/r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip b/libr4d/r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip index df5c79fd..40f01a1d 100644 --- a/libr4d/r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip +++ b/libr4d/r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip @@ -850,7 +850,10 @@ static unsigned int* ru_conv_ctr(RadStream s, const void* out) { if (auto it = blocks.find(key); it != blocks.end()) return it->second; void* p = nullptr; if (hipMalloc(&p, (size_t)kMaxConvCtr * sizeof(unsigned int)) != hipSuccess) return nullptr; - if (hipMemset(p, 0, (size_t)kMaxConvCtr * sizeof(unsigned int)) != hipSuccess) { + // Zero before any launch reads it: the memset is on the null stream, which the engine's streams + // need not wait for, so wait here (as r4d_moe.hip's router counters do). + if (hipMemset(p, 0, (size_t)kMaxConvCtr * sizeof(unsigned int)) != hipSuccess || + hipDeviceSynchronize() != hipSuccess) { (void)hipFree(p); return nullptr; } diff --git a/libr4d/r4d_gemm_fp8a8.hip b/libr4d/r4d_gemm_fp8a8.hip index bbaf0964..970ed3b3 100644 --- a/libr4d/r4d_gemm_fp8a8.hip +++ b/libr4d/r4d_gemm_fp8a8.hip @@ -824,7 +824,10 @@ unsigned int* split_ctr(RadStream s, const void* out) { if (auto it = blocks.find(key); it != blocks.end()) return it->second; void* p = nullptr; if (hipMalloc(&p, (size_t)kMaxCtr * sizeof(unsigned int)) != hipSuccess) return nullptr; - if (hipMemset(p, 0, (size_t)kMaxCtr * sizeof(unsigned int)) != hipSuccess) { + /* Zero before any launch reads it: the memset is on the null stream, which the engine's + * streams need not wait for, so wait here (as r4d_moe.hip's router counters do). */ + if (hipMemset(p, 0, (size_t)kMaxCtr * sizeof(unsigned int)) != hipSuccess || + hipDeviceSynchronize() != hipSuccess) { (void)hipFree(p); return nullptr; } diff --git a/libr4d/r4d_gemm_mxfp4a8_decode.hip b/libr4d/r4d_gemm_mxfp4a8_decode.hip index eaab1ce8..feb173c3 100644 --- a/libr4d/r4d_gemm_mxfp4a8_decode.hip +++ b/libr4d/r4d_gemm_mxfp4a8_decode.hip @@ -472,7 +472,12 @@ unsigned* mxd_out_ctr(const void* out) { const size_t n = R4D_MXD_MAX_N / (R4D_MXD_DWN * 16); unsigned* p = nullptr; if (hipMalloc((void**)&p, n * sizeof(unsigned)) != hipSuccess) return nullptr; - if (hipMemset(p, 0, n * sizeof(unsigned)) != hipSuccess) { (void)hipFree(p); return nullptr; } + // Zero before any launch reads it: the memset is on the null stream, which the engine's streams + // need not wait for, so wait here (as r4d_moe.hip's router counters do). + if (hipMemset(p, 0, n * sizeof(unsigned)) != hipSuccess || hipDeviceSynchronize() != hipSuccess) { + (void)hipFree(p); + return nullptr; + } blocks.emplace(key, p); return p; } diff --git a/libr4d/r4d_hc_bf16.hip b/libr4d/r4d_hc_bf16.hip index 0614b06c..a7d2a5e1 100644 --- a/libr4d/r4d_hc_bf16.hip +++ b/libr4d/r4d_hc_bf16.hip @@ -2153,7 +2153,10 @@ unsigned int* hc_mix_ctr(RadStream s) { if (fi < 0) return nullptr; void* p = nullptr; if (hipMalloc(&p, (size_t)kMaxMixCtr * sizeof(unsigned int)) != hipSuccess) return nullptr; - if (hipMemset(p, 0, (size_t)kMaxMixCtr * sizeof(unsigned int)) != hipSuccess) { + /* Zero before any launch reads it: the memset is on the null stream, which the engine's + * streams need not wait for, so wait here (as r4d_moe.hip's router counters do). */ + if (hipMemset(p, 0, (size_t)kMaxMixCtr * sizeof(unsigned int)) != hipSuccess || + hipDeviceSynchronize() != hipSuccess) { (void)hipFree(p); return nullptr; }