okami.mom/strata

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

Niko1221Merge f-q4-metal (ed1f58d27): the Mac (Metal) engine follows the compiled geometry (q4_lite/q4_mid/q4_big kernel parity 40/40, gate 2 like CUDA), plus the PLE row-buffer fix in src/program/generate.cpp (sized by the compiled hidden size, not 2560; it overran 24 KB at hidden 4096 and crashed Metal serve); default build unchanged (CUDA SASS 3573/3573 and 160/160 host objects, HIP gfx1201/gfx1151 string offsets only, SYCL 129/129 objects and 22/22 checksums, Metal per-token functions identical, UD-Q4_K_XL 10/10 tokens, CUDA IQ3_S 26/26 tokens) (Qwen4 prep)7f46672a4

main
4.8 KiB75 linesraw
1// metal/src/engine/geo.h - the shape the Metal engine is compiled for, from strata::core::kCompiledGeometry (cmake -DSTRATA_GEOMETRY /
2// -DSTRATA_GEOMETRY_FILE, include/strata/core/geometry.hpp), and the #define prelude that hands it to the kernels.
3//
4// Host side: window.cpp, prompt_chain.inc and stream.cpp take their sizes from here instead of from literals (hidden 2560, 640 wide
5// experts, 48 layers, 24 query heads, ...).  For Flash-Next's header every value below is the number it replaced.
6//
7// Kernel side: the MSL sources read their shape from GEO_* macros with Flash-Next's value as the default (`#ifndef GEO_QH`); the
8// prelude defines a macro only when this build's value differs from Flash-Next's, so a Flash-Next build hands the compiler exactly
9// the prelude it always did.  Everything else in the kernels is run-time (the arguments carry n_embd, n_ff, hc_lr, the head counts of
10// the GDN) or a held-fixed constant of the family (state 128, head width 256, 4 hyper-connection streams, indexer 4 x 128).
11#pragma once
12
13#include "strata/core/geometry.hpp"
14
15#include <string>
16
17namespace strata_metal::geo {
18
19inline constexpr const strata::core::ModelGeometry& G = strata::core::kCompiledGeometry;
20
21inline constexpr int N = (int) G.n_embd;             // hidden
22inline constexpr int HC = (int) G.hc;                // hyper-connection streams (4)
23inline constexpr int LR = (int) G.hc_lr;             // hyper-connection low rank
24inline constexpr int NFF = (int) G.n_ff;             // routed (and shared) expert width
25inline constexpr int TOPK = (int) G.n_expert_used;   // experts per token
26inline constexpr int NEXP = (int) G.n_expert;        // routed experts per layer
27inline constexpr int NL = (int) G.n_layers;          // trunk layers
28inline constexpr int QI = (int) G.qsa_interval;      // every QI-th layer is full attention (QI - 1, 2 QI - 1, ...)
29inline constexpr int NQSA = NL / QI;                 // full-attention layers (12)
30inline constexpr int NGDN = NL - NQSA;               // GDN layers (36)
31
32// GDN
33inline constexpr int SS = (int) G.ssm_state_size;    // state width (128)
34inline constexpr int HK = (int) G.ssm_k_heads;       // key heads (16)
35inline constexpr int HV = (int) G.ssm_v_heads;       // value heads (48)
36inline constexpr int CONV = (int) G.ssm_conv_channels;   // 2 * SS * HK + SS * HV = q|k|v channels (10240)
37inline constexpr int ZV = (int) G.ssm_value_dim;     // SS * HV (6144): z, the readout y
38
39// full attention
40inline constexpr int QH = (int) G.n_head;            // query heads (24)
41inline constexpr int QKV = (int) G.n_head_kv;        // kv heads (2)
42inline constexpr int QD = (int) G.head_dim;          // head width (256)
43inline constexpr int IH = (int) G.idx_q_heads;       // indexer heads (4)
44inline constexpr int ID = (int) G.idx_key_dim;       // indexer key width (128)
45inline constexpr int BUD = (int) G.idx_top_k + 3;     // selection budget in cells (2051): top_k + the tail block's 3
46inline constexpr int QCH = 64;                       // cells per attention chunk (qsa.metal QCH)
47inline constexpr int QCHUNKS = (BUD + QCH - 1) / QCH;   // 33
48inline constexpr int QCHUNKS2 = (BUD + 31) / 32;         // 65: the same cells in chunks of 32 (qsa2.metal), the partials buffer's chunk count
49inline constexpr int QPREP = QH + 2 * QKV + IH + 1;  // qsa_prep threadgroups: q heads, k, v, indexer q, and the indexer-key group
50
51// derived sizes (floats unless said)
52inline constexpr int HCD = HC * N;                   // one residual row (hc_dim) = the PLE key width (10240)
53inline constexpr int QFULL = QH * QD * 2;            // attn_q: query and gate (12288)
54inline constexpr int QOUT = QH * QD;                 // the attention output row = attn_output's K (6144)
55inline constexpr int KV = QKV * QD;                  // attn_k / attn_v rows (512)
56inline constexpr int IQR = IH * ID;                  // indexer q_proj rows (512)
57inline constexpr int GDN_FUSED = CONV + ZV + 2 * HV; // the four GDN input projections as one row (16480)
58
59// the kernels' defaults are Flash-Next's: a macro is defined only for what this build changes
60inline std::string prelude() {
61    const strata::core::ModelGeometry d;
62    std::string s;
63    auto def = [&](const char* name, int64_t v, int64_t dv) {
64        if (v != dv) s += std::string("#define ") + name + " " + std::to_string((long long) v) + "\n";
65    };
66    def("GEO_QH", G.n_head, d.n_head);
67    def("GEO_QKV", G.n_head_kv, d.n_head_kv);
68    def("GEO_BUD", G.idx_top_k + 3, d.idx_top_k + 3);
69    def("GEO_TOPK", G.n_expert_used, d.n_expert_used);
70    def("GEO_NJ", G.n_embd / 256, d.n_embd / 256);                      // gr_norm2: elements per thread of one stream's row
71    def("GEO_GRU_NU", (G.hc_lr / 32 + 3) / 4, (d.hc_lr / 32 + 3) / 4);   // gr_up prefetch: units of 32 per lane (4 lanes a row)
72    return s;
73}
74
75}  // namespace strata_metal::geo