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

Router look-ahead: STRATA_LOOKAHEAD_STATS scores each layer's prediction against the real routing (measurement only)

Niko1221e247c611273f
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp
index 861d9e826..f1d8b7cf8 100644
--- a/include/strata/core/expert_source.hpp
+++ b/include/strata/core/expert_source.hpp
@@ -252,6 +252,12 @@ public:
/// A deeper one predicts layer + j from layer's input, which is less exact the further it looks.
void set_depth(int d) { depth_ = d < 1 ? 1 : d > 4 ? 4 : d; }
int depth() const { return depth_; }
+ /// STRATA_LOOKAHEAD_STATS=1 (measurement only): remember each prediction (top k, k+4 and k+10 per token) and let the
+ /// dispatch score it against the experts the next layer really routes outside the GPU cache.
+ void enable_stats(int64_t n_layers);
+ bool stats_on() const { return stats_on_; }
+ void stats_window_start() { gen_.fetch_add(1, std::memory_order_relaxed); }
+ void stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res);
int64_t predicted() const { return predicted_.load(std::memory_order_relaxed); }
int64_t skipped() const { return skipped_.load(std::memory_order_relaxed); }
double busy_ms() const { return (double) busy_us_.load(std::memory_order_relaxed) / 1000.0; }
@@ -269,10 +275,20 @@ private:
bool quit_ = false, pending_ = false, busy_ = false;
int64_t layer_ = -1, n_tok_ = 0;
const int32_t* host_res_ = nullptr;
+ uint64_t sub_gen_ = 0;
ForesightSwap* fs_ = nullptr; ///< (under mu_)
std::vector<float> x_;
std::atomic<int64_t> predicted_{0}, skipped_{0};
std::atomic<uint64_t> busy_us_{0};
+ // STRATA_LOOKAHEAD_STATS
+ bool stats_on_ = false;
+ int64_t words_ = 0;
+ std::unique_ptr<std::atomic<uint64_t>[]> pmask_; // [layer][3 sizes][words]
+ std::unique_ptr<std::atomic<uint64_t>[]> pstamp_; // [layer]: gen + 1 of the prediction (0 = none)
+ std::atomic<uint64_t> gen_{0};
+ uint64_t st_layers_ = 0, st_nopred_ = 0, st_actual_ = 0, st_hit_[3] = {0, 0, 0}, st_pred_[3] = {0, 0, 0},
+ st_predhit_[3] = {0, 0, 0};
+ uint64_t st_windows_ = 0;
};
/// Plan v0.3 P6: what the GPU computes in a verify window's layer, written by the pool (mapped host memory) right
diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp
index add047d0d..5bb26131f 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -2083,6 +2083,12 @@ void FileExpertSource::warm(int64_t layer, const int64_t* experts, int64_t n) {
#endif
}
+static inline int la_pc64(uint64_t x) { // (portable: no compiler builtin)
+ int n = 0;
+ for (; x != 0; x &= x - 1) ++n;
+ return n;
+}
+
RouterLookahead::~RouterLookahead() {
{
std::lock_guard<std::mutex> lk(mu_);
@@ -2131,11 +2137,66 @@ void RouterLookahead::submit(int64_t layer, const float* x, int64_t n_tok, const
std::memcpy(x_.data(), x, (size_t) (n_tok_ * n_embd_) * sizeof(float));
layer_ = layer + 1;
host_res_ = host_res;
+ sub_gen_ = gen_.load(std::memory_order_relaxed);
pending_ = true;
}
cv_.notify_one();
}
+void RouterLookahead::enable_stats(int64_t n_layers) {
+ words_ = (n_expert_ + 63) / 64;
+ pmask_.reset(new std::atomic<uint64_t>[(size_t) (n_layers * 3 * words_)]);
+ pstamp_.reset(new std::atomic<uint64_t>[(size_t) n_layers]);
+ for (int64_t i = 0; i < n_layers * 3 * words_; ++i) pmask_[(size_t) i].store(0, std::memory_order_relaxed);
+ for (int64_t i = 0; i < n_layers; ++i) pstamp_[(size_t) i].store(0, std::memory_order_relaxed);
+ stats_on_ = true;
+}
+
+// the dispatch thread: score the prediction made for `layer` against the experts it routes outside the GPU cache
+void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res) {
+ if (!stats_on_ || layer < 1 || layer >= (int64_t) routers_.size()) return;
+ ++st_layers_;
+ if (pstamp_[(size_t) layer].load(std::memory_order_acquire) != gen_.load(std::memory_order_relaxed) + 1) {
+ ++st_nopred_;
+ return;
+ }
+ std::vector<uint64_t> act((size_t) words_, 0);
+ for (int64_t i = 0; i < n; ++i) {
+ const int32_t e = ids[i];
+ if (e < 0 || e >= n_expert_) continue;
+ if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue;
+ act[(size_t) (e >> 6)] |= 1ull << (e & 63);
+ }
+ uint64_t na = 0;
+ for (int64_t w = 0; w < words_; ++w) na += (uint64_t) la_pc64(act[(size_t) w]);
+ st_actual_ += na;
+ for (int s = 0; s < 3; ++s) {
+ uint64_t hit = 0, np = 0;
+ for (int64_t w = 0; w < words_; ++w) {
+ const uint64_t pm = pmask_[(size_t) ((layer * 3 + s) * words_ + w)].load(std::memory_order_relaxed);
+ hit += (uint64_t) la_pc64(pm & act[(size_t) w]);
+ np += (uint64_t) la_pc64(pm);
+ }
+ st_hit_[s] += hit;
+ st_pred_[s] += np;
+ }
+ if (layer == (int64_t) routers_.size() - 1 && (++st_windows_ % 100) == 0) {
+ const uint64_t scored = st_layers_ - st_nopred_;
+ std::fprintf(stderr, "strata lookahead stats: %llu windows, %llu layers scored, %llu without a prediction (%.1f%%); "
+ "%.2f actual non-resident experts/layer;",
+ (unsigned long long) st_windows_, (unsigned long long) scored, (unsigned long long) st_nopred_,
+ st_layers_ ? 100.0 * (double) st_nopred_ / (double) st_layers_ : 0.0,
+ scored ? (double) st_actual_ / (double) scored : 0.0);
+ static const int add[3] = {0, 4, 10};
+ for (int s = 0; s < 3; ++s)
+ std::fprintf(stderr, " k=%d: recall %.3f precision %.3f (%.2f predicted/layer);", k_ + add[s],
+ st_actual_ ? (double) st_hit_[s] / (double) st_actual_ : 0.0,
+ st_pred_[s] ? (double) st_hit_[s] / (double) st_pred_[s] : 0.0,
+ scored ? (double) st_pred_[s] / (double) scored : 0.0);
+ std::fprintf(stderr, "\n");
+ }
+}
+
void RouterLookahead::run() {
std::vector<float> logits((size_t) (8 * n_expert_));
std::vector<int32_t> order((size_t) n_expert_);
@@ -2144,6 +2205,7 @@ void RouterLookahead::run() {
int64_t layer, nt;
const int32_t* host_res;
ForesightSwap* fs;
+ uint64_t sgen;
{
std::unique_lock<std::mutex> lk(mu_);
cv_.wait(lk, [&] { return quit_ || pending_; });
@@ -2154,6 +2216,7 @@ void RouterLookahead::run() {
nt = n_tok_;
host_res = host_res_;
fs = fs_;
+ sgen = sub_gen_;
}
const auto t0 = std::chrono::steady_clock::now();
const int64_t layer0 = layer;
@@ -2189,17 +2252,29 @@ void RouterLookahead::run() {
}
}
}
+ const int kmax = stats_on_ ? (int) std::min<int64_t>(k_ + 10, n_expert_) : k_;
+ const int ks[3] = {k_, (int) std::min<int64_t>(k_ + 4, n_expert_), kmax};
+ std::vector<uint64_t> sm;
+ if (stats_on_) sm.assign((size_t) (3 * words_), 0);
for (int64_t t = 0; t < nt; ++t) {
const float* lt = logits.data() + (size_t) (t * n_expert_);
for (int64_t e = 0; e < n_expert_; ++e) order[(size_t) e] = (int32_t) e;
- std::partial_sort(order.begin(), order.begin() + k_, order.end(),
+ std::partial_sort(order.begin(), order.begin() + kmax, order.end(),
[&](int32_t a, int32_t b) { return lt[(size_t) a] > lt[(size_t) b]; });
- for (int j = 0; j < k_; ++j) {
+ for (int j = 0; j < kmax; ++j) {
const int64_t e = order[(size_t) j];
if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue; // on the GPU
- if (std::find(want.begin(), want.end(), e) == want.end()) want.push_back(e);
+ if (j < k_ && std::find(want.begin(), want.end(), e) == want.end()) want.push_back(e);
+ if (stats_on_ && dj == 0)
+ for (int s = 0; s < 3; ++s)
+ if (j < ks[s]) sm[(size_t) (s * words_ + (e >> 6))] |= 1ull << (e & 63);
}
}
+ if (stats_on_ && dj == 0) {
+ for (int64_t i = 0; i < 3 * words_; ++i)
+ pmask_[(size_t) (layer * 3 * words_ + i)].store(sm[(size_t) i], std::memory_order_relaxed);
+ pstamp_[(size_t) layer].store(sgen + 1, std::memory_order_release);
+ }
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);
@@ -3380,7 +3455,13 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
d.fail_layer = d.layers;
return;
}
- if (d.lookahead != nullptr) d.lookahead->submit(d.layers, x_f, n_tok, d.host_res); // CS-T: warm layer + 1
+ if (d.lookahead != nullptr) {
+ if (d.lookahead->stats_on()) {
+ if (d.layers == 0) d.lookahead->stats_window_start();
+ d.lookahead->stats_score(d.layers, ids, n_tok * k, d.host_res);
+ }
+ d.lookahead->submit(d.layers, x_f, n_tok, d.host_res); // CS-T: warm layer + 1
+ }
if (k < 1 || n_tok * k > kMaxWindowEntries) {
d.failed = true;
d.fail = "a verify window routes more entries than the expert pool's window tables hold";
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index d3eb73e5c..ebc275959 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -5335,7 +5335,9 @@ int main(int argc, char** argv) {
// 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) &&
+ // STRATA_LOOKAHEAD_STATS=1 (measurement only): run the predictor on any tier and score it against the real routing
+ const bool la_stats = srcp != nullptr && [] { const char* v = std::getenv("STRATA_LOOKAHEAD_STATS"); return v != nullptr && std::atoi(v) != 0; }();
+ if (srcp != nullptr && ((srcp == &src && src.warms()) || fs_ahead || la_stats) &&
[] { 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;
@@ -5348,8 +5350,9 @@ 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, srcp, err, fs_ahead)) {
+ if (ok && lookahead.start(std::move(routers), g.n_embd, g.n_expert, kv ? std::atoi(kv) : 10, srcp, err, fs_ahead || la_stats)) {
drive.d.lookahead = &lookahead;
+ if (la_stats) lookahead.enable_stats(g.n_layers);
if (const char* dv = std::getenv("STRATA_IO_PREFETCH_DEPTH"); dv != nullptr && std::atoi(dv) > 0)
lookahead.set_depth(std::atoi(dv));
else if (srcp == &src && src.io_prefetch())