okami.mom/strata

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

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
6.3 KiB98 linesraw
1// sycl/src/kernels/mmvq_bench.cpp - SYCL port: time the dense native_mmvq kernels on random weights.
2//   mmvq_bench [n_in] [n_out] [ncols]      (weights are random bytes: dequant cost is data-independent)
3#include "strata/kernels/native_mmvq.hpp"
4#include <sycl/sycl.hpp>
5#include <dpct/dpct.hpp>
6#include <algorithm>
7#include <chrono>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <vector>
12#include <cmath>
13int main(int argc, char** argv) {
14    const int n_in = argc > 1 ? std::atoi(argv[1]) : 2560, n_out = argc > 2 ? std::atoi(argv[2]) : 2560, ncols = argc > 3 ? std::atoi(argv[3]) : 4;
15    sycl::queue* s = &dpct::get_in_order_queue();
16    // Q6_K: 210 bytes per 256 weights
17    const size_t wbytes = (size_t) n_out * (n_in / 256) * 210;
18    std::vector<uint8_t> hw(wbytes);
19    for (size_t i = 0; i < wbytes; ++i) hw[i] = (uint8_t) (i * 2654435761u >> 13);
20    for (size_t b = 0; b < wbytes / 210; ++b) { hw[b * 210 + 208] = 0x00; hw[b * 210 + 209] = 0x3c; }   // d = 1.0 (finite)
21    std::vector<float> hx((size_t) ncols * n_in);
22    for (size_t i = 0; i < hx.size(); ++i) hx[i] = (float) ((i * 7919) % 1000) / 500.f - 1.f;
23    void* w = sycl::malloc_device(wbytes, *s);
24    float* x = sycl::malloc_device<float>(hx.size(), *s);
25    void* xq = sycl::malloc_device((size_t) ncols * (n_in / 32) * 36, *s);
26    float* y = sycl::malloc_device<float>((size_t) ncols * n_out, *s);
27    s->memcpy(w, hw.data(), wbytes).wait(); s->memcpy(x, hx.data(), hx.size() * 4).wait();
28    strata::kernels::native_quantize_q8_1(x, xq, n_in, ncols, s);
29    {   // correctness: the 16-byte kernel against the shared one
30        std::vector<float> y0((size_t) ncols * n_out), y1(y0.size());
31        strata::kernels::native_mmvq_set_q6k_wide(false);
32        strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s); s->wait(); s->memcpy(y0.data(), y, y0.size() * 4).wait();
33        strata::kernels::native_mmvq_set_q6k_wide(true);
34        strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s); s->wait(); s->memcpy(y1.data(), y, y1.size() * 4).wait();
35        double num = 0, den = 0; for (size_t i = 0; i < y0.size(); ++i) { num += std::fabs((double) y0[i] - y1[i]); den += std::fabs((double) y0[i]); }
36        std::printf("wide vs shared: rel %.3e over %zu outputs (first %g vs %g)\n", num / (den > 0 ? den : 1), y0.size(), y0[0], y1[0]);
37        strata::kernels::native_mmvq_set_q6k_wide(std::getenv("STRATA_MMVQ_WIDE") == nullptr || std::atoi(std::getenv("STRATA_MMVQ_WIDE")) != 0);
38    }
39    {   // warm up until the clocks are up (~300 ms of work), then time
40        const auto w0 = std::chrono::steady_clock::now();
41        while (std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - w0).count() < 300) {
42            for (int i = 0; i < 20; ++i) strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s);
43            s->wait();
44        }
45    }
46    const int it = 400;
47    const auto t0 = std::chrono::steady_clock::now();
48    for (int i = 0; i < it; ++i) strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s);
49    s->wait();
50    const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / it;
51    std::vector<float> hy((size_t) ncols * n_out);
52    s->memcpy(hy.data(), y, hy.size() * 4).wait();
53    unsigned long long cs = 1469598103934665603ull;
54    for (float v : hy) { unsigned u; std::memcpy(&u, &v, 4); cs = (cs ^ u) * 1099511628211ull; }
55    std::printf("q6_k mmvq %d x %d, %d cols: %.1f us  (%.1f GB/s of weights)  checksum %016llx\n", n_in, n_out, ncols, us, wbytes / us / 1e3, cs);
56    // STRATA_BENCH_PLANAR=1 (#2340): the same matrix as STRATA_Q6_PLANAR planes, bitwise against the GGUF-layout result above; then a
57    // gathered row subset (the MTP draft head's case) against the same rows of the full result. Exit 1 on any difference.
58    const char* pe = std::getenv("STRATA_BENCH_PLANAR");
59    if (pe && std::atoi(pe) != 0) {
60        void* w2 = sycl::malloc_device(wbytes, *s);
61        s->memcpy(w2, hw.data(), wbytes).wait();
62        void* wp = strata::kernels::native_q6_k_planar_adopt(w2, n_in, n_out, "bench matrix");
63        if (!strata::kernels::native_q6_k_is_planar(wp)) { std::printf("planar: adopt declined\n"); return 1; }
64        float* y2 = sycl::malloc_device<float>((size_t) ncols * n_out, *s);
65        strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s); s->wait();
66        std::vector<float> hp((size_t) ncols * n_out);
67        s->memcpy(hp.data(), y2, hp.size() * 4).wait();
68        const bool same = std::memcmp(hp.data(), hy.data(), hp.size() * 4) == 0;
69        for (int i = 0; i < 20; ++i) strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s);
70        s->wait();
71        const auto p0 = std::chrono::steady_clock::now();
72        for (int i = 0; i < it; ++i) strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s);
73        s->wait();
74        const double pus = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - p0).count() / it;
75        std::printf("q6_k planar %d x %d, %d cols: %.1f us  (%.1f GB/s of weights)  bitwise %s\n", n_in, n_out, ncols, pus, wbytes / pus / 1e3,
76                    same ? "EQUAL" : "DIFFERENT");
77        int rc = same ? 0 : 1;
78        const int nsel = std::max(1, n_out / 3);
79        std::vector<int32_t> rows(nsel);
80        for (int i = 0; i < nsel; ++i) rows[i] = (int32_t) (((uint64_t) i * 2654435761ull + 12345) % (uint64_t) n_out);
81        int32_t* rd = sycl::malloc_device<int32_t>(nsel, *s);
82        s->memcpy(rd, rows.data(), nsel * 4).wait();
83        void* wg = strata::kernels::native_q6_k_planar_gather(wp, n_in, rd, nsel, "bench subset");
84        if (!wg) { std::printf("planar gather: declined\n"); return 1; }
85        float* y3 = sycl::malloc_device<float>((size_t) ncols * nsel, *s);
86        strata::kernels::native_q6_k_mmvq(wg, xq, y3, n_in, nsel, ncols, s); s->wait();
87        std::vector<float> hg((size_t) ncols * nsel);
88        s->memcpy(hg.data(), y3, hg.size() * 4).wait();
89        size_t bad = 0;
90        for (int c = 0; c < ncols; ++c)
91            for (int i = 0; i < nsel; ++i)
92                if (std::memcmp(&hg[(size_t) c * nsel + i], &hy[(size_t) c * n_out + rows[i]], 4) != 0) ++bad;
93        std::printf("q6_k planar gather %d of %d rows, %d cols: %zu of %zu outputs differ\n", nsel, n_out, ncols, bad, hg.size());
94        if (bad) rc = 1;
95        return rc;
96    }
97    return 0;
98}