okami.mom/strata

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

Niko1221Merge f-texts-b20: start-up lines of a build without MMQ no longer name MMQ kernelsa6859aa7d

main
13.0 KiB215 linesraw
1// src/core/nv_defaults.cpp - see include/strata/core/nv_defaults.hpp.
2#include "strata/core/nv_defaults.hpp"
3
4#include "strata/core/defaults_026.hpp"
5#include "strata/core/emulate.hpp"
6#include "strata/core/sm120_series.hpp"
7#include "strata/prefill/fused_tile_rule.hpp"
8
9#include <cstdio>
10#include <cstdlib>
11
12namespace strata::core {
13
14namespace {
15bool off(const char* name) {
16    const char* v = std::getenv(name);
17    return v != nullptr && v[0] == '0';
18}
19}  // namespace
20
21std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int minor) {
22    return nv_default_env(major, minor, strata::qwen_defaults_026());
23}
24
25std::vector<std::pair<std::string, std::string>> nv_default_env(int major, int minor, bool defaults026) {
26    std::vector<std::pair<std::string, std::string>> t;
27    const int mj = strata::cc_major_of(major), mn = strata::cc_minor_of(minor);
28    if (mj == 8 && mn == 6) {
29        if (off("STRATA_SM86_PERF")) return t;
30        t = {
31            // 148-nv, RTX 3060 + RTX A4000 (IQ3_XXS, default flags, 8 pairs): the stack +9.6% / +14.6% (8/8 each), identity gate IDENTICAL.
32            // Each is in because removing it from the full stack cost speed on a card (drop-one, 4 rounds) and saved none on the other.
33            // Repeatable output (0.2.15, option 2 of phaseA-tests/par/RESULTS-REPEAT.md (d)): STRATA_ADAPT_NOWAIT=1 is no longer a default here.
34            // With it, a verify window ran a swapped-in expert on the GPU or the CPU by its copy's timing, and two starts of the same request
35            // differed (RTX A6000, fixed draft table: 7 of 24 requests identical; without it 24 of 24 in 3 of 3 starts).  Its cost there:
36            // decode 0.988 [0.969, 1.002] over 3 pairs.  It was in the stack for "A4000 -6.3 points when removed (with ADAPT_LAG), 3060 -0.8";
37            // ADAPT_LAG=2 stays, so the copies still get two windows to land.  STRATA_ADAPT_NOWAIT=1 brings it back.
38            {"STRATA_ADAPT_LAG", "2"},
39            {"STRATA_CPU_IQ_ALL_EXACT1", "1"},   // A4000 -3.3, 3060 -0.6
40            {"STRATA_SIDE_MIXER", "2"},          // A4000 -1.2, 3060 -2.3
41            {"STRATA_ATTN_DAILY", "1"},          // -1.2, -1.3
42            {"STRATA_GR_UP75", "1"},             // -0.9, -1.8
43            {"STRATA_GU_SWIGLU", "1"},           // -0.8, -1.4
44            {"STRATA_QSA_ROWS", "1"},            // -0.5, -1.2
45            {"STRATA_B6_MMVQ_ROWS", "1"},        // A4000 0, 3060 -1.7
46        };
47    } else if (mj == 6 && mn == 0) {
48        if (off("STRATA_SM60_PERF")) return t;
49        t = {
50            // 148-nv, Tesla P100 (IQ3_XXS, default flags, 8 pairs): the stack +17.8% (8/8), identity gate IDENTICAL.
51            {"STRATA_ADAPT_NOWAIT", "1"},        // -10 points when removed (with ADAPT_LAG)
52            {"STRATA_ADAPT_LAG", "2"},
53            {"STRATA_SIDE_MIXER", "2"},          // -1.8
54            {"STRATA_FUSE_WAIT", "2"},           // -1.6
55            {"STRATA_B6_MMVQ_ROWS", "1"},        // -1.3
56            {"STRATA_FUSE_DECODE", "1"},         // -1.3
57            {"STRATA_GR_UP75", "1"},             // -1.2
58        };
59    } else if (mj == 12 && mn == 1) {
60        // GB10 (NVIDIA DGX Spark, 48 SMs, unified memory): only what was measured on that part goes in.  New in 0.2.7.1, so
61        // STRATA_DEFAULTS=026 (the master switch of everything since 0.2.6) leaves the table out, like STRATA_SM121_PERF=0.
62        if (off("STRATA_SM121_PERF") || defaults026) return t;
63        t = {
64            // the fused IQ prompt kernels.  IQ3_S, 64K context, fresh 4K/16K/32K prompts at T=0, 3 paired rounds against
65            // the same tree without it: prompt 1.139 / 1.149 / 1.121, decode unchanged; 5 of 5 greedy answers byte-
66            // identical with it on and off (docs/DGX_SPARK_LINUX.md; HANDOVER §6.2).  It rounds differently from MMQ,
67            // so it is a default with an off switch, like the sm_86 table.
68            {"STRATA_PF_FUSED", "1"},
69            // the same fused prompt kernels for the K-quant experts of Unsloth's UD-Q4_K_XL files (Q4_K / Q5_K gate/up, Q5_1 / Q8_0 down) instead of llama.cpp's MMQ.
70            // UD-Q4_K_XL (Flash-Next), expert cache budget 12000 (15,283 of 24,576 experts resident, so the box keeps over 16 GiB of its 121 GiB free), two starts a pair,
71            // against the FP16 prompt path's text, 48 real-text prompts (24 at 2,048 and 24 at 4,096 tokens, 256 forced tokens each, 12,288 positions): forced-token
72            // KL kq / MMQ 0.895 [0.808, 0.989] (0.864 [0.756, 0.976] at 2K, 0.928 [0.791, 1.088] at 4K), top-1 against the FP16 path's 96.31% / 95.74%, the same
73            // text twice byte for byte on both sides (repeat floor 0); prompt speed kq / MMQ, 10 interleaved pairs, median of the per-round ratios 1.10 / 1.26 / 1.18 / 1.53 at 2K / 4K / 8K / 20K (faster in 7 / 8 / 8 / 9 of 10; the rounds scatter 0.5 to 2.0)
74            // (phaseA-tests/par/RESULTS-SPARK-KQ.md).  STRATA_PF_FUSED_KQ=0 is the way back (a switch you set is kept); it is off with the rest of the table by STRATA_SM121_PERF=0.
75            {"STRATA_PF_FUSED_KQ", "1"},
76            // NVFP4 prompt MMQ with FP4 activations (W4A4), the Blackwell path the engine keeps opt-in.  On this part, the
77            // NVFP4 pack measured prompt 1.106x at 4K and 1.229x at 16K against the default W4A8 MMQ, decode unchanged
78            // (1.015x), and 24 of 24 greedy answers byte-identical with it on and off.  Opt-in upstream because it is only
79            // checked on Blackwell; switched on here for the same reason as the row above, with the same off switch.
80            {"STRATA_PREFILL_NVFP4", "w4a4"},
81            // the hyper-connection read's fused up projection + mix (gr_upmix).  Measured on IQ3_S against the unfused read:
82            // decode 1.054x [1.007, 1.089], prompt unchanged, and 24 of 24 greedy answers byte-identical (the parity test
83            // src/prefill/gr_upmix_parity.cu covers it upstream).  The HC read is ~13% of a decode round here, which is
84            // why a fusion on it moves the number at all.
85            {"STRATA_HC_UPMIX", "1"},
86            // Run the mixer's input-only QSA/GDN work on graph branches.  With the grouped experts' mode 8 selected,
87            // fixed-work IQ3_S decode measured +6.68% paired median and +6.67% geometric mean over 8 balanced pairs
88            // (AB +11.60%, BA +6.68%, identical 512-token output and verifier work).  The branch changes no arithmetic.
89            {"STRATA_SIDE_MIXER", "2"},
90            // the cost-aware MTP window: pick the verify window from what a draft costs against what it has been paying.
91            // Measured on IQ3_S: decode 1.047x [1.019, 1.079], prompt unchanged, 24 of 24 greedy answers byte-identical.
92            // It only changes which drafts are attempted, and every draft is verified, so the answers cannot drift.
93            {"STRATA_SPEC_COST", "1"},
94        };
95    }
96    return t;
97}
98
99std::vector<std::string> apply_nv_defaults(int major, int minor) {
100    const int mj = strata::cc_major_of(major), mn = strata::cc_minor_of(minor);
101    std::vector<std::string> set;
102    for (const auto& kv : nv_default_env(major, minor)) {
103        if (std::getenv(kv.first.c_str()) != nullptr) continue;   // the user's setting wins, whatever it is
104#if defined(_WIN32)
105        _putenv_s(kv.first.c_str(), kv.second.c_str());
106#else
107        setenv(kv.first.c_str(), kv.second.c_str(), 0);
108#endif
109        set.push_back(kv.first);
110    }
111    if (!set.empty()) {
112        const bool sm86 = mj == 8;
113        const bool sm121 = mj == 12 && mn == 1;
114        // sm_86 / sm_60: bit-exact switches.  sm_121: the fused prompt path and the FP4-activation NVFP4 prompt round differently from the
115        // paths they replace (24 of 24 greedy answers were identical on the Spark; no KL run), so the line does not say "exact".
116        std::fprintf(stderr, "strata: %s: %zu %sspeed switches on by default (%s=0 turns them off%s; a switch you set is kept): ",
117                     sm86 ? "sm_86 (Ampere)" : sm121 ? "sm_121 (GB10)" : "sm_60 (Pascal)", set.size(), sm121 ? "" : "exact ",
118                     sm86 ? "STRATA_SM86_PERF" : sm121 ? "STRATA_SM121_PERF" : "STRATA_SM60_PERF",
119                     sm121 ? ", as does STRATA_DEFAULTS=026" : "");
120        for (size_t i = 0; i < set.size(); ++i) std::fprintf(stderr, "%s%s", i ? " " : "", set[i].c_str() + 7);
121        std::fprintf(stderr, "\n");
122    }
123    return set;
124}
125
126std::vector<std::pair<std::string, std::string>> nv_kq_default_env(int min_major, int min_minor) {
127    return nv_kq_default_env(min_major, min_minor, strata::qwen_defaults_026());
128}
129
130std::vector<std::pair<std::string, std::string>> nv_kq_default_env(int min_major, int min_minor, bool defaults026) {
131    std::vector<std::pair<std::string, std::string>> t;
132    if (defaults026) return t;
133    if (strata::cc_major_of(min_major) < 8) return t;   // Volta / Turing / Pascal: not measured, keep the FP16 prompt path
134    const char* sw = std::getenv("STRATA_NV_KQ_DEFAULT");
135    if (sw != nullptr && sw[0] == '0') return t;
136    if (!kNvKqDefaultOn && !(sw != nullptr && sw[0] == '1')) return t;
137    t.push_back({"STRATA_MMQ_KQ", "1"});
138    (void) min_minor;
139    return t;
140}
141
142bool nv_kq_fused_here(int major, int minor, int sms) {
143    namespace ft = strata::prefill::fused_tile;
144    if (ft::pf_env(std::getenv("STRATA_PF_FUSED")) == 0) return false;
145    const ft::Card c{strata::sm120_default(major, minor), major == 12 && minor == 1, sms, ft::s86_class(major, minor, off("STRATA_PF_FUSED_SM86"))};
146    return ft::kq_takes(ft::pf_env(std::getenv("STRATA_PF_FUSED_KQ")), c, ft::sm40_env_of(std::getenv("STRATA_PF_FUSED_SM40")));
147}
148
149std::string nv_kq_default_line(bool fused_here) {
150    return nv_kq_default_line(fused_here, true);
151}
152
153std::string nv_kq_default_line(bool fused_here, bool mmq_built) {
154    if (!mmq_built) {   // a build without MMQ: Strata's own kernels read every K-quant layer
155        if (fused_here)
156            return "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on Strata's own kernels "
157                   "(Q4_K and Q5_K gate/up with a Q5_1 or Q8_0 down); the 'prompt kernels' line below gives the counts";
158        return "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on Strata's own int8 kernels (this engine has no MMQ kernels)";
159    }
160    if (fused_here)
161        return "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on Strata's own kernels where they cover the layer "
162               "(Q4_K and Q5_K gate/up with a Q5_1 or Q8_0 down) and on llama.cpp's MMQ kernels for the rest (STRATA_PF_FUSED_KQ=0 gives all of them back to MMQ)";
163    return "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on llama.cpp's MMQ kernels "
164           "(STRATA_MMQ_KQ=0 or STRATA_NV_KQ_DEFAULT=0 turns it off)";
165}
166
167bool nv_kq_expert_type(int ggml_type) {
168    // ggml type ids: Q5_1 = 7, Q4_K = 12, Q5_K = 13, Q6_K = 14 (the cases of prefill::mmq::supported() behind kq_ok())
169    return ggml_type == 7 || ggml_type == 12 || ggml_type == 13 || ggml_type == 14;
170}
171
172bool nv_kq_line_wanted(const std::vector<std::pair<int, int>>& layer_gate_up_down_types) {
173    for (const auto& l : layer_gate_up_down_types)
174        if (nv_kq_expert_type(l.first) || nv_kq_expert_type(l.second)) return true;
175    return false;
176}
177
178std::vector<std::string> apply_nv_kq_default(int min_major, int min_minor, bool fused_here, bool say) {
179    std::vector<std::string> set;
180    for (const auto& kv : nv_kq_default_env(min_major, min_minor)) {
181        if (std::getenv(kv.first.c_str()) != nullptr) continue;   // the user's setting wins, whatever it is
182#if defined(_WIN32)
183        _putenv_s(kv.first.c_str(), kv.second.c_str());
184#else
185        setenv(kv.first.c_str(), kv.second.c_str(), 0);
186#endif
187        set.push_back(kv.first);
188    }
189    if (say && !set.empty())   // the engine passes say = false and prints the line after the expert layout is loaded, only for a set with K-quant experts (nv_kq_line_wanted)
190        std::fprintf(stderr, "%s\n", nv_kq_default_line(fused_here).c_str());
191    return set;
192}
193
194void apply_spec_adapt_default(int major, int minor) {
195    const int mj = strata::cc_major_of(major), mn = strata::cc_minor_of(minor);
196    // sm_86 only: the P100 (sm_60) gained 4-16% on JSON/code/chat/tool calls but lost 3% on the 4K agent transcript (5/5 pairs), so there it stays opt-in
197    // Owner round 9 (2026-10-22): on the z440 A4000s the 5-pair A/B against 0.2.2 was 0.995 (code 0.996, json 1.019, tool 0.973,
198    // agent 0.991) and the 3060 was length-confounded, so the earlier +9..17% did not reproduce: opt-in (STRATA_SPEC_ADAPT_SM86=1)
199    // until a clean A/B shows it.
200    const bool sm86 = mj == 8 && mn == 6;
201    if (!sm86) return;
202    if (const char* v = std::getenv("STRATA_SPEC_ADAPT_SM86"); v == nullptr || v[0] != '1') return;
203    if (off("STRATA_SM86_PERF")) return;
204    const std::pair<const char*, const char*> kv[2] = {{"STRATA_SPEC_ADAPT_EVERY", "2"}, {"STRATA_SPEC_ADAPT_WARM", "200"}};
205    for (const auto& p : kv) {
206        if (std::getenv(p.first) != nullptr) continue;
207#if defined(_WIN32)
208        _putenv_s(p.first, p.second);
209#else
210        setenv(p.first, p.second, 0);
211#endif
212    }
213}
214
215}  // namespace strata::core