okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-145-havok (#2364 multi-user series, opt-in)df44bcb71

main
4.0 KiB81 linesraw
1// Host-only opt-in grouping permutation. No HIP calls, device math, or precision changes.
2#pragma once
3
4#include <algorithm>
5#include <cstddef>
6#include <cstdint>
7#include <cstdlib>
8#include <cstring>
9#include <vector>
10
11namespace strata::prefill::detail {
12
13inline bool mmq_resident_sort_flag(const char* value) {
14    return value != nullptr && std::strcmp(value, "1") == 0;
15}
16
17inline bool mmq_resident_sort_requested() {
18    static const bool on = mmq_resident_sort_flag(std::getenv("STRATA_MMQ_RESIDENT_SORT_NE"));
19    return on;
20}
21
22// STRATA_MMQ_GROUP_SORT=1: the same row-count order for the full 16-expert groups only; the last, partial group keeps
23// its experts in ID order. A full group's launch has 160 * ntx (gate/up) or 320 * ntx (down) tiles, at or above the
24// 90 % tile efficiency on the cards' SM counts, so it runs one block per tile with no stream-k K split whatever its tile
25// width: each expert's rows get the same bits in any full group. The partial group can take the stream-k split, whose
26// K boundaries depend on the group's tile count - leaving it unchanged keeps the prompt's bits.
27inline bool mmq_group_sort_requested() {
28    static const bool on = mmq_resident_sort_flag(std::getenv("STRATA_MMQ_GROUP_SORT"));
29    return on;
30}
31
32// How many of `n` ordered experts the sort may move: all of them (STRATA_MMQ_RESIDENT_SORT_NE), or the full groups
33// of `group` experts (STRATA_MMQ_GROUP_SORT).
34inline size_t mmq_resident_sort_span(size_t n, size_t group, bool keep_tail) {
35    return keep_tail && group > 0 ? n - n % group : n;
36}
37
38inline bool mmq_resident_sort_eligible(bool requested, bool use_mmq, bool native_layout, bool has_cache,
39                                      const int32_t* layer_resident, int64_t n_expert,
40                                      bool stream_all, bool layer_stream_empty) {
41    // native-layout packs too: slot/src/off are built the same way for both layouts, and nothing reads off[e + 1] as a
42    // count (4x GP100, sm_60: 1.2K prompt 7.27 -> 5.32 s, 35K 63.3 -> 54.1 s, the same bits)
43    (void) native_layout;
44    if (!requested || !use_mmq || !has_cache || !layer_resident || n_expert <= 0) return false;
45    // All experts, including unselected ones: leave every partial/streamed layer in ID order.
46    if (stream_all && !layer_stream_empty) return false;
47    for (int64_t e = 0; e < n_expert; ++e) if (layer_resident[e] < 0) return false;
48    return true;
49}
50
51// Preconditions are established by the existing caller: IDs were range-checked and counted;
52// order contains each positive-count expert once in ID order; off has count.size()+1 entries;
53// routes fits its original int32 offsets, and slot/src each have routes writable entries.
54// After sorting, off[e] is an expert-indexed START, not an ID-prefix end at off[e+1].
55// Only order[0, span) is sorted (span >= order.size(): all of it); the entries after it keep their positions.
56inline void mmq_resident_sort_rows(const int32_t* ids, int64_t routes, int32_t k,
57                                   const std::vector<int32_t>& count, std::vector<int32_t>& off,
58                                   std::vector<int32_t>& order, int32_t* slot, int32_t* src,
59                                   size_t span = SIZE_MAX) {
60    std::stable_sort(order.begin(), order.begin() + (ptrdiff_t) std::min(span, order.size()), [&](int32_t a, int32_t b) {
61        return count[(size_t) a] != count[(size_t) b]
62            ? count[(size_t) a] < count[(size_t) b] : a < b;
63    });
64    std::fill(off.begin(), off.end(), 0);
65    int32_t cursor = 0;
66    for (int32_t e : order) {
67        off[(size_t) e] = cursor;
68        cursor += count[(size_t) e];
69    }
70    off.back() = cursor;
71    std::vector<int32_t> fill(off.begin(), off.end() - 1);
72    // Preserve token-major/rank-major traversal, hence the original within-expert row order.
73    for (int64_t i = 0; i < routes; ++i) {
74        const int32_t e = ids[(size_t) i];
75        const int32_t p = fill[(size_t) e]++;
76        slot[(size_t) i] = p;
77        src[(size_t) p] = (int32_t) (i / k);
78    }
79}
80
81}  // namespace strata::prefill::detail