Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
113 lines
7.3 KiB
C++
113 lines
7.3 KiB
C++
// 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
|
|
// elsewhere, --experts overrides, DS4_TOPK over both. No GPU needed.
|
|
#include "ds4_experts_rule.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
using strata::ds4::experts_per_word;
|
|
using strata::ds4::experts_test_override;
|
|
|
|
static int fails = 0;
|
|
#define CHECK_EQ(a, b) \
|
|
do { \
|
|
const long long x = (a), y = (b); \
|
|
if (x != y) { \
|
|
std::printf("FAIL %s:%d: %s = %lld, expected %lld\n", __FILE__, __LINE__, #a, x, y); \
|
|
++fails; \
|
|
} \
|
|
} while (0)
|
|
|
|
int main() {
|
|
const uint64_t GiB = 1ull << 30;
|
|
// the owner's 12 GB-class cards (RTX 5070 12227 MiB, RTX 3060 12 GB), 16 GiB cards: faster mode by default
|
|
CHECK_EQ(experts_per_word(-1, 12227ull << 20, 6), 5);
|
|
CHECK_EQ(experts_per_word(-1, 12 * GiB, 6), 5);
|
|
CHECK_EQ(experts_per_word(-1, 16 * GiB, 6), 5);
|
|
CHECK_EQ(experts_per_word(-1, 8 * GiB, 6), 5);
|
|
// 24 GB and bigger, and several cards: the model's own 6
|
|
CHECK_EQ(experts_per_word(-1, 16 * GiB + 1, 6), 6);
|
|
CHECK_EQ(experts_per_word(-1, 24 * GiB, 6), 6);
|
|
CHECK_EQ(experts_per_word(-1, 4 * 32 * GiB, 6), 6);
|
|
CHECK_EQ(experts_per_word(-1, 2 * 12 * GiB, 6), 6); // two 12 GB cards hold 24 GiB in all
|
|
// a GPU that shares the PC's RAM (Strix Halo, ~91 GiB visible): faster mode whatever the size; discrete cards by size only
|
|
CHECK_EQ(experts_per_word(-1, 91 * GiB, 6, true), 5);
|
|
CHECK_EQ(experts_per_word(-1, 24 * GiB, 6, false), 6); // a discrete 24 GB card
|
|
CHECK_EQ(experts_per_word(-1, 12 * GiB, 6, false), 5); // a discrete 12 GB card
|
|
CHECK_EQ(experts_per_word(6, 91 * GiB, 6, true), 6); // --experts 6 wins on the unified-memory GPU too
|
|
CHECK_EQ(experts_per_word(-1, 91 * GiB, 4, true), 4); // never raised
|
|
// unknown memory (0): the model's count, never a silent cut
|
|
CHECK_EQ(experts_per_word(-1, 0, 6), 6);
|
|
// the flag decides, both ways
|
|
CHECK_EQ(experts_per_word(6, 12 * GiB, 6), 6);
|
|
CHECK_EQ(experts_per_word(4, 12 * GiB, 6), 4);
|
|
CHECK_EQ(experts_per_word(5, 96 * GiB, 6), 5);
|
|
CHECK_EQ(experts_per_word(9, 12 * GiB, 6), 6); // never more than the model has
|
|
CHECK_EQ(experts_per_word(1, 12 * GiB, 6), 1);
|
|
// a model that routes to 1 (or no) expert: left alone
|
|
CHECK_EQ(experts_per_word(-1, 12 * GiB, 1), 1);
|
|
CHECK_EQ(experts_per_word(-1, 12 * GiB, 0), 0);
|
|
// a model that routes to fewer than 5: the rule never raises it
|
|
CHECK_EQ(experts_per_word(-1, 12 * GiB, 4), 4);
|
|
// the test override wins over the rule and the flag
|
|
#if defined(_WIN32)
|
|
_putenv("DS4_TOPK=4");
|
|
#else
|
|
setenv("DS4_TOPK", "4", 1);
|
|
#endif
|
|
CHECK_EQ(experts_test_override(experts_per_word(-1, 96 * GiB, 6), 6), 4);
|
|
CHECK_EQ(experts_test_override(experts_per_word(6, 12 * GiB, 6), 6), 4);
|
|
#if defined(_WIN32)
|
|
_putenv("DS4_TOPK=9");
|
|
#else
|
|
setenv("DS4_TOPK", "9", 1);
|
|
#endif
|
|
CHECK_EQ(experts_test_override(5, 6), 5); // out of range: ignored
|
|
#if defined(_WIN32)
|
|
_putenv("DS4_TOPK=");
|
|
#else
|
|
unsetenv("DS4_TOPK");
|
|
#endif
|
|
CHECK_EQ(experts_test_override(5, 6), 5);
|
|
// 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
|
|
// 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.
|
|
using strata::ds4::prefill_chunk_default;
|
|
// 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)
|
|
CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12227ull << 20), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12 * GiB), 4096);
|
|
// the same cards with --tp 1 on one card: share 0.06 < 0.5 -> the same 4096
|
|
CHECK_EQ(prefill_chunk_default(true, 0.06, false, true, false, 12 * GiB), 4096);
|
|
// 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
|
|
CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 16 * GiB - 1), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 16 * GiB), 8192);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.10, false, true, false, 24 * GiB), 8192);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.0, false, true, false, 8 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.0, false, true, false, 0), 8192);
|
|
// a 12 GB HIP card is not measured: the big chunk
|
|
CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, true, 12 * GiB), 8192);
|
|
// a 5090-class card (32 GB, share 0.19): in place 8192; with the experts copied into RAM 4096
|
|
CHECK_EQ(prefill_chunk_default(false, 0.19, false, true, false, 32 * GiB), 8192);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.19, true, true, false, 32 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, false, 32 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.10, true, true, false, 16 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.05, true, true, false, 24 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.06, true, true, false, 12227ull << 20), 4096);
|
|
// 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
|
|
CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 8 * GiB), 4096);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 0), 8192);
|
|
// 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)
|
|
CHECK_EQ(prefill_chunk_default(true, 0.19, false, true, true, 32 * GiB), 8192);
|
|
CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, true, 32 * GiB), 8192);
|
|
// 4 x R9700 (share 0.86), single-card default run (--tp 1 --threads off): the small chunk
|
|
CHECK_EQ(prefill_chunk_default(true, 0.86, false, true, true, 32 * GiB), 1024);
|
|
CHECK_EQ(prefill_chunk_default(true, 0.50, false, true, true, 32 * GiB), 1024);
|
|
CHECK_EQ(prefill_chunk_default(true, 0.49, false, true, true, 32 * GiB), 8192);
|
|
// threads / tp runs (own_default false) keep their defaults whatever the share: 8192 streamed, 2048 for a pack that is not a GGUF
|
|
CHECK_EQ(prefill_chunk_default(false, 0.86, false, true, true, 32 * GiB), 8192);
|
|
CHECK_EQ(prefill_chunk_default(false, 0.30, false, false, false, 24 * GiB), 2048);
|
|
// 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
|
|
// allocated whole at load for that context and a chunk that does not fit there is halved before the first prompt (4096 -> 2048 -> 1024 ...)
|
|
if (fails == 0) std::printf("experts rule: all checks passed\n");
|
|
return fails == 0 ? 0 : 1;
|
|
}
|