okami.mom/strata

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

Andy Slatersycl/A770: fused GR read with more, smaller work-groups on Arc A-seriesd4b48b338

main
3.3 KiB59 linesraw
1// sycl/src/kernels/gr_bench.cpp - SYCL port: time fused_gr_read_multi (norm + down + up) for 1-6 tokens on random
2// weights at the Coder's shapes; prints a checksum of every output so two builds/switches can be compared bitwise.
3// Both GR decompositions: STRATA_GR_MORE_GROUPS=0 / =1 (default: on for Arc A-series only); STRATA_GR_LARGE_GRF the same.
4#include "strata/kernels/fused_gr.hpp"
5#include <sycl/sycl.hpp>
6#include <dpct/dpct.hpp>
7#include <chrono>
8#include <cstdio>
9#include <cstring>
10#include <vector>
11int main() {
12    using namespace strata::kernels;
13    sycl::queue* s = &dpct::get_in_order_queue();
14    constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, TM = kFusedGrMaxT;
15    auto bf16 = [](float f) { uint32_t u; std::memcpy(&u, &f, 4); return (uint16_t) (u >> 16); };
16    std::vector<uint16_t> wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D);
17    uint32_t r = 12345;
18    auto rnd = [&] { r = r * 1664525u + 1013904223u; return ((r >> 8) & 0xffff) / 65536.0f - 0.5f; };
19    for (auto& v : wd) v = bf16(rnd() * 0.05f);
20    for (auto& v : wu) v = bf16(rnd() * 0.05f);
21    for (auto& v : wi) v = bf16(rnd() * 0.05f);
22    std::vector<float> hR((size_t) TM * D), hn(D);
23    for (auto& v : hR) v = rnd();
24    for (auto& v : hn) v = 1.0f + 0.1f * rnd();
25    auto dev = [&](const void* h, size_t b) { void* p = sycl::malloc_device(b, *s); s->memcpy(p, h, b).wait(); return p; };
26    auto* d_wd = (uint16_t*) dev(wd.data(), wd.size() * 2);
27    auto* d_wu = (uint16_t*) dev(wu.data(), wu.size() * 2);
28    auto* d_wi = (uint16_t*) dev(wi.data(), wi.size() * 2);
29    auto* d_R = (float*) dev(hR.data(), hR.size() * 4);
30    auto* d_n = (float*) dev(hn.data(), hn.size() * 4);
31    float* xn = sycl::malloc_device<float>((size_t) TM * D, *s);
32    float* out = sycl::malloc_device<float>((size_t) TM * (LR + HC + HC + N), *s);
33    FusedGrArgs a[TM];
34    for (int t = 0; t < TM; ++t) {
35        float* o = out + (size_t) t * (LR + HC + HC + N);
36        a[t].R = d_R + (size_t) t * D; a[t].w_norm = d_n; a[t].w_down = d_wd; a[t].w_up = d_wu; a[t].w_inject = d_wi;
37        a[t].lo = o; a[t].rs = o + LR; a[t].inject_out = o + LR + HC; a[t].mixed = o + LR + 2 * HC;
38    }
39    for (int T = 1; T <= 6; ++T) {
40        const auto w0 = std::chrono::steady_clock::now();
41        while (std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - w0).count() < 150) {
42            for (int i = 0; i < 20; ++i) fused_gr_read_multi(a, T, xn, s);
43            s->wait();
44        }
45        const int it = 500;
46        const auto t0 = std::chrono::steady_clock::now();
47        for (int i = 0; i < it; ++i) fused_gr_read_multi(a, T, xn, s);
48        s->wait();
49        const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / it;
50        std::vector<float> h((size_t) T * (LR + HC + HC + N));
51        s->memcpy(h.data(), out, h.size() * 4).wait();
52        uint64_t cs = 1469598103934665603ull;
53        double sum = 0;
54        for (float v : h) { uint32_t u; std::memcpy(&u, &v, 4); cs = (cs ^ u) * 1099511628211ull; sum += v; }
55        std::printf("fused GR read, %d tokens: %7.1f us (%.0f GB/s of weights)  checksum %016llx  sum %.9e\n", T, us,
56                    (wd.size() + wu.size() + wi.size()) * 2.0 / us / 1e3, (unsigned long long) cs, sum);
57    }
58    return 0;
59}