okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
6.2 KiB165 linesraw
1// src/prefill/moe_ws.cu - see include/strata/prefill/moe_ws.hpp: which layers and products take the weight-stationary
2// kernels (moe_ws_gu_iq2.cu, moe_ws_gu_iq3.cu, moe_ws_down.cu), their work tables and launches.
3#include "common.cuh"
4#include "mmq.cuh"
5
6#include "moe_ws.cuh"
7
8#include <algorithm>
9#include <atomic>
10#include <cmath>
11#include <cstdio>
12#include <cstdlib>
13#include <vector>
14
15namespace strata::prefill::ws {
16namespace {
17
18// ws / per-group MMQ (one 512-expert top-10 layer, lognormal counts sigma 0.5 and 1.0, the worse of the two; isolated
19// sustained medians measured on a GP100 die, the variant moe_ws_*.cu picks) at 2.5, 10, 35 and 160 rows per routed
20// expert
21struct Measured { int type; float r[4]; };
22constexpr double kU[4] = {2.5, 10.0, 35.0, 160.0};
23constexpr Measured kMeasured[] = {
24    {GGML_TYPE_IQ2_XXS, {0.276f, 0.494f, 0.728f, 0.754f}},
25    {GGML_TYPE_IQ2_XS, {0.299f, 0.513f, 0.750f, 0.824f}},
26    {GGML_TYPE_IQ2_S, {0.317f, 0.520f, 0.754f, 0.816f}},
27    {GGML_TYPE_IQ3_XXS, {0.329f, 0.514f, 0.755f, 0.786f}},
28    {GGML_TYPE_IQ3_S, {0.347f, 0.521f, 0.715f, 0.793f}},
29    {GGML_TYPE_Q2_0, {0.246f, 0.389f, 0.555f, 0.614f}},
30    {GGML_TYPE_IQ4_NL, {0.292f, 0.492f, 0.682f, 0.742f}},
31};
32
33const Measured* measured(int t) {
34    for (const Measured& m : kMeasured)
35        if (m.type == t) return &m;
36    return nullptr;
37}
38bool gu_type(int t) {
39    return t == GGML_TYPE_IQ2_XXS || t == GGML_TYPE_IQ2_XS || t == GGML_TYPE_IQ2_S || t == GGML_TYPE_IQ3_XXS ||
40           t == GGML_TYPE_IQ3_S;
41}
42
43}  // namespace
44
45namespace {
46std::atomic<int> g_mode_override{-1};   // set_mode (tests); -1: the environment
47}  // namespace
48
49void set_mode(int v) { g_mode_override.store(v < 0 ? -1 : (v != 0 ? 1 : 0), std::memory_order_relaxed); }
50
51int mode() {
52#if defined(__HIPCC__)
53    return 0;
54#else
55    const int o = g_mode_override.load(std::memory_order_relaxed);
56    if (o >= 0) return o;
57    static const int m = [] {
58        const char* e = std::getenv("STRATA_MMQ_WS");   // opt-in
59        if (e == nullptr || std::atoi(e) != 1) return 0;
60        // GP100 (cc 6.0) only: tuned and checked there (any visible GPU that is not: off for all)
61        int n = 0;
62        if (cudaGetDeviceCount(&n) != cudaSuccess || n <= 0) { cudaGetLastError(); return 0; }
63        for (int d = 0; d < n; ++d) {
64            cudaDeviceProp p{};
65            if (cudaGetDeviceProperties(&p, d) != cudaSuccess || p.major != 6 || p.minor != 0) { cudaGetLastError(); return 0; }
66        }
67        return 1;
68    }();
69    return m;
70#endif
71}
72
73bool supported(int gt, int dt) { return mode() != 0 && gu_type(gt) && (dt == GGML_TYPE_Q2_0 || dt == GGML_TYPE_IQ4_NL); }
74
75namespace {
76std::atomic<int> g_fast_override{-1};   // set_fast (benches, tests); -1: the environment
77}  // namespace
78
79int fast() {
80    const int o = g_fast_override.load(std::memory_order_relaxed);
81    if (o >= 0) return o;
82    static const int f = [] {
83        const char* e = std::getenv("STRATA_WS_FAST");   // opt-in
84        return e != nullptr && std::atoi(e) == 1 ? 1 : 0;
85    }();
86    return f;
87}
88
89void set_fast(int v) { g_fast_override.store(v < 0 ? -1 : (v != 0 ? 1 : 0), std::memory_order_relaxed); }
90
91bool faster(int t, double u) {
92    const Measured* m = measured(t);
93    if (m == nullptr) return false;
94    // linear in log u between the measured points, the ends held
95    if (u <= kU[0]) return m->r[0] < 1.0f;
96    for (int i = 1; i < 4; ++i)
97        if (u <= kU[i]) {
98            const double f = std::log(u / kU[i - 1]) / std::log(kU[i] / kU[i - 1]);
99            return m->r[i - 1] + f * (m->r[i] - m->r[i - 1]) < 1.0;
100        }
101    return m->r[3] < 1.0f;
102}
103
104size_t chunks(const int32_t* order, size_t n, const int32_t* cnt, const int32_t* bounds, Chunk* out) {
105    // largest expert first: MMQ's order is ascending by count where the resident sort ran (walked backwards), else
106    // by id (sorted here)
107    bool asc = true;
108    for (size_t j = 1; j < n && asc; ++j) asc = cnt[order[j - 1]] <= cnt[order[j]];
109    static thread_local std::vector<int32_t> by;
110    by.resize(n);
111    for (size_t j = 0; j < n; ++j) by[j] = (int32_t) (n - 1 - j);
112    if (!asc)
113        std::stable_sort(by.begin(), by.end(), [&](int32_t a, int32_t b) { return cnt[order[a]] > cnt[order[b]]; });
114    size_t k = 0;
115    for (const int32_t j : by) {
116        const int32_t c = cnt[order[j]], b = bounds[j];
117        for (int32_t tk = 0; tk < c; tk += kChunkRows) out[k++] = Chunk{j, b + tk, std::min(kChunkRows, c - tk), 0};
118    }
119    return k;
120}
121
122bool gate_up(const Layer& L, void* stream) {
123    if (L.n <= 0 || L.nchunks <= 0) return true;
124    const double u = (double) L.rows / L.n;
125    if (mode() != 1 || !faster(L.gu_type, u)) return false;
126    const cudaStream_t s = (cudaStream_t) stream;
127    switch (L.gu_type) {
128        case GGML_TYPE_IQ2_XXS: detail::gu_iq2_xxs(L, u, s); break;
129        case GGML_TYPE_IQ2_XS: detail::gu_iq2_xs(L, u, s); break;
130        case GGML_TYPE_IQ2_S: detail::gu_iq2_s(L, u, s); break;
131        case GGML_TYPE_IQ3_XXS: detail::gu_iq3_xxs(L, u, s); break;
132        case GGML_TYPE_IQ3_S: detail::gu_iq3_s(L, u, s); break;
133        default:
134            std::fprintf(stderr, "prefill ws: gate/up type %d has no kernel\n", L.gu_type);
135            std::exit(1);
136    }
137    return true;
138}
139
140void down(const Layer& L, void* stream) {
141    if (L.n <= 0 || L.nchunks <= 0) return;
142    const double u = (double) L.rows / L.n;
143    const cudaStream_t s = (cudaStream_t) stream;
144    switch (L.d_type) {
145        case GGML_TYPE_Q2_0: detail::dn_q2_0(L, u, s); break;
146        case GGML_TYPE_IQ4_NL: {
147            const bool f = fast() == 1;
148            if (f) {   // once per process: the switch is on and the WS path runs
149                static const bool said = [] {
150                    std::fprintf(stderr, "strata prefill: STRATA_WS_FAST=1: the weight-stationary IQ4_NL down's 32-token "
151                                         "tier with switched token pairs and transposed dy (the same Dm bits)\n");
152                    return true;
153                }();
154                (void) said;
155            }
156            detail::dn_iq4_nl(L, u, s, f);
157            break;
158        }
159        default:
160            std::fprintf(stderr, "prefill ws: down type %d has no kernel\n", L.d_type);
161            std::exit(1);
162    }
163}
164
165}  // namespace strata::prefill::ws