okami.mom/strata

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

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

main
9.3 KiB178 linesraw
1// metal/tests/mma_parity.cpp - the simdgroup-matrix GEMV (mma.metal): parity vs double for every dense format, each
2// token of a window bitwise a one-token call, and its speed on the packs' shapes against the per-lane GEMV.
3//
4//   mma_parity            parity
5//   mma_parity --bench    speed, 1 / 3 / 8 tokens
6#include "mtl_rt.h"
7#include "ref_quant.h"
8
9#include <algorithm>
10#include <cmath>
11#include <cstdio>
12#include <cstring>
13#include <string>
14#include <vector>
15
16namespace {
17struct MmaArgs { uint32_t n_in, n_out, row_bytes, x_stride, y_stride; };
18struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };
19double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
20int xkind(int t) {
21    switch (t) {
22        case ref::Q2_0: return 1;
23        case ref::Q4_K: case ref::Q5_K: return 2;
24        case ref::Q6_K: case ref::Q3_K: return 3;
25        default: return 0;
26    }
27}
28}  // namespace
29
30int main(int argc, char** argv) {
31    const bool bench = argc > 1 && std::string(argv[1]) == "--bench";
32    mtl::Context cx;
33    const int lib = cx.compile({"quant.metal", "units.metal", "gemv.metal", "mma.metal"});
34    std::printf("device %s\n", cx.device_name().c_str());
35    const int MAXT = 8;
36    mtl::Pipeline pprep = cx.pipeline(lib, "xprep");
37    const std::vector<int> types = {ref::BF16, ref::Q8_0, ref::Q4_0, ref::Q5_0, ref::IQ4_NL, ref::IQ4_XS, ref::Q2_0,
38                                    ref::IQ3_S, ref::Q3_K, ref::Q4_K, ref::Q5_K, ref::Q6_K};
39    if (!bench) {
40        int fails = 0;
41        for (int t : types)
42            for (int n_in : {2560, 640, 6144}) {
43                if (n_in % ref::block_elems(t) || (n_in == 640 && ref::block_elems(t) == 256)) continue;
44                const int n_out = 100;
45                const size_t rb = ref::row_bytes(t, n_in);
46                mtl::Buffer W = cx.alloc(rb * n_out), x = cx.alloc(4 * n_in * MAXT), y = cx.alloc(4 * n_out * MAXT),
47                            y1 = cx.alloc(4 * n_out * MAXT);
48                ref::random_blocks(t, W.as<uint8_t>(), n_out, n_in, 41 + t, 0.02f);
49                ref::Rng r(9);
50                for (int i = 0; i < n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
51                std::vector<float> wf((size_t)n_in * n_out);
52                ref::dequant(t, W.ptr, wf.data(), wf.size());
53                for (int nt : {1, 3, 8}) {
54                    mtl::Pipeline p = cx.pipeline(lib, "gemv_mma_" + std::to_string(t) + "_2", mtl::Constants().i(4, nt));
55                    MmaArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)rb, (uint32_t)n_in, (uint32_t)n_out};
56                    mtl::Dispatch d;
57                    d.p = &p;
58                    d.args = {mtl::Arg::B(W), mtl::Arg::B(x), mtl::Arg::B(y), mtl::Arg::V(a)};
59                    d.grid[0] = (n_out + 15) / 16;
60                    d.tg[0] = 64;
61                    cx.run({d});
62                    double se = 0, sr = 0;
63                    for (int tt = 0; tt < nt; ++tt)
64                        for (int o = 0; o < n_out; ++o) {
65                            double ref = 0;
66                            for (int k = 0; k < n_in; ++k) ref += (double)wf[(size_t)o * n_in + k] * x.as<float>()[tt * n_in + k];
67                            const double e = y.as<float>()[tt * n_out + o] - ref;
68                            se += e * e;
69                            sr += ref * ref;
70                        }
71                    // the window property: token tt's column vs a one-token call on x shifted to row 0
72                    int notex = 0;
73                    if (nt > 1) {
74                        mtl::Pipeline p1 = cx.pipeline(lib, "gemv_mma_" + std::to_string(t) + "_2", mtl::Constants().i(4, 1));
75                        for (int tt = 0; tt < nt; ++tt) {
76                            mtl::Dispatch d1 = d;
77                            d1.p = &p1;
78                            d1.args = {mtl::Arg::B(W), mtl::Arg::B(x, 4 * (size_t)tt * n_in), mtl::Arg::B(y1), mtl::Arg::V(a)};
79                            // rows past the window come from the following tokens' memory: padding only
80                            cx.run({d1});
81                            notex += std::memcmp(y1.ptr, y.as<float>() + tt * n_out, 4 * n_out) != 0;
82                        }
83                    }
84                    const double rel = std::sqrt(se / sr);
85                    const bool ok = rel < 1e-5 && notex == 0;
86                    fails += !ok;
87                    std::printf("mma parity %-7s n_in %5d ntok %d: rel L2 %.2e, window == one token: %s  %s\n",
88                                ref::type_name(t), n_in, nt, rel, nt == 1 ? "-" : notex ? "NO" : "bitwise", ok ? "PASS" : "FAIL");
89                }
90                cx.free(W);
91                cx.free(x);
92                cx.free(y);
93                cx.free(y1);
94            }
95        std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
96        return fails ? 1 : 0;
97    }
98    struct Shape { const char* name; int type, n_in, n_out; };
99    const std::vector<Shape> shapes = {{"gdn qkv", ref::IQ4_XS, 2560, 10240}, {"gdn qkv", ref::Q3_K, 2560, 10240},
100                                       {"gdn qkv", ref::Q4_K, 2560, 10240},   {"gdn qkv", ref::Q6_K, 2560, 10240},
101                                       {"gdn out", ref::Q5_K, 6144, 2560},    {"qsa q", ref::Q2_0, 2560, 12288},
102                                       {"router", ref::BF16, 2560, 512},      {"lm head", ref::Q5_K, 2560, 248320}};
103    std::printf("%-9s %-7s %5s | %9s %9s | %s\n", "shape", "type", "ntok", "lane us", "mma us", "mma GB/s (best SG)");
104    for (const Shape& s : shapes) {
105        const size_t rb = ref::row_bytes(s.type, s.n_in), mb = rb * s.n_out;
106        const int copies = (int)std::max<size_t>(4, std::min<size_t>(64, (1ull << 30) / mb));
107        mtl::Buffer W = cx.alloc(mb * copies), x = cx.alloc(4 * s.n_in * MAXT), y = cx.alloc(4 * (size_t)s.n_out * MAXT),
108                    xp = cx.alloc(4 * (4 * s.n_in * MAXT + s.n_in / 16 * MAXT));
109        {
110            std::vector<uint8_t> tmp(mb);
111            ref::random_blocks(s.type, tmp.data(), s.n_out, s.n_in, 3, 0.02f);
112            for (int c = 0; c < copies; ++c) std::memcpy(W.as<uint8_t>() + (size_t)c * mb, tmp.data(), mb);
113        }
114        ref::Rng r(5);
115        for (int i = 0; i < s.n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
116        {
117            mtl::Dispatch d;
118            d.p = &pprep;
119            const uint32_t n = s.n_in, nt = MAXT;
120            d.args = {mtl::Arg::B(x), mtl::Arg::B(xp), mtl::Arg::V(n), mtl::Arg::V(nt)};
121            d.tg[0] = 256;
122            d.grid[0] = (s.n_in / 16 * MAXT + 255) / 256;
123            cx.run({d});
124        }
125        const size_t plane = (size_t)4 * s.n_in * MAXT;
126        for (int nt : {1, 3, 8}) {
127            auto time = [&](std::vector<mtl::Dispatch>& ds) {
128                cx.warm(ds, 0.1);
129                std::vector<double> t;
130                for (int i = 0; i < 5; ++i) t.push_back(cx.run(ds) / ds.size());
131                return median(t);
132            };
133            // the per-lane GEMV with its usual tile
134            const int L = s.n_in >= 6144 ? 32 : (s.n_out <= 640 || s.type == ref::Q2_0 || s.type == ref::BF16) ? 32 : 16;
135            const int S = s.n_in >= 6144 ? 8 : 4, K = s.n_in >= 6144 ? 2 : 1;
136            mtl::Pipeline pl = cx.pipeline(lib, "gemv_" + std::to_string(s.type) + "_" + std::to_string(L) + "_1_" +
137                                                    std::to_string(S) + "_" + std::to_string(K), mtl::Constants().i(0, nt));
138            GemvArgs ga{(uint32_t)s.n_in, (uint32_t)s.n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)s.n_in, (uint32_t)s.n_out};
139            std::vector<mtl::Dispatch> dl;
140            for (int k = 0; k < std::max(copies, 16); ++k) {
141                mtl::Dispatch d;
142                d.p = &pl;
143                d.args = {mtl::Arg::B(W, (size_t)(k % copies) * mb), mtl::Arg::B(xp, plane * xkind(s.type)),
144                          mtl::Arg::B(xp, plane * 4), mtl::Arg::B(y), mtl::Arg::V(ga)};
145                const int rpt = (S / K) * (32 / L);
146                d.grid[0] = (s.n_out + rpt - 1) / rpt;
147                d.tg[0] = S * 32;
148                dl.push_back(d);
149            }
150            const double tl = time(dl);
151            double best = 1e9;
152            int bsg = 0;
153            for (int sgn : {1, 2, 4}) {
154                mtl::Pipeline pm = cx.pipeline(lib, "gemv_mma_" + std::to_string(s.type) + "_" + std::to_string(sgn),
155                                               mtl::Constants().i(4, nt));
156                MmaArgs ma{(uint32_t)s.n_in, (uint32_t)s.n_out, (uint32_t)rb, (uint32_t)s.n_in, (uint32_t)s.n_out};
157                std::vector<mtl::Dispatch> dm;
158                for (int k = 0; k < std::max(copies, 16); ++k) {
159                    mtl::Dispatch d;
160                    d.p = &pm;
161                    d.args = {mtl::Arg::B(W, (size_t)(k % copies) * mb), mtl::Arg::B(x), mtl::Arg::B(y), mtl::Arg::V(ma)};
162                    d.grid[0] = (s.n_out + 8 * sgn - 1) / (8 * sgn);
163                    d.tg[0] = 32 * sgn;
164                    dm.push_back(d);
165                }
166                const double tm = time(dm);
167                if (tm < best) { best = tm; bsg = sgn; }
168            }
169            std::printf("%-9s %-7s %5d | %9.2f %9.2f | %6.1f (SG %d)  lane %6.1f GB/s\n", s.name, ref::type_name(s.type), nt,
170                        tl * 1e6, best * 1e6, mb / best / 1e9, bsg, mb / tl / 1e9);
171        }
172        cx.free(W);
173        cx.free(x);
174        cx.free(y);
175        cx.free(xp);
176    }
177    return 0;
178}