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
10.8 KiB159 linesraw
1// src/core/nv_defaults_test.cpp - the NVIDIA per-architecture default tables (no GPU needed): the sm_121 (GB10, DGX Spark) table, its
2// two off switches, and that no other card's table changed.
3#include "strata/core/nv_chunk.hpp"
4#include "strata/core/nv_defaults.hpp"
5
6#include <cstdio>
7#include <cstdlib>
8#include <string>
9
10static int fails = 0;
11#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0)
12
13static void unset(const char* k) {
14#if defined(_WIN32)
15    _putenv_s(k, "");
16#else
17    unsetenv(k);
18#endif
19}
20static void put(const char* k, const char* v) {
21#if defined(_WIN32)
22    _putenv_s(k, v);
23#else
24    setenv(k, v, 1);
25#endif
26}
27
28static const std::string* find(const std::vector<std::pair<std::string, std::string>>& t, const char* k) {
29    for (const auto& kv : t)
30        if (kv.first == k) return &kv.second;
31    return nullptr;
32}
33
34int main() {
35    using namespace strata::core;
36    unset("STRATA_SM121_PERF");
37    unset("STRATA_SM86_PERF");
38    unset("STRATA_SM60_PERF");
39    unset("STRATA_EMULATE_CC");
40    // GB10 (12.1): the six measured switches, and nothing else
41    {
42        const auto t = nv_default_env(12, 1, false);
43        CHECK(t.size() == 6);
44        CHECK(find(t, "STRATA_PF_FUSED") && *find(t, "STRATA_PF_FUSED") == "1");
45        CHECK(find(t, "STRATA_PF_FUSED_KQ") && *find(t, "STRATA_PF_FUSED_KQ") == "1");
46        CHECK(find(t, "STRATA_PREFILL_NVFP4") && *find(t, "STRATA_PREFILL_NVFP4") == "w4a4");
47        CHECK(find(t, "STRATA_HC_UPMIX") && *find(t, "STRATA_HC_UPMIX") == "1");
48        CHECK(find(t, "STRATA_SIDE_MIXER") && *find(t, "STRATA_SIDE_MIXER") == "2");
49        CHECK(find(t, "STRATA_SPEC_COST") && *find(t, "STRATA_SPEC_COST") == "1");
50    }
51    // STRATA_DEFAULTS=026 (the 0.2.7 master switch; the sm_121 table is newer than 0.2.6) empties it
52    CHECK(nv_default_env(12, 1, true).empty());
53    // STRATA_SM121_PERF=0 empties it; any other value keeps it
54    put("STRATA_SM121_PERF", "0");
55    CHECK(nv_default_env(12, 1, false).empty());
56    put("STRATA_SM121_PERF", "1");
57    CHECK(nv_default_env(12, 1, false).size() == 6);
58    unset("STRATA_SM121_PERF");
59    // the other cards: sm_120 (RTX 50) and sm_100 get nothing from this file; the sm_86 / sm_60 tables are unchanged by the flag
60    CHECK(nv_default_env(12, 0, false).empty());
61    CHECK(nv_default_env(10, 0, false).empty());
62    CHECK(nv_default_env(8, 9, false).empty());
63    CHECK(nv_default_env(7, 5, false).empty());
64    {
65        const auto a = nv_default_env(8, 6, false), b = nv_default_env(8, 6, true);
66        CHECK(!a.empty() && a == b);               // STRATA_DEFAULTS=026 does not touch the 2026-10 sm_86 table
67        const auto p = nv_default_env(6, 0, false), q = nv_default_env(6, 0, true);
68        CHECK(!p.empty() && p == q);
69        CHECK(!find(a, "STRATA_PF_FUSED") && !find(p, "STRATA_PF_FUSED"));
70        // repeatable output: sm_86 keeps ADAPT_LAG=2 but no longer turns ADAPT_NOWAIT on (option 2); sm_60 keeps both
71        CHECK(!find(a, "STRATA_ADAPT_NOWAIT"));
72        CHECK(find(a, "STRATA_ADAPT_LAG") && *find(a, "STRATA_ADAPT_LAG") == "2");
73        CHECK(find(p, "STRATA_ADAPT_NOWAIT") && *find(p, "STRATA_ADAPT_NOWAIT") == "1");
74        CHECK(find(p, "STRATA_ADAPT_LAG") && *find(p, "STRATA_ADAPT_LAG") == "2");
75    }
76    // round 0212: STRATA_MMQ_KQ=1 is a default for compute capability 8.0+ (owner yes 11-03), with its own off switches
77    unset("STRATA_NV_KQ_DEFAULT");
78    CHECK(kNvKqDefaultOn);
79    for (int major : {8, 9, 10, 12}) CHECK(nv_kq_default_env(major, 0, false).size() == 1);   // on without any switch
80    put("STRATA_NV_KQ_DEFAULT", "1");
81    for (int major : {8, 9, 10, 12}) {
82        const auto k = nv_kq_default_env(major, 0, false);
83        CHECK(k.size() == 1 && find(k, "STRATA_MMQ_KQ") && *find(k, "STRATA_MMQ_KQ") == "1");
84    }
85    CHECK(nv_kq_default_env(7, 5, false).empty());     // Turing / Volta / Pascal keep the FP16 prompt path
86    CHECK(nv_kq_default_env(6, 0, false).empty());
87    CHECK(nv_kq_default_env(12, 0, true).empty());     // STRATA_DEFAULTS=026
88    put("STRATA_NV_KQ_DEFAULT", "0");
89    CHECK(nv_kq_default_env(12, 0, false).empty());    // its own off switch
90    unset("STRATA_NV_KQ_DEFAULT");
91    // the start-up line of that default: the earlier line byte for byte where Strata's own kernels do not read the K-quant experts, one that says what happens where they do
92    CHECK(nv_kq_default_line(false) ==
93          "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on llama.cpp's MMQ kernels (STRATA_MMQ_KQ=0 or STRATA_NV_KQ_DEFAULT=0 turns it off)");
94    CHECK(nv_kq_default_line(true) ==
95          "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 (Q4_K and Q5_K gate/up with a Q5_1 or Q8_0 down) "
96          "and on llama.cpp's MMQ kernels for the rest (STRATA_PF_FUSED_KQ=0 gives all of them back to MMQ)");
97    CHECK(nv_kq_default_line(true, true) == nv_kq_default_line(true) && nv_kq_default_line(false, true) == nv_kq_default_line(false));
98    // a build without MMQ names no MMQ kernel it does not have
99    CHECK(nv_kq_default_line(true, false) ==
100          "strata: a model with K-quant experts (Unsloth UD-Q4/Q5/Q6_K_XL) reads its prompt on Strata's own kernels (Q4_K and Q5_K gate/up with a Q5_1 or Q8_0 down); "
101          "the 'prompt kernels' line below gives the counts");
102    CHECK(nv_kq_default_line(false, false) ==
103          "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)");
104    {   // the line is printed only for a loaded set that has K-quant experts (ggml types: IQ2_XS 17, IQ3_S 21, IQ4_NL 20, IQ2_S 22, Q8_0 8, Q5_1 7, Q4_K 12, Q5_K 13, Q6_K 14)
105        CHECK(nv_kq_expert_type(12) && nv_kq_expert_type(13) && nv_kq_expert_type(14) && nv_kq_expert_type(7));
106        CHECK(!nv_kq_expert_type(8) && !nv_kq_expert_type(17) && !nv_kq_expert_type(21) && !nv_kq_expert_type(20) && !nv_kq_expert_type(22) && !nv_kq_expert_type(-1) && !nv_kq_expert_type(0));
107        CHECK(!nv_kq_line_wanted({}));                                                                   // nothing loaded
108        CHECK(!nv_kq_line_wanted({{21, 20}, {21, 20}, {22, 20}, {17, 20}}));                              // IQ3_S / IQ2_S / IQ2_XS with IQ4_NL down: no line
109        CHECK(!nv_kq_line_wanted({{21, 8}, {17, 8}}));                                                   // Q8_0 down only: the switch decides nothing there
110        CHECK(nv_kq_line_wanted({{12, 7}}));                                                             // UD-Q4_K_XL: Q4_K gate/up, Q5_1 down
111        CHECK(nv_kq_line_wanted({{21, 20}, {13, 8}, {21, 20}}));                                         // one Q5_K layer among i-quant ones is enough
112        CHECK(nv_kq_line_wanted({{12, 14}}));                                                            // Q6_K down
113        CHECK(nv_kq_line_wanted({{21, 7}}));                                                             // only a Q5_1 down (i-quant gate/up): the switch decides that product, so the line stands
114        CHECK(nv_kq_line_wanted({{21, 20}, {21, 7}}));
115        // apply_nv_kq_default is not called here: its two-argument path reads strata::qwen_defaults_026(), which caches STRATA_DEFAULTS at its first call,
116        // and the one-argument check at the end of main needs that first call to see STRATA_DEFAULTS=026.
117    }
118    {   // which cards: fused_tile::kq_takes with the engine's switches (raw compute capability, SM count)
119        unset("STRATA_PF_FUSED"); unset("STRATA_PF_FUSED_KQ"); unset("STRATA_PF_FUSED_SM40"); unset("STRATA_PF_FUSED_SM86"); 
120        CHECK(nv_kq_fused_here(8, 6, 84) && nv_kq_fused_here(8, 6, 80) && nv_kq_fused_here(12, 0, 110) && nv_kq_fused_here(12, 0, 80));   // RTX A6000, RTX PRO 5000 class
121        CHECK(nv_kq_fused_here(8, 0, 108) && nv_kq_fused_here(8, 9, 128));                                                                  // the sm_86 class
122        CHECK(nv_kq_fused_here(8, 6, 48) && nv_kq_fused_here(12, 0, 48));                                                                   // 40-79 SMs: the staged launches
123        CHECK(!nv_kq_fused_here(8, 6, 39) && !nv_kq_fused_here(12, 1, 48) && !nv_kq_fused_here(7, 5, 68) && !nv_kq_fused_here(8, 7, 16) && !nv_kq_fused_here(9, 0, 132));   // under 40 SMs, GB10, Turing, Orin, Hopper
124        put("STRATA_PF_FUSED_SM40", "0");
125        CHECK(!nv_kq_fused_here(8, 6, 48) && nv_kq_fused_here(8, 6, 84));
126        unset("STRATA_PF_FUSED_SM40");
127        put("STRATA_PF_FUSED_KQ", "0");
128        CHECK(!nv_kq_fused_here(8, 6, 84) && !nv_kq_fused_here(12, 0, 110));                                                                // the way back
129        put("STRATA_PF_FUSED_KQ", "1");
130        CHECK(nv_kq_fused_here(8, 6, 84) && nv_kq_fused_here(12, 1, 48) && nv_kq_fused_here(7, 5, 68));                                      // the opt-in: every card
131        put("STRATA_PF_FUSED", "0");
132        CHECK(!nv_kq_fused_here(8, 6, 84) && !nv_kq_fused_here(12, 1, 48));                                                                 // STRATA_PF_FUSED=0: every fused kernel off
133        unset("STRATA_PF_FUSED"); unset("STRATA_PF_FUSED_KQ");
134        put("STRATA_PF_FUSED_SM86", "0");
135        CHECK(!nv_kq_fused_here(8, 6, 84) && nv_kq_fused_here(12, 0, 110));                                                                 // the sm_86 class diagnostic switch
136        unset("STRATA_PF_FUSED_SM86");
137    }
138    // the other tables are unchanged by it
139    CHECK(nv_default_env(12, 0, false).empty());
140    // round 0212: the NVIDIA prompt-chunk ceiling of a bare `--prefill auto` (nv_chunk.hpp)
141    CHECK(nv_chunk_ceiling(nullptr, false) == 32768);
142    CHECK(nv_chunk_ceiling("1", false) == 32768);
143    CHECK(nv_chunk_ceiling("16", false) == 16384);
144    CHECK(nv_chunk_ceiling("0", false) == 0);          // STRATA_NV_CHUNK=0 keeps 8192
145    CHECK(nv_chunk_ceiling(nullptr, true) == 0);       // STRATA_DEFAULTS=026 keeps 8192
146    CHECK(nv_chunk_ceiling("16", true) == 0);
147    CHECK(nv_chunk_card_ok(7, 0));                                                    // Volta (V100): measured, takes the raise
148    CHECK(!nv_chunk_card_ok(7, 5));                                                   // Turing: not measured, keeps 8192
149    CHECK(!nv_chunk_card_ok(6, 0) && !nv_chunk_card_ok(6, 1) && !nv_chunk_card_ok(6, 2));   // Pascal: not measured, keeps 8192
150    CHECK(!nv_chunk_card_ok(5, 2) && !nv_chunk_card_ok(0, 0));
151    CHECK(nv_chunk_card_ok(8, 0) && nv_chunk_card_ok(8, 6) && nv_chunk_card_ok(9, 0) && nv_chunk_card_ok(12, 0));
152    CHECK(nv_chunk_step_down(32768) == 16384 && nv_chunk_step_down(16384) == 8192 && nv_chunk_step_down(8192) == 8192);
153    // the one-argument form reads STRATA_DEFAULTS
154    put("STRATA_DEFAULTS", "026");
155    CHECK(nv_default_env(12, 1).empty());
156    unset("STRATA_DEFAULTS");
157    if (fails == 0) std::printf("nv_defaults_test: ok\n");
158    return fails == 0 ? 0 : 1;
159}