okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
8.9 KiB131 linesraw
1// Strata. The experts-per-word rule of the DeepSeek V4.1 engine (src/dsv41/ds4_experts_rule.hpp): 5 on a card with 16 GiB or less, 6
2// elsewhere, --experts overrides, DS4_TOPK over both.  No GPU needed.
3#include "ds4_experts_rule.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7
8using strata::ds4::experts_per_word;
9using strata::ds4::experts_test_override;
10using strata::ds4::copy_all_fits;
11
12static int fails = 0;
13#define CHECK_EQ(a, b)                                                                              \
14    do {                                                                                            \
15        const long long x = (a), y = (b);                                                           \
16        if (x != y) {                                                                               \
17            std::printf("FAIL %s:%d: %s = %lld, expected %lld\n", __FILE__, __LINE__, #a, x, y);    \
18            ++fails;                                                                                \
19        }                                                                                           \
20    } while (0)
21
22int main() {
23    const uint64_t GiB = 1ull << 30;
24    // the owner's 12 GB-class cards (RTX 5070 12227 MiB, RTX 3060 12 GB), 16 GiB cards: faster mode by default
25    CHECK_EQ(experts_per_word(-1, 12227ull << 20, 6), 5);
26    CHECK_EQ(experts_per_word(-1, 12 * GiB, 6), 5);
27    CHECK_EQ(experts_per_word(-1, 16 * GiB, 6), 5);
28    CHECK_EQ(experts_per_word(-1, 8 * GiB, 6), 5);
29    // 24 GB and bigger, and several cards: the model's own 6
30    CHECK_EQ(experts_per_word(-1, 16 * GiB + 1, 6), 6);
31    CHECK_EQ(experts_per_word(-1, 24 * GiB, 6), 6);
32    CHECK_EQ(experts_per_word(-1, 4 * 32 * GiB, 6), 6);
33    CHECK_EQ(experts_per_word(-1, 2 * 12 * GiB, 6), 6);   // two 12 GB cards hold 24 GiB in all
34    // a GPU that shares the PC's RAM (Strix Halo, ~91 GiB visible): faster mode whatever the size; discrete cards by size only
35    CHECK_EQ(experts_per_word(-1, 91 * GiB, 6, true), 5);
36    CHECK_EQ(experts_per_word(-1, 24 * GiB, 6, false), 6);   // a discrete 24 GB card
37    CHECK_EQ(experts_per_word(-1, 12 * GiB, 6, false), 5);   // a discrete 12 GB card
38    CHECK_EQ(experts_per_word(6, 91 * GiB, 6, true), 6);     // --experts 6 wins on the unified-memory GPU too
39    CHECK_EQ(experts_per_word(-1, 91 * GiB, 4, true), 4);    // never raised
40    // unknown memory (0): the model's count, never a silent cut
41    CHECK_EQ(experts_per_word(-1, 0, 6), 6);
42    // the flag decides, both ways
43    CHECK_EQ(experts_per_word(6, 12 * GiB, 6), 6);
44    CHECK_EQ(experts_per_word(4, 12 * GiB, 6), 4);
45    CHECK_EQ(experts_per_word(5, 96 * GiB, 6), 5);
46    CHECK_EQ(experts_per_word(9, 12 * GiB, 6), 6);   // never more than the model has
47    CHECK_EQ(experts_per_word(1, 12 * GiB, 6), 1);
48    // a model that routes to 1 (or no) expert: left alone
49    CHECK_EQ(experts_per_word(-1, 12 * GiB, 1), 1);
50    CHECK_EQ(experts_per_word(-1, 12 * GiB, 0), 0);
51    // a model that routes to fewer than 5: the rule never raises it
52    CHECK_EQ(experts_per_word(-1, 12 * GiB, 4), 4);
53    // the test override wins over the rule and the flag
54#if defined(_WIN32)
55    _putenv("DS4_TOPK=4");
56#else
57    setenv("DS4_TOPK", "4", 1);
58#endif
59    CHECK_EQ(experts_test_override(experts_per_word(-1, 96 * GiB, 6), 6), 4);
60    CHECK_EQ(experts_test_override(experts_per_word(6, 12 * GiB, 6), 6), 4);
61#if defined(_WIN32)
62    _putenv("DS4_TOPK=9");
63#else
64    setenv("DS4_TOPK", "9", 1);
65#endif
66    CHECK_EQ(experts_test_override(5, 6), 5);   // out of range: ignored
67#if defined(_WIN32)
68    _putenv("DS4_TOPK=");
69#else
70    unsetenv("DS4_TOPK");
71#endif
72    CHECK_EQ(experts_test_override(5, 6), 5);
73    // prompt chunk: prefill_chunk_default(own_default, gpu_share, gguf_copy, file_experts, hip, min_vram, big=8192).  What `strata launch` runs on one card
74    // passes neither --tp nor --threads, but the engine's default tp is 2, so own_default is false there; a launch with several cards passes --tp 1.
75    using strata::ds4::prefill_chunk_default;
76    // RTX 5070 / RTX 3060 (12 GB), launch default: experts in place on a card under 16 GiB -> 4096 (their logs said 8192 before this rule)
77    CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12227ull << 20), 4096);
78    CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12 * GiB), 4096);
79    // the same cards with --tp 1 on one card: share 0.06 < 0.5 -> the same 4096
80    CHECK_EQ(prefill_chunk_default(true, 0.06, false, true, false, 12 * GiB), 4096);
81    // 16 GB cards (15.9 GiB total) 4096; a card with exactly 16 GiB or more keeps the big chunk; short VRAM (8 GB) 4096; unknown VRAM the big one
82    CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 16 * GiB - 1), 4096);
83    CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 16 * GiB), 8192);
84    CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 24 * GiB), 8192);
85    CHECK_EQ(prefill_chunk_default(false, 0.0, false, true, false, 8 * GiB), 4096);
86    CHECK_EQ(prefill_chunk_default(false, 0.0, false, true, false, 0), 8192);
87    // a 12 GB HIP card is not measured: the big chunk
88    CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, true, 12 * GiB), 8192);
89    // a 5090-class card (32 GB, share 0.19): in place 8192; with the experts copied into RAM 4096
90    CHECK_EQ(prefill_chunk_default(false, 0.19, false, true, false, 32 * GiB), 8192);
91    CHECK_EQ(prefill_chunk_default(false, 0.19, true, true, false, 32 * GiB), 4096);
92    CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, false, 32 * GiB), 4096);
93    CHECK_EQ(prefill_chunk_default(false, 0.10, true, true, false, 16 * GiB), 4096);
94    CHECK_EQ(prefill_chunk_default(false, 0.05, true, true, false, 24 * GiB), 4096);
95    CHECK_EQ(prefill_chunk_default(false, 0.06, true, true, false, 12227ull << 20), 4096);
96    // copied into RAM on a card under 10 GiB (short VRAM): not the copy rule; the in-place rule for a small card gives 4096, unknown VRAM 8192
97    CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 8 * GiB), 4096);
98    CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 0), 8192);
99    // 1 x R9700 (HIP, 32 GB, share 0.19), single-card default run (tp 1 passed by a multi-card box): 8192, also when copied (HIP is not measured at 4096)
100    CHECK_EQ(prefill_chunk_default(true, 0.19, false, true, true, 32 * GiB), 8192);
101    CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, true, 32 * GiB), 8192);
102    // 4 x R9700 (share 0.86), single-card default run (--tp 1 --threads off): the small chunk
103    CHECK_EQ(prefill_chunk_default(true, 0.86, false, true, true, 32 * GiB), 1024);
104    CHECK_EQ(prefill_chunk_default(true, 0.50, false, true, true, 32 * GiB), 1024);
105    CHECK_EQ(prefill_chunk_default(true, 0.49, false, true, true, 32 * GiB), 8192);
106    // threads / tp runs (own_default false) keep their defaults whatever the share: 8192 streamed, 2048 for a pack that is not a GGUF
107    CHECK_EQ(prefill_chunk_default(false, 0.86, false, true, true, 32 * GiB), 8192);
108    CHECK_EQ(prefill_chunk_default(false, 0.30, false, false, false, 24 * GiB), 2048);
109    // the rule takes no --max-context: a 128K session on a 12 GB card gets the same chunk, because the chunk's buffers and the KV are
110    // allocated whole at load for that context and a chunk that does not fit there is halved before the first prompt (4096 -> 2048 -> 1024 ...)
111    // copy_all_fits: the 251 GB R9700 box (108 GB lane) copies; a 64 GB PC, a 125 GB box with the 2.25 pack's lane, an unknown RAM do not
112    {
113        const double GB = 1e9, margin = 8.0 * (1ull << 30);
114        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (236 * GB), margin, 1e300), 1);
115        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (110 * GB), margin, 1e300), 0);   // inside the margin
116        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (116 * GB), margin, 1e300), 0);   // 108 + 8.6 > 116
117        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (118 * GB), margin, 1e300), 1);
118        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (101 * GB), margin, 1e300), 0);   // the 125 GB box, free RAM 101 GB
119        CHECK_EQ(copy_all_fits(92 * GB, ~uint64_t{0}, margin, 1e300), 0);             // RAM not known
120        CHECK_EQ(copy_all_fits(0, (uint64_t) (236 * GB), margin, 1e300), 0);          // nothing on the CPU lane
121        // Windows: the commit room (RAM + page file beside its margin) must hold the lane too, whatever the free RAM says
122        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (236 * GB), margin, 90 * GB), 0);
123        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (236 * GB), margin, 108 * GB), 1);
124        // the owner-PC class: 64 GB RAM, a 12 GB card (a lane of ~120 GB), 40 GB free, a 90 GB commit limit
125        CHECK_EQ(copy_all_fits(120 * GB, (uint64_t) (40 * GB), margin, 60 * GB), 0);
126        // a 192 GB Windows PC with a 4 GB page file: 150 GB free RAM but only 100 GB of commit room: no
127        CHECK_EQ(copy_all_fits(108 * GB, (uint64_t) (150 * GB), margin, 100 * GB), 0);
128    }
129    if (fails == 0) std::printf("experts rule: all checks passed\n");
130    return fails == 0 ? 0 : 1;
131}