okami.mom/strata

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

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
3.3 KiB59 linesraw
1// sycl/src/kernels/q6k_align_bench.cpp - SYCL port: does Q6_K's 210-byte (2-byte aligned) block stride cost the wide
2// mmvq kernel its bandwidth? Same weights at stride 210 and repacked at 224 (16-byte aligned); outputs must match.
3#include "strata/kernels/native_mmvq.hpp"
4#include <sycl/sycl.hpp>
5#include <dpct/dpct.hpp>
6#include <chrono>
7#include <cmath>
8#include <cstdio>
9#include <cstring>
10#include <functional>
11#include <vector>
12int main() {
13    sycl::queue* s = &dpct::get_in_order_queue();
14    const int shapes[][2] = {{2560, 10240}, {2560, 12288}, {6144, 2560}, {2560, 248320}};
15    for (auto& sh : shapes) {
16        const int n_in = sh[0], n_out = sh[1];
17        const size_t nb = (size_t) n_out * (n_in / 256);
18        std::vector<uint8_t> hw(nb * 210), hp(nb * 224, 0);
19        for (size_t i = 0; i < hw.size(); ++i) hw[i] = (uint8_t) (i * 2654435761u >> 13) & 0x3b;
20        for (size_t b = 0; b < nb; ++b) std::memcpy(&hp[b * 224], &hw[b * 210], 210);
21        void* w = sycl::malloc_device(hw.size(), *s);
22        void* wp = sycl::malloc_device(hp.size(), *s);
23        s->memcpy(w, hw.data(), hw.size()).wait(); s->memcpy(wp, hp.data(), hp.size()).wait();
24        for (int nc : {1, 2, 4, 6}) {
25            std::vector<float> hx((size_t) nc * n_in);
26            for (size_t i = 0; i < hx.size(); ++i) hx[i] = (float) ((i * 7919) % 1000) / 500.f - 1.f;
27            float* x = sycl::malloc_device<float>(hx.size(), *s);
28            void* xq = sycl::malloc_device((size_t) nc * (n_in / 32) * 36, *s);
29            float* y = sycl::malloc_device<float>((size_t) nc * n_out, *s);
30            s->memcpy(x, hx.data(), hx.size() * 4).wait();
31            strata::kernels::native_quantize_q8_1(x, xq, n_in, nc, s);
32            auto time = [&](const std::function<void()>& run, std::vector<float>& out) {
33                const auto w0 = std::chrono::steady_clock::now();
34                while (std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - w0).count() < 150) {
35                    for (int i = 0; i < 10; ++i) run();
36                    s->wait();
37                }
38                const int it = 200;
39                const auto t0 = std::chrono::steady_clock::now();
40                for (int i = 0; i < it; ++i) run();
41                s->wait();
42                out.resize((size_t) nc * n_out);
43                s->memcpy(out.data(), y, out.size() * 4).wait();
44                return std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / it;
45            };
46            std::vector<float> y0, y1;
47            const double a = time([&] { strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, nc, s); }, y0);
48            const double b = time([&] { strata::kernels::native_q6_k_mmvq_stride224(wp, xq, y, n_in, n_out, nc, s); }, y1);
49            double num = 0, den = 0;
50            for (size_t i = 0; i < y0.size(); ++i) { num += std::fabs((double) y0[i] - y1[i]); den += std::fabs((double) y0[i]); }
51            const double gb = nb * 210.0 / 1e3;
52            std::printf("q6_k %5d x %6d cols %d: stride 210 %7.1f us %6.1f GB/s | stride 224 %7.1f us %6.1f GB/s (%.2fx) | rel diff %.1e\n",
53                        n_in, n_out, nc, a, gb / a, b, gb / b, a / b, num / (den > 0 ? den : 1));
54            sycl::free(x, *s); sycl::free(xq, *s); sycl::free(y, *s);
55        }
56        sycl::free(w, *s); sycl::free(wp, *s);
57    }
58    return 0;
59}