okami.mom/strata

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

Niko1221Merge f-peer-decline-fused: --peer-device keeps the fused prompt kernels in a build without MMQ331828134

main
12.6 KiB171 linesraw
1// own_only_line (include/strata/prefill/own_only_line.hpp): the start-up sentence of an engine built without llama.cpp's MMQ prompt kernels, from the
2// layer counts per path.  No GPU, no model.
3#include <cstdio>
4#include <string>
5
6#include "strata/prefill/own_only_line.hpp"
7#include "strata/prefill/plain_floor_rule.hpp"
8
9using strata::prefill::OwnOnlySplit;
10using strata::prefill::own_only_line;
11
12static int failed = 0;
13#define CHECK_LINE(got, want) do { const std::string g_ = (got); if (g_ != (want)) { std::fprintf(stderr, "FAIL %s:%d\n  got:  %s\n  want: %s\n", __FILE__, __LINE__, g_.c_str(), (want)); ++failed; } } while (0)
14
15int main() {
16    // Strix-like: every layer on the fused kernels for big chunks, on the staged fused entry for small ones
17    {
18        OwnOnlySplit big; big.fused = 48;
19        OwnOnlySplit small; small.staged = 48;
20        CHECK_LINE(own_only_line(48, 1024, big, small, false),
21                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry.");
22    }
23    // an IQ pack no fused kernel covers: every layer on the int8 kernels at every size -> one clause
24    {
25        OwnOnlySplit s; s.i8 = 48;
26        CHECK_LINE(own_only_line(48, 1024, s, s, false), "strata: prompt kernels: every chunk runs 48 of 48 layers on the int8 kernels.");
27    }
28    // mixed: 20 fused / 28 int8 at the floor and above, the staged entry for the 20 below it
29    {
30        OwnOnlySplit big; big.fused = 20; big.i8 = 28;
31        OwnOnlySplit small; small.staged = 20; small.i8 = 28;
32        CHECK_LINE(own_only_line(48, 1024, big, small, false),
33                   "strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels and 28 on the int8 kernels; "
34                   "shorter chunks run 20 on the staged fused entry and 28 on the int8 kernels.");
35        // the same card without the staged entry: the fused layers of a short chunk take the int8 kernels
36        OwnOnlySplit small2; small2.i8 = 48;
37        CHECK_LINE(own_only_line(48, 1024, big, small2, false),
38                   "strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels and 28 on the int8 kernels; "
39                   "shorter chunks run 48 on the int8 kernels.");
40    }
41    // FP16 above 0: a further part
42    {
43        OwnOnlySplit big; big.fused = 20; big.i8 = 25; big.fp16 = 3;
44        OwnOnlySplit small; small.staged = 20; small.i8 = 25; small.fp16 = 3;
45        CHECK_LINE(own_only_line(48, 1024, big, small, false),
46                   "strata: prompt kernels: chunks of 1024 tokens or more run 20 of 48 layers on the fused kernels, 25 on the int8 kernels and 3 on the FP16 path; "
47                   "shorter chunks run 20 on the staged fused entry, 25 on the int8 kernels and 3 on the FP16 path.");
48        OwnOnlySplit all; all.fp16 = 48;
49        CHECK_LINE(own_only_line(48, 1024, all, all, false), "strata: prompt kernels: every chunk runs 48 of 48 layers on the FP16 path.");
50    }
51    // --peer-device: the clause, the counts as they then are
52    {
53        OwnOnlySplit s; s.i8 = 40; s.fp16 = 8;
54        CHECK_LINE(own_only_line(48, 1024, s, s, true),
55                   "strata: prompt kernels: every chunk runs 40 of 48 layers on the int8 kernels and 8 on the FP16 path (--peer-device: the fused kernels are off).");
56    }
57    // peer_keeps_mmq: only an engine with MMQ keeps the walk on MMQ for --peer-device; without MMQ the share is declined and the walk keeps its own kernels
58    {
59        static_assert(!strata::prefill::peer_keeps_mmq(false, false), "no flag, with MMQ: the walk's own kernels");
60        static_assert(!strata::prefill::peer_keeps_mmq(false, true), "no flag, without MMQ: the walk's own kernels");
61        static_assert(strata::prefill::peer_keeps_mmq(true, false), "--peer-device with MMQ: MMQ only");
62        static_assert(!strata::prefill::peer_keeps_mmq(true, true), "--peer-device without MMQ: the walk's own kernels (the share is declined)");
63        // the start-up line of a build without MMQ with --peer-device on: the counts without the flag and no clause
64        OwnOnlySplit big; big.fused = 48;
65        OwnOnlySplit small; small.staged = 48;
66        CHECK_LINE(own_only_line(48, 1024, big, small, strata::prefill::peer_keeps_mmq(true, true)),
67                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry.");
68#if defined(STRATA_PF_OWN_ONLY)   // the test built as an engine without MMQ is (prefill.cpp kPfOwnOnlyBuild): the same answers from this build's flag
69        constexpr bool own_only_build = true;
70#else
71        constexpr bool own_only_build = false;
72#endif
73        const std::string with_flag = own_only_line(48, 1024, big, small, strata::prefill::peer_keeps_mmq(true, own_only_build));
74        const bool has_clause = with_flag.find("(--peer-device: the fused kernels are off)") != std::string::npos;
75        if (has_clause == own_only_build) { std::fprintf(stderr, "FAIL %s:%d: --peer-device clause %s in this build (own-only %d)\n", __FILE__, __LINE__, has_clause ? "present" : "missing", (int) own_only_build); ++failed; }
76    }
77    // the Tesla P100 in a build without MMQ (no fused kernels; the int8 default under the chunk limit, the FP16 path from it): the stream floor is the limit: FP16 from it up
78    {
79        OwnOnlySplit big; big.fp16 = 48;
80        OwnOnlySplit small; small.i8 = 48;
81        CHECK_LINE(own_only_line(48, 1024, big, small, false),
82                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the FP16 path; shorter chunks run 48 on the int8 kernels.");
83    }
84    // the counts of own_only_count_layer: 48 Hadamard-folded fused-only layers (Gyro-S): the staged entry and the int8 kernels read no folded layer, so below the floor they are on the FP16 path
85    {
86        OwnOnlySplit big, small;
87        for (int l = 0; l < 48; ++l) strata::prefill::own_only_count_layer(big, small, true, false, false, true, false);
88        CHECK_LINE(own_only_line(48, 1024, big, small, false),
89                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path.");
90    }
91    // the same layers unfolded: the staged entry takes them (the line as before the change)
92    {
93        OwnOnlySplit big, small;
94        for (int l = 0; l < 48; ++l) strata::prefill::own_only_count_layer(big, small, true, false, false, true, true);
95        CHECK_LINE(own_only_line(48, 1024, big, small, false),
96                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the staged fused entry.");
97    }
98    // a mix: 40 folded and 8 unfolded fused-only layers; and an int8 layer and a gated class keep their counts
99    {
100        OwnOnlySplit big, small;
101        for (int l = 0; l < 40; ++l) strata::prefill::own_only_count_layer(big, small, true, false, false, true, false);
102        for (int l = 0; l < 8; ++l) strata::prefill::own_only_count_layer(big, small, true, false, false, true, true);
103        CHECK_LINE(own_only_line(48, 1024, big, small, false),
104                   "strata: prompt kernels: chunks of 1024 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 8 on the staged fused entry and 40 on the FP16 path.");
105        OwnOnlySplit b2, s2;
106        strata::prefill::own_only_count_layer(b2, s2, false, true, true, true, true);   // an int8 layer on a gated class: FP16 in the long class, int8 below
107        if (!(b2.fp16 == 1 && s2.i8 == 1 && b2.i8 == 0)) { std::fprintf(stderr, "FAIL %s:%d: gated int8 layer\n", __FILE__, __LINE__); ++failed; }
108    }
109    // The printed floor is the one the rule returns once the share is armed and the card has answered (plain_floor_rule.hpp floor_with_share), not the value at the
110    // time the plan is made.  Plain Q2_0 pack in a build without MMQ: 48 layers on the fused kernels at the floor and above, 48 on the FP16 path below it.
111    {
112        using strata::prefill::floor_with_share;
113        using strata::prefill::OwnOnlyLineDeferred;
114        OwnOnlySplit big; big.fused = 48;
115        OwnOnlySplit small; small.fp16 = 48;
116        const char* line_1536 = "strata: prompt kernels: chunks of 1536 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path.";
117        const char* line_3072 = "strata: prompt kernels: chunks of 3072 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path.";
118        const char* line_64 = "strata: prompt kernels: chunks of 64 tokens or more run 48 of 48 layers on the fused kernels; shorter chunks run 48 on the FP16 path.";
119        // the plan is made first (the old line printed 64 here): nothing is due until the share is armed
120        OwnOnlyLineDeferred d;
121        d.plan(48, big, small, false);
122        if (d.due()) { std::fprintf(stderr, "FAIL %s:%d: due before the share is armed\n", __FILE__, __LINE__); ++failed; }
123        d.mark_ready();
124        if (!d.due()) { std::fprintf(stderr, "FAIL %s:%d: not due after the share is armed\n", __FILE__, __LINE__); ++failed; }
125        // a discrete CUDA card, share armed: the plain pack's floor 1,536
126        CHECK_LINE(d.take(floor_with_share(true, 3072, 64)), line_1536);
127        if (d.due()) { std::fprintf(stderr, "FAIL %s:%d: printed twice\n", __FILE__, __LINE__); ++failed; }
128        // the same plan with the rule switched off (STRATA_PF_PLAIN_FLOOR=0) or on an integrated card: the share's limit when armed, else the pack's 64
129        OwnOnlyLineDeferred e;
130        e.plan(48, big, small, false);
131        e.mark_ready();
132        CHECK_LINE(e.take(floor_with_share(false, 3072, 64)), line_3072);
133        OwnOnlyLineDeferred f;
134        f.mark_ready();   // armed before the plan was made: the plan prints at once
135        if (f.due()) { std::fprintf(stderr, "FAIL %s:%d: due without counts\n", __FILE__, __LINE__); ++failed; }
136        f.plan(48, big, small, false);
137        if (!f.due()) { std::fprintf(stderr, "FAIL %s:%d: a plan made after the share was armed is not due\n", __FILE__, __LINE__); ++failed; }
138        CHECK_LINE(f.take(floor_with_share(false, 0, 64)), line_64);
139    }
140    // The "prompt pieces" line (default_stream_floor() caches the pack's own 64): the same wait, and the floor in force when it prints, not the 64 it was cached with.
141    {
142        using strata::prefill::floor_with_share;
143        using strata::prefill::OwnOnlyLineDeferred;
144        const char* tail = " tokens or more read on the fused kernels, as long prompts do. STRATA_PF_PLAIN_FLOOR=0 keeps the earlier rule; STRATA_PREFILL_STREAM_MIN=N sets the number by hand.";
145        const std::string p1536 = std::string("prefill: prompt pieces of 1536") + tail;
146        const std::string p3072 = std::string("prefill: prompt pieces of 3072") + tail;
147        const std::string p64 = std::string("prefill: prompt pieces of 64") + tail;
148        OwnOnlyLineDeferred d;
149        d.want_pieces(true);
150        if (d.pieces_due() || d.due()) { std::fprintf(stderr, "FAIL %s:%d: pieces line due before the share is armed\n", __FILE__, __LINE__); ++failed; }
151        d.mark_ready();
152        if (!d.pieces_due() || d.due()) { std::fprintf(stderr, "FAIL %s:%d: pieces line not due (or the plan line due without counts)\n", __FILE__, __LINE__); ++failed; }
153        CHECK_LINE(d.take_pieces(floor_with_share(true, 3072, 64)), p1536.c_str());   // discrete card, plain pack, share armed
154        if (d.pieces_due()) { std::fprintf(stderr, "FAIL %s:%d: pieces line printed twice\n", __FILE__, __LINE__); ++failed; }
155        CHECK_LINE(strata::prefill::stream_floor_pieces_line(floor_with_share(false, 3072, 64), true), p3072.c_str());   // rule off / integrated, share armed
156        CHECK_LINE(strata::prefill::stream_floor_pieces_line(floor_with_share(false, 0, 64), true), p64.c_str());        // rule off / integrated, no share: as before
157        // a trellis pack's line does not name STRATA_PF_PLAIN_FLOOR, which never moves its floor
158        CHECK_LINE(strata::prefill::stream_floor_pieces_line(1536, false),
159                   "prefill: prompt pieces of 1536 tokens or more read on the fused kernels, as long prompts do. STRATA_PREFILL_STREAM_MIN=N sets the number by hand.");
160        OwnOnlyLineDeferred t;
161        t.want_pieces(false);
162        t.mark_ready();
163        CHECK_LINE(t.take_pieces(1536), "prefill: prompt pieces of 1536 tokens or more read on the fused kernels, as long prompts do. STRATA_PREFILL_STREAM_MIN=N sets the number by hand.");
164    }
165    if (failed) {
166        std::fprintf(stderr, "own_only_line_test: %d check(s) failed\n", failed);
167        return 1;
168    }
169    std::printf("own_only_line_test: OK\n");
170    return 0;
171}