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

expert_source, s2_expert_grouped: guard the verify window's fixed tables

Guillaume PUTIERc9d3fd68774c
diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp
index 6e007928e..d235b9580 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -772,6 +772,14 @@ void expert_pool_dispatch(void* user, const float* x_f, const int32_t* ids, cons
d.experts += k;
}
+namespace {
+// the verify window's per-entry tables in `expert_pool_dispatch_multi` (`kind`, `distinct`, `first_of`)
+// are fixed arrays of this many entries: MAXT tokens of the model's 10 routed experts must fit, and a larger k is
+// refused at run time rather than written past them.
+constexpr int64_t kMaxWindowEntries = 128;
+static_assert(strata::kernels::cpu::MAXT * 10 <= kMaxWindowEntries, "a verify window's entries overflow the tables");
+} // namespace
+
void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32_t* ids, int64_t n_tok, int64_t k,
float* out) {
using namespace strata::kernels::cpu;
@@ -782,6 +790,12 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
d.fail_layer = d.layers;
return;
}
+ 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";
+ d.fail_layer = d.layers;
+ return;
+ }
if ((int64_t) d.act_multi.size() < n_tok) d.act_multi.resize((size_t) MAXT);
const ExpertLayout& lay = expert_layout();
const bool native = lay.native;
@@ -802,9 +816,9 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
// ---- plan v0.3 P6: the GPU's share, decided and published FIRST so the GPU starts while the CPU works.
// Distinct experts in routing order; resident ones and the last pcie_num/256 of the missed ones go to the GPU.
const int64_t n = n_tok * k;
- int32_t kind[128]; // per entry: -1 CPU, 0 VRAM, 1 PCIe
- if (d.plan != nullptr && n <= 128 && n <= d.plan->cap) {
- int64_t distinct[128], first_of[128];
+ int32_t kind[kMaxWindowEntries]; // per entry: -1 CPU, 0 VRAM, 1 PCIe
+ if (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap) {
+ int64_t distinct[kMaxWindowEntries], first_of[kMaxWindowEntries];
int nd = 0, nmiss = 0;
for (int64_t i = 0; i < n; ++i) {
first_of[i] = i;
diff --git a/src/kernels/cuda/s2_expert_grouped.cu b/src/kernels/cuda/s2_expert_grouped.cu
index 90761ae6e..3cbf61259 100644
--- a/src/kernels/cuda/s2_expert_grouped.cu
+++ b/src/kernels/cuda/s2_expert_grouped.cu
@@ -16,6 +16,7 @@
#include "strata/kernels/s2_expert_grouped.hpp"
#include "strata/kernels/quantize_act.hpp"
+#include "strata/kernels/verify_kernels.hpp"
#include <cuda_fp16.h>
#include <cuda_runtime.h>
@@ -512,6 +513,9 @@ namespace {
// hit kernel's.
constexpr int GU_CHUNKS = (H / 32 + 31) / 32; // 3: chunks of a gate/up row per lane (80 chunks / 32 lanes)
constexpr int GMAX = 8; // entries per group (tokens routed to one expert in a window)
+// a group holds one entry per token of the window routed to its expert, and the kernels below keep at
+// most GMAX of them (`min(..., GMAX)`): a longer window would drop entries without a word.
+static_assert(GMAX >= kVerifyMaxT, "a verify window's group can exceed GMAX entries");
constexpr int GU_ROWS = 32; // gate/up rows per block: 4 per warp
constexpr int D_ROWS = 64; // down rows per block: 8 per warp