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++

perf(expert): the stage buffers the cache fill copies from, pinned with cudaHostAlloc

Zhong Uncle475912c62afc
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp
index 76a0fa891..f4f43a61c 100644
--- a/include/strata/core/expert_source.hpp
+++ b/include/strata/core/expert_source.hpp
@@ -679,8 +679,16 @@ private:
// reused only once `kStageAge` layer changes have passed since its blob was last asked for, so a pointer holds
// through the layer it was asked in and the next ones (the pool computes a layer's misses before the next).
static constexpr uint64_t kStageAge = 3;
+ // A stage buffer feeds cudaMemcpyAsync (the cache fill), so it is cudaHostAlloc'd where the driver allows:
+ // a pageable source would be staged through the driver's bounce buffer - an extra copy at a fraction of
+ // the transfer rate, with the calling thread doing it. `pinned` picks the free (defined in the .cpp: the
+ // header has no cuda_runtime.h).
+ struct StageBufFree {
+ bool pinned = false;
+ void operator()(uint8_t* p) const noexcept;
+ };
std::mutex stage_mu_;
- std::vector<std::unique_ptr<uint8_t[]>> stage_buf_;
+ std::vector<std::unique_ptr<uint8_t[], StageBufFree>> stage_buf_;
std::vector<int64_t> stage_key_;
std::vector<uint64_t> stage_epoch_, stage_used_;
std::vector<char> stage_busy_; ///< being filled (outside stage_mu_): never a victim
@@ -696,6 +704,7 @@ private:
uint64_t epoch_ = 0;
int64_t last_layer_ = -1;
bool stage_grew_ = false;
+ bool stage_pin_said_ = false, stage_pin_failed_said_ = false; ///< the one-time notes in claim_stage
std::atomic<int64_t> ram_reads_{0};
std::atomic<uint64_t> file_read_bytes_{0};
// ---- the Linux I/O path (set_io_prefetch)
diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp
index 04fa1cec2..99d185e9e 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -1142,6 +1142,11 @@ bool FileExpertSource::copy_from_files(int64_t layer, int64_t expert, uint8_t* d
return true;
}
+void FileExpertSource::StageBufFree::operator()(uint8_t* p) const noexcept {
+ if (pinned) (void) cudaFreeHost(p);
+ else delete[] p;
+}
+
// A blob assembled from the three role slices. The buffer of a (layer, expert) is reused for another only once
// its blob has not been asked for during `kStageAge` layers (begin_layer) or 256 assemblies, whichever comes
// first, and never while it is being filled: the pool computes a layer's misses before it starts the next, and a
@@ -1170,8 +1175,24 @@ bool FileExpertSource::claim_stage(int64_t key, size_t& v, bool& fill, bool ahea
v = i;
}
if (v == stage_buf_.size()) {
- stage_buf_.emplace_back(new (std::nothrow) uint8_t[(size_t) stage_blob_]);
- if (!stage_buf_.back()) { stage_buf_.pop_back(); return false; }
+ // Pinned, where the driver allows: a stage buffer is the source of the cache fill's cudaMemcpyAsync,
+ // and a pageable source is staged through the driver's bounce buffer - an extra copy at a fraction of
+ // the transfer rate, on the calling thread. A failure falls back to pageable for this buffer.
+ void* p = nullptr;
+ const cudaError_t pin_err = cudaHostAlloc(&p, (size_t) stage_blob_, cudaHostAllocDefault);
+ const bool pinned = pin_err == cudaSuccess && p != nullptr;
+ if (!pinned) (void) cudaGetLastError(); // the failed alloc's sticky error is ours, not the caller's
+ uint8_t* raw = pinned ? (uint8_t*) p : new (std::nothrow) uint8_t[(size_t) stage_blob_];
+ if (raw == nullptr) return false;
+ stage_buf_.emplace_back(raw, StageBufFree{pinned});
+ if (pinned && !stage_pin_said_) {
+ stage_pin_said_ = true;
+ std::fprintf(stderr, "FileExpertSource: the stage buffers are pinned (cudaHostAlloc)\n");
+ } else if (!pinned && !stage_pin_failed_said_) {
+ stage_pin_failed_said_ = true;
+ std::fprintf(stderr, "FileExpertSource: cudaHostAlloc failed for a stage buffer (%s) - pageable it is\n",
+ cudaGetErrorString(pin_err));
+ }
stage_key_.push_back(-1);
stage_epoch_.push_back(0);
stage_used_.push_back(0);