okami.mom/strata

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

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

main
6.2 KiB116 linesraw
1// metal/tests/mm_bench.cpp - matrix throughput (TFLOP/s) of MPP matmul2d and simdgroup_matrix, fp16/bf16/fp32
2// operands, fp32 results, on a square 4096^3 and on prompt-expert shapes; checks one product against the CPU.
3//   mm_bench
4#include "mtl_rt.h"
5#include "ref_quant.h"
6
7#include <algorithm>
8#include <cmath>
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <string>
13#include <vector>
14
15namespace {
16struct MmArgs { int M, N, K; };
17double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
18}  // namespace
19
20int main() {
21    mtl::Context cx;
22    std::printf("device %s, metal4 %d\n", cx.device_name().c_str(), cx.supports_metal4());
23    const int lib = cx.compile({"mm_bench.metal"}, "#define NO_GGML_COMMON 1\n");
24    const int MAX = 4096;
25    mtl::Buffer A = cx.alloc((size_t)MAX * MAX * 4), B = cx.alloc((size_t)MAX * MAX * 4), C = cx.alloc((size_t)MAX * MAX * 4);
26    ref::Rng r(1);
27    // fill as half and float views of the same random values (each kernel reads its own type)
28    auto fill = [&](int elem) {
29        for (size_t i = 0; i < (size_t)MAX * MAX; ++i) {
30            const float v = r.sym();
31            if (elem == 4) { A.as<float>()[i] = v; B.as<float>()[i] = r.sym(); }
32            else if (elem == 2) { A.as<uint16_t>()[i] = ref::f32_to_f16(v); B.as<uint16_t>()[i] = ref::f32_to_f16(r.sym()); }
33            else { A.as<uint16_t>()[i] = ref::f32_to_bf16(v); B.as<uint16_t>()[i] = ref::f32_to_bf16(r.sym()); }
34        }
35    };
36    struct K { std::string name; int elem; int tm, tn, sg; bool mpp; };
37    const std::vector<K> ks = {
38        {"mpp_mm_half_64_32_4", 2, 64, 32, 4, true},  {"mpp_mm_half_64_64_4", 2, 64, 64, 4, true},
39        {"mpp_mm_half_128_64_4", 2, 128, 64, 4, true}, {"mpp_mm_half_32_64_4", 2, 32, 64, 4, true},
40        {"mpp_mm_half_64_128_4", 2, 64, 128, 4, true}, {"mpp_mm_half_128_128_4", 2, 128, 128, 4, true},
41        {"mpp_mm_half_16_64_2", 2, 16, 64, 2, true},   {"mpp_mm_bfloat_64_64_4", 3, 64, 64, 4, true},
42        {"mpp_mm_bfloat_128_64_4", 3, 128, 64, 4, true}, {"mpp_mm_float_64_32_4", 4, 64, 32, 4, true},
43        {"mpp_mm_float_64_64_4", 4, 64, 64, 4, true},  {"mpp_mm_float_128_64_4", 4, 128, 64, 4, true},
44        {"sg_mm_half", 2, 64, 64, 4, false},           {"sg_mm_float", 4, 64, 64, 4, false},
45        {"mpp_mm_half_64_16_2", 2, 64, 16, 2, true},   {"mpp_mm_half_64_16_4", 2, 64, 16, 4, true},
46        {"mpp_mm_half_128_16_4", 2, 128, 16, 4, true}, {"mpp_mm_half_128_32_4", 2, 128, 32, 4, true},
47        {"mpp_mm_half_64_32_2", 2, 64, 32, 2, true},   {"mpp_mm_half_32_16_2", 2, 32, 16, 2, true},
48        {"mpp_mm_half_32_32_2", 2, 32, 32, 2, true},   {"mpp_mm_half_16_32_1", 2, 16, 32, 1, true},
49        {"mpp_mm_half_16_16_1", 2, 16, 16, 1, true},   {"mpp_mm_half_128_48_4", 2, 128, 48, 4, true},
50        {"mpp_mm_half_64_48_4", 2, 64, 48, 4, true}};
51    struct Shape { const char* name; int M, N, K; };
52    const std::vector<Shape> shapes = {{"square 4096^3", 4096, 4096, 4096},
53                                       {"expert gu 128 tok x 1280 x 2560", 128, 1280, 2560},
54                                       {"expert down 128 tok x 2560 x 640", 128, 2560, 640},
55                                       {"dense qkv 2048 tok x 10240 x 2560", 2048, 4096, 2560},
56                                       {"expert gu 16 tok x 1280 x 2560", 16, 1280, 2560},
57                                       {"expert gu^T 1280 x 48 tok x 2560", 1280, 48, 2560},
58                                       {"expert gu^T 1280 x 16 tok x 2560", 1280, 16, 2560},
59                                       {"many gu^T 12800 x 48 tok x 2560", 12800, 48, 2560},
60                                       {"many gu 48 tok x 12800 x 2560", 48, 12800, 2560}};
61    int filled = 0;
62    const char* only = std::getenv("MMB_ONLY");   // a substring of the kernel names to run
63    for (const K& k : ks) {
64        if (only && k.name.find(only) == std::string::npos) continue;
65        if (filled != k.elem) { fill(k.elem); filled = k.elem; }
66        mtl::Pipeline p = cx.pipeline(lib, k.name);
67        // correctness on a small product (M = tm*2, N = tn*2, K = 256) against the CPU
68        {
69            const int M = k.tm * 2, N = k.tn * 2, KK = 256;
70            MmArgs a{M, N, KK};
71            mtl::Dispatch d;
72            d.p = &p;
73            d.args = {mtl::Arg::B(A), mtl::Arg::B(B), mtl::Arg::B(C), mtl::Arg::V(a)};
74            d.grid[0] = N / k.tn;
75            d.grid[1] = M / k.tm;
76            d.tg[0] = 32 * k.sg;
77            cx.run({d});
78            double se = 0, sr = 0;
79            for (int i = 0; i < M; ++i)
80                for (int j = 0; j < N; ++j) {
81                    double ref = 0;
82                    for (int q = 0; q < KK; ++q) {
83                        auto val = [&](const mtl::Buffer& X, size_t idx) -> double {
84                            if (k.elem == 4) return X.as<float>()[idx];
85                            if (k.elem == 2) return ref::f16_to_f32(X.as<uint16_t>()[idx]);
86                            return ref::bf16_to_f32(X.as<uint16_t>()[idx]);
87                        };
88                        ref += val(A, (size_t)i * KK + q) * val(B, (size_t)j * KK + q);
89                    }
90                    const double e = C.as<float>()[i * N + j] - ref;
91                    se += e * e;
92                    sr += ref * ref;
93                }
94            std::printf("%-26s check rel L2 %.2e\n", k.name.c_str(), std::sqrt(se / sr));
95        }
96        for (const Shape& s : shapes) {
97            if (s.M % k.tm || s.N % k.tn) continue;
98            MmArgs a{s.M, s.N, s.K};
99            mtl::Dispatch d;
100            d.p = &p;
101            d.args = {mtl::Arg::B(A), mtl::Arg::B(B), mtl::Arg::B(C), mtl::Arg::V(a)};
102            d.grid[0] = s.N / k.tn;
103            d.grid[1] = s.M / k.tm;
104            d.tg[0] = 32 * k.sg;
105            const int reps = std::max(1, (int)(2e10 / (2.0 * s.M * s.N * s.K)));
106            std::vector<mtl::Dispatch> ds(std::min(reps, 200), d);
107            cx.warm(ds, 0.2);
108            std::vector<double> t;
109            for (int i = 0; i < 5; ++i) t.push_back(cx.run(ds) / ds.size());
110            const double m = median(t);
111            std::printf("%-26s %-36s %8.1f us  %6.2f TFLOP/s\n", k.name.c_str(), s.name, m * 1e6,
112                        2.0 * s.M * s.N * s.K / m / 1e12);
113        }
114    }
115    return 0;
116}