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

Look-ahead stats: vote-ranked top-M coverage of the misses and of the PCIe share

Niko1221b02a03d97c28
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp
index f1d8b7cf8..66466aa2e 100644
--- a/include/strata/core/expert_source.hpp
+++ b/include/strata/core/expert_source.hpp
@@ -257,7 +257,7 @@ public:
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);
+ void stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res, int pcie_num = 0);
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; }
@@ -285,10 +285,13 @@ private:
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::unique_ptr<std::atomic<int32_t>[]> pvote_; // [layer][kVoteN]: experts by votes, expert + 1 (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;
+ uint64_t st_vhit_[3] = {0, 0, 0}, st_vpred_[3] = {0, 0, 0}, st_pch_ = 0, st_pcov_[3] = {0, 0, 0};
+ static constexpr int kVoteN = 16;
};
/// 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 b8fc200b4..eae8f6630 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -2149,11 +2149,13 @@ void RouterLookahead::enable_stats(int64_t n_layers) {
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);
+ pvote_.reset(new std::atomic<int32_t>[(size_t) (n_layers * kVoteN)]);
+ for (int64_t i = 0; i < n_layers * kVoteN; ++i) pvote_[(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) {
+void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n, const int32_t* host_res, int pcie_num) {
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) {
@@ -2170,6 +2172,35 @@ void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n,
uint64_t na = 0;
for (int64_t w = 0; w < words_; ++w) na += (uint64_t) la_pc64(act[(size_t) w]);
st_actual_ += na;
+ { // the vote-ranked top M (the experts most tokens predict) against the misses, and against the PCIe share
+ static const int vm[3] = {4, 8, 12};
+ int32_t vt[kVoteN];
+ int nv = 0;
+ for (int i = 0; i < kVoteN; ++i) {
+ const int32_t v = pvote_[(size_t) (layer * kVoteN + i)].load(std::memory_order_relaxed);
+ if (v == 0) break;
+ vt[nv++] = v - 1;
+ }
+ // the misses in routing order (first appearance), the PCIe share = the last (n_miss * pcie_num / 256) of them
+ std::vector<int32_t> order_miss;
+ 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;
+ if (std::find(order_miss.begin(), order_miss.end(), e) == order_miss.end()) order_miss.push_back(e);
+ }
+ const int m = ((int) order_miss.size() * pcie_num) >> 8;
+ st_pch_ += (uint64_t) m;
+ for (int s = 0; s < 3; ++s) {
+ const int top = std::min(vm[s], nv);
+ st_vpred_[s] += (uint64_t) top;
+ for (int j = 0; j < top; ++j) {
+ if ((act[(size_t) (vt[j] >> 6)] >> (vt[j] & 63)) & 1ull) ++st_vhit_[s];
+ const auto it = std::find(order_miss.begin(), order_miss.end(), vt[j]);
+ if (it != order_miss.end() && (int) (it - order_miss.begin()) >= (int) order_miss.size() - m) ++st_pcov_[s];
+ }
+ }
+ }
for (int s = 0; s < 3; ++s) {
uint64_t hit = 0, np = 0;
for (int64_t w = 0; w < words_; ++w) {
@@ -2195,6 +2226,14 @@ void RouterLookahead::stats_score(int64_t layer, const int32_t* ids, int64_t n,
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);
+ static const int vm[3] = {4, 8, 12};
+ for (int s = 0; s < 3; ++s)
+ std::fprintf(stderr, " votes top%d: %.2f/layer, covers %.3f of the misses (precision %.3f), %.3f of the PCIe share (%.2f/layer);",
+ vm[s], scored ? (double) st_vpred_[s] / (double) scored : 0.0,
+ st_actual_ ? (double) st_vhit_[s] / (double) st_actual_ : 0.0,
+ st_vpred_[s] ? (double) st_vhit_[s] / (double) st_vpred_[s] : 0.0,
+ st_pch_ ? (double) st_pcov_[s] / (double) st_pch_ : 0.0,
+ scored ? (double) st_pch_ / (double) scored : 0.0);
std::fprintf(stderr, "\n");
}
}
@@ -2273,6 +2312,25 @@ void RouterLookahead::run() {
}
}
if (stats_on_ && dj == 0) {
+ // votes: how many tokens put each non-resident expert in their top k_
+ std::vector<std::pair<int, int32_t>> votes; // (sum of 64 - rank, expert)
+ 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(),
+ [&](int32_t a, int32_t b) { return lt[(size_t) a] > lt[(size_t) b]; });
+ for (int j = 0; j < k_; ++j) {
+ const int32_t e = order[(size_t) j];
+ if (host_res != nullptr && host_res[(size_t) (layer * n_expert_ + e)] >= 0) continue;
+ auto it = std::find_if(votes.begin(), votes.end(), [&](const auto& v) { return v.second == e; });
+ if (it == votes.end()) votes.emplace_back(64 - j, e);
+ else it->first += 64 - j;
+ }
+ }
+ std::sort(votes.begin(), votes.end(), [](const auto& a, const auto& b) { return a.first > b.first; });
+ for (int i = 0; i < kVoteN; ++i)
+ pvote_[(size_t) (layer * kVoteN + i)].store(i < (int) votes.size() ? votes[(size_t) i].second + 1 : 0,
+ std::memory_order_relaxed);
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);
@@ -3460,7 +3518,7 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
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->stats_score(d.layers, ids, n_tok * k, d.host_res, d.pcie_num);
}
d.lookahead->submit(d.layers, x_f, n_tok, d.host_res); // CS-T: warm layer + 1
}