okami.mom/strata

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

Niko1221Merge f-143-intel: B70 prompt and decode speed (copy engine, wide kv_append, no GEMM staging on bmg, lend mirror, fused dequant, lean verify graph, the xe expert ranking); same output tokens, B70 4K prompt 8.5 -> 3.3 s (128K config), decode +14-21% on prompts outside the ranking's corpus2eba71548

main
6.3 KiB124 linesraw
1// xmx_group_parity: iq_xmx_grouped (the A770 port's fused decode + grouped XMX) against the engine's dequant +
2// oneMKL per expert, on random expert blobs of each format the native packs use.  Also times both.
3//   xmx_group_parity [mean_rows=160] [n=16]
4#include "strata/kernels/iq_kernels.hpp"
5#include "strata/sycl_queue.hpp"
6#include <sycl/sycl.hpp>
7#include <dpct/dpct.hpp>
8#include <dpct/blas_utils.hpp>
9#include <chrono>
10#include <cmath>
11#include <cstdio>
12#include <cstdlib>
13#include <random>
14#include <vector>
15static uint16_t f2h(float f) { return sycl::bit_cast<uint16_t>(sycl::half(f)); }
16int main(int argc, char** argv) {
17    const int mean = argc > 1 ? std::atoi(argv[1]) : 160;
18    const int n = argc > 2 ? std::atoi(argv[2]) : 16;
19    sycl::queue* q = &dpct::get_in_order_queue();
20#ifdef STRATA_NO_DG2_XMX
21    std::printf("xmx_group_parity: SKIP (the sub-group 8 / 8x8x16 kernels are not built for this AOT device)");
22    return 0;
23#else
24#ifndef STRATA_XMX_SG16
25    if (strata::intel_gpu_gen(q->get_device()) == strata::IntelGpuGen::Battlemage) {
26        std::printf("xmx_group_parity: SKIP (this build has the sub-group 8 / 8x8x16 kernels; Battlemage needs the AOT bmg build)");
27        return 0;
28    }
29#endif
30#endif
31    dpct::blas::descriptor_ptr hd = new dpct::blas::descriptor();
32    hd->set_queue(q);
33    std::mt19937 rng(3);
34    std::vector<int32_t> cnt(n);
35    std::gamma_distribution<double> gd(1.5, mean / 1.5);
36    int64_t tot = 0;
37    for (auto& c : cnt) { c = std::max(1, (int) gd(rng)); tot += c; }
38    int fails = 0;
39    struct Case { int ty; bool gu; };
40    // the formats of the native packs' experts that the prompt path's fused XMX takes (prefill.cpp xmx2_fmt)
41    for (Case cs : {Case{18, true}, Case{21, true}, Case{22, true}, Case{23, true}, Case{20, false}, Case{42, false}}) {
42        const int64_t K = cs.gu ? 2560 : 640, N = cs.gu ? 1280 : 2560, n_ff = 640;
43        const int blk_elems = cs.ty == 20 ? 32 : cs.ty == 42 ? 64 : 256;
44        const size_t blk_bytes = strata::kernels::iq_row_bytes(cs.ty, blk_elems);
45        const size_t mat_bytes = strata::kernels::iq_row_bytes(cs.ty, K) * (cs.gu ? n_ff : N);
46        if (blk_bytes == 0 || mat_bytes == 0) { std::printf("type %d: not supported\n", cs.ty); continue; }
47        auto blob = [&]() {
48            std::vector<uint8_t> b(mat_bytes);
49            for (auto& x : b) x = (uint8_t) rng();
50            if (cs.ty != 29)   // d = 0.0625 at each block's start (IQ1_M keeps its scale in the scales field)
51                for (size_t o = 0; o + 1 < b.size(); o += blk_bytes) { b[o] = 0x00; b[o + 1] = 0x2C; }
52            return b;
53        };
54        std::vector<uint8_t*> dg(n), du(n);
55        std::vector<const uint8_t*> pg(n), pu(n);
56        for (int i = 0; i < n; ++i) {
57            auto g = blob();
58            dg[i] = sycl::malloc_device<uint8_t>(mat_bytes, *q);
59            q->memcpy(dg[i], g.data(), mat_bytes);
60            pg[i] = dg[i];
61            if (cs.gu) {
62                auto u = blob();
63                du[i] = sycl::malloc_device<uint8_t>(mat_bytes, *q);
64                q->memcpy(du[i], u.data(), mat_bytes);
65                pu[i] = du[i];
66            }
67        }
68        std::vector<uint16_t> hx((size_t) tot * K);
69        std::normal_distribution<float> nd(0.f, 1.f);
70        for (auto& x : hx) x = f2h(nd(rng) * 0.25f);
71        uint16_t* dx = sycl::malloc_device<uint16_t>(hx.size(), *q);
72        q->memcpy(dx, hx.data(), hx.size() * 2);
73        uint16_t* dw = sycl::malloc_device<uint16_t>((size_t) N * K, *q);
74        float* yr = sycl::malloc_device<float>((size_t) tot * N, *q);
75        float* yx = sycl::malloc_device<float>((size_t) tot * N, *q);
76        q->wait();
77        auto ref = [&]() {
78            int64_t o = 0;
79            for (int i = 0; i < n; ++i) {
80                if (cs.gu) strata::kernels::iq_dequant_gu_f16(cs.ty, dg[i], du[i], n_ff, K, dw, q);
81                else strata::kernels::iq_dequant_f16(cs.ty, dg[i], N * K, dw, q);
82                const float a = 1.f, b = 0.f;
83                dpct::blas::gemm(hd, oneapi::mkl::transpose::trans, oneapi::mkl::transpose::nontrans, (int) N, cnt[i],
84                                 (int) K, &a, dw, dpct::library_data_t::real_half, (int) K, dx + o * K,
85                                 dpct::library_data_t::real_half, (int) K, &b, yr + o * N,
86                                 dpct::library_data_t::real_float, (int) N, dpct::compute_type::f32);
87                o += cnt[i];
88            }
89        };
90        auto mine = [&]() {
91            strata::kernels::iq_xmx_grouped(cs.ty, pg.data(), cs.gu ? pu.data() : nullptr, cnt.data(), n, dx, yx,
92                                            (int) N, (int) K, q);
93        };
94        auto tm = [&](auto f) {
95            f(); q->wait();
96            const auto a = std::chrono::steady_clock::now();
97            for (int r = 0; r < 10; ++r) f();
98            q->wait();
99            return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - a).count() / 10;
100        };
101        const double t_ref = tm(ref), t_x = tm(mine);
102        std::vector<float> a((size_t) tot * N), b((size_t) tot * N);
103        q->memcpy(a.data(), yr, a.size() * 4);
104        q->memcpy(b.data(), yx, b.size() * 4).wait();
105        double e = 0, mx = 0;
106        size_t bad = 0, bad_ref = 0, mism = 0;
107        for (size_t i = 0; i < a.size(); ++i) {
108            if (!std::isfinite(a[i])) { ++bad_ref; if (std::isfinite(b[i])) ++mism; continue; }
109            if (!std::isfinite(b[i])) { ++bad; continue; }
110            e = std::max(e, (double) std::fabs(a[i] - b[i]));
111            mx = std::max(mx, (double) std::fabs(a[i]));
112        }
113        if (bad_ref) std::printf("  (reference non-finite %zu of %zu, fused finite there %zu)\n", bad_ref, a.size(), mism);
114        const bool ok = bad == 0 && mism == 0 && mx > 0 && e <= 2e-3 * mx;
115        fails += !ok;
116        std::printf("type %2d %-7s rows %5lld: dequant+oneMKL %7.3f ms | fused xmx %7.3f ms (%.2fx)  relerr %.1e  %s\n",
117                    cs.ty, cs.gu ? "gate/up" : "down", (long long) tot, t_ref, t_x, t_ref / t_x, e / (mx > 0 ? mx : 1),
118                    ok ? "OK" : "FAIL");
119        for (int i = 0; i < n; ++i) { sycl::free(dg[i], *q); if (cs.gu) sycl::free(du[i], *q); }
120        sycl::free(dx, *q); sycl::free(dw, *q); sycl::free(yr, *q); sycl::free(yx, *q);
121    }
122    std::printf("%s\n", fails ? "FAILED" : "all OK");
123    return fails ? 1 : 0;
124}