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++
One prefetch design (#1348): the router look-ahead feeds the Foresight swap space (ForesightSwap::predict, STRATA_FS_AHEAD), slot state under a mutex
Niko1221ef362cc4abbb
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hppindex f4f43a61c..af9af8652 100644--- a/include/strata/core/expert_source.hpp+++ b/include/strata/core/expert_source.hpp@@ -205,6 +205,7 @@ protected:/// next - takes each token's top `k` experts, drops the ones the GPU cache or the RAM copy holds, and asks the/// source to `warm` the rest, so their pages are on the way while layer l computes. A prediction only warms pages:/// it never changes which experts are computed or how.+struct ForesightSwap;class RouterLookahead {public:RouterLookahead() = default;@@ -213,7 +214,11 @@ public:RouterLookahead& operator=(const RouterLookahead&) = delete;/// `routers[l]`: layer l's ffn_gate_inp as BF16 bits, n_expert rows of n_embd.bool start(std::vector<std::vector<uint16_t>> routers, int64_t n_embd, int64_t n_expert, int k, ExpertSource* src,- std::string& err);+ std::string& err, bool allow_cold_source = false);+ /// One prefetch design (#1348): the predicted experts that no cache holds also go to the Foresight swap space+ /// (ForesightSwap::predict), which copies them to VRAM ahead of the layer. nullptr detaches it (waits for a+ /// prediction in progress), so the swap space can be destroyed first.+ void set_foresight(ForesightSwap* fs);/// Layer `layer`'s MoE input for `n_tok` tokens (host floats): predict and warm layer + 1. Never waits: a/// prediction still running for an earlier layer makes this one skip.void submit(int64_t layer, const float* x, int64_t n_tok, const int32_t* host_res);@@ -238,6 +243,7 @@ private:bool quit_ = false, pending_ = false, busy_ = false;int64_t layer_ = -1, n_tok_ = 0;const int32_t* host_res_ = nullptr;+ ForesightSwap* fs_ = nullptr; ///< (under mu_)std::vector<float> x_;std::atomic<int64_t> predicted_{0}, skipped_{0};std::atomic<uint64_t> busy_us_{0};diff --git a/include/strata/core/foresight_swap.hpp b/include/strata/core/foresight_swap.hppindex a689f8fc5..379f4079e 100644--- a/include/strata/core/foresight_swap.hpp+++ b/include/strata/core/foresight_swap.hpp@@ -13,6 +13,9 @@// is simply not used (the CPU computes the expert as before). Not with STRATA_VERIFY_DEVICE_PLAN (the device would// plan resident layers without the host).//+// With STRATA_FS_AHEAD=1 (default 1 when the swap space is on) the router look-ahead (RouterLookahead, one layer ahead on the+// previous layer's MoE input) also feeds the slots, not only the page warming of the file tier.+//// Switches: STRATA_FS_SLOTS (per layer and card), STRATA_FS_BUDGET (copies per card per finished window, default 32),// STRATA_FS_ADMIT (misses within the last 4 windows before an expert is copied, default 1), STRATA_FS_DEPTH,// STRATA_FS_VERIFY=1 (test mode: every slot is read back and compared with its source the first time it is used).@@ -58,7 +61,7 @@ struct ForesightSwap {std::atomic<uint64_t> completed{0}; ///< finished verify windows (the serve loops count them)std::atomic<bool> failed{false};// counters (dispatch thread)- uint64_t hits = 0, copies = 0, busy = 0, over_budget = 0, pending = 0;+ uint64_t hits = 0, copies = 0, busy = 0, over_budget = 0, pending = 0, predicted = 0; // (under smu_)bool verify = false; ///< STRATA_FS_VERIFY=1: read every landed slot back and compare (slow, tests)uint64_t verify_ok = 0, verify_bad = 0;@@ -71,9 +74,16 @@ struct ForesightSwap {bool take(int64_t layer, int32_t expert, unsigned long long& ptr);/// The pool, for an expert the CPU computes now: maybe copy it into a slot of its layer for the next windows.void note_miss(ExpertSource* src, int64_t layer, int32_t expert);+ /// The router look-ahead (any thread), for an expert the next layer's router predicts and no cache holds: maybe copy+ /// it into a slot of that layer now, so that the copy has landed when the layer's plan is made. One prefetch design+ /// with note_miss (the same slots, budget and reuse rule); a prediction only ever moves bytes, the plan is still made+ /// by the host pool and the model's own router stays authoritative.+ void predict(ExpertSource* src, int64_t layer, int32_t expert);std::string report() const;private:+ void request_locked(ExpertSource* src, int64_t layer, int32_t expert, bool predicted);+ std::mutex smu_; ///< slot, where, card_* and the counters: the pool thread and the look-ahead threadstd::mutex mu_;std::condition_variable cv_;std::deque<int64_t> queue_; ///< slot indices whose copy the filler issuesdiff --git a/src/core/expert_source.cpp b/src/core/expert_source.cppindex dc77a2802..f79d7cd65 100644--- a/src/core/expert_source.cpp+++ b/src/core/expert_source.cpp@@ -1848,9 +1848,20 @@ RouterLookahead::~RouterLookahead() {if (thread_.joinable()) thread_.join();}+void RouterLookahead::set_foresight(ForesightSwap* fs) {+ for (;;) {+ {+ std::lock_guard<std::mutex> lk(mu_);+ if (fs != nullptr || !busy_) { fs_ = fs; return; }+ }+ // detaching: a prediction in progress finishes first (the swap space is about to go away)+ std::this_thread::sleep_for(std::chrono::milliseconds(1));+ }+}+bool RouterLookahead::start(std::vector<std::vector<uint16_t>> routers, int64_t n_embd, int64_t n_expert, int k,- ExpertSource* src, std::string& err) {- if (src == nullptr || !src->warms()) { err = "RouterLookahead: the expert source does not warm"; return false; }+ ExpertSource* src, std::string& err, bool allow_cold_source) {+ if (src == nullptr || (!allow_cold_source && !src->warms())) { err = "RouterLookahead: the expert source does not warm"; return false; }if (n_embd % 8 != 0) { err = "RouterLookahead: n_embd is not a multiple of 8"; return false; }for (const auto& r : routers)if (r.size() != (size_t) (n_embd * n_expert)) { err = "RouterLookahead: a router of another shape"; return false; }@@ -1885,6 +1896,7 @@ void RouterLookahead::run() {for (;;) {int64_t layer, nt;const int32_t* host_res;+ ForesightSwap* fs;{std::unique_lock<std::mutex> lk(mu_);cv_.wait(lk, [&] { return quit_ || pending_; });@@ -1894,6 +1906,7 @@ void RouterLookahead::run() {layer = layer_;nt = n_tok_;host_res = host_res_;+ fs = fs_;}const auto t0 = std::chrono::steady_clock::now();const int64_t layer0 = layer;@@ -1941,6 +1954,8 @@ void RouterLookahead::run() {}}src_->warm(layer, want.data(), (int64_t) want.size());+ if (fs != nullptr && dj == 0) // the next layer only: a deeper guess is too often wrong to spend a copy on+ for (const int64_t e : want) fs->predict(src_, layer, (int32_t) e);predicted_.fetch_add((int64_t) want.size(), std::memory_order_relaxed);}busy_us_.fetch_add((uint64_t) std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count(),@@ -4150,6 +4165,7 @@ void ForesightSwap::fill_loop() {}bool ForesightSwap::take(int64_t layer, int32_t expert, unsigned long long& ptr) {+ std::lock_guard<std::mutex> smu_lk(smu_);const int s = where[(size_t) (layer * n_expert + expert)];if (s < 0) return false;Slot& sl = slot[(size_t) (layer * slots + s)];@@ -4186,11 +4202,22 @@ bool ForesightSwap::take(int64_t layer, int32_t expert, unsigned long long& ptr)}void ForesightSwap::note_miss(ExpertSource* src, int64_t layer, int32_t expert) {+ std::lock_guard<std::mutex> smu_lk(smu_);+ request_locked(src, layer, expert, false);+}++void ForesightSwap::predict(ExpertSource* src, int64_t layer, int32_t expert) {+ if (layer < 0 || layer >= n_layers || expert < 0 || expert >= n_expert) return;+ std::lock_guard<std::mutex> smu_lk(smu_);+ request_locked(src, layer, expert, true);+}++void ForesightSwap::request_locked(ExpertSource* src, int64_t layer, int32_t expert, bool predicted_copy) {if (failed.load(std::memory_order_relaxed)) return;const size_t key = (size_t) (layer * n_expert + expert);if (where[key] >= 0) return; // already on its wayconst uint64_t now = completed.load(std::memory_order_acquire);- if (admit > 1) {+ if (admit > 1 && !predicted_copy) {if (now > miss_at[key] + 4) miss_cnt[key] = 0;miss_at[key] = now;if (miss_cnt[key] < 255) ++miss_cnt[key];@@ -4229,6 +4256,7 @@ void ForesightSwap::note_miss(ExpertSource* src, int64_t layer, int32_t expert)where[key] = (int16_t) best;++card_used[(size_t) c];++copies;+ if (predicted_copy) ++predicted;{std::lock_guard<std::mutex> lk(mu_);queue_.push_back(layer * slots + best);@@ -4237,11 +4265,12 @@ void ForesightSwap::note_miss(ExpertSource* src, int64_t layer, int32_t expert)}std::string ForesightSwap::report() const {+ std::lock_guard<std::mutex> smu_lk(const_cast<std::mutex&>(smu_));char buf[320];- std::snprintf(buf, sizeof buf, "foresight swap: %llu experts served from the swap space, %llu copies, %llu not landed "- "yet, %llu no free slot, %llu over budget%s",- (unsigned long long) hits, (unsigned long long) copies, (unsigned long long) pending,- (unsigned long long) busy, (unsigned long long) over_budget, verify ? "" : "");+ std::snprintf(buf, sizeof buf, "foresight swap: %llu experts served from the swap space, %llu copies (%llu from the "+ "router look-ahead), %llu not landed yet, %llu no free slot, %llu over budget%s",+ (unsigned long long) hits, (unsigned long long) copies, (unsigned long long) predicted,+ (unsigned long long) pending, (unsigned long long) busy, (unsigned long long) over_budget, verify ? "" : "");std::string r = buf;if (verify) r += ", verified " + std::to_string(verify_ok) + " ok / " + std::to_string(verify_bad) + " BAD";return r;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex f1984d054..c243f8f22 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -4869,7 +4869,12 @@ int main(int argc, char** argv) {// input predicts its experts and their pages are warmed meanwhile. It only warms pages; STRATA_LOOKAHEAD=0 is// the A/B arm, STRATA_LOOKAHEAD_K the experts per token (default 10).strata::core::RouterLookahead lookahead;- if (srcp == &src && src.warms() && [] { const char* v = std::getenv("STRATA_LOOKAHEAD"); return v == nullptr || std::atoi(v) != 0; }()) {+ // #1348, one prefetch design: with the Foresight swap space on (STRATA_FS_SLOTS) the look-ahead also runs on the pinned-RAM+ // tiers, where there are no pages to warm, and feeds its predictions to the swap space (STRATA_FS_AHEAD=0: not)+ const bool fs_ahead = srcp != nullptr && [] { const char* v = std::getenv("STRATA_FS_SLOTS"); return v != nullptr && std::atoi(v) > 0; }() &&+ [] { const char* v = std::getenv("STRATA_FS_AHEAD"); return v == nullptr || std::atoi(v) != 0; }();+ if (srcp != nullptr && ((srcp == &src && src.warms()) || fs_ahead) &&+ [] { const char* v = std::getenv("STRATA_LOOKAHEAD"); return v == nullptr || std::atoi(v) != 0; }()) {std::vector<std::vector<uint16_t>> routers((size_t) g.n_layers);bool ok = true;for (int64_t l = 0; l < g.n_layers && ok; ++l) {@@ -4881,11 +4886,11 @@ int main(int argc, char** argv) {ok = cudaMemcpy(routers[(size_t) l].data(), w->data, (size_t) w->bytes, cudaMemcpyDeviceToHost) == cudaSuccess;}const char* kv = std::getenv("STRATA_LOOKAHEAD_K");- if (ok && lookahead.start(std::move(routers), g.n_embd, g.n_expert, kv ? std::atoi(kv) : 10, &src, err)) {+ if (ok && lookahead.start(std::move(routers), g.n_embd, g.n_expert, kv ? std::atoi(kv) : 10, srcp, err, fs_ahead)) {drive.d.lookahead = &lookahead;if (const char* dv = std::getenv("STRATA_IO_PREFETCH_DEPTH"); dv != nullptr && std::atoi(dv) > 0)lookahead.set_depth(std::atoi(dv));- else if (src.io_prefetch())+ else if (srcp == &src && src.io_prefetch())lookahead.set_depth(2);std::fprintf(stderr, "strata generate: routing-aware prefetch of the file tier on (the next layer's router)\n");} else {@@ -7261,7 +7266,13 @@ int main(int argc, char** argv) {for (int64_t l = 0; l < g.n_layers; ++l) card[(size_t) l] = multi_gpu ? stage_of(l) : 0;fs_swap.reset(strata::core::foresight_swap_from_env(g.n_layers, g.n_expert, card, devs));drive.d.fs = fs_swap.get();+ if (fs_swap && drive.d.lookahead != nullptr) lookahead.set_foresight(fs_swap.get());}+ // the look-ahead thread must not touch the swap space once it is gone (declared after it: destroyed first)+ struct FsDetach {+ strata::core::RouterLookahead* la;+ ~FsDetach() { la->set_foresight(nullptr); }+ } fs_detach{&lookahead};drive.d.pcie_num = std::max(0, std::min(256, (int) (o.pcie_frac * 256.0 + 0.5)));const bool all_experts_resident = !host_res.empty() &&std::all_of(host_res.begin(), host_res.end(), [](int32_t r) { return r >= 0; });