From bec9edf1e026db8c32bc71b7f496fa7f52307635 Mon Sep 17 00:00:00 2001 From: havok2-htwo Date: Mon, 12 Oct 2026 04:07:50 +0200 Subject: [PATCH] prefill: decide STRATA_MMQ_GROUP_SORT per layer once, in the MMQ plan mmq::whole_tiles walks every visible GPU and every tile width through the MMQ config table; the sort hook called it twice for each MoE layer of each prompt chunk, on the host path between the routed-ids download and the expert launches. Its verdict depends only on the layer's weight formats and the device set, so MmqPlan now keeps it as a per-layer flag (taken only when STRATA_MMQ_GROUP_SORT=1 and STRATA_MMQ_RESIDENT_SORT_NE is unset) and logs how many layers qualify. Same order, same bits; without the switch nothing new runs. --- src/prefill/prefill.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 4c39f4b9..e52d0f05 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -1019,6 +1019,9 @@ struct MmqPlan { // and the bytes the down product reads past its matrix there (zeroed in the slot's guard before each group) std::vector direct; std::vector d_over; + // STRATA_MMQ_GROUP_SORT=1 (without STRATA_MMQ_RESIDENT_SORT_NE): per MMQ layer, whether both products' 16-expert + // launches run whole tiles on every visible GPU (mmq::whole_tiles), so its full groups may be taken in row-count order + std::vector group_sort; }; const MmqPlan& mmq_plan() { static const MmqPlan plan = [] { @@ -1066,6 +1069,19 @@ const MmqPlan& mmq_plan() { std::fprintf(stderr, "strata: prompt MMQ reads resident experts in place (STRATA_MMQ_DIRECT=1): %lld of %lld " "layers\n", (long long) nd, (long long) layers); } + // the verdict depends only on the layer's formats and the visible GPUs: taken once here, not per chunk + p.group_sort.assign(p.layer.size(), 0); + if (on && detail::mmq_group_sort_requested() && !detail::mmq_resident_sort_requested()) { + int64_t ns = 0; + for (int64_t l = 0; l < layers; ++l) { + if (!p.layer[(size_t) l]) continue; + const int gt = lay.native ? lay.fmt[(size_t) l].gu_type : 42, dt = lay.native ? lay.fmt[(size_t) l].d_type : 42; + p.group_sort[(size_t) l] = mmq::whole_tiles(gt, 1280, MMQ_GROUP) && mmq::whole_tiles(dt, N, MMQ_GROUP); + ns += p.group_sort[(size_t) l]; + } + std::fprintf(stderr, "strata: prompt MMQ sorts the full expert groups (STRATA_MMQ_GROUP_SORT=1): %lld of %lld " + "layers\n", (long long) ns, (long long) layers); + } return p; }(); return plan; @@ -3379,12 +3395,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // (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. // STRATA_MMQ_GROUP_SORT=1 (opt-in, exact): the same order for the full groups only, where each - // tile is one block's whole K (mmq::whole_tiles); the partial group keeps its ID order and bits. + // tile is one block's whole K (MmqPlan::group_sort); the partial group keeps its ID order and bits. // STRATA_MMQ_RESIDENT_SORT_NE=1 takes precedence (it sorts the partial group too). const bool sort_all = detail::mmq_resident_sort_requested(); - const bool sort_full = !sort_all && detail::mmq_group_sort_requested() && use_mmq && - order.size() >= (size_t) MMQ_GROUP && mmq::whole_tiles(mmq_gt, 1280, MMQ_GROUP) && - mmq::whole_tiles(mmq_dt, N, MMQ_GROUP); + const bool sort_full = !sort_all && use_mmq && mmq_plan().group_sort[(size_t) l] && + order.size() >= (size_t) MMQ_GROUP; if (detail::mmq_resident_sort_eligible((sort_all || sort_full) && on_peer.empty(), 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]))