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)
This commit is contained in:
uncle daddy authored and Niko1221 committed 2026-10-07 04:07:42 +02:00
1 parent 49c50f49f7
commit bc089d5d97
2 files changed
+7 -2

No files matched your search

+1
View File
@@ -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
+6 -2
View File
@@ -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();
}
};