okami.mom/strata

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

Niko1221Merge f-small-walk: in a build without MMQ, long prompts on 8 GB cards use the layer-major walk with the fused kernels420e03b12

main
5.8 KiB94 linesraw
1// own_only_switches (include/strata/prefill/own_only_switches.hpp): the opt-in fused-kernel switches a build without MMQ turns on by default.  No GPU, no model.
2#include <cstdio>
3#include <cstring>
4#include <string>
5#include <utility>
6#include <vector>
7
8#include "strata/prefill/own_only_switches.hpp"
9
10using strata::prefill::own_only_default_switches;
11using strata::prefill::own_only_switch_on;
12
13static int failed = 0;
14#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
15
16int main() {
17    // CUDA: the list the build had before the Q2_0 kernel joined, in its order, then the Q2_0 / Q2_0 switch last
18    const std::vector<std::string> cuda_before = {"STRATA_PF_FUSED", "STRATA_PF_FUSED_Q50", "STRATA_PF_FUSED_Q8D", "STRATA_PF_FUSED_Q8GU",
19                                                  "STRATA_PF_FUSED_Q6K", "STRATA_PF_FUSED_KQ", "STRATA_PF_FUSED_TQ", "STRATA_PF_FUSED_SM75"};
20    const auto cuda = own_only_default_switches(false);
21    CHECK(cuda.size() == cuda_before.size() + 1);
22    for (size_t i = 0; i < cuda_before.size() && i < cuda.size(); ++i) CHECK(cuda_before[i] == cuda[i]);
23    CHECK(own_only_switch_on("STRATA_PF_FUSED_Q2N", false));
24    // HIP: unchanged (the gfx12 Q2_0 kernel is a default of its own there); no Turing switch, no Q2N
25    const std::vector<std::string> hip_before = {"STRATA_PF_FUSED", "STRATA_PF_FUSED_Q50", "STRATA_PF_FUSED_Q8D", "STRATA_PF_FUSED_Q8GU",
26                                                 "STRATA_PF_FUSED_Q6K", "STRATA_PF_FUSED_KQ", "STRATA_PF_FUSED_TQ"};
27    const auto hip = own_only_default_switches(true);
28    CHECK(hip.size() == hip_before.size());
29    for (size_t i = 0; i < hip_before.size() && i < hip.size(); ++i) CHECK(hip_before[i] == hip[i]);
30    CHECK(!own_only_switch_on("STRATA_PF_FUSED_Q2N", true));
31    CHECK(!own_only_switch_on("STRATA_PF_FUSED_SM75", true));
32    CHECK(own_only_switch_on("STRATA_PF_FUSED_SM75", false));
33    // names that are not in the list stay off (the other opt-ins of the kernels are not defaults)
34    CHECK(!own_only_switch_on("STRATA_PF_FUSED_SM86", false));
35    CHECK(!own_only_switch_on("STRATA_PF_FUSED_IQ2S", false));
36    CHECK(!own_only_switch_on("", false));
37    // no name twice
38    for (size_t i = 0; i < cuda.size(); ++i)
39        for (size_t j = i + 1; j < cuda.size(); ++j) CHECK(std::strcmp(cuda[i], cuda[j]) != 0);
40    // the recorder: a fake environment; a name that was there before the initialiser ran is the user's and is not recorded, the others are set and recorded in list order
41    {
42        std::vector<std::pair<std::string, std::string>> env;   // name -> value
43        const auto get = [&](const char* k) -> const char* {
44            for (const auto& kv : env)
45                if (kv.first == k) return kv.second.c_str();
46            return nullptr;
47        };
48        const auto set = [&](const char* k) { env.emplace_back(k, "1"); };
49        std::vector<std::string> rec;
50        env.emplace_back("STRATA_PF_FUSED_Q8D", "0");   // the user named one of them (their way back)
51        strata::prefill::own_only_apply_defaults(false, get, set, rec);
52        CHECK(rec.size() == cuda.size() - 1);
53        CHECK(!strata::prefill::own_only_set_by_build("STRATA_PF_FUSED_Q8D", rec));
54        CHECK(std::string(get("STRATA_PF_FUSED_Q8D")) == "0");        // kept
55        CHECK(strata::prefill::own_only_set_by_build("STRATA_PF_FUSED", rec));
56        CHECK(std::string(get("STRATA_PF_FUSED")) == "1");
57        CHECK(strata::prefill::own_only_set_by_build("STRATA_PF_FUSED_Q2N", rec));
58        // "named by the user" (what the small-card rule reads): the build's own STRATA_PF_FUSED=1 is not, the same text from the user is, "" is neither
59        CHECK(!strata::prefill::own_only_user_named("STRATA_PF_FUSED", get("STRATA_PF_FUSED"), rec));
60        CHECK(strata::prefill::own_only_user_named("STRATA_PF_FUSED_Q8D", get("STRATA_PF_FUSED_Q8D"), rec));
61        CHECK(!strata::prefill::own_only_user_named("STRATA_PF_FUSED", nullptr, rec));
62        CHECK(!strata::prefill::own_only_user_named("STRATA_PF_FUSED_Q8D", "", rec));
63        // a user who set STRATA_PF_FUSED itself (either value) before the initialiser: not recorded, so still the user's
64        for (const char* v : {"1", "0"}) {
65            env.clear();
66            rec.clear();
67            env.emplace_back("STRATA_PF_FUSED", v);
68            strata::prefill::own_only_apply_defaults(false, get, set, rec);
69            CHECK(!strata::prefill::own_only_set_by_build("STRATA_PF_FUSED", rec));
70            CHECK(strata::prefill::own_only_user_named("STRATA_PF_FUSED", get("STRATA_PF_FUSED"), rec));
71            CHECK(std::string(get("STRATA_PF_FUSED")) == v);
72        }
73        // nothing set: every name of the list is the build's; the HIP list is shorter
74        env.clear();
75        rec.clear();
76        strata::prefill::own_only_apply_defaults(true, get, set, rec);
77        CHECK(rec.size() == hip.size());
78        CHECK(strata::prefill::own_only_set_by_build("STRATA_PF_FUSED", rec));
79        CHECK(!strata::prefill::own_only_set_by_build("STRATA_PF_FUSED_SM75", rec));
80        // the process-wide list is empty until the engine's initialiser (a binary that does not link prefill.cpp) runs
81        CHECK(strata::prefill::own_only_recorded().empty());
82        CHECK(!strata::prefill::own_only_user_named("STRATA_PF_FUSED", nullptr));
83        CHECK(strata::prefill::own_only_user_named("STRATA_PF_FUSED", "1"));   // no record: the user's
84        CHECK(!strata::prefill::own_only_build());                              // not an engine without MMQ until its initialiser says so
85        strata::prefill::own_only_build_flag() = true;
86        CHECK(strata::prefill::own_only_build());
87    }
88    if (failed) {
89        std::fprintf(stderr, "own_only_switches_test: %d check(s) failed\n", failed);
90        return 1;
91    }
92    std::printf("own_only_switches_test: OK\n");
93    return 0;
94}