okami.mom/strata

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

Niko1221Merge f-q4-metal (ed1f58d27): the Mac (Metal) engine follows the compiled geometry (q4_lite/q4_mid/q4_big kernel parity 40/40, gate 2 like CUDA), plus the PLE row-buffer fix in src/program/generate.cpp (sized by the compiled hidden size, not 2560; it overran 24 KB at hidden 4096 and crashed Metal serve); default build unchanged (CUDA SASS 3573/3573 and 160/160 host objects, HIP gfx1201/gfx1151 string offsets only, SYCL 129/129 objects and 22/22 checksums, Metal per-token functions identical, UD-Q4_K_XL 10/10 tokens, CUDA IQ3_S 26/26 tokens) (Qwen4 prep)7f46672a4

main
21.4 KiB383 linesraw
1// metal/tests/gemv_parity.cpp - the dense GEMV (gemv.metal) per format: parity against double, and speed per tile
2// shape on the model's projection shapes.
3//
4//   gemv_parity [--nin A,B,C]           parity: every format, n_in 2560/640/10240 (or --nin), 1..8 tokens
5//   gemv_parity --bench [--pack Q2_0|IQ3_XXS] [--ntok N] [--all]   speed on the pack's shapes (best tile per shape)
6//   gemv_parity --one TYPE NIN NOUT [--ntok N]                      sweep one shape
7//   GEMV_F16=1 gemv_parity --one TYPE NIN NOUT [--ntok N]           the fp16 tiles (STRATA_METAL_F16) against the fp32 gemv
8#include "gguf_lite.h"
9#include "mtl_rt.h"
10#include "ref_quant.h"
11
12#include <algorithm>
13#include <cmath>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <string>
18#include <vector>
19
20namespace {
21
22struct GemvArgs { uint32_t n_in, n_out, n_tok, row_bytes, x_stride, y_stride; };
23struct Cfg { int L, R, S, K; };
24const std::vector<Cfg> kCfgs = {{32, 1, 4, 1}, {32, 2, 4, 1}, {16, 1, 4, 1}, {16, 2, 4, 1}, {8, 1, 4, 1},
25                                {8, 2, 4, 1},  {4, 2, 4, 1},  {4, 1, 4, 1},  {32, 1, 4, 4}, {32, 2, 4, 2},
26                                {32, 1, 8, 8}, {16, 1, 8, 4}, {32, 1, 8, 2}, {8, 1, 8, 1},  {1, 1, 4, 1},
27                                {1, 2, 4, 1},  {2, 1, 4, 1},  {2, 2, 4, 1},  {1, 1, 2, 1},  {4, 4, 4, 1}};
28int rows_per_tg(Cfg c) { return (c.S / c.K) * (32 / c.L) * c.R; }
29int unit_of(int t) {
30    switch (t) {
31        case ref::Q2_0: case ref::IQ3_S: case ref::IQ3_XXS: case ref::IQ2_XXS: case ref::IQ2_XS: case ref::IQ2_S:
32        case ref::BF16: case ref::F32: return 16;
33        default: return 32;
34    }
35}
36bool cfg_ok(Cfg c, int type, int n_in) {
37    const int U = n_in / unit_of(type);
38    return U % c.K == 0 && U / c.K >= c.L / 2;
39}
40std::string kname(int t, Cfg c) {
41    return "gemv_" + std::to_string(t) + "_" + std::to_string(c.L) + "_" + std::to_string(c.R) + "_" +
42           std::to_string(c.S) + "_" + std::to_string(c.K);
43}
44double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
45// which prepared x a format reads (units.metal XK_*)
46int xkind(int t) {
47    switch (t) {
48        case ref::Q2_0: return 1;
49        case ref::Q4_K: case ref::Q5_K: return 2;
50        case ref::Q6_K: case ref::Q3_K: return 3;
51        default: return 0;
52    }
53}
54// x [MAXT][n] -> XP [4][MAXT][n] + S16 [MAXT][n/16]
55mtl::Dispatch xprep_dispatch(const mtl::Pipeline& p, const mtl::Buffer& x, const mtl::Buffer& xp, uint32_t n, uint32_t nt) {
56    mtl::Dispatch d;
57    d.p = &p;
58    d.args = {mtl::Arg::B(x), mtl::Arg::B(xp), mtl::Arg::V(n), mtl::Arg::V(nt)};
59    d.tg[0] = 256;
60    d.grid[0] = (n / 16 * nt + 255) / 256;
61    return d;
62}
63
64int type_of(const std::string& s) {
65    for (int t : {ref::Q2_0, ref::Q4_0, ref::Q5_0, ref::Q8_0, ref::Q3_K, ref::Q4_K, ref::Q5_K, ref::Q6_K, ref::IQ4_NL,
66                  ref::IQ4_XS, ref::IQ3_S, ref::BF16})
67        if (s == ref::type_name(t)) return t;
68    std::fprintf(stderr, "unknown type %s\n", s.c_str());
69    std::exit(2);
70}
71
72struct Shape { const char* name; int type, n_in, n_out, per_token; };   // per_token: calls per decoded token
73
74}  // namespace
75
76int main(int argc, char** argv) {
77    bool bench = false, all = false;
78    double worst16 = -1;   // GEMV_F16: the worst tile
79    int ntok = 1;
80    std::string gguf_path;
81    std::vector<int> nins = {2560, 640, 10240};   // --nin A,B,C: the parity run's row lengths (the model's: hidden, expert width, value dim, ...)
82    std::string pack = "Q2_0";
83    std::vector<Shape> one;
84    for (int i = 1; i < argc; ++i) {
85        std::string a = argv[i];
86        if (a == "--bench") bench = true;
87        else if (a == "--all") all = true;
88        else if (a == "--ntok") ntok = std::atoi(argv[++i]);
89        else if (a == "--nin") {
90            nins.clear();
91            for (const char* q = argv[++i]; *q;) { nins.push_back(std::atoi(q)); while (*q && *q != ',') ++q; if (*q) ++q; }
92        }
93        else if (a == "--pack") pack = argv[++i];
94        else if (a == "--gguf") gguf_path = argv[++i];
95        else if (a == "--one") {
96            const int t = type_of(argv[i + 1]);
97            one.push_back({"one", t, std::atoi(argv[i + 2]), std::atoi(argv[i + 3]), 1});
98            i += 3;
99            bench = true;
100            all = true;
101        } else { std::fprintf(stderr, "unknown argument %s\n", a.c_str()); return 2; }
102    }
103    mtl::Context cx;
104    const int lib = cx.compile({"quant.metal", "units.metal", "gemv.metal"}, std::getenv("GEMV_F16") ? "#define STRATA_METAL_F16 1\n" : "");
105    std::printf("device %s\n", cx.device_name().c_str());
106    const int MAXT = 8;
107    mtl::Pipeline pprep = cx.pipeline(lib, "xprep");
108
109    if (!gguf_path.empty()) {
110        // real tensors: the first type-distinct ones of the file (experts: expert 0's matrix), 1 and 3 tokens
111        gguf::File f;
112        std::string err;
113        if (!f.open(gguf_path, err)) { std::fprintf(stderr, "%s\n", err.c_str()); return 2; }
114        mtl::Buffer Wf = cx.wrap(f.map, f.map_bytes);
115        std::map<int, const gguf::Tensor*> pick;
116        for (const auto& kv : f.tensors) {
117            const gguf::Tensor& t = kv.second;
118            if (t.shape.size() < 2 || t.name.find("token_embd") != std::string::npos ||
119                t.name.find("per_layer") != std::string::npos)
120                continue;
121            const int ty = t.type;
122            const bool gemv_type = ty == ref::BF16 || ty == ref::Q8_0 || ty == ref::Q4_0 || ty == ref::Q5_0 ||
123                                   ty == ref::IQ4_NL || ty == ref::IQ4_XS || ty == ref::Q2_0 || ty == ref::IQ3_S ||
124                                   ty == ref::Q3_K || ty == ref::Q4_K || ty == ref::Q5_K || ty == ref::Q6_K;
125            if (gemv_type && !pick.count(ty)) pick[ty] = &t;
126        }
127        int fails = 0;
128        for (const auto& kv : pick) {
129            const gguf::Tensor& t = *kv.second;
130            const int ty = t.type, n_in = (int)t.shape[0], n_out = std::min<int>((int)t.shape[1], 4096);
131            const size_t rb = ref::row_bytes(ty, n_in);
132            mtl::Buffer x = cx.alloc(4 * n_in * MAXT), y = cx.alloc(4 * (size_t)n_out * MAXT),
133                        xp = cx.alloc(4 * (4 * n_in * MAXT + n_in / 16 * MAXT));
134            ref::Rng r(3);
135            for (int i = 0; i < n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
136            cx.run({xprep_dispatch(pprep, x, xp, n_in, MAXT)});
137            const size_t plane = (size_t)4 * n_in * MAXT;
138            std::vector<float> wf((size_t)n_in * n_out);
139            ref::dequant(ty, (const uint8_t*)f.map + t.offset, wf.data(), wf.size());
140            Cfg c = n_in >= 6144 ? Cfg{32, 1, 8, 2} : n_in <= 640 ? Cfg{8, 1, 4, 1} : Cfg{16, 1, 4, 1};
141            if (!cfg_ok(c, ty, n_in)) c = Cfg{32, 1, 4, 1};
142            for (int nt : {1, 3}) {
143                mtl::Pipeline p = cx.pipeline(lib, kname(ty, c), mtl::Constants().i(0, nt));
144                GemvArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)n_in, (uint32_t)n_out};
145                mtl::Dispatch d;
146                d.p = &p;
147                d.args = {mtl::Arg::B(Wf, t.offset), mtl::Arg::B(xp, plane * xkind(ty)), mtl::Arg::B(xp, plane * 4),
148                          mtl::Arg::B(y), mtl::Arg::V(a)};
149                d.grid[0] = (n_out + rows_per_tg(c) - 1) / rows_per_tg(c);
150                d.tg[0] = c.S * 32;
151                cx.run({d});
152                double se = 0, sr = 0;
153                for (int tt = 0; tt < nt; ++tt)
154                    for (int o = 0; o < n_out; ++o) {
155                        double ref = 0;
156                        for (int k = 0; k < n_in; ++k) ref += (double)wf[(size_t)o * n_in + k] * x.as<float>()[tt * n_in + k];
157                        const double e = y.as<float>()[tt * n_out + o] - ref;
158                        se += e * e;
159                        sr += ref * ref;
160                    }
161                const double rel = std::sqrt(se / sr);
162                const bool ok = rel < 1e-5 && std::isfinite(rel);
163                fails += !ok;
164                std::printf("real %-7s %-34s %5d x %5d ntok %d: rel L2 %.2e  %s\n", ref::type_name(ty), t.name.c_str(),
165                            n_out, n_in, nt, rel, ok ? "PASS" : "FAIL");
166            }
167        }
168        std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
169        return fails ? 1 : 0;
170    }
171    if (!bench) {
172        int fails = 0;
173        for (int t : {ref::F32, ref::BF16, ref::Q8_0, ref::Q4_0, ref::Q5_0, ref::IQ4_NL, ref::IQ4_XS, ref::Q2_0, ref::IQ3_S,
174                      ref::Q3_K, ref::Q4_K, ref::Q5_K, ref::Q6_K}) {
175            for (int n_in : nins) {
176                if (n_in % ref::block_elems(t)) continue;
177                const int n_out = 100;   // not a multiple of the rows per threadgroup on purpose
178                const size_t rb = ref::row_bytes(t, n_in);
179                mtl::Buffer W = cx.alloc(rb * n_out), x = cx.alloc(4 * n_in * MAXT), y = cx.alloc(4 * n_out * MAXT);
180                mtl::Buffer xp = cx.alloc(4 * (4 * n_in * MAXT + n_in / 16 * MAXT));
181                ref::random_blocks(t, W.as<uint8_t>(), n_out, n_in, 31 + t + n_in);
182                ref::Rng r(7 + n_in);
183                for (int i = 0; i < n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
184                cx.run({xprep_dispatch(pprep, x, xp, n_in, MAXT)});
185                const size_t plane = (size_t)4 * n_in * MAXT;
186                std::vector<float> wf((size_t)n_in * n_out);
187                ref::dequant(t, W.ptr, wf.data(), wf.size());
188                for (int nt : {1, 3, 8}) {
189                    double worst = 0;
190                    int not_exact = 0;
191                    std::string worst_cfg;
192                    for (Cfg c : kCfgs) {
193                        if (!cfg_ok(c, t, n_in)) continue;
194                        mtl::Pipeline p = cx.pipeline(lib, kname(t, c), mtl::Constants().i(0, nt));
195                        GemvArgs a{(uint32_t)n_in, (uint32_t)n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)n_in,
196                                   (uint32_t)n_out};
197                        std::memset(y.ptr, 0, y.bytes);
198                        mtl::Dispatch d;
199                        d.p = &p;
200                        d.args = {mtl::Arg::B(W), mtl::Arg::B(xp, plane * xkind(t)), mtl::Arg::B(xp, plane * 4),
201                                  mtl::Arg::B(y), mtl::Arg::V(a)};
202                        d.grid[0] = (n_out + rows_per_tg(c) - 1) / rows_per_tg(c);
203                        d.tg[0] = c.S * 32;
204                        cx.run({d});
205                        double se = 0, sr = 0;
206                        for (int tt = 0; tt < nt; ++tt)
207                            for (int o = 0; o < n_out; ++o) {
208                                double ref = 0;
209                                for (int k = 0; k < n_in; ++k) ref += (double)wf[(size_t)o * n_in + k] * x.as<float>()[tt * n_in + k];
210                                const double e = y.as<float>()[tt * n_out + o] - ref;
211                                se += e * e;
212                                sr += ref * ref;
213                            }
214                        const double rel = std::sqrt(se / sr);
215                        if (rel > worst) { worst = rel; worst_cfg = kname(t, c); }
216                        if (nt > 1) {
217                            // every token of a window must be bitwise what a one-token call gives
218                            std::vector<float> ywin((float*)y.ptr, (float*)y.ptr + nt * n_out);
219                            mtl::Pipeline p1 = cx.pipeline(lib, kname(t, c), mtl::Constants().i(0, 1));
220                            GemvArgs a1 = a;
221                            a1.n_tok = 1;
222                            for (int tt = 0; tt < nt; ++tt) {
223                                mtl::Dispatch d1 = d;
224                                d1.p = &p1;
225                                d1.args = {mtl::Arg::B(W), mtl::Arg::B(xp, plane * xkind(t) + 4 * (size_t)tt * n_in),
226                                           mtl::Arg::B(xp, plane * 4 + 4 * (size_t)tt * (n_in / 16)), mtl::Arg::B(y),
227                                           mtl::Arg::V(a1)};
228                                cx.run({d1});
229                                if (std::memcmp(y.ptr, ywin.data() + tt * n_out, 4 * n_out) != 0) ++not_exact;
230                            }
231                        }
232                    }
233                    const bool ok = worst < 1e-5 && not_exact == 0;
234                    fails += !ok;
235                    std::printf("parity %-7s n_in %5d ntok %d: worst rel L2 over %zu tiles %.2e (%s), window == one "
236                                "token: %s  %s\n",
237                                ref::type_name(t), n_in, nt, kCfgs.size(), worst, worst_cfg.c_str(),
238                                nt == 1 ? "-" : not_exact ? "NO" : "bitwise", ok ? "PASS" : "FAIL");
239                }
240                cx.free(W);
241                cx.free(x);
242                cx.free(y);
243                cx.free(xp);
244            }
245        }
246        std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS");
247        return fails ? 1 : 0;
248    }
249
250    // ---- bench
251    std::vector<Shape> shapes = one;
252    if (shapes.empty()) {
253        if (pack == "Q2_0")
254            shapes = {
255                {"hc down (x4/layer)", ref::BF16, 10240, 320, 4 * 48}, {"hc up (x4/layer)", ref::BF16, 320, 10240, 4 * 48},
256                {"router", ref::BF16, 2560, 512, 48},                 {"gdn qkv", ref::IQ4_XS, 2560, 10240, 13},
257                {"gdn qkv", ref::Q3_K, 2560, 10240, 18},              {"gdn qkv", ref::Q4_K, 2560, 10240, 4},
258                {"gdn gate", ref::Q3_K, 2560, 6144, 25},              {"gdn out", ref::IQ4_XS, 6144, 2560, 17},
259                {"gdn out", ref::Q4_K, 6144, 2560, 11},               {"qsa q", ref::Q3_K, 2560, 12288, 6},
260                {"qsa out", ref::Q5_K, 6144, 2560, 4},                {"qsa k", ref::Q3_K, 2560, 512, 3},
261                {"shexp gate", ref::Q2_0, 2560, 640, 21},             {"shexp down", ref::Q4_0, 640, 2560, 16},
262                {"shexp down", ref::Q2_0, 640, 2560, 16},             {"indexer q", ref::BF16, 2560, 512, 12},
263                {"ssm alpha", ref::BF16, 2560, 48, 36},               {"lm head", ref::Q5_K, 2560, 248320, 1},
264                {"gdn qkv", ref::Q6_K, 2560, 10240, 1},
265            };
266        else
267            shapes = {
268                {"hc down (x4/layer)", ref::BF16, 10240, 320, 4 * 48}, {"hc up (x4/layer)", ref::BF16, 320, 10240, 4 * 48},
269                {"gdn qkv", ref::IQ4_XS, 2560, 10240, 11},            {"gdn qkv", ref::Q6_K, 2560, 10240, 14},
270                {"gdn qkv", ref::Q4_K, 2560, 10240, 8},               {"gdn out", ref::Q6_K, 6144, 2560, 19},
271                {"gdn gate", ref::IQ3_S, 2560, 6144, 10},             {"gdn gate", ref::IQ4_XS, 2560, 6144, 11},
272                {"qsa out", ref::Q6_K, 6144, 2560, 10},               {"qsa q", ref::IQ4_XS, 2560, 12288, 4},
273                {"shexp down", ref::IQ4_NL, 640, 2560, 40},           {"shexp up", ref::IQ3_S, 2560, 640, 27},
274                {"lm head", ref::Q5_K, 2560, 248320, 1},
275            };
276    }
277    std::vector<int> ntoks = {ntok};
278    std::printf("%-20s %-7s %6s %7s %5s %9s %9s %8s  %s\n", "shape", "type", "n_in", "n_out", "ntok", "us", "GB/s",
279                "MB", "best tile (L,R,S,K)");
280    for (const Shape& s : shapes) {
281        const size_t rb = ref::row_bytes(s.type, s.n_in), mb = rb * s.n_out;
282        const int copies = (int)std::max<size_t>(4, std::min<size_t>(256, (1ull << 30) / mb));
283        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);
284        {
285            std::vector<uint8_t> tmp(mb);
286            ref::random_blocks(s.type, tmp.data(), s.n_out, s.n_in, 99);
287            for (int c = 0; c < copies; ++c) std::memcpy(W.as<uint8_t>() + (size_t)c * mb, tmp.data(), mb);
288        }
289        ref::Rng r(5);
290        for (int i = 0; i < s.n_in * MAXT; ++i) x.as<float>()[i] = r.sym();
291        mtl::Buffer xp = cx.alloc(4 * (4 * s.n_in * MAXT + s.n_in / 16 * MAXT));
292        cx.run({xprep_dispatch(pprep, x, xp, s.n_in, MAXT)});
293        const size_t plane = (size_t)4 * s.n_in * MAXT;
294        // GEMV_F16=1: gemv16_* over the format's prepared plane converted to half; each tile's rel L2 against the fp32 gemv
295        const bool f16 = std::getenv("GEMV_F16") != nullptr;
296        mtl::Buffer xh, yr;
297        if (f16) { xh = cx.alloc(2 * (size_t)s.n_in * MAXT); yr = cx.alloc(4 * (size_t)s.n_out * MAXT); }
298        if (f16)   // the half plane from the raw x: the K-quants 2^-8 at odd positions, Q2_0 4^-(i % 4), the rest raw
299            for (size_t i = 0; i < (size_t)s.n_in * MAXT; ++i) {
300                const size_t k = i % (size_t)s.n_in;
301                const bool p2 = s.type == ref::Q3_K || s.type == ref::Q4_K || s.type == ref::Q5_K || s.type == ref::Q6_K;
302                const float f = p2 ? ((k & 1) ? 1.0f / 256 : 1.0f) : s.type == ref::Q2_0 ? 1.0f / float(1 << (2 * (k & 3))) : 1.0f;
303                xh.as<_Float16>()[i] = (_Float16)(xp.as<float>()[i] * f);
304            }
305        for (int nt : ntoks) {
306            if (f16) {   // the fp32 reference of copy 0 with the default tile
307                mtl::Pipeline pr = cx.pipeline(lib, kname(s.type, Cfg{16, 1, 4, 1}), mtl::Constants().i(0, nt).i(9, 0));
308                GemvArgs a{(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};
309                mtl::Dispatch d;
310                d.p = &pr;
311                d.args = {mtl::Arg::B(W), mtl::Arg::B(xp, plane * xkind(s.type)), mtl::Arg::B(xp, plane * 4), mtl::Arg::B(yr), mtl::Arg::V(a)};
312                d.grid[0] = (s.n_out + rows_per_tg(Cfg{16, 1, 4, 1}) - 1) / rows_per_tg(Cfg{16, 1, 4, 1});
313                d.tg[0] = 128;
314                cx.run({d});
315            }
316            double best = 1e9;
317            Cfg bc{};
318            const bool gx = std::getenv("GEMV_X") != nullptr;   // gemvx_* (x staged in threadgroup memory)
319            const uint32_t tgm = (uint32_t)(4 * nt * (s.n_in + s.n_in / 16) + 15) / 16 * 16;
320            for (Cfg c : gx ? std::vector<Cfg>{{32, 1, 4, 1}, {32, 2, 4, 1}, {16, 1, 4, 1}, {16, 2, 4, 1}, {8, 1, 4, 1},
321                                               {8, 2, 4, 1}, {4, 2, 4, 1}, {16, 2, 8, 1}, {8, 2, 8, 1}, {16, 4, 4, 1},
322                                               {32, 2, 8, 1}, {2, 2, 4, 1}}
323                            : kCfgs) {
324                if (!cfg_ok(c, s.type, s.n_in)) continue;
325                if (!all && !(c.K == 1 || s.n_in >= 6144)) continue;
326                if (gx && tgm > 32768) continue;
327                if (f16 && !(c.K == 1 || (c.L == 32 && c.S == 8 && c.K == 2) || (c.L == 16 && c.S == 8 && c.K == 4))) continue;
328                if (f16 && ((c.L == 32 && c.R == 1 && c.S == 4 && c.K == 4) || (c.L == 1 && c.R == 2) || (c.L == 1 && c.S == 2) ||
329                            (c.L == 4 && c.R == 4) || (c.L == 8 && c.S == 8))) continue;
330                const std::string kn = gx ? "gemvx_" + std::to_string(s.type) + "_" + std::to_string(c.L) + "_" + std::to_string(c.R) +
331                                                 "_" + std::to_string(c.S)
332                                          : f16 ? "gemv16" + kname(s.type, c).substr(4) : kname(s.type, c);
333                mtl::Pipeline p = cx.pipeline(lib, kn, mtl::Constants().i(0, nt).i(9, std::getenv("GEMV_PROBE") ? std::atoi(std::getenv("GEMV_PROBE")) : 0));
334                GemvArgs a{(uint32_t)s.n_in, (uint32_t)s.n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)s.n_in,
335                           (uint32_t)s.n_out};
336                std::vector<mtl::Dispatch> ds;
337                const int R = std::max(copies, 16);
338                for (int k = 0; k < R; ++k) {
339                    mtl::Dispatch d;
340                    d.p = &p;
341                    d.args = {mtl::Arg::B(W, (size_t)(k % copies) * mb), f16 ? mtl::Arg::B(xh) : mtl::Arg::B(xp, plane * xkind(s.type)),
342                              mtl::Arg::B(xp, plane * 4), mtl::Arg::B(y), mtl::Arg::V(a)};
343                    d.grid[0] = (s.n_out + rows_per_tg(c) - 1) / rows_per_tg(c);
344                    d.tg[0] = c.S * 32;
345                    if (gx) d.tg_mem = tgm;
346                    ds.push_back(d);
347                }
348                cx.warm(ds, 0.05);
349                std::vector<double> t;
350                for (int i = 0; i < 5; ++i) t.push_back(cx.run(ds) / R);
351                const double m = median(t);
352                double rel = 0;
353                if (f16) {   // ds[0] reads copy 0: its output against the fp32 reference
354                    cx.run({ds[0]});
355                    double se = 0, sr = 0;
356                    for (size_t i = 0; i < (size_t)s.n_out * nt; ++i) {
357                        const double g = y.as<float>()[i], r0 = yr.as<float>()[i];
358                        se += (g - r0) * (g - r0);
359                        sr += r0 * r0;
360                    }
361                    rel = std::sqrt(se / sr);
362                    worst16 = std::max(worst16, rel);
363                }
364                if (one.size()) std::printf("   tile %2d,%d,%d,%d: %8.2f us  %6.1f GB/s%s\n", c.L, c.R, c.S, c.K, m * 1e6, mb / m / 1e9,
365                                            f16 ? (" rel L2 vs fp32 " + std::to_string(rel)).c_str() : "");
366                if (m < best) { best = m; bc = c; }
367            }
368            std::printf("%-20s %-7s %6d %7d %5d %9.2f %9.1f %8.2f  %d,%d,%d,%d\n", s.name, ref::type_name(s.type), s.n_in,
369                        s.n_out, nt, best * 1e6, mb / best / 1e9, mb / 1e6, bc.L, bc.R, bc.S, bc.K);
370        }
371        cx.free(W);
372        cx.free(x);
373        cx.free(y);
374        cx.free(xp);
375        cx.free(xh);
376        cx.free(yr);
377    }
378    if (worst16 >= 0) {   // GEMV_F16: half products of 8, float scales - ~1e-3 of the fp32 output
379        std::printf("fp16: worst rel L2 vs fp32 %.2e  %s\n", worst16, worst16 < 4e-3 ? "PASS" : "FAIL");
380        return worst16 < 4e-3 ? 0 : 1;
381    }
382    return 0;
383}