okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
8.8 KiB160 linesraw
1// metal/tests/fuse_parity.cpp - the decode latency fusions against the kernels they replace, BITWISE, for 1..4
2// tokens:
3//   gemv_cat (gemv.metal)   ssm_alpha + ssm_beta (BF16 2560 -> 48 + 48) in one dispatch  ==  two gemv dispatches
4//   router_route            the router GEMV + moe_route in one dispatch  ==  gemv + moe_route (logits, ids, w, plan;
5//                           twice in a row: the counter resets)
6//   gr_norm_comb (gr2.metal) the MoE combine folded into the next gr_norm  ==  moe_combine_sh + gr_norm (R', xn)
7#include "mtl_rt.h"
8#include "ref_quant.h"
9
10#include <cstdio>
11#include <cstring>
12#include <string>
13#include <vector>
14
15namespace {
16struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };
17struct MoeArgs {
18    uint64_t gate_off, up_off, down_off, gu_stride, d_stride;
19    uint32_t gu_row, d_row, n_embd, n_ff, n_tok, topk, n_expert, gx_gu, gx_dn, pad;
20};
21constexpr int P_INTS = 16 + 128 + 128 + 4 + 128 * 3, P_TOKENT = 16 + 128 + 128 + 4 + 128 * 2;
22struct GrArgs { uint32_t n_embd, hc, hc_lr, n_tok, apply; float eps; };
23struct GlueArgs { uint32_t n, n_tok, n_embd, topk; };
24}  // namespace
25
26int main() {
27    mtl::Context cx;
28    const int lib = cx.compile({"quant.metal", "units.metal", "moe.metal", "gemv.metal", "router.metal", "gr.metal",
29                                "gr2.metal", "glue.metal"});
30    std::printf("device %s\n", cx.device_name().c_str());
31    int fails = 0;
32    const int N = 2560, NO = 48, MAXT = 8;
33    const size_t rb = ref::row_bytes(ref::BF16, N);
34    mtl::Buffer Wa = cx.alloc(rb * NO), Wb = cx.alloc(rb * NO), xp = cx.alloc(4 * ((size_t)4 * MAXT * N + MAXT * N / 16)),
35                y1 = cx.alloc(4 * MAXT * NO), y2 = cx.alloc(4 * MAXT * NO), z1 = cx.alloc(4 * MAXT * NO), z2 = cx.alloc(4 * MAXT * NO);
36    ref::random_blocks(ref::BF16, Wa.as<uint8_t>(), NO, N, 1, 0.05f);
37    ref::random_blocks(ref::BF16, Wb.as<uint8_t>(), NO, N, 2, 0.05f);
38    ref::Rng r(3);
39    for (size_t i = 0; i < xp.bytes / 4; ++i) xp.as<float>()[i] = r.sym();
40    for (int nt = 1; nt <= 4; ++nt) {
41        mtl::Pipeline pg = cx.pipeline(lib, "gemv_30_32_1_4_1", mtl::Constants().i(0, nt)),
42                      pc = cx.pipeline(lib, "gemvc_30_32_1_4", mtl::Constants().i(0, nt));
43        const size_t s16 = (size_t)4 * 4 * MAXT * N;
44        GemvArgs a{(uint32_t)N, (uint32_t)NO, (uint32_t)nt, (uint32_t)rb, (uint32_t)N, (uint32_t)NO};
45        auto g = [&](const mtl::Buffer& W, const mtl::Buffer& y) {
46            mtl::Dispatch d;
47            d.p = &pg;
48            d.args = {mtl::Arg::B(W), mtl::Arg::B(xp), mtl::Arg::B(xp, s16), mtl::Arg::B(y), mtl::Arg::V(a)};
49            d.grid[0] = NO / 4;
50            d.tg[0] = 128;
51            return d;
52        };
53        cx.run({g(Wa, y1), g(Wb, y2)});
54        GemvArgs ac = a;
55        ac.n_out = 2 * NO;
56        const uint32_t sp = NO;
57        mtl::Dispatch d;
58        d.p = &pc;
59        d.args = {mtl::Arg::B(Wa), mtl::Arg::B(xp), mtl::Arg::B(xp, s16), mtl::Arg::B(z1), mtl::Arg::V(ac), mtl::Arg::B(Wb),
60                  mtl::Arg::B(z2), mtl::Arg::V(sp)};
61        d.grid[0] = 2 * NO / 4;
62        d.tg[0] = 128;
63        cx.run({d});
64        const bool ok = std::memcmp(y1.ptr, z1.ptr, 4 * nt * NO) == 0 && std::memcmp(y2.ptr, z2.ptr, 4 * nt * NO) == 0;
65        fails += !ok;
66        std::printf("gemv_cat (alpha + beta) ntok %d: bitwise %s\n", nt, ok ? "PASS" : "FAIL");
67    }
68    // ---- router_route
69    {
70        for (int rty : {ref::BF16, ref::F32})
71        for (const int NE : {512, 256}) {   // (256: the expert-pruned Coder; the logits rows keep their 512-float stride)
72        const size_t rbr = ref::row_bytes(rty, N);
73        mtl::Buffer Wr = cx.alloc(rbr * NE), lg1 = cx.alloc(4 * MAXT * 512), lg2 = cx.alloc(4 * MAXT * 512),
74                    id1 = cx.alloc(4 * MAXT * 10), id2 = cx.alloc(4 * MAXT * 10), w1 = cx.alloc(4 * MAXT * 10),
75                    w2 = cx.alloc(4 * MAXT * 10), pl1 = cx.alloc(4 * P_INTS), pl2 = cx.alloc(4 * P_INTS), cnt = cx.alloc(16);
76        ref::random_blocks(rty, Wr.as<uint8_t>(), NE, N, 9, 0.05f);
77        mtl::Pipeline pr = cx.pipeline(lib, "moe_route");
78        for (int nt = 1; nt <= 4; ++nt) {
79            mtl::Pipeline pg = cx.pipeline(lib, "gemv_" + std::to_string(rty) + "_32_1_4_1", mtl::Constants().i(0, nt)),
80                          pf = cx.pipeline(lib, "router_route_" + std::to_string(rty), mtl::Constants().i(0, nt));
81            const size_t s16 = (size_t)4 * 4 * MAXT * N;
82            GemvArgs ga{(uint32_t)N, (uint32_t)NE, (uint32_t)nt, (uint32_t)rbr, (uint32_t)N, 512u};
83            MoeArgs ma{};
84            ma.n_tok = nt;
85            ma.topk = 10;
86            ma.n_expert = NE;
87            ma.gx_gu = 5;
88            ma.gx_dn = 20;
89            mtl::Dispatch g;
90            g.p = &pg;
91            g.args = {mtl::Arg::B(Wr), mtl::Arg::B(xp), mtl::Arg::B(xp, s16), mtl::Arg::B(lg1), mtl::Arg::V(ga)};
92            g.grid[0] = NE / 4;
93            g.tg[0] = 128;
94            mtl::Dispatch rt;
95            rt.p = &pr;
96            rt.args = {mtl::Arg::B(lg1), mtl::Arg::B(id1), mtl::Arg::B(w1), mtl::Arg::B(pl1), mtl::Arg::V(ma)};
97            rt.tg[0] = 512;
98            std::memset(pl1.ptr, 0, pl1.bytes);
99            cx.run({g, rt});
100            bool ok = true;
101            for (int rep_ = 0; rep_ < 2; ++rep_) {
102                std::memset(pl2.ptr, 0, pl2.bytes);
103                mtl::Dispatch f;
104                f.p = &pf;
105                f.args = {mtl::Arg::B(Wr), mtl::Arg::B(xp), mtl::Arg::B(xp, s16), mtl::Arg::B(lg2), mtl::Arg::V(ga), mtl::Arg::B(id2),
106                          mtl::Arg::B(w2), mtl::Arg::B(pl2), mtl::Arg::V(ma), mtl::Arg::B(cnt)};
107                f.grid[0] = NE / 16;
108                f.tg[0] = 512;
109                cx.run({f});
110                ok = ok && std::memcmp(lg1.ptr, lg2.ptr, 4 * nt * 512) == 0 && std::memcmp(id1.ptr, id2.ptr, 4 * nt * 10) == 0 &&
111                     std::memcmp(w1.ptr, w2.ptr, 4 * nt * 10) == 0 && std::memcmp(pl1.ptr, pl2.ptr, 4 * P_INTS) == 0 &&
112                     cnt.as<uint32_t>()[0] == 0;
113            }
114            fails += !ok;
115            std::printf("router_route %s ntok %d: logits, ids, w, plan bitwise (2 runs) %s\n", ref::type_name(rty), nt,
116                        ok ? "PASS" : "FAIL");
117        }
118        }
119    }
120    // ---- gr_norm_comb
121    {
122        const int HC = 4, TOPK = 10;
123        mtl::Buffer R0 = cx.alloc(4 * MAXT * HC * N), R1 = cx.alloc(4 * MAXT * HC * N), R2 = cx.alloc(4 * MAXT * HC * N),
124                    ye = cx.alloc(4 * (size_t)MAXT * TOPK * N), ysh = cx.alloc(4 * MAXT * N), sgv = cx.alloc(4 * MAXT),
125                    plan = cx.alloc(4 * P_INTS), inj = cx.alloc(4 * MAXT * HC), wn = cx.alloc(4 * HC * N), bo = cx.alloc(4 * MAXT * N),
126                    x1 = cx.alloc(4 * MAXT * HC * N), x2 = cx.alloc(4 * MAXT * HC * N);
127        for (size_t i = 0; i < R0.bytes / 4; ++i) R0.as<float>()[i] = r.sym();
128        for (size_t i = 0; i < ye.bytes / 4; ++i) ye.as<float>()[i] = r.sym();
129        for (size_t i = 0; i < ysh.bytes / 4; ++i) ysh.as<float>()[i] = r.sym();
130        for (int i = 0; i < MAXT; ++i) sgv.as<float>()[i] = r.sym() * 3;
131        for (int i = 0; i < MAXT * HC; ++i) inj.as<float>()[i] = r.sym() * 3;
132        for (int i = 0; i < HC * N; ++i) wn.as<float>()[i] = 0.5f + r.uni();
133        for (int i = 0; i < MAXT * TOPK; ++i) plan.as<int>()[P_TOKENT + i] = (i * 7) % (MAXT * TOPK);
134        mtl::Pipeline pcm = cx.pipeline(lib, "moe_combine_sh"), pn = cx.pipeline(lib, "gr_norm"), pnc = cx.pipeline(lib, "gr_norm_comb");
135        for (int nt = 1; nt <= 4; ++nt) {
136            std::memcpy(R1.ptr, R0.ptr, R0.bytes);
137            std::memcpy(R2.ptr, R0.ptr, R0.bytes);
138            GlueArgs ga{(uint32_t)N, (uint32_t)nt, (uint32_t)N, TOPK};
139            GrArgs gr{(uint32_t)N, HC, 320, (uint32_t)nt, 1, 1e-6f};
140            mtl::Dispatch c; c.p = &pcm;
141            c.args = {mtl::Arg::B(ye), mtl::Arg::B(plan), mtl::Arg::B(ysh), mtl::Arg::B(sgv), mtl::Arg::B(bo), mtl::Arg::V(ga)};
142            c.grid[0] = (nt * N + 255) / 256; c.tg[0] = 256;
143            mtl::Dispatch n1; n1.p = &pn;
144            n1.args = {mtl::Arg::B(R1), mtl::Arg::B(bo), mtl::Arg::B(inj), mtl::Arg::B(wn), mtl::Arg::B(x1), mtl::Arg::V(gr)};
145            n1.grid[0] = HC; n1.grid[1] = nt; n1.tg[0] = 256;
146            cx.run({c, n1});
147            const uint32_t tk = TOPK;
148            mtl::Dispatch n2; n2.p = &pnc;
149            n2.args = {mtl::Arg::B(R2), mtl::Arg::B(ye), mtl::Arg::B(inj), mtl::Arg::B(wn), mtl::Arg::B(x2), mtl::Arg::V(gr),
150                       mtl::Arg::B(plan), mtl::Arg::B(ysh), mtl::Arg::B(sgv), mtl::Arg::V(tk)};
151            n2.grid[0] = HC; n2.grid[1] = nt; n2.tg[0] = 256;
152            cx.run({n2});
153            const bool ok = std::memcmp(R1.ptr, R2.ptr, 4 * (size_t)nt * HC * N) == 0 && std::memcmp(x1.ptr, x2.ptr, 4 * (size_t)nt * HC * N) == 0;
154            fails += !ok;
155            std::printf("gr_norm_comb ntok %d: R', xn bitwise %s\n", nt, ok ? "PASS" : "FAIL");
156        }
157    }
158    std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
159    return fails ? 1 : 0;
160}