From bc089d5d972bd3514767842e2ecae2a7e8eb7ada Mon Sep 17 00:00:00 2001 From: uncle daddy Date: Tue, 6 Oct 2026 13:11:55 +0200 Subject: [PATCH] prefill: let Stager threads sleep instead of yield-spinning The prefill Stager waited by spinning: a yield loop on `issued`, and cudaEventSynchronize on events created without cudaEventBlockingSync (which spins under the default scheduler). With --mmap-experts there are 32 stagers. On Linux they inherit the host thread's single-core pin and starve it: on a DGX Spark (GB10) prompts were read ~10x slower (IQ3_S 8K 91 -> 1,222 tok/s, 30K 88 -> 1,388 tok/s with this change). Unpinned instead, the 32 spinners took every core for the whole prompt (process CPU ~1,800%); with the waits sleeping it is ~100%. - ring wait: std::atomic::wait, with notify_all in issued_one() and finish() - dma_done events: cudaEventDisableTiming | cudaEventBlockingSync - HIP shim: cudaEventBlockingSync -> hipEventBlockingSync (untested) Decode speed and output tokens are unchanged. (PR #1057) --- include/strata/hip_compat/cuda_runtime.h | 1 + src/prefill/prefill.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.h index 8be095c1..d669f0fe 100644 --- a/include/strata/hip_compat/cuda_runtime.h +++ b/include/strata/hip_compat/cuda_runtime.h @@ -36,6 +36,7 @@ #define cudaEventCreateWithFlags hipEventCreateWithFlags #define cudaEventDestroy hipEventDestroy #define cudaEventDisableTiming hipEventDisableTiming +#define cudaEventBlockingSync hipEventBlockingSync #define cudaEventElapsedTime hipEventElapsedTime #define cudaEventQuery hipEventQuery #define cudaEventRecord hipEventRecord diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 8499a36f..3d5500a2 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -360,7 +360,7 @@ struct Stager { pageable[(size_t) i].resize(blob_bytes); buf[i] = pageable[(size_t) i].data(); } - if (cudaEventCreateWithFlags(&dma_done[i], cudaEventDisableTiming) != cudaSuccess) return false; + if (cudaEventCreateWithFlags(&dma_done[i], cudaEventDisableTiming | cudaEventBlockingSync) != cudaSuccess) return false; } cudaGetDevice(&device); for (int t = 0; t < nthreads; ++t) threads.emplace_back([this] { work(); }); @@ -391,8 +391,10 @@ struct Stager { const int j = claim(seen); if (j < 0) { active.fetch_sub(1, std::memory_order_acq_rel); break; } const int b = j % kRing; + // The copies run ahead of the DMAs, so most of these threads spend most of a prompt waiting here: sleep + // (atomic wait, and a blocking-sync event below), not a yield spin - 32 spinners took every core. if (j >= kRing) // job j - kRing's DMA from this buffer is queued - while (issued.load(std::memory_order_acquire) <= j - kRing) std::this_thread::yield(); + for (int i; (i = issued.load(std::memory_order_acquire)) <= j - kRing;) issued.wait(i); // and done - for a generation's first kRing jobs that is the previous generation's last DMA from // the buffer, which nothing else waits for when a chunk ends without a sync (no MTP) or the DMA // was a ring entry the routing skipped (an event never recorded returns at once) @@ -441,11 +443,13 @@ struct Stager { void issued_one(int j, cudaStream_t copy) { cudaEventRecord(dma_done[j % kRing], copy); issued.store(j + 1, std::memory_order_release); + issued.notify_all(); } /// No job is running after this (the end of a layer, or an early return in the middle of one). void finish() { head.store((uint64_t) gen << 32, std::memory_order_release); // n = 0: nothing more to claim issued.store(1 << 30, std::memory_order_release); + issued.notify_all(); while (active.load(std::memory_order_acquire) != 0) std::this_thread::yield(); } };