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

prefill: MMQ groups of a fully resident layer in row-count order (opt-in STRATA_MMQ_RESIDENT_SORT_NE=1)

Niko1221c6a0d4033be7
diff --git a/src/prefill/mmq_resident_sort.hpp b/src/prefill/mmq_resident_sort.hpp
new file mode 100644
index 000000000..2e03856b6
--- /dev/null
+++ b/src/prefill/mmq_resident_sort.hpp
@@ -0,0 +1,59 @@
+// Host-only opt-in grouping permutation. No HIP calls, device math, or precision changes.
+#pragma once
+
+#include <algorithm>
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <vector>
+
+namespace strata::prefill::detail {
+
+inline bool mmq_resident_sort_flag(const char* value) {
+ return value != nullptr && std::strcmp(value, "1") == 0;
+}
+
+inline bool mmq_resident_sort_requested() {
+ static const bool on = mmq_resident_sort_flag(std::getenv("STRATA_MMQ_RESIDENT_SORT_NE"));
+ return on;
+}
+
+inline bool mmq_resident_sort_eligible(bool requested, bool use_mmq, bool native_layout, bool has_cache,
+ const int32_t* layer_resident, int64_t n_expert,
+ bool stream_all, bool layer_stream_empty) {
+ if (!requested || !use_mmq || native_layout || !has_cache || !layer_resident || n_expert <= 0) return false;
+ // All experts, including unselected ones: leave every partial/streamed layer in ID order.
+ if (stream_all && !layer_stream_empty) return false;
+ for (int64_t e = 0; e < n_expert; ++e) if (layer_resident[e] < 0) return false;
+ return true;
+}
+
+// Preconditions are established by the existing caller: IDs were range-checked and counted;
+// order contains each positive-count expert once in ID order; off has count.size()+1 entries;
+// routes fits its original int32 offsets, and slot/src each have routes writable entries.
+// After sorting, off[e] is an expert-indexed START, not an ID-prefix end at off[e+1].
+inline void mmq_resident_sort_rows(const int32_t* ids, int64_t routes, int32_t k,
+ const std::vector<int32_t>& count, std::vector<int32_t>& off,
+ std::vector<int32_t>& order, int32_t* slot, int32_t* src) {
+ std::stable_sort(order.begin(), order.end(), [&](int32_t a, int32_t b) {
+ return count[(size_t) a] != count[(size_t) b]
+ ? count[(size_t) a] < count[(size_t) b] : a < b;
+ });
+ std::fill(off.begin(), off.end(), 0);
+ int32_t cursor = 0;
+ for (int32_t e : order) {
+ off[(size_t) e] = cursor;
+ cursor += count[(size_t) e];
+ }
+ off.back() = cursor;
+ std::vector<int32_t> fill(off.begin(), off.end() - 1);
+ // Preserve token-major/rank-major traversal, hence the original within-expert row order.
+ for (int64_t i = 0; i < routes; ++i) {
+ const int32_t e = ids[(size_t) i];
+ const int32_t p = fill[(size_t) e]++;
+ slot[(size_t) i] = p;
+ src[(size_t) p] = (int32_t) (i / k);
+ }
+}
+
+} // namespace strata::prefill::detail
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index dc35c2be3..83ff730b8 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -1,5 +1,6 @@
// src/prefill/prefill.cpp - see include/strata/prefill/prefill.hpp.
#include "strata/prefill/prefill.hpp"
+#include "mmq_resident_sort.hpp"
#include "strata/core/mtp.hpp"
#include "strata/core/progress.hpp"
#include "strata/core/on_device.hpp"
@@ -1742,6 +1743,17 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
slot_h[(size_t) i] = p;
src_h[(size_t) p] = (int32_t) (i / K);
}
+ // the experts, in id order: resident ones from VRAM, the others through the staging ring
+ std::vector<int32_t> order;
+ for (int32_t e = 0; e < m.g->n_expert; ++e) if (m.cnt[(size_t) e] > 0) order.push_back(e);
+ // Aurora (STRATA_MMQ_RESIDENT_SORT_NE=1, opt-in): a layer whose experts are ALL resident and
+ // run through MMQ groups takes them in row-count order, so each 16-expert group's max_rows
+ // (its padded tile rows) is close to its experts' own; slot/src/off are rebuilt coherently
+ // before the upload, the routed ids, weights and the within-expert row order stay as they are.
+ if (detail::mmq_resident_sort_eligible(detail::mmq_resident_sort_requested(), use_mmq, lay.native,
+ m.cache != nullptr, m.host_res ? m.host_res + (size_t) l * m.g->n_expert : nullptr,
+ m.g->n_expert, stream_all, !stream_all || seq_start[(size_t) l] == seq_start[(size_t) l + 1]))
+ detail::mmq_resident_sort_rows(ids_h, T * K, (int32_t) K, m.cnt, m.off, order, slot_h, src_h);
if (grp_mapped) {
copy_i32(m.slot_dev, m.grp_dev + m.grp_tk, T * K, m.cs);
copy_i32(m.src_dev, m.grp_dev + 2 * m.grp_tk, T * K, m.cs);
@@ -1749,9 +1761,6 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
cudaMemcpyAsync(m.slot_dev, m.slot_host.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice, m.cs);
cudaMemcpyAsync(m.src_dev, m.src_host.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice, m.cs);
}
- // the experts, in id order: resident ones from VRAM, the others through the staging ring
- std::vector<int32_t> order;
- for (int32_t e = 0; e < m.g->n_expert; ++e) if (m.cnt[(size_t) e] > 0) order.push_back(e);
n_order = order.size();
const size_t mmq_gub = use_mmq ? mmq::matrix_bytes(mmq_gt, 1280, N) : 0;
const size_t mmq_db = use_mmq ? mmq::matrix_bytes(mmq_dt, N, 640) : 0;