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++
Foresight swap space: per-layer VRAM slots refilled on their own stream with recently missed experts, served as GPU groups at the pool's existing per-layer decision (STRATA_FS_SLOTS, off by default)
Andy14f9e4c50b35
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hppindex 1bbdb4130..76a0fa891 100644--- a/include/strata/core/expert_source.hpp+++ b/include/strata/core/expert_source.hpp@@ -271,7 +271,10 @@ struct GpuPlanSink {};/// The adapter's own state. One per session, reused every layer so the token path allocates nothing (P2.T10).+struct ForesightSwap;+struct ExpertDispatch {+ ForesightSwap* fs = nullptr; ///< Foresight swap space (STRATA_FS_SLOTS; null = off)strata::kernels::cpu::ExpertPool* pool = nullptr;ExpertSource* src = nullptr;RouterLookahead* lookahead = nullptr; ///< CS-T: warms the next layer's predicted file-tier expertsdiff --git a/include/strata/core/foresight_swap.hpp b/include/strata/core/foresight_swap.hppnew file mode 100644index 000000000..92bb631b7--- /dev/null+++ b/include/strata/core/foresight_swap.hpp@@ -0,0 +1,85 @@+#pragma once+// Foresight swap space (opt-in: STRATA_FS_SLOTS > 0; unset = nothing here exists and the engine is unchanged).+//+// A few VRAM slots per layer on each card, apart from the expert cache, refilled on their own copy stream with experts+// that just missed. The verify pool asks, for every expert it would otherwise give the CPU, whether that expert sits+// in a slot whose copy has landed; if so the expert becomes a plain GPU group (kind 0) pointing at the slot, exactly+// like a cache hit - the grouped kernel reads whatever address the plan carries, so no kernel changes. The host pool+// decides and the GPU follows the plan, so both always agree on who computes an expert.+//+// Safety: a slot is refilled only when every window that may have read it has finished: each plan reference stamps+// the slot with the count of finished verify windows, and a slot is reusable once `depth` more windows finished+// (STRATA_FS_DEPTH, default 4; --pipeline-windows 2 keeps at most two windows in flight). A copy that has not landed+// 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).+//+// 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.+#include <atomic>+#include <condition_variable>+#include <cstdint>+#include <deque>+#include <memory>+#include <mutex>+#include <string>+#include <thread>+#include <vector>++namespace strata::core {++class ExpertSource;++struct ForesightSwap {+ struct Slot {+ int32_t expert = -1;+ bool ready = false; ///< the copy has landed (dispatch thread only)+ bool loading = false; ///< a copy is assigned and not yet seen landed (dispatch thread only)+ std::atomic<bool> issued{false}; ///< the filler has queued the copy and recorded `ev`+ uint64_t last_ref = 0; ///< finished-window count when a plan last pointed here+ unsigned long long dptr = 0; ///< the slot's device address+ const uint8_t* src = nullptr; ///< the host blob being copied in+ uint64_t bytes = 0;+ void* ev = nullptr; ///< cudaEvent_t on the layer's card+ };++ int slots = 0, budget = 32, admit = 1, depth = 4;+ int64_t n_layers = 0, n_expert = 0;+ std::unique_ptr<Slot[]> slot; ///< n_layers x slots+ std::vector<int16_t> where; ///< (layer, expert) -> slot in its layer, -1+ std::vector<uint8_t> miss_cnt; ///< STRATA_FS_ADMIT > 1: recent misses per (layer, expert)+ std::vector<uint64_t> miss_at;+ std::vector<int> layer_card, layer_dev;+ std::vector<void*> card_stream; ///< cudaStream_t per card+ std::vector<void*> card_mem; ///< the slots' allocation per card+ std::vector<int> card_dev;+ std::vector<uint64_t> card_round;+ std::vector<int> card_used;+ 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;++ /// Allocates the slots on each card and starts the filler. `card_of_layer[l]` indexes `devices` (CUDA ids).+ bool init(int slots_per_layer, int64_t layers, int64_t experts, const std::vector<int>& card_of_layer,+ const std::vector<int>& devices, std::string& err);+ ~ForesightSwap();++ /// The pool, for a routed expert that is not in the cache: true and its device address when a landed slot holds it.+ 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);+ std::string report() const;++private:+ std::mutex mu_;+ std::condition_variable cv_;+ std::deque<int64_t> queue_; ///< slot indices whose copy the filler issues+ bool stop_ = false;+ std::thread filler_;+ void fill_loop();+};++ForesightSwap* foresight_swap_from_env(int64_t layers, int64_t experts, const std::vector<int>& card_of_layer,+ const std::vector<int>& devices);++} // namespace strata::corediff --git a/src/core/expert_source.cpp b/src/core/expert_source.cppindex fcf8b71e8..8dc53de56 100644--- a/src/core/expert_source.cpp+++ b/src/core/expert_source.cpp@@ -1,5 +1,7 @@// src/core/expert_source.cpp - the adapter. See the header for the three clauses of the contract.#include "strata/core/expert_source.hpp"+#include "strata/core/foresight_swap.hpp"+#include "strata/core/on_device.hpp"#include "strata/core/remote_experts.hpp"#include "strata/core/peer_experts.hpp"#include "strata/kernels/cpu/expert_layout.hpp"@@ -3029,7 +3031,10 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32kd = 2; // multi-GPU: the second GPU computes it} else if (helper_holds(e)) {// left to the helper: kind -1 until RemoteExperts::begin() claims the row (see above)+ } else if (d.fs != nullptr && d.fs->take(d.layers, e, ptr)) {+ kd = 0; // Foresight: a landed swap-space slot holds it - a GPU group like a hit} else {+ if (d.fs != nullptr) d.fs->note_miss(d.src, d.layers, e);if (miss_rank >= nmiss - m && fetches < P.staging_cap && fetches < 64) {const uint8_t* src = d.src->pinned(d.layers, e) ? d.src->blob(d.layers, e) : nullptr;if (src != nullptr) {@@ -4014,3 +4019,211 @@ const uint8_t* ArenaExpertSource::blob(int64_t layer, int64_t expert) {}} // namespace strata::core++// ---- Foresight swap space (see include/strata/core/foresight_swap.hpp) ------------------------------------------+namespace strata::core {++bool ForesightSwap::init(int slots_per_layer, int64_t layers, int64_t experts, const std::vector<int>& card_of_layer,+ const std::vector<int>& devices, std::string& err) {+ slots = slots_per_layer;+ n_layers = layers;+ n_expert = experts;+ const size_t ns = (size_t) (layers * slots);+ slot.reset(new Slot[ns]);+ where.assign((size_t) (layers * experts), -1);+ miss_cnt.assign((size_t) (layers * experts), 0);+ miss_at.assign((size_t) (layers * experts), 0);+ layer_card = card_of_layer;+ layer_dev.resize((size_t) layers);+ card_dev = devices;+ card_stream.assign(devices.size(), nullptr);+ card_mem.assign(devices.size(), nullptr);+ card_round.assign(devices.size(), ~0ull);+ card_used.assign(devices.size(), 0);+ const auto& lay = strata::kernels::cpu::expert_layout();+ for (size_t c = 0; c < devices.size(); ++c) {+ uint64_t bytes = 0;+ for (int64_t l = 0; l < layers; ++l)+ if (card_of_layer[(size_t) l] == (int) c) bytes += (uint64_t) slots * (uint64_t) lay.blob_bytes(l);+ if (bytes == 0) continue;+ const OnDevice on(devices[c]);+ cudaStream_t s = nullptr;+ if (cudaMalloc(&card_mem[c], bytes) != cudaSuccess ||+ cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking) != cudaSuccess) {+ cudaGetLastError();+ err = "cannot allocate " + std::to_string(bytes >> 20) + " MiB on CUDA" + std::to_string(devices[c]) ++ " (raise --vram-reserve-mib by about that much)";+ return false;+ }+ card_stream[c] = s;+ uint64_t off = 0;+ for (int64_t l = 0; l < layers; ++l) {+ if (card_of_layer[(size_t) l] != (int) c) continue;+ layer_dev[(size_t) l] = devices[c];+ const uint64_t bb = (uint64_t) lay.blob_bytes(l);+ for (int s2 = 0; s2 < slots; ++s2) {+ Slot& sl = slot[(size_t) (l * slots + s2)];+ sl.dptr = (unsigned long long) ((uint8_t*) card_mem[c] + off);+ sl.bytes = bb;+ cudaEvent_t ev = nullptr;+ if (cudaEventCreateWithFlags(&ev, cudaEventDisableTiming) != cudaSuccess) {+ cudaGetLastError();+ err = "cannot create the copy events";+ return false;+ }+ sl.ev = ev;+ off += bb;+ }+ }+ }+ filler_ = std::thread([this] { fill_loop(); });+ return true;+}++ForesightSwap::~ForesightSwap() {+ {+ std::lock_guard<std::mutex> lk(mu_);+ stop_ = true;+ }+ cv_.notify_all();+ if (filler_.joinable()) filler_.join();+ for (size_t c = 0; c < card_dev.size(); ++c) {+ const OnDevice on(card_dev[c]);+ if (card_stream[c]) cudaStreamSynchronize((cudaStream_t) card_stream[c]);+ if (card_mem[c]) cudaFree(card_mem[c]);+ if (card_stream[c]) cudaStreamDestroy((cudaStream_t) card_stream[c]);+ }+ if (slot)+ for (int64_t i = 0; i < n_layers * slots; ++i)+ if (slot[(size_t) i].ev) cudaEventDestroy((cudaEvent_t) slot[(size_t) i].ev);+}++void ForesightSwap::fill_loop() {+ int cur_dev = -1;+ for (;;) {+ int64_t idx;+ {+ std::unique_lock<std::mutex> lk(mu_);+ cv_.wait(lk, [&] { return stop_ || !queue_.empty(); });+ if (stop_) return;+ idx = queue_.front();+ queue_.pop_front();+ }+ Slot& sl = slot[(size_t) idx];+ const int64_t l = idx / slots;+ const int dev = layer_dev[(size_t) l];+ if (dev != cur_dev) { cudaSetDevice(dev); cur_dev = dev; }+ cudaStream_t s = (cudaStream_t) card_stream[(size_t) layer_card[(size_t) l]];+ if (cudaMemcpyAsync((void*) sl.dptr, sl.src, sl.bytes, cudaMemcpyHostToDevice, s) != cudaSuccess ||+ cudaEventRecord((cudaEvent_t) sl.ev, s) != cudaSuccess) {+ cudaGetLastError();+ if (!failed.exchange(true)) std::fprintf(stderr, "strata serve: foresight swap: a copy failed - swap space off\n");+ continue; // never marked issued: the slot is never used+ }+ sl.issued.store(true, std::memory_order_release);+ }+}++bool ForesightSwap::take(int64_t layer, int32_t expert, unsigned long long& ptr) {+ const int s = where[(size_t) (layer * n_expert + expert)];+ if (s < 0) return false;+ Slot& sl = slot[(size_t) (layer * slots + s)];+ if (!sl.ready) {+ if (!sl.loading || !sl.issued.load(std::memory_order_acquire)) { ++pending; return false; }+ const cudaError_t q = cudaEventQuery((cudaEvent_t) sl.ev);+ if (q != cudaSuccess) {+ if (q != cudaErrorNotReady) cudaGetLastError();+ ++pending;+ return false;+ }+ sl.ready = true;+ sl.loading = false;+ }+ sl.last_ref = completed.load(std::memory_order_acquire);+ ptr = sl.dptr;+ ++hits;+ return true;+}++void ForesightSwap::note_miss(ExpertSource* src, int64_t layer, int32_t expert) {+ 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 way+ const uint64_t now = completed.load(std::memory_order_acquire);+ if (admit > 1) {+ if (now > miss_at[key] + 4) miss_cnt[key] = 0;+ miss_at[key] = now;+ if (miss_cnt[key] < 255) ++miss_cnt[key];+ if (miss_cnt[key] < admit) return;+ }+ const int c = layer_card[(size_t) layer];+ if (card_round[(size_t) c] != now) { card_round[(size_t) c] = now; card_used[(size_t) c] = 0; }+ if (card_used[(size_t) c] >= budget) { ++over_budget; return; }+ if (src == nullptr || !src->pinned(layer, expert)) return; // async copies need the pinned arena+ const uint8_t* b = src->blob(layer, expert);+ if (b == nullptr) return;+ int best = -1;+ uint64_t best_ref = ~0ull;+ for (int s = 0; s < slots; ++s) { // an empty slot, else the one used longest ago that no window can still read+ Slot& sl = slot[(size_t) (layer * slots + s)];+ if (sl.loading) continue;+ if (sl.expert < 0) { best = s; break; }+ if (sl.last_ref + (uint64_t) depth > now) continue;+ if (sl.last_ref < best_ref) { best_ref = sl.last_ref; best = s; }+ }+ if (best < 0) { ++busy; return; }+ Slot& sl = slot[(size_t) (layer * slots + best)];+ if (sl.expert >= 0) where[(size_t) (layer * n_expert + sl.expert)] = -1;+ sl.expert = expert;+ sl.ready = false;+ sl.loading = true;+ sl.issued.store(false, std::memory_order_relaxed);+ sl.src = b;+ sl.last_ref = now;+ where[key] = (int16_t) best;+ ++card_used[(size_t) c];+ ++copies;+ {+ std::lock_guard<std::mutex> lk(mu_);+ queue_.push_back(layer * slots + best);+ }+ cv_.notify_one();+}++std::string ForesightSwap::report() const {+ char buf[256];+ 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",+ (unsigned long long) hits, (unsigned long long) copies, (unsigned long long) pending,+ (unsigned long long) busy, (unsigned long long) over_budget);+ return buf;+}++ForesightSwap* foresight_swap_from_env(int64_t layers, int64_t experts, const std::vector<int>& card_of_layer,+ const std::vector<int>& devices) {+ const char* v = std::getenv("STRATA_FS_SLOTS");+ const int slots = v ? std::atoi(v) : 0;+ if (slots <= 0) return nullptr;+ if (const char* dp = std::getenv("STRATA_VERIFY_DEVICE_PLAN"); dp && std::atoi(dp) != 0) {+ std::fprintf(stderr, "strata serve: foresight swap: off beside STRATA_VERIFY_DEVICE_PLAN (the device would plan "+ "resident layers without the host)\n");+ return nullptr;+ }+ auto* f = new ForesightSwap();+ auto env_int = [](const char* name, int def, int lo) { const char* e = std::getenv(name); const int x = e ? std::atoi(e) : def; return x < lo ? lo : x; };+ f->budget = env_int("STRATA_FS_BUDGET", 32, 1);+ f->admit = env_int("STRATA_FS_ADMIT", 1, 1);+ f->depth = env_int("STRATA_FS_DEPTH", 4, 2);+ std::string err;+ if (!f->init(std::min(slots, 64), layers, experts, card_of_layer, devices, err)) {+ std::fprintf(stderr, "strata serve: foresight swap: %s - swap space off\n", err.c_str());+ delete f;+ return nullptr;+ }+ std::fprintf(stderr, "strata serve: foresight swap: %d slots per layer on %zu card(s), up to %d copies per card and "+ "window, admit after %d miss(es), reuse after %d windows\n",+ f->slots, devices.size(), f->budget, f->admit, f->depth);+ return f;+}++} // namespace strata::corediff --git a/src/program/generate.cpp b/src/program/generate.cppindex ce8e00474..6a98a1d5d 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -25,6 +25,7 @@#include "strata/core/conversation_memory.hpp"#include "strata/core/coupled_draft.hpp"#include "strata/core/expert_source.hpp"+#include "strata/core/foresight_swap.hpp"#include "strata/core/pinned.hpp"#include "strata/core/remote_experts.hpp"#include "strata/core/on_device.hpp"@@ -7212,6 +7213,18 @@ int main(int argc, char** argv) {}}drive.d.plan = ver.plan_sink();+ // Foresight swap space (STRATA_FS_SLOTS; unset = null and nothing changes)+ std::unique_ptr<strata::core::ForesightSwap> fs_swap;+ {+ int main_dev = 0;+ cudaGetDevice(&main_dev);+ std::vector<int> devs{main_dev};+ for (auto& st : stages) devs.push_back(st->dev);+ std::vector<int> card((size_t) g.n_layers, 0);+ 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();+ }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; });@@ -10277,6 +10290,7 @@ int main(int argc, char** argv) {draft_offered += A.T - 1;draft_accepted += a;++rounds;+ if (drive.d.fs) drive.d.fs->completed.fetch_add(1, std::memory_order_release);++dec_windows;dec_T += A.T;bool eos = false;@@ -10527,6 +10541,7 @@ int main(int argc, char** argv) {}std::fflush(stdout);++rounds;+ if (drive.d.fs) drive.d.fs->completed.fetch_add(1, std::memory_order_release);const Clock::time_point tw2 = Clock::now();// coupled drafts with penalties: the next window's row-0 history (`consumed` holds this window's// commit, outv[a] is its row 0) - the drafts extend it on the device as the verify rows will@@ -10585,6 +10600,7 @@ int main(int argc, char** argv) {else std::fprintf(stderr, "strata decode GPU stages, stage %d (ms/window):%s\n", st, pr.c_str());}}+ if (drive.d.fs) std::fprintf(stderr, "strata serve: %s\n", drive.d.fs->report().c_str());if (!cancelled) {// a prompt stopped halfway leaves the session somewhere between two chunks: nothing to continue from// (the checkpoints taken while reading it are still good)