Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

D-4: refill the prompt path's lent cache slots with queued copies and one wait

Niko122168072d30d439
diff --git a/include/strata/core/expert_cache.hpp b/include/strata/core/expert_cache.hpp
index da856d75d..8aa062557 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 9f5fc981f..5138f3b50 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 aa2bbe4c5..9993f1ab0 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -87,6 +87,11 @@
#include <vector>
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<double, std::milli>(Clock::now() - tr).count());