okami.mom/strata

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

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

main
13.1 KiB273 linesraw
1// Strata. `strata-dsv41 --pack-info`: the pack as a description, before anything is loaded onto a GPU - the type map
2// (layer x role), the bytes an expert and a token, the tier sizes, and the tensors whose type has no device kernel (they
3// would go to the CPU lane, or stop the load).  Host only, no device, no weights read: tensors.txt and ds4.txt are the
4// pack's whole description (tools/dsv41/pack_gguf.py).  The same report is the first line of every benchmark log.
5#include "ds4_packinfo.hpp"
6#include "ds4_iq.cuh"      // iq::supported / iq::name: the device-type list below is derived from the kernels, not copied
7
8#include <algorithm>
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <fstream>
13#include <map>
14#include <set>
15#include <string>
16#include <vector>
17
18#if defined(_MSC_VER)
19#include <cstring>
20#define strtok_r strtok_s
21#endif
22namespace strata::ds4 {
23namespace {
24
25// The ggml block types the device kernels take (ds4_iq.cu's Ty<> / DS4_IQ_SWITCH + ds4_gguf.cu's MXFP4).  A name here
26// and not in the pack's tensor list is fine; a tensor whose type is not here is the CPU lane's (ggml-cpu's row dots).
27// It was a copy of the supported() switch and fell behind when IQ1_S, IQ1_M and IQ4_NL got kernels (2026-10-23), so it is
28// now the switch itself, by type name.
29
30bool device_type(const std::string& t) {
31    static const std::set<std::string> ok = [] {
32        std::set<std::string> s = {"MXFP4", "TQK6", "TQK7", "TQK5X", "TQK4X", "TQK5T", "TQK4T"};   // ds4_gguf.cu / trellis
33        for (int ty = 0; ty < 64; ++ty) {
34            const char* n = gguf::iq::name(ty);
35            if (n && std::strcmp(n, "?") != 0 && gguf::iq::supported(ty)) s.insert(n);
36        }
37        return s;
38    }();
39    return ok.count(t) != 0;
40}
41// The types the dense path reads as they are (ds4_gguf.cu: BF16 / F32 / MXFP8 rows, Q8_0 rows; ds4_iq's GEMV for the
42// K-quants is the same device support as the experts').
43bool dense_type(const std::string& t) {
44    return t == "BF16" || t == "F32" || t == "F16" || t == "MXFP8" || t == "Q8_0" || device_type(t);
45}
46
47struct Tensor {
48    std::string dtype;
49    double bytes = 0;
50    std::vector<long long> shape;
51};
52
53double gb(double b) { return b / 1e9; }
54
55}  // namespace
56
57int pack_info(const std::string& dir, std::FILE* out) {
58    std::map<std::string, std::string> kv;
59    {
60        std::ifstream f(dir + "/ds4.txt");
61        if (!f) {
62            std::fprintf(out, "pack-info: %s/ds4.txt: not found\n", dir.c_str());
63            return 1;
64        }
65        std::string line;
66        while (std::getline(f, line)) {
67            const size_t sp = line.find(' ');
68            if (sp != std::string::npos) kv[line.substr(0, sp)] = line.substr(sp + 1);
69        }
70    }
71    auto num = [&](const char* k, long long d) {
72        auto it = kv.find(k);
73        return it == kv.end() ? d : std::atoll(it->second.c_str());
74    };
75    const long long nl = num("num_hidden_layers", 0), ne = num("n_routed_experts", 0), topk = num("num_experts_per_tok", 0);
76    const long long dim = num("hidden_size", 0), ff = num("moe_intermediate_size", 0), vocab = num("vocab_size", 0);
77    std::map<std::string, Tensor> t;
78    std::vector<std::string> files;
79    std::map<int, double> need;   // bytes the pack needs from each file (its largest tensor end)
80    {
81        std::FILE* f = std::fopen((dir + "/tensors.txt").c_str(), "r");
82        if (f == nullptr) {
83            std::fprintf(out, "pack-info: %s/tensors.txt: not found\n", dir.c_str());
84            return 1;
85        }
86        std::vector<char> buf(1 << 16);
87        while (std::fgets(buf.data(), (int) buf.size(), f) != nullptr) {
88            char* s = buf.data();
89            if (s[0] == 'f') {   // file <i> <name>
90                char name[4096];
91                int i = 0;
92                if (std::sscanf(s, "file %d %4095s", &i, name) == 2) {
93                    if ((int) files.size() <= i) files.resize((size_t) i + 1);
94                    files[(size_t) i] = name;
95                }
96                continue;
97            }
98            if (s[0] != 't') continue;
99            char* save = nullptr;
100            ::strtok_r(s, " \n", &save);
101            const char* name = ::strtok_r(nullptr, " \n", &save);
102            const char* dt = ::strtok_r(nullptr, " \n", &save);
103            const char* fi = ::strtok_r(nullptr, " \n", &save);
104            const char* off = ::strtok_r(nullptr, " \n", &save);
105            const char* nb = ::strtok_r(nullptr, " \n", &save);
106            if (name == nullptr || dt == nullptr || fi == nullptr || off == nullptr || nb == nullptr) continue;
107            Tensor x;
108            x.dtype = dt;
109            x.bytes = (double) std::strtoull(nb, nullptr, 10);
110            const int idx = std::atoi(fi);
111            need[idx] = std::max(need[idx], (double) std::strtoull(off, nullptr, 10) + x.bytes);
112            while (const char* d = ::strtok_r(nullptr, " \n", &save)) x.shape.push_back(std::atoll(d));
113            t[name] = std::move(x);
114        }
115        std::fclose(f);
116    }
117    if (t.empty()) {
118        std::fprintf(out, "pack-info: %s/tensors.txt: no tensors\n", dir.c_str());
119        return 1;
120    }
121    // the files the pack points at: they must be there, and at least as long as the largest tensor end says (the weights
122    // live elsewhere; a pack whose model_dir was cleaned up still describes itself perfectly - this is the check)
123    const std::string md = kv.count("model_dir") ? kv["model_dir"] : "";
124    int missing = 0;
125    for (const auto& [i, n] : need) {
126        const std::string name = (size_t) i < files.size() ? files[(size_t) i] : "";
127        if (name.empty()) {
128            std::fprintf(out, "  file %d: no name in tensors.txt\n", i);
129            ++missing;
130            continue;
131        }
132        const std::string path = md + "/" + name;
133        std::FILE* f = std::fopen(path.c_str(), "rb");
134        if (f == nullptr) {
135            std::fprintf(out, "  MISSING %s (%.2f GB wanted)\n", path.c_str(), n / 1e9);
136            ++missing;
137            continue;
138        }
139#if defined(_WIN32)
140        _fseeki64(f, 0, SEEK_END);   // (long is 32 bits there: the weights are far past 2 GB)
141        const long long sz = _ftelli64(f);
142#else
143        std::fseek(f, 0, SEEK_END);
144        const long long sz = std::ftell(f);
145#endif
146        std::fclose(f);
147        if ((double) sz + 0.5 < n) {
148            std::fprintf(out, "  SHORT %s: %.2f GB there, %.2f GB wanted\n", path.c_str(), (double) sz / 1e9, n / 1e9);
149            ++missing;
150        }
151    }
152
153    // ---- the expert map: layer -> role types and bytes an expert
154    struct Role {
155        std::string type[3];
156        double bytes[3] = {0, 0, 0};
157        bool mixed = false;
158        int n = 0;
159    };
160    std::vector<Role> lay((size_t) nl);
161    std::map<std::string, long long> type_count;   // tensor count a type (the experts' 3 roles)
162    double experts_bytes = 0;
163    std::set<std::string> no_kernel;
164    for (long long il = 0; il < nl; ++il) {
165        Role& r = lay[(size_t) il];
166        const std::string pre = "layers." + std::to_string(il) + ".ffn.experts.";
167        for (long long e = 0; e < ne; ++e) {
168            const char* role[3] = {"w1", "w3", "w2"};
169            for (int k = 0; k < 3; ++k) {
170                auto it = t.find(pre + std::to_string(e) + "." + role[k] + ".weight");
171                if (it == t.end()) {
172                    std::fprintf(out, "pack-info: %s%s%lld.%s.weight: missing\n", pre.c_str(), "", e, role[k]);
173                    return 1;
174                }
175                if (e == 0) {
176                    r.type[k] = it->second.dtype;
177                    r.bytes[k] = it->second.bytes;
178                } else if (it->second.dtype != r.type[k] || it->second.bytes != r.bytes[k]) {
179                    r.mixed = true;
180                }
181                if (e == 0) {
182                    ++type_count[it->second.dtype];
183                    if (!device_type(it->second.dtype)) no_kernel.insert("layer " + std::to_string(il) + " " + role[k] + " " + it->second.dtype);
184                }
185            }
186        }
187        r.n = (int) ne;
188        experts_bytes += (double) ne * (r.bytes[0] + r.bytes[1] + r.bytes[2]);
189    }
190    const double per_expert = nl > 0 ? experts_bytes / ((double) nl * (double) ne) : 0;
191    // (a token picks `topk` experts a layer: sum over the layers of topk x one expert of that layer)
192    double token_bytes = 0;
193    for (long long il = 0; il < nl; ++il) token_bytes += (double) topk * (lay[(size_t) il].bytes[0] + lay[(size_t) il].bytes[1] + lay[(size_t) il].bytes[2]);
194    const double expert_params = (double) nl * (double) ne * 3.0 * (double) ff * (double) dim;
195
196    // ---- the dense side: everything that is not an expert tensor
197    double dense = 0, dense_embed = 0, dense_head = 0, dense_mtp = 0, dense_vision = 0, dense_engram = 0;
198    std::set<std::string> dense_types;
199    for (const auto& [name, x] : t) {
200        if (name.find(".ffn.experts.") != std::string::npos && name.rfind("layers.", 0) == 0) continue;
201        dense += x.bytes;
202        dense_types.insert(x.dtype);
203        if (name == "embed.weight") dense_embed += x.bytes;
204        else if (name == "head.weight") dense_head += x.bytes;
205        else if (name.rfind("mtp.", 0) == 0) dense_mtp += x.bytes;
206        else if (name.find(".engram.") != std::string::npos) dense_engram += x.bytes;
207        else if (name.rfind("vision.", 0) == 0 || name.rfind("aligner.", 0) == 0) dense_vision += x.bytes;
208        if (!dense_type(x.dtype)) no_kernel.insert("dense " + name + " " + x.dtype);
209    }
210
211    // ---- report
212    std::fprintf(out, "pack-info %s\n", dir.c_str());
213    std::fprintf(out, "  model_dir %s   format %s   %lld layers x %lld experts, %lld used; dim %lld, ff %lld, vocab %lld\n",
214                 kv.count("model_dir") ? kv["model_dir"].c_str() : "?", kv.count("format") ? kv["format"].c_str() : "?", nl, ne, topk,
215                 dim, ff, vocab);
216    std::fprintf(out, "  layer  gate+up   down      bytes/expert   MB/token\n");
217    for (long long il = 0; il < nl; ++il) {
218        const Role& r = lay[(size_t) il];
219        const double eb = r.bytes[0] + r.bytes[1] + r.bytes[2];
220        std::fprintf(out, "  %5lld  %-9s %-9s %13.0f %10.2f%s\n", il, r.type[0].c_str(), r.type[2].c_str(), eb,
221                     (double) topk * eb / 1e6, r.mixed ? "  (mixed)" : "");
222    }
223    std::fprintf(out, "  expert tensors by type:");
224    for (const auto& [ty, n] : type_count) std::fprintf(out, " %s %lld", ty.c_str(), n);
225    std::fprintf(out, "\n");
226    std::fprintf(out, "  an expert %.2f MB, %lld picks a token: %.3f GB a token\n", per_expert / 1e6, topk, token_bytes / 1e9);
227    std::fprintf(out, "  all experts %.1f GB at %.2f bpw (%.0f G parameters), dense %.2f GB (embed %.2f, head %.2f, mtp %.2f, vision %.2f, engram %.2f)\n",
228                 gb(experts_bytes), expert_params > 0 ? experts_bytes * 8.0 / expert_params : 0.0, expert_params / 1e9, gb(dense),
229                 gb(dense_embed), gb(dense_head), gb(dense_mtp), gb(dense_vision), gb(dense_engram));
230    std::fprintf(out, "  dense types:");
231    for (const auto& d : dense_types) std::fprintf(out, " %s", d.c_str());
232    std::fprintf(out, "\n");
233    // the geometry the engine needs at run time but that is easy to lose in a pack built by another tool: DSpark's block
234    // size / noise token / markov rank (0 there means it drafts one token a run, i.e. it is off) and the Engram's layers
235    {
236        auto kvn = [&](const char* k) {
237            auto it = kv.find(k);
238            return it == kv.end() ? 0 : std::atoll(it->second.c_str());
239        };
240        const int n_mtp = (int) std::count_if(t.begin(), t.end(), [](const auto& p) { return p.first.rfind("mtp.", 0) == 0; });
241        const long long nl_eg = num("engram_layer_ids", -1);
242        std::fprintf(out, "  dspark: block_size %lld, noise_token_id %lld, markov_rank %lld, routed experts %lld of %lld%s\n",
243                     num("dspark_block_size", 0), num("dspark_noise_token_id", 0), num("dspark_markov_rank", 0),
244                     num("dspark_n_routed_experts", 0), num("dspark_num_experts_per_tok", 0),
245                     num("dspark_block_size", 0) == 0 ? "  <- DSpark cannot draft (block size 0)" : "");
246        std::fprintf(out, "  mtp tensors: %d%s\n", n_mtp, n_mtp == 0 ? "  <- no DSpark drafter in the pack" : "");
247        auto it = kv.find("engram_layer_ids");
248        std::fprintf(out, "  engram layers: %s%s\n", it == kv.end() ? "(none)" : it->second.c_str(),
249                     it == kv.end() ? "  <- the Engram tables are not used" : "");
250    }
251    if (no_kernel.empty()) {
252        std::fprintf(out, "  no GPU kernel: none (every tensor is a device type)\n");
253    } else {
254        std::fprintf(out, "  no GPU kernel (%zu): the CPU lane takes them, they cannot go in the VRAM tier\n", no_kernel.size());
255        int shown = 0;
256        for (const std::string& s : no_kernel) {
257            std::fprintf(out, "    %s\n", s.c_str());
258            if (++shown >= 40) {
259                std::fprintf(out, "    ... %zu more\n", no_kernel.size() - (size_t) shown);
260                break;
261            }
262        }
263    }
264    if (missing > 0) {
265        std::fprintf(out, "  %d of %zu files missing or short: this pack cannot load (build it again, or point model_dir at "
266                          "the weights)\n", missing, need.size());
267        return 2;
268    }
269    std::fprintf(out, "  files: %zu, all present and long enough\n", need.size());
270    return 0;
271}
272
273}  // namespace strata::ds4