okami.mom/strata

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

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
8.2 KiB173 linesraw
1// src/core/layout.cpp - the shape checks.  See the header for why this exists.
2#include "strata/core/layout.hpp"
3
4#include <cstdio>
5
6namespace strata::core {
7namespace {
8
9/// A required 2-D shape.  `ne0` is the CONTIGUOUS axis, matching the manifest and `s_gemv`'s convention
10/// (`y[o] = sum_i x[i]*W[i][o]`, row o contiguous of length ne0).
11struct Want2 {
12    const char* suffix;
13    int64_t ne0;
14    int64_t ne1;
15    bool qsa_only;
16    bool gdn_only;
17};
18
19/// A required element count and ENGINE FORM for a 1-D tensor.
20///
21/// `kind` is not decoration.  The engine form of a 1-D tensor is whatever `WeightKind` the loader applied -
22/// `F32` copies 4 B/elem, `Bf16InF32` re-rounds to 2 - and a consumer that casts either one to `const float*`
23/// reads 2x its length.  **THAT IS NOT HYPOTHETICAL: `ffn_gate_inp_shexp.weight` is BF16, so the arena holds
24/// 5120 B for 2560 elements, and `shared_expert` read it as f32.  Every one of the 2560 MoE outputs came out
25/// non-finite** - a wrong answer loud enough to notice, which is the lucky version.  Reading 5120 B past the
26/// end of a tensor inside a 4.5 GiB arena does not fault, so the same mistake on a tensor followed by
27/// plausible bytes would have produced plausible logits.
28struct Want1 {
29    const char* suffix;
30    int64_t elements;
31    WeightKind kind;
32    bool qsa_only;
33    bool gdn_only;
34};
35
36bool fail(std::string& err, const LayerView& v, const char* suffix, const char* what, int64_t got,
37          int64_t want) {
38    char buf[512];
39    std::snprintf(buf, sizeof buf, "layer %lld: %s %s is %lld, the kernels require %lld",
40                  (long long) v.layer(), v.name(suffix).c_str(), what, (long long) got, (long long) want);
41    err = buf;
42    return false;
43}
44
45bool check_one(const WeightTable& t, const ModelGeometry& g, int64_t layer, std::string& err) {
46    const LayerView v(t, layer);
47    const bool qsa = is_qsa_layer(g, layer);
48
49    // ---- the 2-D tensor set, per layer family
50    const Want2 want2[] = {
51        // gated residual, EVERY layer - `gr_read` takes w_down (hc_lr, hc_dim) and w_up (hc_lr, hc_dim)
52        // after its own transpose, so the pack's orientation is [hc_dim, hc_lr] and [hc_lr, hc_dim].
53        {"hc_attn_down.weight", g.hc_dim(), g.hc_lr, false, false},
54        {"hc_attn_up.weight", g.hc_lr, g.hc_dim(), false, false},
55        {"hc_attn_inject.weight", g.hc_dim(), g.hc, false, false},
56        {"hc_ffn_down.weight", g.hc_dim(), g.hc_lr, false, false},
57        {"hc_ffn_up.weight", g.hc_lr, g.hc_dim(), false, false},
58        {"hc_ffn_inject.weight", g.hc_dim(), g.hc, false, false},
59        // MoE, EVERY layer
60        {"ffn_gate_inp.weight", g.n_embd, g.n_expert, false, false},
61        {"ffn_gate_shexp.weight", g.n_embd, g.n_ff, false, false},
62        {"ffn_up_shexp.weight", g.n_embd, g.n_ff, false, false},
63        {"ffn_down_shexp.weight", g.n_ff, g.n_embd, false, false},
64        // GDN only
65        {"attn_qkv.weight", g.n_embd, g.ssm_conv_channels, false, true},
66        {"attn_gate.weight", g.n_embd, g.ssm_value_dim, false, true},
67        {"ssm_out.weight", g.ssm_value_dim, g.n_embd, false, true},
68        {"ssm_conv1d.weight", g.ssm_d_conv, g.ssm_conv_channels, false, true},
69        {"ssm_alpha.weight", g.n_embd, g.ssm_v_heads, false, true},
70        {"ssm_beta.weight", g.n_embd, g.ssm_v_heads, false, true},
71        // QSA only
72        {"attn_q.weight", g.n_embd, 2 * g.n_head * g.head_dim, true, false},
73        {"attn_k.weight", g.n_embd, g.n_head_kv * g.head_dim, true, false},
74        {"attn_v.weight", g.n_embd, g.n_head_kv * g.head_dim, true, false},
75        {"attn_output.weight", g.n_head * g.head_dim, g.n_embd, true, false},
76        // THE INDEXER.  `q_proj` is the QUERY count and `k_proj` is the KEY width, and they are different
77        // numbers - conflating them is what made the planner's indexer term 4x too big in round 194.
78        {"indexer.q_proj.weight", g.n_embd, g.idx_q_heads * g.idx_key_dim, true, false},
79        {"indexer.k_proj.weight", g.n_embd, g.idx_key_dim, true, false},
80    };
81    for (const Want2& w : want2) {
82        if (w.qsa_only && !qsa) continue;
83        if (w.gdn_only && qsa) continue;
84        const WeightRef* r = v.get(w.suffix);
85        if (!r) {
86            err = "layer " + std::to_string(layer) + ": missing " + v.name(w.suffix);
87            return false;
88        }
89        if (r->ne0 != w.ne0) return fail(err, v, w.suffix, "ne0", r->ne0, w.ne0);
90        if (r->ne1 != w.ne1) return fail(err, v, w.suffix, "ne1", r->ne1, w.ne1);
91    }
92
93    // ---- the 1-D set.  EVERY ONE OF THESE IS `F32` EXCEPT the shared expert's scalar gate, which is BF16 -
94    // and that single exception is the one a reader would not guess, so it is written down rather than
95    // inferred from the tensor count.
96    const Want1 want1[] = {
97        {"hc_attn_norm.weight", g.hc_dim(), WeightKind::F32, false, false},
98        {"hc_ffn_norm.weight", g.hc_dim(), WeightKind::F32, false, false},
99        {"ffn_gate_inp_shexp.weight", g.n_embd, WeightKind::Bf16InF32, false, false},
100        {"ssm_a", g.ssm_v_heads, WeightKind::F32, false, true},
101        {"ssm_dt.bias", g.ssm_v_heads, WeightKind::F32, false, true},
102        {"ssm_norm.weight", g.ssm_state_size, WeightKind::F32, false, true},
103        {"attn_q_norm.weight", g.head_dim, WeightKind::F32, true, false},
104        {"attn_k_norm.weight", g.head_dim, WeightKind::F32, true, false},
105        {"indexer.q_norm.weight", g.idx_key_dim, WeightKind::F32, true, false},
106        {"indexer.k_norm.weight", g.idx_key_dim, WeightKind::F32, true, false},
107    };
108    for (const Want1& w : want1) {
109        if (w.qsa_only && !qsa) continue;
110        if (w.gdn_only && qsa) continue;
111        const WeightRef* r = v.get(w.suffix);
112        if (!r) {
113            err = "layer " + std::to_string(layer) + ": missing " + v.name(w.suffix);
114            return false;
115        }
116        if (r->elements != w.elements) return fail(err, v, w.suffix, "elements", r->elements, w.elements);
117        if (r->kind != w.kind) {
118            // The byte count is what makes this a REAL check and not a label: a 4 B/elem tensor holds
119            // `elements * 4`, a 2 B/elem one holds `elements * 2`, and a consumer reading it as the wrong one
120            // walks off the end.
121            const uint64_t want_bytes = (uint64_t) w.elements *
122                                        (w.kind == WeightKind::Bf16InF32 ? 2u : 4u);
123            char buf[512];
124            std::snprintf(buf, sizeof buf,
125                          "layer %lld: %s is engine form %d (%llu B), the kernels read it as form %d (%llu B)",
126                          (long long) layer, v.name(w.suffix).c_str(), (int) r->kind,
127                          (unsigned long long) r->bytes, (int) w.kind, (unsigned long long) want_bytes);
128            err = buf;
129            return false;
130        }
131    }
132    return true;
133}
134
135}  // namespace
136
137std::string LayerView::name(const char* suffix) const {
138    return "blk." + std::to_string(layer_) + "." + suffix;
139}
140
141bool check_layer(const WeightTable& table, const ModelGeometry& g, int64_t layer, std::string& err) {
142    if (layer < 0 || layer >= g.n_layers) {
143        err = "layer " + std::to_string(layer) + " is outside 0.." + std::to_string(g.n_layers - 1);
144        return false;
145    }
146    return check_one(table, g, layer, err);
147}
148
149bool check_all(const WeightTable& table, const ModelGeometry& g, std::string& err) {
150    int64_t n_qsa = 0, n_gdn = 0;
151    for (int64_t l = 0; l < g.n_layers; ++l) {
152        if (!check_one(table, g, l, err)) return false;
153        if (is_qsa_layer(g, l)) {
154            ++n_qsa;
155        } else {
156            ++n_gdn;
157        }
158    }
159    // The split is derived twice over in `docs/semantics.md` - the interval and an explicit ratio array -
160    // and both give 36 GDN and 12 QSA.  Counting them here is the check that the LAYER TYPE PREDICATE and
161    // the pack agree, which no per-tensor shape check can see.
162    if (n_qsa != g.n_qsa_layers() || n_gdn != g.n_gdn_layers()) {
163        char buf[256];
164        std::snprintf(buf, sizeof buf, "layer split is %lld QSA / %lld GDN, the geometry says %lld / %lld",
165                      (long long) n_qsa, (long long) n_gdn, (long long) g.n_qsa_layers(),
166                      (long long) g.n_gdn_layers());
167        err = buf;
168        return false;
169    }
170    return true;
171}
172
173}  // namespace strata::core