okami.mom/strata

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

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

main
5.4 KiB118 linesraw
1// metal/tests/pair_parity.cpp - gemv_pair (the shared expert's gate + up + SwiGLU in one dispatch) vs double, for
2// format pairs of the packs, 1 / 3 / 8 tokens; and the raw-activation Q2_0 GEMV (type 142) the down then uses.
3#include "mtl_rt.h"
4#include "ref_quant.h"
5
6#include <cmath>
7#include <cstdio>
8#include <string>
9#include <utility>
10#include <vector>
11
12namespace {
13struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };
14int xkind(int t) {
15    switch (t) {
16        case ref::Q2_0: return 1;
17        case ref::Q4_K: case ref::Q5_K: return 2;
18        case ref::Q6_K: case ref::Q3_K: return 3;
19        default: return 0;
20    }
21}
22}  // namespace
23
24int main() {
25    mtl::Context cx;
26    const int lib = cx.compile({"quant.metal", "units.metal", "gemv.metal"});
27    mtl::Pipeline pprep = cx.pipeline(lib, "xprep");
28    const int NIN = 2560, NOUT = 640, MAXT = 8;
29    mtl::Buffer x = cx.alloc(4 * NIN * MAXT), xp = cx.alloc(4 * (4 * NIN * MAXT + NIN / 16 * MAXT)), h = cx.alloc(4 * NOUT * MAXT);
30    ref::Rng r(4);
31    for (int i = 0; i < NIN * MAXT; ++i) x.as<float>()[i] = r.sym();
32    {
33        mtl::Dispatch d;
34        d.p = &pprep;
35        const uint32_t n = NIN, nt = MAXT;
36        d.args = {mtl::Arg::B(x), mtl::Arg::B(xp), mtl::Arg::V(n), mtl::Arg::V(nt)};
37        d.tg[0] = 256;
38        d.grid[0] = (NIN / 16 * MAXT + 255) / 256;
39        cx.run({d});
40    }
41    const size_t plane = (size_t)4 * NIN * MAXT;
42    const std::vector<std::pair<int, int>> pairs = {{ref::Q2_0, ref::Q3_K}, {ref::Q3_K, ref::Q2_0}, {ref::IQ4_XS, ref::IQ3_S},
43                                                    {ref::Q4_K, ref::Q6_K}, {ref::Q5_K, ref::Q4_K}, {ref::Q6_K, ref::IQ3_S},
44                                                    {ref::Q2_0, ref::Q2_0}, {ref::IQ3_S, ref::Q4_K}};
45    int fails = 0;
46    for (auto [fg, fu] : pairs) {
47        const size_t rg = ref::row_bytes(fg, NIN), ru = ref::row_bytes(fu, NIN);
48        mtl::Buffer Wg = cx.alloc(rg * NOUT), Wu = cx.alloc(ru * NOUT);
49        ref::random_blocks(fg, Wg.as<uint8_t>(), NOUT, NIN, 7 + fg, 0.02f);
50        ref::random_blocks(fu, Wu.as<uint8_t>(), NOUT, NIN, 9 + fu, 0.02f);
51        std::vector<float> wg((size_t)NIN * NOUT), wu((size_t)NIN * NOUT);
52        ref::dequant(fg, Wg.ptr, wg.data(), wg.size());
53        ref::dequant(fu, Wu.ptr, wu.data(), wu.size());
54        for (int nt : {1, 3, 8}) {
55            mtl::Pipeline p = cx.pipeline(lib, "gemv_pair_" + std::to_string(fg) + "_" + std::to_string(fu),
56                                          mtl::Constants().i(0, nt));
57            GemvArgs a{(uint32_t)NIN, (uint32_t)NOUT, (uint32_t)nt, (uint32_t)rg, (uint32_t)NIN, (uint32_t)NOUT};
58            const uint32_t rbu = (uint32_t)ru;
59            mtl::Dispatch d;
60            d.p = &p;
61            d.args = {mtl::Arg::B(Wg), mtl::Arg::B(xp, plane * xkind(fg)), mtl::Arg::B(Wu), mtl::Arg::B(xp, plane * xkind(fu)),
62                      mtl::Arg::B(xp, plane * 4), mtl::Arg::B(h), mtl::Arg::V(a), mtl::Arg::V(rbu)};
63            d.grid[0] = NOUT / 4;
64            d.tg[0] = 128;
65            cx.run({d});
66            double se = 0, sr = 0;
67            for (int t = 0; t < nt; ++t)
68                for (int o = 0; o < NOUT; ++o) {
69                    double g = 0, u = 0;
70                    for (int k = 0; k < NIN; ++k) {
71                        g += (double)wg[(size_t)o * NIN + k] * x.as<float>()[t * NIN + k];
72                        u += (double)wu[(size_t)o * NIN + k] * x.as<float>()[t * NIN + k];
73                    }
74                    const double ref = g / (1.0 + std::exp(-g)) * u;
75                    const double e = h.as<float>()[t * NOUT + o] - ref;
76                    se += e * e;
77                    sr += ref * ref;
78                }
79            const double rel = std::sqrt(se / sr);
80            fails += !(rel < 1e-5);
81            std::printf("pair %-7s + %-7s ntok %d: rel L2 %.2e  %s\n", ref::type_name(fg), ref::type_name(fu), nt, rel,
82                        rel < 1e-5 ? "PASS" : "FAIL");
83        }
84        cx.free(Wg);
85        cx.free(Wu);
86    }
87    // the raw-activation Q2_0 GEMV
88    {
89        const int ni = 640, no = 2560;
90        const size_t rb = ref::row_bytes(ref::Q2_0, ni);
91        mtl::Buffer W = cx.alloc(rb * no), y = cx.alloc(4 * no * MAXT);
92        ref::random_blocks(ref::Q2_0, W.as<uint8_t>(), no, ni, 3, 0.02f);
93        std::vector<float> wf((size_t)ni * no);
94        ref::dequant(ref::Q2_0, W.ptr, wf.data(), wf.size());
95        mtl::Pipeline p = cx.pipeline(lib, "gemv_142_8_1_4_1", mtl::Constants().i(0, 3));
96        GemvArgs a{(uint32_t)ni, (uint32_t)no, 3, (uint32_t)rb, (uint32_t)ni, (uint32_t)no};
97        mtl::Dispatch d;
98        d.p = &p;
99        d.args = {mtl::Arg::B(W), mtl::Arg::B(x), mtl::Arg::B(x), mtl::Arg::B(y), mtl::Arg::V(a)};
100        d.grid[0] = no / 16;
101        d.tg[0] = 128;
102        cx.run({d});
103        double se = 0, sr = 0;
104        for (int t = 0; t < 3; ++t)
105            for (int o = 0; o < no; ++o) {
106                double ref = 0;
107                for (int k = 0; k < ni; ++k) ref += (double)wf[(size_t)o * ni + k] * x.as<float>()[t * ni + k];
108                const double e = y.as<float>()[t * no + o] - ref;
109                se += e * e;
110                sr += ref * ref;
111            }
112        const double rel = std::sqrt(se / sr);
113        fails += !(rel < 1e-5);
114        std::printf("Q2_0 raw-x GEMV 640 -> 2560, 3 tokens: rel L2 %.2e  %s\n", rel, rel < 1e-5 ? "PASS" : "FAIL");
115    }
116    std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
117    return fails ? 1 : 0;
118}