okami.mom/strata

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

Niko1221Merge f-i8-mmqoff-int 70ef6d873 into f-p4-int (the build without llama.cpp's MMQ prompt kernels takes its own kernels by default)6572fbaf1

main
11.1 KiB180 linesraw
1// i8_expert_rule (include/strata/kernels/i8_expert_rule.hpp): when a prompt layer's expert groups take our own int8 kernels (native_expert_i8).
2// Nothing set: the HIP cards gfx1030 / gfx1031 / gfx1032 take them (groups averaging 256 entries or more, the layout and the device allowing it); every
3// other card keeps MMQ.  STRATA_EXPERT_I8=1 asks for them on any card, =0 goes back to the old kernels on the class.  No GPU, no model.
4#include <cstdio>
5#include <initializer_list>
6
7#include "strata/kernels/i8_expert_rule.hpp"
8
9using namespace strata::kernels;
10
11static int failed = 0;
12#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
13
14int main() {
15    // the state of the variable
16    CHECK(i8_switch_parse(nullptr) == -1);
17    CHECK(i8_switch_parse("") == -1);
18    CHECK(i8_switch_parse("0") == 0);
19    CHECK(i8_switch_parse("1") == 1);
20    CHECK(i8_switch_parse("yes") == 1);
21    CHECK(i8_switch_parse("0x") == 0);
22    // the class: exactly the three RDNA2 targets, with or without the feature suffix hipGetDeviceProperties adds
23    CHECK(gfx_arch_is_gfx103x_i8("gfx1030"));
24    CHECK(gfx_arch_is_gfx103x_i8("gfx1031"));
25    CHECK(gfx_arch_is_gfx103x_i8("gfx1032"));
26    CHECK(gfx_arch_is_gfx103x_i8("gfx1031:sramecc-:xnack-"));
27    CHECK(!gfx_arch_is_gfx103x_i8("gfx1034"));
28    CHECK(!gfx_arch_is_gfx103x_i8("gfx1035"));
29    CHECK(!gfx_arch_is_gfx103x_i8("gfx1036"));
30    CHECK(!gfx_arch_is_gfx103x_i8("gfx1100"));
31    CHECK(!gfx_arch_is_gfx103x_i8("gfx1151"));
32    CHECK(!gfx_arch_is_gfx103x_i8("gfx1201"));
33    CHECK(!gfx_arch_is_gfx103x_i8("gfx906"));
34    CHECK(!gfx_arch_is_gfx103x_i8("gfx10310"));
35    CHECK(!gfx_arch_is_gfx103x_i8(""));
36    CHECK(!gfx_arch_is_gfx103x_i8(nullptr));   // a CUDA card
37    // nothing set: the class takes them from 256 entries per group, smaller groups and every other card keep today's path
38    CHECK(i8_expert_takes(-1, "gfx1031", 256, true));
39    CHECK(i8_expert_takes(-1, "gfx1030", 4096, true));
40    CHECK(i8_expert_takes(-1, "gfx1032:sramecc-:xnack-", 300, true));
41    CHECK(!i8_expert_takes(-1, "gfx1031", 255, true));
42    CHECK(!i8_expert_takes(-1, "gfx1031", 0, true));
43    CHECK(!i8_expert_takes(-1, "gfx1031", 4096, false));   // a format or a size the kernel does not cover, or a card whose shared memory is too small
44    CHECK(!i8_expert_takes(-1, "gfx1201", 4096, true));    // RDNA4: MMQ / the fused kernels
45    CHECK(!i8_expert_takes(-1, "gfx1100", 4096, true));
46    CHECK(!i8_expert_takes(-1, "gfx1034", 4096, true));
47    CHECK(!i8_expert_takes(-1, nullptr, 4096, true));      // CUDA
48    CHECK(!i8_expert_takes(-1, "", 4096, true));
49    // =0: the way back, on the class and everywhere
50    CHECK(!i8_expert_takes(0, "gfx1031", 4096, true));
51    CHECK(!i8_expert_takes(0, "gfx1030", 100000, true));
52    CHECK(!i8_expert_takes(0, nullptr, 4096, true));
53    // =1: any card that the kernel covers, same size floor
54    CHECK(i8_expert_takes(1, "gfx1201", 256, true));
55    CHECK(i8_expert_takes(1, nullptr, 4096, true));
56    CHECK(i8_expert_takes(1, "gfx1031", 256, true));
57    CHECK(!i8_expert_takes(1, "gfx1201", 255, true));
58    CHECK(!i8_expert_takes(1, "gfx1201", 4096, false));
59    // which of the two reasons: the class default only when the variable is unset
60    CHECK(i8_expert_class_default(-1, "gfx1031"));
61    CHECK(!i8_expert_class_default(1, "gfx1031"));
62    CHECK(!i8_expert_class_default(0, "gfx1031"));
63    CHECK(!i8_expert_class_default(-1, "gfx1201"));
64    CHECK(!i8_expert_class_default(-1, nullptr));
65    // a build without llama.cpp's MMQ: an unset switch reads as 1 on every card, a value the user set is kept; a build with MMQ is the table above
66    CHECK(i8_switch_effective(-1, true) == 1);
67    CHECK(i8_switch_effective(0, true) == 0);
68    CHECK(i8_switch_effective(1, true) == 1);
69    CHECK(i8_switch_effective(-1, false) == -1);
70    CHECK(i8_switch_effective(0, false) == 0);
71    CHECK(i8_switch_effective(1, false) == 1);
72    {
73        // state {unset, 0, 1} x build {with MMQ, without} x card {RDNA2 gfx1030, gfx1100, Strix gfx1151, R9700 gfx1201, an NVIDIA card}: takes at 4,096 entries, the formats covered
74        const char* const cards[] = {"gfx1030", "gfx1100", "gfx1151", "gfx1201:sramecc-:xnack-", nullptr, ""};
75        for (const char* gcn : cards) {
76            const bool rdna2 = gfx_arch_is_gfx103x_i8(gcn);
77            // with MMQ: unchanged (only RDNA2 by default)
78            CHECK(i8_expert_takes(i8_switch_effective(-1, false), gcn, 4096, true) == rdna2);
79            CHECK(!i8_expert_takes(i8_switch_effective(0, false), gcn, 4096, true));
80            CHECK(i8_expert_takes(i8_switch_effective(1, false), gcn, 4096, true));
81            // without MMQ: on by default everywhere, =0 the way back, =1 as before
82            CHECK(i8_expert_takes(i8_switch_effective(-1, true), gcn, 4096, true));
83            CHECK(!i8_expert_takes(i8_switch_effective(0, true), gcn, 4096, true));
84            CHECK(i8_expert_takes(i8_switch_effective(1, true), gcn, 4096, true));
85            // the size floor and the format check are the same in both builds
86            CHECK(!i8_expert_takes(i8_switch_effective(-1, true), gcn, 255, true));
87            CHECK(!i8_expert_takes(i8_switch_effective(-1, true), gcn, 4096, false));
88            // which reason the engine's start line gives
89            CHECK(i8_expert_class_default(-1, gcn, true));
90            CHECK(!i8_expert_class_default(1, gcn, true));
91            CHECK(!i8_expert_class_default(0, gcn, true));
92            CHECK(i8_expert_class_default(-1, gcn, false) == rdna2);
93            CHECK(i8_expert_class_default(-1, gcn) == rdna2);   // (the old two-argument form)
94        }
95    }
96    // the order of the paths for a layer of a chunk: the fused kernels, then the staged group entry, then int8, then FP16 (a column "fused covers this layer and is on" and one for the staged entry);
97    // the same for a switch left unset in a build without MMQ and for a user-set 1: the int8 kernels get only what nothing faster takes
98    for (const char* gcn : {"gfx1201", "gfx1151", "gfx1100", "", "gfx1030"}) {
99        for (int raw : {-1, 1}) {
100            CHECK(i8_expert_takes(i8_switch_effective(raw, true), gcn, 4096, true));      // the kernel itself would take the layer on every card
101            CHECK(!i8_layer_takes(raw, true, gcn, 4096, true, true, false));              // fused covers the layer and is on: not int8
102            CHECK(!i8_layer_takes(raw, true, gcn, 4096, true, false, true));              // a short chunk the staged group entry takes: not int8
103            CHECK(!i8_layer_takes(raw, true, gcn, 4096, true, true, true));
104            CHECK(i8_layer_takes(raw, true, gcn, 4096, true, false, false));              // what is left: int8
105            CHECK(!i8_layer_takes(raw, true, gcn, 4096, false, false, false));            // a format the kernel does not cover: FP16
106        }
107        CHECK(!i8_layer_takes(0, true, gcn, 4096, true, false, false));                   // the way back
108        CHECK(!i8_layer_takes(-1, false, gcn, 4096, true, true, false));                  // (with MMQ: fused first as well)
109        CHECK(i8_layer_takes(-1, false, gcn, 4096, true, false, false) == gfx_arch_is_gfx103x_i8(gcn));
110    }
111    // the few-rows route: the limit (STRATA_I8_FEWROWS: not set = the class default, 0 = off, N = N rows per expert)
112    CHECK(i8_fewrows_limit(nullptr, 10) == 10);
113    CHECK(i8_fewrows_limit("", 8) == 8);
114    CHECK(i8_fewrows_limit("0", 10) == 0);
115    CHECK(i8_fewrows_limit("0x", 10) == 0);
116    CHECK(i8_fewrows_limit("12", 10) == 12);
117    CHECK(i8_fewrows_limit("6", 8) == 6);
118    CHECK(i8_fewrows_limit("yes", 10) == 10);
119    CHECK(i8_fewrows_limit("-3", 8) == 8);
120    CHECK(i8_fewrows_limit("99999999999", 10) > 100000);   // a huge number does not wrap to a small one
121    // the class default: the P100 (6.0) 6, every other card 7 (6.1, 7.0, 7.5, 8.6, 12.0, a HIP card with no capability at all)
122    CHECK(i8_fewrows_class_default(6, 0) == 6);
123    CHECK(i8_fewrows_class_default(6, 1) == 7);
124    CHECK(i8_fewrows_class_default(7, 0) == 7);
125    CHECK(i8_fewrows_class_default(8, 6) == 7);
126    CHECK(i8_fewrows_class_default(12, 0) == 7);
127    CHECK(i8_fewrows_class_default(0, 0) == 7);
128    // the decision: rows strictly under limit x experts go to the grouped kernels (512 experts: 5,119 rows is under 10 per expert, 5,120 is 10)
129    CHECK(i8_fewrows_takes(10, 5119, 512));
130    CHECK(!i8_fewrows_takes(10, 5120, 512));
131    CHECK(!i8_fewrows_takes(10, 20480, 512));
132    CHECK(i8_fewrows_takes(8, 4095, 512));
133    CHECK(!i8_fewrows_takes(8, 4096, 512));
134    CHECK(i8_fewrows_takes(10, 2560, 400));      // 256 tokens x 10 over the 400 experts that got rows: 6.4 per expert
135    CHECK(!i8_fewrows_takes(0, 100, 512));       // the route is off
136    CHECK(!i8_fewrows_takes(10, 100, 0));        // no experts: nothing to decide
137    CHECK(!i8_fewrows_takes(-1, 100, 512));
138    // the 8-entry slices: not set (-1) = tile 2 only, 1 = tiles 2-4, 0 = none; tile 8 (the 128-entry kernel) never has them
139    CHECK(i8_n8_takes(-1, 2));
140    CHECK(!i8_n8_takes(-1, 3));
141    CHECK(!i8_n8_takes(-1, 4));
142    CHECK(!i8_n8_takes(-1, 8));
143    CHECK(i8_n8_takes(1, 2));
144    CHECK(i8_n8_takes(1, 3));
145    CHECK(i8_n8_takes(1, 4));
146    CHECK(!i8_n8_takes(1, 8));
147    CHECK(!i8_n8_takes(0, 2));
148    CHECK(!i8_n8_takes(0, 4));
149    CHECK(i8_n8_takes(i8_switch_parse(nullptr), 2));
150    CHECK(i8_n8_takes(i8_switch_parse(""), 2));
151    CHECK(!i8_n8_takes(i8_switch_parse("0"), 2));
152    CHECK(i8_n8_takes(i8_switch_parse("1"), 4));
153    // the P100 class rule (build without MMQ, switch not set): the int8 default applies under the stream floor (stream_all_min, 1,024 tokens by default) only; the FP16 path from it up (the build with MMQ's default there)
154    {
155        const int64_t lim = 1024;   // stream_all_min() by default: the rule is the comparison, rows at floor - 1, floor, floor + 1
156        CHECK(i8_chunk_default_allows(-1, true, true, 1, lim));
157        CHECK(i8_chunk_default_allows(-1, true, true, lim - 1, lim));
158        CHECK(!i8_chunk_default_allows(-1, true, true, lim, lim));
159        CHECK(!i8_chunk_default_allows(-1, true, true, lim + 1, lim));
160        CHECK(i8_chunk_default_allows(-1, true, true, 300, lim));
161        CHECK(i8_chunk_default_allows(-1, true, true, 1000, lim));
162        CHECK(i8_chunk_default_allows(-1, true, true, lim - 1, lim));
163        CHECK(!i8_chunk_default_allows(-1, true, true, lim, lim));
164        CHECK(!i8_chunk_default_allows(-1, true, true, 2048, lim));
165        CHECK(!i8_chunk_default_allows(-1, true, true, 8192, lim));
166        // a value the user set keeps its meaning at every size (the rule is not asked to veto it)
167        CHECK(i8_chunk_default_allows(1, true, true, 8192, lim));
168        CHECK(i8_chunk_default_allows(0, true, true, 8192, lim));
169        // not the P100 (6.1, 7.x, 8.x, a HIP card): no limit; a build with MMQ: no limit (its P100 default is the FP16 path already)
170        CHECK(i8_chunk_default_allows(-1, true, false, 8192, lim));
171        CHECK(i8_chunk_default_allows(-1, false, true, 8192, lim));
172        CHECK(i8_chunk_default_allows(-1, false, false, 8192, lim));
173    }
174    if (failed) {
175        std::fprintf(stderr, "i8_expert_rule_test: %d check(s) failed\n", failed);
176        return 1;
177    }
178    std::printf("i8_expert_rule_test passed\n");
179    return 0;
180}