From 68072d30d439a0a0b4f7cc73f844b903af0dab57 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 29 Sep 2026 10:43:11 +0200 Subject: [PATCH] D-4: refill the prompt path's lent cache slots with queued copies and one wait The same copies on the same (legacy) stream, but queued instead of waited on one by one: the same bytes, ordered the same against earlier work. Q2_0 on a 5070 (1,782 slots): 162 -> 96 ms per prompt, 4K and 32K outputs identical. The rest (publishing slots in the background while decode starts) would hide the last ~2% of the time to first token on a 4K prompt; not done. STRATA_REFILL_BLOCKING=1 keeps the old loop. --- include/strata/core/expert_cache.hpp | 4 ++++ src/core/expert_cache.cpp | 26 ++++++++++++++++++++++++++ src/program/generate.cpp | 24 ++++++++++++++++++------ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/include/strata/core/expert_cache.hpp b/include/strata/core/expert_cache.hpp index da856d75..8aa06255 100644 --- a/include/strata/core/expert_cache.hpp +++ b/include/strata/core/expert_cache.hpp @@ -126,6 +126,10 @@ public: /// against a `cudaStreamNonBlocking` one. The first version used the async form and `verify_slot` refused /// the whole run with "slot 0 differs from the arena at byte 0" - which is the check doing its job. bool fill_slot_blocking(int32_t slot, const uint8_t* host_blob, std::string& err, int64_t bytes = 0); + /// perf-review D-4: `fill_slot_blocking`'s copy on the same (legacy) stream, but queued: many slots are refilled + /// with one `sync_queued` at the end instead of a wait per slot. Same ordering against earlier work, same bytes. + bool fill_slot_queued(int32_t slot, const uint8_t* host_blob, std::string& err, int64_t bytes = 0); + bool sync_queued(std::string& err); /// Reads `slot` back to the host and compares it to `host_blob`, byte for byte. **THE ONLY THING THAT SAYS /// THE CACHE HOLDS THE EXPERT IT CLAIMS TO.** A slot table that is right about indices and wrong about diff --git a/src/core/expert_cache.cpp b/src/core/expert_cache.cpp index 9f5fc981..5138f3b5 100644 --- a/src/core/expert_cache.cpp +++ b/src/core/expert_cache.cpp @@ -265,6 +265,32 @@ bool ExpertCache::fill_slot_blocking(int32_t slot, const uint8_t* host_blob, std return true; } +bool ExpertCache::fill_slot_queued(int32_t slot, const uint8_t* host_blob, std::string& err, int64_t bytes) { + const size_t n = (size_t) (bytes > 0 && bytes <= blob_ ? bytes : blob_); + uint8_t* dst = device_slot(slot); + if (dst == nullptr || host_blob == nullptr) { + err = dst == nullptr ? "ExpertCache::fill_slot_queued: slot outside the arena" + : "ExpertCache::fill_slot_queued: the host blob is null"; + return false; + } + const cudaError_t e = cudaMemcpyAsync(dst, host_blob, n, cudaMemcpyHostToDevice, (cudaStream_t) 0); + if (e != cudaSuccess) { + err = std::string("ExpertCache::fill_slot_queued: ") + cudaGetErrorString(e); + return false; + } + ++fills_; + return true; +} + +bool ExpertCache::sync_queued(std::string& err) { + const cudaError_t e = cudaStreamSynchronize((cudaStream_t) 0); + if (e != cudaSuccess) { + err = std::string("ExpertCache::sync_queued: ") + cudaGetErrorString(e); + return false; + } + return true; +} + bool ExpertCache::verify_slot(int32_t slot, const uint8_t* host_blob, std::string& err, int64_t bytes) { const int64_t nb = bytes > 0 && bytes <= blob_ ? bytes : blob_; const uint8_t* src = device_slot(slot); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index aa2bbe4c..9993f1ab 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -87,6 +87,11 @@ #include namespace { +// perf-review D-4: the lent slots are refilled with queued copies and one wait; STRATA_REFILL_BLOCKING=1 waits on each +bool refill_blocking() { + static const bool v = std::getenv("STRATA_REFILL_BLOCKING") != nullptr; + return v; +} using Clock = std::chrono::steady_clock; @@ -3798,13 +3803,15 @@ int main(int argc, char** argv) { auto refill = [&](std::string& e) -> bool { if (lent_now.empty()) return true; tr("refill start", (long long) lent_now.size()); - for (const auto& [i, slot] : lent_now) { + for (const auto& [i, slot] : lent_now) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each) const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert); - if (b == nullptr || !xcache.fill_slot_blocking(slot, b, e, - (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert))) + const int64_t nb = (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert); + if (b == nullptr || !(refill_blocking() ? xcache.fill_slot_blocking(slot, b, e, nb) + : xcache.fill_slot_queued(slot, b, e, nb))) return false; host_res[(size_t) i] = slot; } + if (!xcache.sync_queued(e)) return false; cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); lent_now.clear(); lent_chunk = 0; @@ -4250,15 +4257,20 @@ int main(int argc, char** argv) { // refill the lent slots from the arena and give them back to the decode tier if (!lent.empty()) { const Clock::time_point tr = Clock::now(); - for (const auto& [i, slot] : lent) { + for (const auto& [i, slot] : lent) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each) const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert); - if (b == nullptr || !xcache.fill_slot_blocking(slot, b, err, - (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert))) { + const int64_t nb = (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert); + if (b == nullptr || !(refill_blocking() ? xcache.fill_slot_blocking(slot, b, err, nb) + : xcache.fill_slot_queued(slot, b, err, nb))) { std::fprintf(stderr, "strata generate: refilling a lent slot failed: %s\n", err.c_str()); return 1; } host_res[(size_t) i] = slot; } + if (!xcache.sync_queued(err)) { + std::fprintf(stderr, "strata generate: refilling the lent slots failed: %s\n", err.c_str()); + return 1; + } cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); std::fprintf(stderr, "strata generate: %zu lent slots refilled in %.1f ms\n", lent.size(), std::chrono::duration(Clock::now() - tr).count());