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

#136 P3: with the fused experts the prompt path's MoE buffers hold only what the fused kernels need

Niko122130e5ba4a31f4
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index a4859cea4..adf173fe6 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -85,9 +85,9 @@ constexpr int64_t C = 10240, ZV = 6144, HV = 48;
inline int64_t MAXBLOB() { return (int64_t) strata::kernels::cpu::expert_layout().max_blob; }
constexpr int STAGE = 8; // host->device expert staging ring (chunks below stream_all_min())
// Step 3: from this chunk size on, every non-resident expert of every layer streams in a fixed order through a
-// RING_MAX-slot ring (nearly all 512 are routed at such a chunk), so the copy engine keeps working through the
+// ring_slots()-slot ring (nearly all 512 are routed at such a chunk), so the copy engine keeps working through the
// attention halves instead of waiting for each layer's routing.
-constexpr int RING_MAX = 512; // the arrays; the ring itself is ring_slots()
+constexpr int RING_MAX = 1024; // the arrays; the ring itself is ring_slots(), at most ring_cap()
// The chunk size from which every expert streams: 1024 since 0.1.30 (was 2048). Measured on the 5070, Q2_0 / IQ2_XS,
// fixed cache: 1,500-token prompts 621 -> 785 / 612 -> 735 tok/s, 2,000 727 -> 934 / 712 -> 892, 4,000 (its last
// chunk) 779 -> 912 / 766 -> 844, the same output. Below ~1,000 tokens the output changed on Q2_0 (a smaller chunk
@@ -108,6 +108,9 @@ int g_ring_override = 0; // #340: set by a layer split (Prefill::set_ring_over
// +5% / +3% over MMQ, at 512 +19-22% / +11-12% (MMQ itself at 512: -2% / -1%). 1024 slots (RING_MAX 1024): 4K
// +24-27%, but 32K -> +6%: its chunk falls from 8192 to 6144.
inline bool fused_ring() { return fused::enabled() && !strata::kernels::cpu::expert_layout().native; }
+// the largest ring: 512 slots (a layer's experts), 1024 with the fused experts (#136 P3: two layers' - their smaller
+// buffers leave the VRAM for it)
+inline int ring_cap() { return fused_ring() ? RING_MAX : 512; }
inline int ring_slots(size_t T) {
const char* v = std::getenv("STRATA_PREFILL_RING");
#if defined(STRATA_USE_HIP)
@@ -122,7 +125,7 @@ inline int ring_slots(size_t T) {
const int pinned_ring = fused_ring() ? 512 : 384;
const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : (g_pinned_share >= 0.9 ? pinned_ring : 96);
if (v && r == STAGE) return STAGE; // Explicit opt-in to routed-only staging, including large chunks.
- const int big = r < 16 ? 16 : r > RING_MAX ? RING_MAX : r;
+ const int big = r < 16 ? 16 : r > ring_cap() ? ring_cap() : r;
return (int64_t) T >= stream_all_min() ? big : STAGE;
}
constexpr int DQ = 2; // dequantized-expert ring (FP16 gate/up + down)
@@ -512,20 +515,41 @@ const MmqPlan& mmq_plan() {
}();
return plan;
}
-uint64_t moe_set_bytes(size_t T, int64_t n_expert) {
+// #136 P3: a layout whose chunks run the fused experts (STRATA_PF_FUSED=1, the Q2_0 pack, a streamed chunk of
+// stream_all_min() tokens or more). Its GU, H and Xq hold only the fused path's grouping tables, int8 H and per-token
+// int8 activations, and Hq nothing: ~100 KB a token less than MMQ's FP32 GU / H and per-slot q8_1 rows, which is what
+// lets a bigger chunk or ring fit in the slots the prompt path borrows. A last chunk below stream_all_min() still runs
+// MMQ in the same buffers, so each keeps MMQ's size for stream_all_min() - 1 tokens. `src`: the layout streams experts
+// (Prefill::init got an ExpertSource; without one no chunk takes the streamed walk, so no chunk is fused).
+bool fused_layout(size_t T, bool src) {
+ return src && fused_ring() && mmq_plan().any && ring_slots(T) > STAGE && (int64_t) T >= stream_all_min();
+}
+// The MoE buffers MMQ and the fused path share: GU and H in floats, Xq and Hq in bytes. Without `fused` (the
+// default): MMQ's, for T tokens.
+struct MoeBufs { size_t gu, h, xq, hq; };
+MoeBufs moe_bufs(size_t T, int64_t n_expert, bool fused) {
+ if (!fused) return {T * K * 1280, T * K * 640, mmq::q8_bytes((int64_t) (T * K), N), mmq::q8_bytes((int64_t) (T * K), 640)};
+ const size_t ts = (size_t) std::min<int64_t>((int64_t) T, stream_all_min() - 1); // MMQ's last small chunk
+ return {std::max(ts * K * 1280, (fused::group_bytes((int64_t) (T * K), (int) n_expert) + 3) / 4),
+ std::max(ts * K * 640, (fused::act_bytes((int64_t) (T * K), 640) + 3) / 4),
+ std::max(mmq::q8_bytes((int64_t) (ts * K), N), fused::act_bytes((int64_t) T, N)),
+ mmq::q8_bytes((int64_t) (ts * K), 640)};
+}
+uint64_t moe_set_bytes(size_t T, int64_t n_expert, bool fused) {
const MmqPlan& mp = mmq_plan();
+ const MoeBufs mb = moe_bufs(T, n_expert, fused);
Alloc a; a.count_only = true; bool ok = true;
a.take<float>(T * n_expert, ok); a.take<float>(T * K, ok); a.take<int32_t>(T * K, ok); a.take<int32_t>(T * K, ok);
a.take<int32_t>(T * K, ok);
if (mp.fallback) a.take<uint16_t>(T * K * N, ok);
- a.take<float>(T * K * 1280, ok);
+ a.take<float>(mb.gu, ok);
if (mp.fallback) a.take<uint16_t>(T * K * 640, ok);
a.take<float>(T * K * N, ok); a.take<float>(T * 640, ok);
a.take<float>(T * 640, ok); a.take<uint16_t>(T * 640, ok); a.take<float>(T * N, ok); a.take<float>(T, ok);
if (mp.any) {
- a.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), N), ok);
- a.take<float>(T * K * 640, ok);
- a.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), 640), ok);
+ a.take<uint8_t>(mb.xq, ok);
+ a.take<float>(mb.h, ok);
+ a.take<uint8_t>(mb.hq, ok);
}
return a.used;
}
@@ -565,7 +589,7 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
m.borrowed = borrow != nullptr;
bool ok = true;
// one-time: events, the stager, the host buffers (for the largest chunk), the identity page table
- for (int i = 0; i < RING_MAX; ++i) {
+ for (int i = 0; i < ring_cap(); ++i) {
if (cudaEventCreateWithFlags(&m.copied[i], cudaEventDisableTiming) != cudaSuccess) ok = false;
if (cudaEventCreateWithFlags(&m.used[i], cudaEventDisableTiming) != cudaSuccess) ok = false;
}
@@ -669,8 +693,10 @@ bool Prefill::carve(size_t T, void* alloc) {
m.max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
{
// one region for the attention half's and the MoE half's scratch (see gdn_set_bytes)
+ const bool fz = fused_layout(T, m.src != nullptr);
+ const MoeBufs mb = moe_bufs(T, m.g->n_expert, fz);
const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch,
- m.attn_batch, s), moe_set_bytes(T, m.g->n_expert)});
+ m.attn_batch, s), moe_set_bytes(T, m.g->n_expert, fz)});
uint8_t* base = o.take<uint8_t>((size_t) region, ok);
m.region = base;
m.region_bytes = region;
@@ -693,15 +719,15 @@ bool Prefill::carve(size_t T, void* alloc) {
m.slot_dev = c.take<int32_t>(T * K, ok); m.src_dev = c.take<int32_t>(T * K, ok);
const MmqPlan& mp = mmq_plan();
m.Xs = mp.fallback ? c.take<uint16_t>(T * K * N, ok) : nullptr;
- m.GU = c.take<float>(T * K * 1280, ok);
+ m.GU = c.take<float>(mb.gu, ok);
m.Hh = mp.fallback ? c.take<uint16_t>(T * K * 640, ok) : nullptr;
m.Dm = c.take<float>(T * K * N, ok);
m.sgate = c.take<float>(T * 640, ok); m.sup = c.take<float>(T * 640, ok); m.sh_h = c.take<uint16_t>(T * 640, ok);
m.shared = c.take<float>(T * N, ok); m.sg = c.take<float>(T, ok);
if (mp.any) {
- m.Xq = c.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), N), ok);
- m.H = c.take<float>(T * K * 640, ok);
- m.Hq = c.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), 640), ok);
+ m.Xq = c.take<uint8_t>(mb.xq, ok);
+ m.H = c.take<float>(mb.h, ok);
+ m.Hq = c.take<uint8_t>(mb.hq, ok);
}
if (base == nullptr) ok = false;
}
@@ -925,7 +951,7 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session
const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),
- moe_set_bytes(T, g.n_expert)}), ok);
+ moe_set_bytes(T, g.n_expert, fused_layout(T, true))}), ok);
for (int i = 0; i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }
if (mmq_plan().any) {
const MmqPlan& mp = mmq_plan();
@@ -1639,6 +1665,9 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
++k;
} else { // not streamed: resident (the walk streams all others)
b.blob[e - b.e0] = m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e]);
+ // counted whether routed or not (the routing stays on the GPU): at a streamed
+ // chunk's size (>= 1024 tokens x 10 of 512) nearly every expert is routed
+ ++stats_.experts_resident;
}
}
b.e1 = e;