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; }