okami.mom/strata

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

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

main
5.2 KiB71 linesraw
1// Strata. Experts per word: the "faster mode" rule of the DeepSeek V4.1 engine.  Header only, no GPU code (tests/dsv41/experts_rule_test.cpp).
2//
3// The model routes every word to its top 6 experts.  On a PC with a small graphics card the CPU lane and the SSD set the speed, and
4// each expert dropped saves their bytes: the router's top N are kept and their weights renormalised (the same selection the full
5// model makes, a shorter list).  The default is 5 where the graphics memory is 16 GiB or less, or the GPU shares the PC's RAM, the model's own count elsewhere;
6// --experts N says it outright (--experts 6: full quality), DS4_TOPK=N is the test override over everything.
7#pragma once
8
9#include <cstdint>
10#include <cstdlib>
11
12namespace strata::ds4 {
13
14constexpr uint64_t kFasterModeVramMax = 16ull << 30;   // GiB of graphics memory in all, at most
15constexpr int kFasterModeExperts = 5;
16
17// requested: --experts (-1: not given, the rule decides); vram_bytes: the total memory of the cards the engine uses;
18// model_k: the experts a word takes in the model; integrated: a GPU that shares the PC's RAM (Strix Halo: part of the experts live only on
19// the SSD, each expert dropped is less read - measured 5 against 6 on a Radeon 8065S: decode +11..13%, prompts +5..7%).  The result is in 1 .. model_k.
20inline int experts_per_word(int requested, uint64_t vram_bytes, int model_k, bool integrated = false) {
21    if (model_k <= 1) return model_k;
22    int n = requested;
23    if (n < 1) n = integrated || (vram_bytes != 0 && vram_bytes <= kFasterModeVramMax) ? kFasterModeExperts : model_k;
24    if (n > model_k) n = model_k;
25    return n < 1 ? 1 : n;
26}
27
28// Prompt chunk (--prefill-chunk) default, one rule decided once at load from bytes (no timing).  The chunk's buffers come out of the VRAM the
29// expert tier would have, and every chunk streams the experts the cards do not hold once, so a bigger chunk is fewer passes.  Measured (8K prompt):
30//   - the GPUs hold most of the experts (4 x R9700, share 0.86): 1024 370 tok/s, 8192 214  -> small chunk;
31//   - the experts are read in place and the CPU lane streams most of them (RTX 5070 / 3060 12 GB, 1 x R9700): 8192 best (5070 244 against 186
32//     at 1024, 3060 228 against 175, 1 x R9700 431 against 200) -> the big chunk (the GGUF pack's long-standing default);
33//   - the experts copied into RAM (--mmap-experts off): H100 32 GiB 2048 502, 4096 538, 8192 495 -> 4096 (not measured on HIP: no).
34// Reference KL against the original model does not move with the chunk (1024 0.1705, 2048 0.1692, 4096 0.1688, 8192 0.1695).
35// Alloc failure halves the chunk at load (4096 -> 2048 -> 1024 ...), KV and score buffers are allocated whole at the full --max-context.
36constexpr uint64_t kChunk4096MinVram = 10ull << 30;
37constexpr uint64_t kChunkInPlace4096Below = 16ull << 30;
38// own_default: a single-card run (no --threads, tp 1); gpu_share: the share of the routed experts the cards can hold (1 = all);
39// gguf_copy: a GGUF pack with --mmap-experts off; file_experts: the experts stay in the files; hip: a HIP build; min_vram_bytes: the
40// smallest card; big: the chunk of the streamed case (the engine's kVlRows, 8192)
41inline int prefill_chunk_default(bool own_default, double gpu_share, bool gguf_copy, bool file_experts, bool hip, uint64_t min_vram_bytes,
42                                 int big = 8192) {
43    if (own_default && gpu_share >= 0.5) return 1024;
44    if (gguf_copy && !hip && min_vram_bytes >= kChunk4096MinVram) return 4096;
45    // the experts read in place on a card under 16 GiB (the 12 GB PCs; not measured on HIP): 4096 keeps ~120 more experts resident than 8192 (RTX 5070
46    // 287 against 165, RTX 3060 386 against 277) - 5 interleaved pairs, 4096 against 8192, decode: 5070 agent x1.020 (5/5) code x1.004 (3/5), 3060
47    // agent x1.018 (4/5) code x1.091 (5/5); an agent turn of 3K new tokens on a 16K context takes the same time (5070 18.9-19.2 s against 19.1-19.4 s,
48    // 3060 21.4 s against 20.9-21.4 s) - for 9% less speed on a cold 8K prompt (5070 223 against 244 tok/s)
49    if (file_experts && !hip && min_vram_bytes != 0 && min_vram_bytes < kChunkInPlace4096Below) return 4096;
50    return file_experts ? big : 2048;
51}
52
53// --mmap-experts auto copies all the CPU lane's experts into RAM at load (no demand store) only when they fit: lane_bytes of experts,
54// ram_avail the RAM this process can still get (~0: unknown -> no), ram_margin left to the rest, commit_room what Windows still lets it
55// commit beside its margin (1e300: no such limit, Linux).  All or none: a lane that does not fit both leaves the demand store as it was.
56// Measured 1 x R9700 + 251 GB, 2.00 pack: decode +10-25% (the copies sit on huge pages, the store's page-locked arena does not).
57inline bool copy_all_fits(double lane_bytes, uint64_t ram_avail, double ram_margin, double commit_room) {
58    if (!(lane_bytes > 0.0) || ram_avail == ~uint64_t{0}) return false;
59    return lane_bytes + ram_margin <= (double) ram_avail && lane_bytes <= commit_room;
60}
61
62// DS4_TOPK=N (a test, not for users): over the flag and the rule; returns the count to use
63inline int experts_test_override(int chosen, int model_k) {
64    if (const char* v = std::getenv("DS4_TOPK")) {
65        const int n = std::atoi(v);
66        if (n >= 1 && n <= model_k) return n;
67    }
68    return chosen;
69}
70
71}  // namespace strata::ds4