okami.mom/strata

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

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
7.8 KiB160 linesraw
1// metal/src/engine/setup_host.cpp - the host-side state and sizes of the CUDA kernel files that the shared host code
2// sets and reads while it starts the engine (path switches, scratch sizes, the RoPE table and its config).  Copied
3// from gr.cu, shared_expert.cu, native_*.cu, ple.cu, qsa_decode_attn.cu, rope.cu, native_rope.cu,
4// s2_expert_grouped.cu; keep in step.  On Metal none of these steers a kernel: the decode window runs mac-kern's
5// chain (metal/src/engine/window.cpp); they keep the host side's allocations and checks as on the PC.
6#include "strata/kernels/gr.hpp"
7#include "strata/kernels/native_gdn.hpp"
8#include "strata/kernels/native_moe.hpp"
9#include "strata/kernels/native_qsa.hpp"
10#include "strata/kernels/native_qsa_indexer.hpp"
11#include "strata/kernels/native_rope.hpp"
12#include "strata/kernels/native_router.hpp"
13#include "strata/kernels/ngram.hpp"
14#include "strata/kernels/ple.hpp"
15#include "strata/kernels/qsa_decode_attn.hpp"
16#include "strata/kernels/rope.hpp"
17#include "strata/kernels/rope_scaling.hpp"
18#include "strata/kernels/s2_expert_grouped.hpp"
19#include "strata/kernels/shared_expert.hpp"
20#include "strata/kernels/fused_gr.hpp"
21#include "strata/prefill/gemm.hpp"
22
23#include <atomic>
24#include <cmath>
25#include <cstdint>
26#include <cstdio>
27
28namespace strata::kernels {
29
30namespace {
31bool g_fp32_activations = false, g_native_mmvf = false, g_sh_native_bf16 = false, g_ple_native_bf16 = false,
32     g_ple_native_postops = false;
33std::atomic<bool> g_moe_combine{false}, g_gdn{false}, g_router{false}, g_qsa{false}, g_qsa_indexer{false}, g_rope{false};
34RopeScaling g_rope_scaling;
35}  // namespace
36
37// ---- path switches (each .cu file's own flag)
38void gr_set_fp32_activations(bool enabled) { g_fp32_activations = enabled; }
39void gr_set_native_mmvf(bool enabled) { g_native_mmvf = enabled; }
40bool gr_native_mmvf_enabled() { return g_native_mmvf; }
41void shared_expert_set_native_bf16(bool enabled) { g_sh_native_bf16 = enabled; }
42bool shared_expert_native_bf16_enabled() { return g_sh_native_bf16; }
43void ple_set_native_bf16(bool enabled) { g_ple_native_bf16 = enabled; }
44bool ple_native_bf16_enabled() { return g_ple_native_bf16; }
45void ple_set_native_postops(bool enabled) { g_ple_native_postops = enabled; }
46bool ple_native_postops_enabled() { return g_ple_native_postops; }
47void native_moe_combine_set_enabled(bool v) { g_moe_combine.store(v, std::memory_order_relaxed); }
48bool native_moe_combine_enabled() { return g_moe_combine.load(std::memory_order_relaxed); }
49void native_gdn_set_enabled(bool v) { g_gdn.store(v, std::memory_order_relaxed); }
50bool native_gdn_enabled() { return g_gdn.load(std::memory_order_relaxed); }
51void native_router_set_enabled(bool v) { g_router.store(v, std::memory_order_relaxed); }
52bool native_router_enabled() { return g_router.load(std::memory_order_relaxed); }
53void native_qsa_set_enabled(bool v) { g_qsa.store(v, std::memory_order_relaxed); }
54bool native_qsa_enabled() { return g_qsa.load(std::memory_order_relaxed); }
55void native_qsa_indexer_set_enabled(bool v) { g_qsa_indexer.store(v, std::memory_order_relaxed); }
56bool native_qsa_indexer_enabled() { return g_qsa_indexer.load(std::memory_order_relaxed); }
57void native_rope_set_enabled(bool v) { g_rope.store(v, std::memory_order_relaxed); }
58bool native_rope_enabled() { return g_rope.load(std::memory_order_relaxed); }
59void fused_gr_check() {}   // the CUDA read variants' self-test: no variants on Metal
60
61// ---- scratch sizes
62uint64_t ple_block_scratch_bytes() {
63    const size_t f = (size_t) (5 * NG_HC_DIM + NG_N_EMBD + NG_HC) * sizeof(float);
64    const size_t q = (size_t) (NG_N_EMBD / 32) * 34;
65    const size_t e = (size_t) NG_N_EMBD * sizeof(uint16_t);
66    return ((f + 15) & ~(size_t) 15) + ((q + 15) & ~(size_t) 15) + e + 256;
67}
68uint64_t qsa_decode_attn_scratch_floats(int64_t cap, const QsaShapes& s) {
69    constexpr int HD = 256, CHUNK = 64;
70    const int64_t chunks = (cap + CHUNK - 1) / CHUNK;
71    return (uint64_t) chunks * (uint64_t) s.n_head * (HD + 2) + 64;
72}
73uint64_t shared_expert_scratch_bytes(int64_t n_ff) {
74    const uint64_t a = ((uint64_t) n_ff * 4 + 15) & ~15ull;
75    const uint64_t q0 = ((uint64_t) (n_ff / 32) * 34 + 15) & ~15ull;
76    const uint64_t qk = ((uint64_t) (n_ff / 256) * 292 + 15) & ~15ull;
77    return a * 2 + q0 + qk + 32;
78}
79uint64_t moe_hit_grouped_scratch_bytes(int64_t n_hits, int64_t n_embd, int64_t n_ff) {
80    if (n_hits <= 0) return 0;
81    const uint64_t gu = (uint64_t) n_hits * (uint64_t) (2 * n_ff) * 4;
82    const uint64_t q8 = (uint64_t) n_hits * (uint64_t) (n_ff / 32) * 34;
83    const uint64_t hs = (uint64_t) n_hits * (uint64_t) (n_ff / 32) * 4;
84    const uint64_t xh = (uint64_t) (n_embd / 32) * 4;
85    return ((gu + 15) & ~15ull) + ((q8 + 15) & ~15ull) + 2 * ((hs + 15) & ~15ull) + ((xh + 15) & ~15ull);
86}
87size_t gr_workspace_init(const GrShapes& s, void* base, GrWorkspace& out) {
88    const size_t hc_dim = (size_t) s.hc * (size_t) s.n_embd;
89    const size_t sz[5] = {hc_dim * sizeof(float), hc_dim * sizeof(uint16_t), (size_t) s.hc_lr * sizeof(uint16_t),
90                          hc_dim * sizeof(float), (size_t) s.hc_lr * sizeof(float)};
91    size_t al[5], bytes = 0;
92    for (int k = 0; k < 5; ++k) {
93        al[k] = (sz[k] + 15) & ~(size_t) 15;
94        bytes += al[k];
95    }
96    out.bytes = bytes;
97    if (base != nullptr) {
98        unsigned char* p = (unsigned char*) base;
99        void* ptr[5];
100        for (int k = 0; k < 5; ++k) {
101            ptr[k] = p;
102            p += al[k];
103        }
104        out.xn = (float*) ptr[0];
105        out.xq = (uint16_t*) ptr[1];
106        out.lq = (uint16_t*) ptr[2];
107        out.gated = (float*) ptr[3];
108        out.lo = (float*) ptr[4];
109    }
110    return bytes;
111}
112
113// ---- RoPE (rope.cu, native_rope.cu): the config and the host-built angle table
114void rope_scaling_set(const RopeScaling& scaling) { g_rope_scaling = scaling; }
115const RopeScaling& rope_scaling() { return g_rope_scaling; }
116void build_rope_table(int n_rot, double theta, int max_pos, float* cos_tab, float* sin_tab) {
117    const int half = n_rot / 2;
118    for (int p = 0; p < max_pos; ++p)
119        for (int i = 0; i < half; ++i) {
120            const double inv = std::pow(theta, -2.0 * (double) i / (double) n_rot);
121            const double ang = (double) p * inv;
122            cos_tab[(size_t) p * half + i] = (float) std::cos(ang);
123            sin_tab[(size_t) p * half + i] = (float) std::sin(ang);
124        }
125}
126void build_rope_table(int n_rot, const RopeScaling& sc, int max_pos, float* cos_tab, float* sin_tab) {
127    if (sc.type == RopeScalingType::None) {
128        build_rope_table(n_rot, sc.freq_base, max_pos, cos_tab, sin_tab);
129        return;
130    }
131    const int half = n_rot / 2;
132    const double fs = sc.freq_scale();
133    const double ms = sc.mscale();
134    double cd[2];
135    sc.corr_dims(n_rot, cd);
136    const bool correct = sc.ext_factor != 0;
137    for (int p = 0; p < max_pos; ++p)
138        for (int i = 0; i < half; ++i) {
139            const double inv = std::pow(sc.freq_base, -2.0 * (double) i / (double) n_rot);
140            const double extrap = (double) p * inv;
141            const double interp = fs * extrap;
142            double ang = interp;
143            if (correct) {
144                const double ramp = (double) rope_yarn_ramp((float) cd[0], (float) cd[1], i) * sc.ext_factor;
145                ang = interp * (1.0 - ramp) + extrap * ramp;
146            }
147            cos_tab[(size_t) p * half + i] = (float) (std::cos(ang) * ms);
148            sin_tab[(size_t) p * half + i] = (float) (std::sin(ang) * ms);
149        }
150}
151void rope_table_set(const float*, const float*, int, const RopeScaling&) {}   // the window keeps its own table
152void rope_table_release(const float*) {}
153void native_q6_k_unpack(const void*) {}   // nothing packed on Metal
154
155}  // namespace strata::kernels
156
157namespace strata::prefill {
158Gemm::~Gemm() {}   // nothing created on Metal (no prompt GEMMs yet)
159bool prompt_f16() { return false; }   // the CUDA/HIP prompt path's FP16 image mode: not used on Metal
160}  // namespace strata::prefill