okami.mom/strata

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

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

main
11.0 KiB238 linesraw
1// metal/tests/rt_bench.cpp - the M-series GPU's achievable read bandwidth and the cost of a dispatch.
2//
3//   rt_bench bw [MiB]       streaming read kernels over a buffer (default 2048 MiB, far beyond the SLC), per
4//                           threadgroup size and grid size; read+write (copy); small sizes (cache-resident)
5//   rt_bench dispatch       empty / dependent / small kernels: N dispatches in one serial or concurrent encoder,
6//                           with or without barriers, through an indirect command buffer, and one command buffer
7//                           per dispatch (commit + wait round trip)
8#include "mtl_rt.h"
9
10#include <algorithm>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <string>
15#include <vector>
16
17static double median(std::vector<double> v) {
18    std::sort(v.begin(), v.end());
19    return v[v.size() / 2];
20}
21
22template <class F> static void timed(int warm, int reps, F f, double& med, double& best) {
23    for (int i = 0; i < warm; ++i) f();
24    std::vector<double> t;
25    for (int i = 0; i < reps; ++i) t.push_back(f());
26    med = median(t);
27    best = *std::min_element(t.begin(), t.end());
28}
29
30static void bw(mtl::Context& cx, int lib, size_t mib) {
31    const size_t bytes = mib << 20;
32    mtl::Buffer src = cx.alloc(bytes);
33    for (size_t i = 0; i < bytes / 4; ++i) src.as<uint32_t>()[i] = (uint32_t)(i * 2654435761u);
34    mtl::Buffer out = cx.alloc(64u << 20);
35    const uint32_t n = (uint32_t)(bytes / 16);
36    mtl::Pipeline pr = cx.pipeline(lib, "k_read"), pu = cx.pipeline(lib, "k_read_u4"),
37                  pc = cx.pipeline(lib, "k_read_chunk");
38    std::printf("buffer %zu MiB; k_read max threads %d, simd width %d\n", mib, pr.max_threads, pr.simd_width);
39    std::printf("%-12s %6s %8s %10s %10s\n", "kernel", "tg", "groups", "GB/s med", "GB/s best");
40    double best_all = 0;
41    std::string best_cfg;
42    for (const mtl::Pipeline* p : {&pr, &pu}) {
43        for (uint32_t tg : {64u, 128u, 256u, 512u, 1024u}) {
44            if ((int)tg > p->max_threads) continue;
45            for (uint32_t groups : {80u, 160u, 320u, 640u, 1280u, 2560u, 5120u}) {
46                if ((size_t)groups * tg * 4 > out.bytes) continue;
47                mtl::Dispatch d;
48                d.p = p;
49                d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::V(n)};
50                d.grid[0] = groups;
51                d.tg[0] = tg;
52                double med, best;
53                timed(2, 7, [&] { return cx.run({d}); }, med, best);
54                const double gm = bytes / med / 1e9, gb = bytes / best / 1e9;
55                std::printf("%-12s %6u %8u %10.1f %10.1f\n", p == &pr ? "read" : "read_u4", tg, groups, gm, gb);
56                if (gm > best_all) {
57                    best_all = gm;
58                    best_cfg = std::string(p == &pr ? "read" : "read_u4") + " tg " + std::to_string(tg) + " groups " +
59                               std::to_string(groups);
60                }
61            }
62        }
63    }
64    // contiguous chunk per threadgroup
65    for (uint32_t tg : {256u, 512u, 1024u}) {
66        for (uint32_t chunk_kib : {16u, 64u, 256u, 1024u}) {
67            const uint32_t chunk = chunk_kib * 1024 / 16;
68            const uint32_t groups = (n + chunk - 1) / chunk;
69            if ((size_t)groups * tg * 4 > out.bytes) continue;
70            mtl::Dispatch d;
71            d.p = &pc;
72            d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::V(n), mtl::Arg::V(chunk)};
73            d.grid[0] = groups;
74            d.tg[0] = tg;
75            double med, best;
76            timed(2, 7, [&] { return cx.run({d}); }, med, best);
77            const double gm = bytes / med / 1e9;
78            std::printf("chunk %4uK   %6u %8u %10.1f %10.1f\n", chunk_kib, tg, groups, gm, bytes / best / 1e9);
79            if (gm > best_all) {
80                best_all = gm;
81                best_cfg = "chunk " + std::to_string(chunk_kib) + "K tg " + std::to_string(tg);
82            }
83        }
84    }
85    std::printf("BEST read: %.1f GB/s (%s)\n", best_all, best_cfg.c_str());
86
87    // read + write
88    {
89        mtl::Buffer dst = cx.alloc(bytes / 2);
90        mtl::Pipeline pk = cx.pipeline(lib, "k_copy");
91        const uint32_t nn = (uint32_t)(bytes / 2 / 16);
92        double bestc = 0;
93        for (uint32_t tg : {256u, 1024u})
94            for (uint32_t groups : {640u, 2560u}) {
95                mtl::Dispatch d;
96                d.p = &pk;
97                d.args = {mtl::Arg::B(src), mtl::Arg::B(dst), mtl::Arg::V(nn)};
98                d.grid[0] = groups;
99                d.tg[0] = tg;
100                double med, best;
101                timed(2, 7, [&] { return cx.run({d}); }, med, best);
102                bestc = std::max(bestc, (double)bytes / med / 1e9);
103            }
104        std::printf("BEST copy (read+write, both counted): %.1f GB/s\n", bestc);
105        cx.free(dst);
106    }
107    // cache-resident sizes: the same kernel on a small prefix
108    for (size_t kib : {1024ul, 4096ul, 16384ul, 32768ul, 65536ul, 131072ul, 262144ul}) {
109        if (kib * 1024 > bytes) break;
110        const uint32_t nn = (uint32_t)(kib * 1024 / 16);
111        double bestg = 0;
112        for (uint32_t groups : {160u, 640u, 2560u}) {
113            mtl::Dispatch d;
114            d.p = &pu;
115            d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::V(nn)};
116            d.grid[0] = groups;
117            d.tg[0] = 256;
118            // many repetitions in one command buffer, so the launch is amortised
119            const int reps = (int)std::max<size_t>(1, (256ul << 10) / kib * 4);
120            double med, best;
121            timed(1, 5, [&] { return cx.run({d}, reps) / reps; }, med, best);
122            bestg = std::max(bestg, kib * 1024.0 / med / 1e9);
123        }
124        std::printf("resident %7zu KiB: %.1f GB/s\n", kib, bestg);
125    }
126    cx.free(src);
127    cx.free(out);
128}
129
130// A read of `kib` from DRAM per dispatch (each dispatch a different, uncached chunk of a 4 GiB buffer), dependent
131// dispatches in one serial encoder: the bound for a kernel that streams that much once.
132static void bwsize(mtl::Context& cx, int lib) {
133    const size_t big = 4ull << 30;
134    mtl::Buffer src = cx.alloc(big), out = cx.alloc(64u << 20);
135    mtl::Pipeline pr = cx.pipeline(lib, "k_read");
136    std::printf("%10s %8s %10s %10s\n", "chunk KiB", "tg x grp", "us/disp", "GB/s");
137    for (size_t kib : {1152ul, 2304ul, 4500ul, 9000ul, 13500ul, 18000ul, 36000ul, 72000ul}) {
138        const size_t bytes = kib * 1024;
139        const int R = (int)std::min<size_t>(400, big / bytes);
140        double bestt = 1e9;
141        uint32_t bg = 0, btg = 0;
142        for (uint32_t tg : {256u, 1024u})
143            for (uint32_t groups : {80u, 160u, 320u, 640u, 1280u}) {
144                std::vector<mtl::Dispatch> ds;
145                const uint32_t n = (uint32_t)(bytes / 16);
146                for (int r = 0; r < R; ++r) {
147                    mtl::Dispatch d;
148                    d.p = &pr;
149                    d.args = {mtl::Arg::B(src, (size_t)r * ((big / R) & ~4095ull)), mtl::Arg::B(out), mtl::Arg::V(n)};
150                    d.grid[0] = groups;
151                    d.tg[0] = tg;
152                    ds.push_back(d);
153                }
154                double med, best;
155                timed(1, 5, [&] { return cx.run(ds) / R; }, med, best);
156                if (med < bestt) { bestt = med; bg = groups; btg = tg; }
157            }
158        std::printf("%10zu %4ux%-4u %10.2f %10.1f\n", kib, btg, bg, bestt * 1e6, bytes / bestt / 1e9);
159    }
160    cx.free(src);
161    cx.free(out);
162}
163
164static void dispatch(mtl::Context& cx, int lib) {
165    mtl::Buffer buf = cx.alloc(1 << 20), x = cx.alloc(4 * 2560), y = cx.alloc(4 * 2560);
166    mtl::Pipeline pe = cx.pipeline(lib, "k_empty"), pch = cx.pipeline(lib, "k_chain"), ps = cx.pipeline(lib, "k_small");
167    auto mk = [&](const mtl::Pipeline& p, int kind) {
168        mtl::Dispatch d;
169        d.p = &p;
170        if (kind == 2) {
171            d.args = {mtl::Arg::B(x), mtl::Arg::B(y)};
172            d.grid[0] = 10;
173            d.tg[0] = 256;
174        } else {
175            d.args = {mtl::Arg::B(buf)};
176            d.grid[0] = 1;
177            d.tg[0] = 32;
178        }
179        return d;
180    };
181    const int N = 2000;
182    struct K { const char* name; const mtl::Pipeline* p; int kind; } ks[] = {
183        {"empty", &pe, 0}, {"chain", &pch, 1}, {"small", &ps, 2}};
184    std::printf("%-8s %-36s %10s\n", "kernel", "mode", "us/dispatch");
185    for (const K& k : ks) {
186        std::vector<mtl::Dispatch> ds(N, mk(*k.p, k.kind));
187        double med, best;
188        timed(2, 7, [&] { return cx.run(ds, 1, false, false); }, med, best);
189        std::printf("%-8s %-36s %10.2f\n", k.name, "serial encoder", med / N * 1e6);
190        timed(2, 7, [&] { return cx.run(ds, 1, true, false); }, med, best);
191        std::printf("%-8s %-36s %10.2f\n", k.name, "concurrent encoder, no barrier", med / N * 1e6);
192        timed(2, 7, [&] { return cx.run(ds, 1, true, true); }, med, best);
193        std::printf("%-8s %-36s %10.2f\n", k.name, "concurrent encoder + barrier each", med / N * 1e6);
194        timed(2, 7, [&] { return cx.run(ds, 1, false, true); }, med, best);
195        std::printf("%-8s %-36s %10.2f\n", k.name, "serial encoder + barrier each", med / N * 1e6);
196        timed(2, 7, [&] { return cx.run_icb(ds, 1, false); }, med, best);
197        std::printf("%-8s %-36s %10.2f\n", k.name, "ICB, no barrier", med / N * 1e6);
198        timed(2, 7, [&] { return cx.run_icb(ds, 1, true); }, med, best);
199        std::printf("%-8s %-36s %10.2f\n", k.name, "ICB + barrier each", med / N * 1e6);
200    }
201    // command buffer round trips
202    {
203        std::vector<mtl::Dispatch> one(1, mk(pe, 0));
204        double gpu = 0;
205        const int R = 300;
206        cx.run_wall(one, 20);
207        const double wall = cx.run_wall(one, R, &gpu);
208        std::printf("%-8s %-36s %10.2f  (GPU %.2f us)\n", "empty", "1 cmdbuf/dispatch, commit+wait wall", wall / R * 1e6,
209                    gpu / R * 1e6);
210        std::vector<mtl::Dispatch> d100(100, mk(pe, 0));
211        const double wall2 = cx.run_wall(d100, 100, &gpu);
212        std::printf("%-8s %-36s %10.2f  (GPU %.2f us)\n", "empty", "100 dispatches/cmdbuf, wall per cb", wall2 / 100 * 1e6,
213                    gpu / 100 * 1e6);
214    }
215    // the fixed cost of one command buffer as seen by the GPU timer (1 dispatch)
216    {
217        std::vector<mtl::Dispatch> one(1, mk(pe, 0));
218        double med, best;
219        timed(5, 21, [&] { return cx.run(one); }, med, best);
220        std::printf("%-8s %-36s %10.2f\n", "empty", "single dispatch cmdbuf GPU time", med * 1e6);
221    }
222    cx.free(buf);
223    cx.free(x);
224    cx.free(y);
225}
226
227int main(int argc, char** argv) {
228    mtl::Context cx;
229    std::printf("device: %s  apple9 %d apple10 %d metal4 %d  working set %.1f GiB\n", cx.device_name().c_str(),
230                cx.has_family_apple(9), cx.has_family_apple(10), cx.supports_metal4(),
231                cx.recommended_working_set() / 1073741824.0);
232    const int lib = cx.compile({"rt_bench.metal"}, "#define NO_GGML_COMMON 1\n");
233    const std::string mode = argc > 1 ? argv[1] : "all";
234    if (mode == "bw" || mode == "all") bw(cx, lib, argc > 2 ? std::strtoul(argv[2], nullptr, 10) : 2048);
235    if (mode == "dispatch" || mode == "all") dispatch(cx, lib);
236    if (mode == "bwsize" || mode == "all") bwsize(cx, lib);
237    return 0;
238}