From 6a5902832cfab24dbc45401ea56d7d81078ed657 Mon Sep 17 00:00:00 2001 From: sergiywith Date: Thu, 1 Oct 2026 18:12:36 +0300 Subject: [PATCH] Prompt path: a stager buffer's first job of a generation waits for the previous DMA from it Stager::work waited for a buffer's DMA only from the generation's kRing-th job on (j >= kRing); a generation's first kRing jobs wrote their buffers straight away. The previous generation's last DMAs from those buffers are only known to be done where something else waits for them: the routing sync before a staged layer's start(), or the sync at the end of a chunk with an MTP drafter or stages. A stream_all chunk that ends without one (no --mtp), or a ring entry the routing skipped (its copy is never waited for), leaves a window where the next chunk's first jobs overwrite a pinned buffer a DMA may still read. The event is now waited for on every job; an event never recorded, or already complete, returns at once. IQ2_XS with --mmap-experts --resident-budget-gib 16 (12,144 of 48,668 streamed experts through the stager): the same tokens on a 32K prompt, prefill 6,491 vs 6,546 ms. --- src/prefill/prefill.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index ff069fc4..d292e4ff 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -272,10 +272,12 @@ 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; - if (j >= kRing) { + 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(); - cudaEventSynchronize(dma_done[b]); - } + // 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) + cudaEventSynchronize(dma_done[b]); const Job& jb = jobs[(size_t) j]; if (jb.from == nullptr) std::memcpy(buf[b], jb.src, jb.bytes); else if (!jb.from->copy_blob(jb.l, jb.e, buf[b])) {