okami.mom/strata

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

Niko1221rc1 merge df2-sycl (903badd90): window_rule.hpp/window_rule_test.cpp add/add (kept the mainline draft_on rule, added SYCL's lookup-skip helpers), drafting.py CONF_STEPS (kept the engine-slot steps, added BACKEND_CONF sycl 0.6), SYCL generate.cpp sets wr.draft_onb5abd7792

main
57.0 KiB902 linesraw
1// sycl/src/kernels/dflash2_bench.cpp - DF2-SYCL: what one DFlash2 drafter round costs on an Intel Arc (the Q4_0 pack's real shapes), and checks of the SYCL kernels.
2//   dflash2_bench [test|bench]     (default bench)
3//   env DF2B_POS (ring position, default 600), DF2B_ITERS (default 100), DF2B_NNEW (context rows, default 3), DF2B_ND (drafts, 1..7, default 7),
4//       DF2B_STANDIN_MB (a memory-bound kernel run before each timed round, default 0), DF2B_GAPMS (host gap between rounds, default 0),
5//       DF2B_PROFILE=1 (a marker after every launch: ms per kernel; direct launches only), DF2B_GRAPH=0 (skip the sycl graph replay)
6// One round = the context update for n_new rows (fc 12800 -> 2560, per layer K/V projections, norm + rope, ring writes) + the 8-row block (5 layers: dynamic
7// conv projections, q/k/v, norm + rope, windowed attention over the ring, o, conv + rms, gate/up, swiglu, down) + the head over the draft vocabulary
8// (nd rows, Q8_0 rows through the engine's native_mmvq) + top-16 + the candidate selector.  The weights are random bytes in the pack's real sizes
9// (tools/dflash2_pack.py --q4_0: 268 MB of Q4_0 matrices + 254 MB of bf16 selector codebooks); the embedding is a small table gather.
10#include "strata/kernels/dflash2.hpp"
11#include "strata/kernels/native_mmvq.hpp"
12#include "strata/sycl_queue.hpp"
13
14#include <sycl/sycl.hpp>
15#include <dpct/dpct.hpp>
16
17#include <algorithm>
18#include <chrono>
19#include <cmath>
20#include <cstdint>
21#include <cstdio>
22#include <cstdlib>
23#include <cstring>
24#include <string>
25#include <thread>
26#include <vector>
27
28using namespace strata::kernels;
29namespace sx = sycl::ext::oneapi::experimental;
30
31namespace {
32constexpr int N = 2560, L = 5, I = 7680, NH = 32, NKV = 8, HD = 128, B = 8, RING = 2064, WINDOW = 2048, NDV = 106299, RANK = 256, VOCAB = 248320, GROUP = 16;
33constexpr float EPS = 1e-6f;
34constexpr int GGML_Q8_0 = 8;
35
36uint32_t rs = 12345;
37uint32_t rnd() { rs = rs * 1664525u + 1013904223u; return rs >> 8; }
38float frand() { return (float) (rnd() & 0xffff) / 65536.0f - 0.5f; }
39uint16_t f2h(float f) {
40    uint32_t x; std::memcpy(&x, &f, 4);
41    const uint32_t s = (x >> 16) & 0x8000, e = ((x >> 23) & 0xff), m = x & 0x7fffff;
42    if (e < 103) return (uint16_t) s;
43    if (e > 142) return (uint16_t) (s | 0x7c00);
44    if (e < 113) { const uint32_t mm = (m | 0x800000) >> (126 - e); return (uint16_t) (s | (mm >> 13)); }
45    return (uint16_t) (s | ((e - 112) << 10) | (m >> 13));
46}
47float h2f(uint16_t h) {
48    const uint32_t s = (h & 0x8000u) << 16, e = (h >> 10) & 31, m = h & 1023;
49    uint32_t x;
50    if (e == 0) { if (m == 0) x = s; else { float f = m / 1024.0f / 16384.0f; std::memcpy(&x, &f, 4); x |= s; } }
51    else x = s | ((e + 112) << 23) | (m << 13);
52    float f; std::memcpy(&f, &x, 4); return f;
53}
54double med(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
55double pct(std::vector<double> v, double p) { std::sort(v.begin(), v.end()); return v[std::min(v.size() - 1, (size_t) (p * v.size()))]; }
56int envi(const char* k, int d) { const char* e = std::getenv(k); return e && e[0] ? std::atoi(e) : d; }
57double envd(const char* k, double d) { const char* e = std::getenv(k); return e && e[0] ? std::atof(e) : d; }
58using Clock = std::chrono::steady_clock;
59double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
60
61sycl::queue* Q = nullptr;
62template <class T> T* dev(size_t n) { T* p = sycl::malloc_device<T>(n, *Q); if (!p) { std::fprintf(stderr, "out of device memory (%zu bytes)\n", n * sizeof(T)); std::exit(2); } return p; }
63template <class T> void up(T* d, const std::vector<T>& h) { Q->memcpy(d, h.data(), h.size() * sizeof(T)).wait(); }
64template <class T> std::vector<T> down(const T* d, size_t n) { std::vector<T> h(n); Q->memcpy(h.data(), d, n * sizeof(T)).wait(); return h; }
65
66// A Q4_0 matrix in the tiled layout: n_out * n_in / 2 random bytes (any byte is two valid nibbles) then the fp16 scale plane (n_out * n_in / 32 random small halves).
67struct Mat4 { uint8_t* w; int n_out, n_in; };
68Mat4 make_mat(int n_out, int n_in) {
69    const size_t nib = (size_t) n_out * n_in / 2, nsc = (size_t) n_out * n_in / 32;
70    std::vector<uint8_t> h(nib + nsc * 2);
71    for (size_t i = 0; i < nib; ++i) h[i] = (uint8_t) rnd();
72    for (size_t i = 0; i < nsc; ++i) { const uint16_t d = (uint16_t) (0x1800 + (rnd() % 0xC00)); std::memcpy(&h[nib + i * 2], &d, 2); }
73    uint8_t* d = dev<uint8_t>(h.size());
74    Q->memcpy(d, h.data(), h.size()).wait();
75    return Mat4{d, n_out, n_in};
76}
77float* make_f32(size_t n, float scale, float off = 0.f) {
78    std::vector<float> h(n);
79    for (auto& v : h) v = off + scale * frand();
80    float* d = dev<float>(n);
81    Q->memcpy(d, h.data(), n * 4).wait();
82    return d;
83}
84
85struct Layer { Mat4 q, k, v, o, gate, up, down, akp, mkp; float *base_a, *base_m, *ln_in, *ln_post, *qn, *kn; };
86
87struct Drafter {
88    std::vector<Layer> ly;
89    Mat4 fc, hsel;
90    float *hidden_norm, *final_norm;
91    uint16_t *pred_cb, *succ_cb;
92    uint8_t* head_w; int32_t* dvocab;
93    float* emb_tab;
94    // buffers
95    int32_t* d_meta; int32_t* h_meta; int32_t* h_out; float* h_prob; int32_t* d_out;
96    float *kring[L], *vring[L]; int64_t* tag; float* rt_ctx; float* rt_blk;
97    uint8_t *xq_head, *xqp, *xqc; float *xdp, *xdc;
98    float *taps, *fcy, *kc, *vc, *h, *xn, *dyn, *qv, *qn, *kv, *vv, *kblk, *ao, *o, *gate, *up, *mlp, *part, *fn, *logits, *topval, *hsel_out;
99    int32_t *toptok; uint32_t* topscratch;
100    int nd = 7;
101    std::vector<std::pair<std::string, sycl::event>> marks;
102    bool profile = false;
103    void stamp(const char* name) { if (profile) marks.emplace_back(name, Q->single_task([] {})); }
104};
105
106uint8_t* make_head() {   // Q8_0 rows: 34 bytes a block (fp16 d, 32 int8), NDV rows of N
107    const size_t blocks = (size_t) NDV * (N / 32);
108    std::vector<uint8_t> h(blocks * 34);
109    for (size_t b = 0; b < blocks; ++b) {
110        const uint16_t d = f2h(0.002f + 0.0004f * (float) (rnd() % 10));
111        std::memcpy(&h[b * 34], &d, 2);
112        for (int i = 0; i < 32; ++i) h[b * 34 + 2 + i] = (uint8_t) rnd();
113    }
114    uint8_t* d = dev<uint8_t>(h.size());
115    Q->memcpy(d, h.data(), h.size()).wait();
116    return d;
117}
118
119void build(Drafter& D) {
120    D.ly.resize(L);
121    for (int l = 0; l < L; ++l) {
122        Layer& y = D.ly[l];
123        y.q = make_mat(NH * HD, N); y.k = make_mat(NKV * HD, N); y.v = make_mat(NKV * HD, N); y.o = make_mat(N, NH * HD);
124        y.gate = make_mat(I, N); y.up = make_mat(I, N); y.down = make_mat(N, I); y.akp = make_mat(640, N); y.mkp = make_mat(640, N);
125        y.base_a = make_f32((size_t) 4 * N, 0.2f, 0.5f); y.base_m = make_f32((size_t) 4 * N, 0.2f, 0.5f);
126        y.ln_in = make_f32(N, 0.2f, 1.f); y.ln_post = make_f32(N, 0.2f, 1.f); y.qn = make_f32(HD, 0.2f, 1.f); y.kn = make_f32(HD, 0.2f, 1.f);
127    }
128    D.fc = make_mat(N, 5 * N);
129    D.hsel = make_mat(RANK, N);
130    D.hidden_norm = make_f32(N, 0.2f, 1.f); D.final_norm = make_f32(N, 0.2f, 1.f);
131    {
132        std::vector<uint16_t> cb((size_t) VOCAB * RANK);
133        for (auto& v : cb) v = (uint16_t) (0x3c00 + (rnd() & 0x1ff) - 0x100 - 0x300);   // bf16 near 2^-4 .. 2^-3, either sign mixed below
134        for (size_t i = 0; i < cb.size(); i += 5) cb[i] |= 0x8000;
135        D.pred_cb = dev<uint16_t>(cb.size()); up(D.pred_cb, cb);
136        for (size_t i = 0; i < cb.size(); i += 3) cb[i] ^= 0x8000;
137        D.succ_cb = dev<uint16_t>(cb.size()); up(D.succ_cb, cb);
138    }
139    D.head_w = make_head();
140    {
141        std::vector<int32_t> v(NDV);
142        for (int i = 0; i < NDV; ++i) v[i] = (int32_t) (((int64_t) i * 2654435761ll) % VOCAB);   // draft vocabulary -> token id
143        D.dvocab = dev<int32_t>(NDV); up(D.dvocab, v);
144    }
145    D.emb_tab = make_f32((size_t) 64 * N, 1.0f);
146    D.d_meta = dev<int32_t>(32 + 2048);   // 32 ints, then the rope tables
147    D.h_meta = (int32_t*) strata::host_malloc_polled((32 + 2048) * 4, *Q);   // 32 ints, then the rope tables of the 16 rows (host-computed: no device FP64)
148    D.h_out = (int32_t*) strata::host_malloc_polled(64 * 4, *Q);
149    std::memset(D.h_meta, 0, (32 + 2048) * 4); std::memset(D.h_out, 0, 256);
150    D.h_prob = (float*) D.h_out + 16;
151    D.d_out = dev<int32_t>(64); Q->memset(D.d_out, 0, 256).wait();
152    for (int l = 0; l < L; ++l) { D.kring[l] = make_f32((size_t) RING * NKV * HD, 2.f); D.vring[l] = make_f32((size_t) RING * NKV * HD, 2.f); }
153    D.tag = dev<int64_t>(RING);
154    D.rt_ctx = (float*) (D.d_meta + 32);
155    D.rt_blk = D.rt_ctx + B * 128;
156    D.xq_head = dev<uint8_t>(native_q8_1_bytes(N, B) + 4096);
157    D.xqp = dev<uint8_t>((size_t) 5 * N * 8 + 4096); D.xdp = dev<float>((size_t) 5 * N / 32 * 8 + 64);
158    D.xqc = dev<uint8_t>((size_t) 5 * N * 8 + 4096); D.xdc = dev<float>((size_t) 5 * N / 32 * 8 + 64);
159    Q->memset(D.xqp, 0, (size_t) 5 * N * 8 + 4096); Q->memset(D.xdp, 0, ((size_t) 5 * N / 32 * 8 + 64) * 4);
160    Q->memset(D.xqc, 0, (size_t) 5 * N * 8 + 4096); Q->memset(D.xdc, 0, ((size_t) 5 * N / 32 * 8 + 64) * 4);
161    D.taps = make_f32((size_t) B * 5 * N, 1.0f);
162    D.fcy = dev<float>((size_t) B * N); D.kc = dev<float>((size_t) B * NKV * HD); D.vc = dev<float>((size_t) B * NKV * HD);
163    D.h = dev<float>((size_t) B * N); D.xn = dev<float>((size_t) B * N); D.dyn = dev<float>((size_t) B * 640);
164    D.qv = dev<float>((size_t) B * NH * HD); D.qn = dev<float>((size_t) B * NH * HD); D.kv = dev<float>((size_t) B * NKV * HD); D.vv = dev<float>((size_t) B * NKV * HD);
165    D.kblk = dev<float>((size_t) B * NKV * HD); D.ao = dev<float>((size_t) B * NH * HD); D.o = dev<float>((size_t) B * N);
166    D.gate = dev<float>((size_t) B * I); D.up = dev<float>((size_t) B * I); D.mlp = dev<float>((size_t) B * N);
167    D.part = dev<float>((size_t) kDf2PartChunks * 8 * 32 * 130); D.fn = dev<float>((size_t) B * N);
168    D.logits = dev<float>((size_t) 7 * NDV); D.topval = dev<float>(7 * 16); D.toptok = dev<int32_t>(7 * 16); D.hsel_out = dev<float>(7 * RANK);
169    D.topscratch = dev<uint32_t>((size_t) 7 * kDf2TopBlocks * kDf2TopK * 2);
170    Q->memset(D.tag, 0xff, RING * 8).wait();
171    Q->wait();
172}
173
174void reset_ring(Drafter& D, int pos) {
175    std::vector<int64_t> t(RING, -1);
176    for (int p = std::max(0, pos - (RING - 16)); p < pos; ++p) t[p % RING] = ((int64_t) 1 << 32) | p;
177    Q->memcpy(D.tag, t.data(), RING * 8).wait();
178}
179
180void set_meta(Drafter& D, int n_new, int64_t pos0, int anchor) {
181    const int64_t s = pos0 + n_new;
182    for (int i = 0; i < 8; ++i) { D.h_meta[i] = (int32_t) (pos0 + std::min(i, n_new - 1)); D.h_meta[8 + i] = (int32_t) (s + i); }
183    D.h_meta[16] = anchor; D.h_meta[17] = (int32_t) s; D.h_meta[18] = 1; D.h_meta[24] = anchor;
184    for (int i = 1; i < 8; ++i) D.h_meta[24 + i] = 7;
185    df2_rope_table_cpu(D.h_meta, 16, 10000.0f, (float*) (D.h_meta + 32));
186    for (int i = 0; i < 16; ++i) ((volatile int32_t*) D.h_out)[i] = -1;
187    std::atomic_thread_fence(std::memory_order_seq_cst);
188}
189
190void gemm1(Drafter&, const Mat4& m, float* y, int nc, const uint8_t* xq, const float* xd) {
191    Df2Mat mats[1] = {Df2Mat{m.w, y, m.n_out, true}};
192    df2_gemm(mats, 1, xq, xd, m.n_in, nc, Q);
193}
194void gemm2(const Mat4& a, const Mat4& b, float* ya, float* yb, int nc, const uint8_t* xq, const float* xd) {
195    Df2Mat mats[2] = {Df2Mat{a.w, ya, a.n_out, true}, Df2Mat{b.w, yb, b.n_out, true}};
196    df2_gemm(mats, 2, xq, xd, a.n_in, nc, Q);
197}
198void gemm3(const Mat4& a, const Mat4& b, const Mat4& c, float* ya, float* yb, float* yc, int nc, const uint8_t* xq, const float* xd) {
199    Df2Mat mats[3] = {Df2Mat{a.w, ya, a.n_out, true}, Df2Mat{b.w, yb, b.n_out, true}, Df2Mat{c.w, yc, c.n_out, true}};
200    df2_gemm(mats, 3, xq, xd, a.n_in, nc, Q);
201}
202
203bool g_io = true;
204int g_meta_kernel = 0;   // DF2B_METAKERNEL=1: a kernel copies the meta from host memory
205int g_host_out = 0;   // DF2B_HOSTOUT=1: the select kernel writes the host buffer itself
206std::string g_dup;   // DF2B_DUP=name: every launch stamped `name` runs twice (the marginal cost of that kernel in the round)
207int g_dupn = 1;   // DF2B_DUPN: how many extra launches of the DF2B_DUP kernel (layer 0 only when > 1)
208int g_layer = 0;
209#define DO(name, call) do { call; if (!g_dup.empty() && g_dup == name && (g_dupn == 1 || g_layer == 0)) { for (int rep = 0; rep < g_dupn; ++rep) { call; } } D.stamp(name); } while (0)
210void record_context(Drafter& D, int n) {
211    const int tap_n = 5 * N;
212    DO("quant_rows", df2_quant_rows(D.taps, tap_n, n, D.xqc, D.xdc, Q));
213    DO("gemm:fc", gemm1(D, D.fc, D.fcy, n, D.xqc, D.xdc));
214    DO("rms_quant", df2_rms_quant(D.fcy, D.hidden_norm, nullptr, n, N, EPS, D.xqc, D.xdc, Q));
215    for (int l = 0; l < L; ++l) {
216        DO("gemm:kp", gemm2(D.ly[l].k, D.ly[l].v, D.kc, D.vc, n, D.xqc, D.xdc));
217        DO("kv_ring", df2_kv_ring(D.kc, D.vc, D.ly[l].kn, D.d_meta + 0, D.rt_ctx, D.kring[l], D.vring[l], RING, n, NKV, EPS, Q, D.tag, D.d_meta + 18));
218    }
219}
220
221// the stand-in for the engine's embedding gather: 8 rows of a small table
222void embed_stub(Drafter& D) {
223    const float* tab = D.emb_tab; const int32_t* tok = D.d_meta + 24; float* out = D.h;
224    Q->parallel_for(sycl::nd_range<1>((size_t) B * N, 256), [=](sycl::nd_item<1> it) {
225        const int i = (int) it.get_global_id(0), r = i / N, c = i % N;
226        out[i] = tab[(size_t) (tok[r] & 63) * N + c];
227    });
228}
229
230void record_block(Drafter& D) {
231    DO("embed", embed_stub(D));
232    DO("rms_quant", df2_rms_quant(D.h, D.ly[0].ln_in, D.xn, B, N, EPS, D.xqp, D.xdp, Q));
233    for (int l = 0; l < L; ++l) {
234        Layer& y = D.ly[l]; g_layer = l;
235        DO("gemm:akp", gemm1(D, y.akp, D.dyn, B, D.xqp, D.xdp));
236        DO("conv_quant", df2_conv_quant(D.xn, D.dyn, 640, y.base_a, B, N, GROUP, D.xqp, D.xdp, Q));
237        DO("gemm:qkv", gemm3(y.q, y.k, y.v, D.qv, D.kv, D.vv, B, D.xqp, D.xdp));
238        DO("norm_rope_qk", df2_norm_rope_qk(D.qv, D.kv, y.qn, y.kn, D.rt_blk, D.qn, D.kblk, B, NH, NKV, EPS, Q));
239        DO("attention", df2_attention(D.qn, D.kring[l], D.vring[l], D.kblk, D.vv, D.d_meta + 17, RING, WINDOW, D.part, D.xqp, D.xdp, Q, D.tag, D.d_meta + 18));
240        DO("gemm:o", gemm1(D, y.o, D.o, B, D.xqp, D.xdp));
241        DO("conv_rms_quant", df2_conv_rms_quant(D.o, D.dyn + 320, 640, y.base_a + 2 * N, D.h, y.ln_post, D.xn, B, N, GROUP, EPS, D.xqp, D.xdp, Q));
242        DO("gemm:mkp", gemm1(D, y.mkp, D.dyn, B, D.xqp, D.xdp));
243        DO("conv_quant", df2_conv_quant(D.xn, D.dyn, 640, y.base_m, B, N, GROUP, D.xqp, D.xdp, Q));
244        DO("gemm:gate_up", gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp));
245        DO("swiglu_quant", df2_swiglu_quant(D.gate, D.up, B, I, D.xqp, D.xdp, Q));
246        DO("gemm:down", gemm1(D, y.down, D.mlp, B, D.xqp, D.xdp));
247        const float* next_gamma = l + 1 < L ? D.ly[l + 1].ln_in : D.final_norm;
248        DO("conv_rms_quant", df2_conv_rms_quant(D.mlp, D.dyn + 320, 640, y.base_m + 2 * N, D.h, next_gamma, l + 1 < L ? D.xn : D.fn, B, N, GROUP, EPS, D.xqp, D.xdp, Q));
249    }
250    DO("quant_q8_1", native_quantize_q8_1(D.fn + N, D.xq_head, N, D.nd, Q));
251    DO("quant_rows", df2_quant_rows(D.fn + N, N, D.nd, D.xqp, D.xdp, Q));
252    DO("head", native_mmvq(GGML_Q8_0, D.head_w, D.xq_head, D.logits, N, NDV, D.nd, Q));
253    DO("topk", df2_topk(D.logits, NDV, D.dvocab, D.nd, D.topscratch, D.topval, D.toptok, Q));
254    DO("gemm:hsel", gemm1(D, D.hsel, D.hsel_out, D.nd, D.xqp, D.xdp));
255    DO("select", df2_select(D.hsel_out, D.topval, D.toptok, D.pred_cb, D.succ_cb, RANK, D.nd, D.d_meta + 16, g_host_out ? D.h_out : D.d_out, g_host_out ? (float*) D.h_out + 16 : (float*) D.d_out + 16, Q));
256    if (g_host_out == 0 && g_io) DO("d2h", Q->memcpy(D.h_out, D.d_out, 64 * 4));
257}
258
259void record_round(Drafter& D, int n_new, bool io = true) {
260    g_io = io;
261    // meta + rope tables to the device with one copy (no kernel touches host memory: a kernel that reads or writes host USM slows every later kernel of the process on the A750)
262    if (!io) {
263    } else if (g_meta_kernel) {
264        const int32_t* hm = D.h_meta; int32_t* dm = D.d_meta;
265        Q->parallel_for(sycl::range<1>(32 + 2048), [=](sycl::id<1> i) { dm[i] = hm[i]; });
266    } else {
267        Q->memcpy(D.d_meta, D.h_meta, (32 + 2048) * 4);
268    }
269    D.stamp("meta_rope");
270    record_context(D, n_new);
271    record_block(D);
272}
273
274uint64_t checksum(const void* p, size_t n) { uint64_t h = 1469598103934665603ull; for (size_t i = 0; i < n; ++i) h = (h ^ ((const uint8_t*) p)[i]) * 1099511628211ull; return h; }
275
276// ---------------------------------------------------------------- tests
277int g_fail = 0;
278void check(bool ok, const char* what) { std::printf("  %-64s %s\n", what, ok ? "ok" : "FAIL"); if (!ok) ++g_fail; }
279
280// CPU reference of the Q4_0 GEMM, bitwise (the lane partial sums over the steps in order, fma, the xor shuffle tree)
281void gemm_ref(const std::vector<uint8_t>& w, int n_out, int n_in, const std::vector<int8_t>& xq, const std::vector<float>& xd, int nc, std::vector<float>& y) {
282    const int nb = n_in / 32, steps = nb / 16;
283    const size_t nib = (size_t) n_out * n_in / 2;
284    y.assign((size_t) nc * n_out, 0.f);
285    for (int row = 0; row < n_out; ++row) {
286        const int g = row / 8, rr = row % 8;
287        for (int c = 0; c < nc; ++c) {
288            float lane[32];
289            for (int l = 0; l < 32; ++l) {
290                float acc = 0.f;
291                for (int st = 0; st < steps; ++st) {
292                    const uint8_t* wp = &w[(size_t) g * 8 * (n_in / 2) + (size_t) st * 2048 + (size_t) rr * 256 + (size_t) l * 8];
293                    uint32_t rx, ry; std::memcpy(&rx, wp, 4); std::memcpy(&ry, wp + 4, 4);
294                    const uint32_t ws[4] = {rx & 0x0F0F0F0Fu, (rx >> 4) & 0x0F0F0F0Fu, ry & 0x0F0F0F0Fu, (ry >> 4) & 0x0F0F0F0Fu};
295                    const int8_t* xp = &xq[(size_t) c * n_in + (size_t) st * 512 + (size_t) l * 16];
296                    int isum = 0, xsum = 0;
297                    for (int k = 0; k < 4; ++k)
298                        for (int b = 0; b < 4; ++b) { isum += (int) ((ws[k] >> (8 * b)) & 0xff) * (int) xp[k * 4 + b]; xsum += (int) xp[k * 4 + b]; }
299                    uint16_t dh; std::memcpy(&dh, &w[nib + ((size_t) g * 8 * nb + (size_t) st * 128 + rr * 16 + (l >> 1)) * 2], 2);
300                    const float dw = h2f(dh), xs = xd[(size_t) c * nb + st * 16 + (l >> 1)];
301                    const float prod = dw * xs;
302                    acc = std::fmaf(prod, (float) (isum - 8 * xsum), acc);
303                }
304                lane[l] = acc;
305            }
306            for (int o = 16; o > 0; o >>= 1) { float nl[32]; for (int l = 0; l < 32; ++l) nl[l] = lane[l] + lane[l ^ o]; std::memcpy(lane, nl, sizeof lane); }
307            y[(size_t) c * n_out + row] = lane[0];
308        }
309    }
310}
311
312
313// DF-04: the head, top-k and selector for the first nd rows give exactly the first nd rows of the 7-row path (distinct random rows, so the picks differ per row)
314void prefix_test() {
315    std::printf("DF-04 prefix: head / top-k / selector for nd rows == the first nd rows of the 7-row path (bitwise)\n");
316    uint8_t* head_w = make_head();
317    std::vector<int32_t> vv(NDV); for (int i = 0; i < NDV; ++i) vv[i] = (int32_t) (((int64_t) i * 2654435761ll) % VOCAB);
318    int32_t* dvocab = dev<int32_t>(NDV); up(dvocab, vv);
319    std::vector<uint16_t> cb((size_t) VOCAB * RANK);
320    for (auto& v : cb) v = (uint16_t) (0x3c00 + (rnd() & 0x1ff) - 0x400); for (size_t i = 0; i < cb.size(); i += 5) cb[i] |= 0x8000;
321    uint16_t* pred = dev<uint16_t>(cb.size()); up(pred, cb);
322    for (size_t i = 0; i < cb.size(); i += 3) cb[i] ^= 0x8000;
323    uint16_t* succ = dev<uint16_t>(cb.size()); up(succ, cb);
324    Mat4 hsel = make_mat(RANK, N);
325    float* fn = make_f32((size_t) B * N, 1.0f);
326    uint8_t* xq_head = dev<uint8_t>(native_q8_1_bytes(N, B) + 4096);
327    uint8_t* xqp = dev<uint8_t>((size_t) 5 * N * 8 + 4096); float* xdp = dev<float>((size_t) 5 * N / 32 * 8 + 64);
328    float* logits = dev<float>((size_t) 7 * NDV); float* hs = dev<float>(7 * RANK); float* tv = dev<float>(7 * 16); int32_t* tt = dev<int32_t>(7 * 16);
329    uint32_t* sc = dev<uint32_t>((size_t) 7 * kDf2TopBlocks * kDf2TopK * 2);
330    int32_t* anchor = dev<int32_t>(1); int32_t a5 = 777; Q->memcpy(anchor, &a5, 4).wait();
331    int32_t* otok = dev<int32_t>(8); float* oprob = dev<float>(8);
332    struct Out { std::vector<float> lg, hs, tv; std::vector<int32_t> tt, ot; std::vector<float> op; };
333    auto run = [&](int nd) {
334        native_quantize_q8_1(fn + N, xq_head, N, nd, Q);
335        df2_quant_rows(fn + N, N, nd, xqp, xdp, Q);
336        native_mmvq(GGML_Q8_0, head_w, xq_head, logits, N, NDV, nd, Q);
337        df2_topk(logits, NDV, dvocab, nd, sc, tv, tt, Q);
338        { Df2Mat m1[1] = {Df2Mat{hsel.w, hs, hsel.n_out, true}}; df2_gemm(m1, 1, xqp, xdp, hsel.n_in, nd, Q); }
339        df2_select(hs, tv, tt, pred, succ, RANK, nd, anchor, otok, oprob, Q);
340        Q->wait();
341        Out o; o.lg = down(logits, (size_t) nd * NDV); o.hs = down(hs, (size_t) nd * RANK); o.tv = down(tv, (size_t) nd * 16); o.tt = down(tt, (size_t) nd * 16);
342        o.ot = down(otok, nd); o.op = down(oprob, nd);
343        return o;
344    };
345    const Out full = run(7);
346    bool distinct = false; for (int i = 1; i < 7; ++i) distinct |= full.ot[i] != full.ot[0];
347    check(distinct, "the 7 picks of the reference differ (the test can tell rows apart)");
348    for (int nd : {5, 3, 1}) {
349        const Out o = run(nd);
350        char buf[96];
351        bool ok = std::memcmp(o.lg.data(), full.lg.data(), o.lg.size() * 4) == 0 && std::memcmp(o.hs.data(), full.hs.data(), o.hs.size() * 4) == 0 &&
352                  std::memcmp(o.tv.data(), full.tv.data(), o.tv.size() * 4) == 0 && std::memcmp(o.tt.data(), full.tt.data(), o.tt.size() * 4) == 0 &&
353                  std::memcmp(o.ot.data(), full.ot.data(), o.ot.size() * 4) == 0 && std::memcmp(o.op.data(), full.op.data(), o.op.size() * 4) == 0;
354        std::snprintf(buf, sizeof buf, "nd = %d: logits, selector projection, top-16, picks, probabilities == first %d rows of nd = 7", nd, nd);
355        check(ok, buf);
356    }
357}
358
359int run_tests() {
360    std::printf("device: %s\n", Q->get_device().get_info<sycl::info::device::name>().c_str());
361    struct Shape { const char* name; int n_out, n_in, nmat; };
362    const Shape shapes[] = {{"fc", N, 5 * N, 1}, {"qkv(3 matrices)", NH * HD, N, 3}, {"o", N, NH * HD, 1}, {"gate+up(2)", I, N, 2}, {"down", N, I, 1}, {"akp", 640, N, 1}, {"hsel", RANK, N, 1}};
363    const int variants[] = {17, 28, 12, 11, 10, 24, 25, 34, 40, 41, 42, 43};
364    std::printf("Q4_0 GEMM against the CPU reference (bitwise), every variant, columns 1..8; and the first n columns of the 8-column launch against the n-column launch (DF-03)\n");
365    for (const Shape& s : shapes) {
366        const int n_in = s.n_in;
367        std::vector<Mat4> mats;
368        std::vector<std::vector<uint8_t>> host;
369        std::vector<float*> ys;
370        for (int m = 0; m < s.nmat; ++m) { mats.push_back(make_mat(s.n_out, n_in)); host.push_back(down(mats.back().w, (size_t) s.n_out * n_in / 2 + (size_t) s.n_out * n_in / 16)); ys.push_back(dev<float>((size_t) 8 * s.n_out)); }
371        std::vector<int8_t> xq((size_t) 8 * n_in);
372        std::vector<float> xd((size_t) 8 * n_in / 32);
373        for (auto& v : xq) v = (int8_t) (rnd() % 255 - 127);
374        for (auto& v : xd) v = 0.001f + 0.00002f * (float) (rnd() % 100);
375        int8_t* dxq = dev<int8_t>(xq.size()); float* dxd = dev<float>(xd.size());
376        Q->memcpy(dxq, xq.data(), xq.size()).wait(); Q->memcpy(dxd, xd.data(), xd.size() * 4).wait();
377        bool all_ref = true, all_nc = true;
378        std::vector<std::vector<float>> ref(s.nmat);
379        for (int m = 0; m < s.nmat; ++m) gemm_ref(host[m], s.n_out, n_in, xq, xd, 8, ref[m]);
380        for (int v : variants) {
381            df2_gemm_variant(v);
382            std::vector<std::vector<float>> full(s.nmat);
383            for (int nc = 8; nc >= 1; --nc) {
384                std::vector<Df2Mat> dm;
385                for (int m = 0; m < s.nmat; ++m) { Q->memset(ys[m], 0, (size_t) 8 * s.n_out * 4); dm.push_back(Df2Mat{mats[m].w, ys[m], s.n_out, true}); }
386                df2_gemm(dm.data(), s.nmat, dxq, dxd, n_in, nc, Q);
387                Q->wait();
388                for (int m = 0; m < s.nmat; ++m) {
389                    auto y = down(ys[m], (size_t) nc * s.n_out);
390                    if (nc == 8) { full[m] = y; all_ref &= std::memcmp(y.data(), ref[m].data(), y.size() * 4) == 0; }
391                    else all_nc &= std::memcmp(y.data(), full[m].data(), y.size() * 4) == 0;
392                }
393            }
394        }
395        df2_gemm_variant(0);
396        char buf[160];
397        std::snprintf(buf, sizeof buf, "%s (%d x %d): 8 variants x 8 NC vs CPU", s.name, s.n_out, n_in); check(all_ref, buf);
398        std::snprintf(buf, sizeof buf, "%s: NC < 8 == first NC columns of NC = 8", s.name); check(all_nc, buf);
399    }
400    // top-16 against the CPU (value descending, ties to the lower index)
401    {
402        const int rows = 7;
403        std::vector<float> lg((size_t) rows * NDV);
404        for (auto& v : lg) v = (float) (rnd() % 20000) / 100.f;   // many ties
405        float* dl = dev<float>(lg.size()); up(dl, lg);
406        int32_t* dmap = dev<int32_t>(NDV); std::vector<int32_t> mp(NDV); for (int i = 0; i < NDV; ++i) mp[i] = i * 2; up(dmap, mp);
407        uint32_t* sc = dev<uint32_t>((size_t) rows * kDf2TopBlocks * kDf2TopK * 2); float* tv = dev<float>(rows * 16); int32_t* tt = dev<int32_t>(rows * 16);
408        df2_topk(dl, NDV, dmap, rows, sc, tv, tt, Q); Q->wait();
409        auto hv = down(tv, rows * 16); auto ht = down(tt, rows * 16);
410        bool ok = true;
411        for (int r = 0; r < rows; ++r) {
412            std::vector<int> idx(NDV); for (int i = 0; i < NDV; ++i) idx[i] = i;
413            std::partial_sort(idx.begin(), idx.begin() + 16, idx.end(), [&](int a, int b) { const float va = lg[(size_t) r * NDV + a], vb = lg[(size_t) r * NDV + b]; return va > vb || (va == vb && a < b); });
414            for (int k = 0; k < 16; ++k) ok &= hv[r * 16 + k] == lg[(size_t) r * NDV + idx[k]] && ht[r * 16 + k] == idx[k] * 2;
415        }
416        check(ok, "top-16 of 7 x 106299 logits (ties) == CPU, with the id map");
417    }
418    // quantize rows against the CPU (exact)
419    {
420        const int K = N, rows = 5;
421        std::vector<float> x((size_t) rows * K); for (auto& v : x) v = 3.f * frand();
422        float* dx = dev<float>(x.size()); up(dx, x);
423        int8_t* dq = dev<int8_t>((size_t) rows * K); float* dd = dev<float>((size_t) rows * K / 32);
424        df2_quant_rows(dx, K, rows, dq, dd, Q); Q->wait();
425        auto hq = down(dq, (size_t) rows * K); auto hd = down(dd, (size_t) rows * K / 32);
426        bool ok = true;
427        for (int r = 0; r < rows; ++r)
428            for (int b = 0; b < K / 32; ++b) {
429                float amax = 0; for (int i = 0; i < 32; ++i) amax = std::max(amax, std::fabs(x[(size_t) r * K + b * 32 + i]));
430                const float d = amax / 127.f, inv = amax > 0 ? 127.f / amax : 0.f;
431                ok &= hd[(size_t) r * K / 32 + b] == d;
432                for (int i = 0; i < 32; ++i) ok &= hq[(size_t) r * K + b * 32 + i] == (int8_t) std::nearbyint(x[(size_t) r * K + b * 32 + i] * inv);
433            }
434        check(ok, "df2_quant_rows == CPU (scales and int8, exact)");
435    }
436    // attention against a double-precision CPU reference
437    {
438        Drafter* Dp = new Drafter(); Drafter& D = *Dp;
439        const int POS = 700, n_new = 3;
440        D.kring[0] = make_f32((size_t) RING * NKV * HD, 2.f); D.vring[0] = make_f32((size_t) RING * NKV * HD, 2.f);
441        D.tag = dev<int64_t>(RING); D.part = dev<float>((size_t) kDf2PartChunks * 8 * 32 * 130);
442        std::vector<int64_t> t(RING, -1); for (int p = 0; p < POS + n_new; ++p) t[p % RING] = ((int64_t) 1 << 32) | p; up(D.tag, t);
443        std::vector<float> q((size_t) 8 * 32 * HD), kb((size_t) 8 * NKV * HD), vb(kb.size());
444        for (auto& v : q) v = frand() * 2; for (auto& v : kb) v = frand() * 2; for (auto& v : vb) v = frand() * 2;
445        float *dq = dev<float>(q.size()), *dk = dev<float>(kb.size()), *dv = dev<float>(vb.size()); up(dq, q); up(dk, kb); up(dv, vb);
446        int32_t* ap = dev<int32_t>(1); int32_t sv = POS + n_new; Q->memcpy(ap, &sv, 4).wait(); int32_t* ep = dev<int32_t>(1); int32_t e1 = 1; Q->memcpy(ep, &e1, 4).wait();
447        int8_t* xq = dev<int8_t>(8 * 32 * HD); float* xd = dev<float>(8 * 32 * HD / 32);
448      for (int avk : {1064, 2064, 1128, 2128}) {
449        const int av = avk / 1000, ck = avk % 1000;
450        df2_attention_variant(av); df2_attention_chunk(ck);
451        df2_attention(dq, D.kring[0], D.vring[0], dk, dv, ap, RING, WINDOW, D.part, xq, xd, Q, D.tag, ep); Q->wait();
452        auto kr = down(D.kring[0], (size_t) RING * NKV * HD); auto vr = down(D.vring[0], (size_t) RING * NKV * HD);
453        auto hq = down(xq, (size_t) 8 * 32 * HD); auto hd = down(xd, (size_t) 8 * 32 * HD / 32);
454        double maxerr = 0, maxref = 0;
455        const int s = POS + n_new, lo = std::max(0, s - (WINDOW - 1));
456        for (int row = 0; row < 8; ++row)
457            for (int hh = 0; hh < 32; ++hh) {
458                const int kvh = hh / 4;
459                std::vector<double> sc; std::vector<const float*> vp;
460                for (int j = lo; j < s; ++j) { if (j <= s + row - WINDOW) continue; double a = 0; const float* kp = &kr[((size_t) kvh * RING + j % RING) * HD]; for (int d = 0; d < HD; ++d) a += (double) q[((size_t) row * 32 + hh) * HD + d] * kp[d]; sc.push_back(a / std::sqrt((double) HD)); vp.push_back(&vr[((size_t) kvh * RING + j % RING) * HD]); }
461                for (int j = 0; j < 8; ++j) { double a = 0; const float* kp = &kb[((size_t) j * NKV + kvh) * HD]; for (int d = 0; d < HD; ++d) a += (double) q[((size_t) row * 32 + hh) * HD + d] * kp[d]; sc.push_back(a / std::sqrt((double) HD)); vp.push_back(&vb[((size_t) j * NKV + kvh) * HD]); }
462                double m = -1e300; for (double v : sc) m = std::max(m, v);
463                double Z = 0; std::vector<double> o(HD, 0.0);
464                for (size_t j = 0; j < sc.size(); ++j) { const double w = std::exp(sc[j] - m); Z += w; for (int d = 0; d < HD; ++d) o[d] += w * vp[j][d]; }
465                for (int d = 0; d < HD; ++d) {
466                    const double ref = o[d] / Z;
467                    const size_t blk = (size_t) hh * (HD / 32) + d / 32;   // (kvh * 4 + g) * 4 + d / 32
468                    const double got = (double) hq[(size_t) row * 32 * HD + blk * 32 + d % 32] * hd[(size_t) row * 32 * HD / 32 + blk];
469                    maxerr = std::max(maxerr, std::fabs(got - ref)); maxref = std::max(maxref, std::fabs(ref));
470                }
471            }
472        std::printf("  attention: max |got - ref| = %.3e (max |ref| = %.3e; int8 quantization step ~ %.1e)\n", maxerr, maxref, maxref / 127.0);
473        { char b2[96]; std::snprintf(b2, sizeof b2, "df2_attention v%d chunk %d (ring 700 + 3, tags, window) == CPU double within the int8 step", av, ck); check(maxerr < 2.0 * maxref / 127.0, b2); }
474      }
475      df2_attention_variant(0); df2_attention_chunk(0);
476    }
477    prefix_test();
478    std::printf("%s\n", g_fail ? "SOME TESTS FAILED" : "ALL TESTS PASSED");
479    return g_fail ? 1 : 0;
480}
481
482// ---------------------------------------------------------------- bench
483struct Stats { double med, p5, p95, mean; };
484Stats stats(const std::vector<double>& v) { double s = 0; for (double x : v) s += x; return Stats{med(v), pct(v, 0.05), pct(v, 0.95), s / (double) v.size()}; }
485
486int run_bench() {
487    const int POS = envi("DF2B_POS", 600), ITERS = envi("DF2B_ITERS", 100), NNEW = envi("DF2B_NNEW", 3);
488    const int standin_mb = envi("DF2B_STANDIN_MB", 0);
489    const double gap = envd("DF2B_GAPMS", 0.0);
490    std::printf("device: %s  max wg %zu  mem %.1f GiB\n", Q->get_device().get_info<sycl::info::device::name>().c_str(),
491                (size_t) Q->get_device().get_info<sycl::info::device::max_work_group_size>(),
492                (double) Q->get_device().get_info<sycl::info::device::global_mem_size>() / (1024.0 * 1024 * 1024));
493    Drafter D;
494    D.nd = envi("DF2B_ND", 7);
495    build(D);
496    reset_ring(D, POS);
497    float* standin = nullptr; size_t standin_n = (size_t) standin_mb * 1024 * 1024 / 4; float* sink = nullptr;
498    if (standin_mb) { standin = make_f32(standin_n, 1.f); sink = dev<float>(1024); }
499    auto run_standin = [&] {
500        if (!standin) return;
501        const float* s = standin; float* sk = sink; const size_t n = standin_n;
502        Q->parallel_for(sycl::nd_range<1>(1024 * 256, 256), [=](sycl::nd_item<1> it) {
503            float a = 0; for (size_t i = it.get_global_id(0); i < n; i += 1024 * 256) a += s[i];
504            if (a == 12345.678f) sk[0] = a;
505        });
506    };
507    double vram = 0;
508    std::printf("config: ring pos %d, n_new %d, drafts %d, iters %d, stand-in %d MB, gap %.2f ms\n", POS, NNEW, D.nd, ITERS, standin_mb, gap);
509    (void) vram;
510    // warm up (JIT every kernel)
511    set_meta(D, NNEW, POS, 5);
512    for (int i = 0; i < 6; ++i) { set_meta(D, NNEW, POS, 5); record_round(D, NNEW); Q->wait(); }
513    {   // finite-ness of the round's data (random weights could overflow; NaN / Inf would also slow special-value paths)
514        auto stat = [&](const char* nm, const float* d, size_t n) {
515            auto h = down(d, n); size_t bad = 0; double mx = 0;
516            for (float v : h) { if (!std::isfinite(v)) ++bad; else mx = std::max(mx, (double) std::fabs(v)); }
517            std::printf("data %-8s: %zu values, %zu not finite, max |v| %.3g\n", nm, n, bad, mx);
518        };
519        stat("h", D.h, (size_t) B * N); stat("fn", D.fn, (size_t) B * N); stat("dyn", D.dyn, (size_t) B * 640); stat("qv", D.qv, (size_t) B * NH * HD);
520        stat("ao-ish o", D.o, (size_t) B * N); stat("gate", D.gate, (size_t) B * I); stat("logits", D.logits, (size_t) D.nd * NDV);
521    }
522    uint64_t cs = checksum(D.h_out, 64);
523    std::printf("draft ids: "); for (int i = 0; i < 7; ++i) std::printf("%d ", D.h_out[i]); std::printf("  probs: "); for (int i = 0; i < 7; ++i) std::printf("%.4f ", D.h_prob[i]); std::printf("  checksum %016llx\n", (unsigned long long) cs);
524
525    // (1) direct launches
526    std::vector<double> wall;
527    for (int i = 0; i < ITERS + 10; ++i) {
528        run_standin();
529        if (gap > 0) { Q->wait(); std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(gap)); }
530        Q->wait();
531        const auto t0 = Clock::now();
532        set_meta(D, NNEW, POS, 5);
533        record_round(D, NNEW);
534        Q->wait();
535        const double w = ms_since(t0);
536        if (i >= 10) wall.push_back(w);
537    }
538    const Stats sd = stats(wall);
539    std::printf("DIRECT   wall ms/round: median %.3f  p5 %.3f  p95 %.3f  mean %.3f\n", sd.med, sd.p5, sd.p95, sd.mean);
540    // host submit time alone
541    {
542        std::vector<double> sub;
543        for (int i = 0; i < 30; ++i) { Q->wait(); const auto t0 = Clock::now(); set_meta(D, NNEW, POS, 5); record_round(D, NNEW); sub.push_back(ms_since(t0)); Q->wait(); }
544        std::printf("DIRECT   host submit ms (until the last launch returns): median %.3f\n", med(sub));
545    }
546    // (2) graph replay
547    if (envi("DF2B_GRAPH", 1)) {
548        try {
549            sx::command_graph<sx::graph_state::modifiable> g(Q->get_context(), Q->get_device());
550            g.begin_recording(*Q);
551            record_round(D, NNEW, false);   // compute only: the meta copy and the result copy stay outside the graph
552            g.end_recording();
553            auto exec = g.finalize();
554            std::vector<double> gw;
555            for (int i = 0; i < ITERS + 10; ++i) {
556                run_standin();
557                if (gap > 0) { Q->wait(); std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(gap)); }
558                Q->wait();
559                const auto t0 = Clock::now();
560                set_meta(D, NNEW, POS, 5);
561                Q->memcpy(D.d_meta, D.h_meta, (32 + 2048) * 4);
562                Q->ext_oneapi_graph(exec);
563                Q->memcpy(D.h_out, D.d_out, 64 * 4);
564                Q->wait();
565                if (i >= 10) gw.push_back(ms_since(t0));
566            }
567            const Stats sg = stats(gw);
568            std::printf("GRAPH    wall ms/round: median %.3f  p5 %.3f  p95 %.3f  mean %.3f   checksum %016llx\n", sg.med, sg.p5, sg.p95, sg.mean, (unsigned long long) checksum(D.h_out, 64));
569        } catch (const std::exception& e) {
570            std::printf("GRAPH    unavailable: %s\n", e.what());
571        }
572    }
573    // (3) per-kernel profile (markers after each launch)
574    if (envi("DF2B_PROFILE", 1)) {
575        D.profile = true;
576        std::vector<std::pair<std::string, double>> tot; int nrun = 0;
577        for (int i = 0; i < 20; ++i) {
578            D.marks.clear();
579            Q->wait();
580            sycl::event e0 = Q->single_task([] {});
581            D.marks.emplace_back("start", e0);
582            set_meta(D, NNEW, POS, 5);
583            record_round(D, NNEW);
584            Q->wait();
585            if (i < 3) continue;
586            ++nrun;
587            for (size_t k = 1; k < D.marks.size(); ++k) {
588                const double a = (double) D.marks[k - 1].second.get_profiling_info<sycl::info::event_profiling::command_end>();
589                const double b = (double) D.marks[k].second.get_profiling_info<sycl::info::event_profiling::command_end>();
590                const double ms = (b - a) * 1e-6;
591                auto it = std::find_if(tot.begin(), tot.end(), [&](auto& t) { return t.first == D.marks[k].first; });
592                if (it == tot.end()) tot.emplace_back(D.marks[k].first, ms); else it->second += ms;
593            }
594        }
595        D.profile = false;
596        double sum = 0; for (auto& t : tot) sum += t.second;
597        std::printf("PROFILE (marker after every launch, so each includes its launch gap; ms per round, sum %.3f over %d rounds):\n", sum / nrun, nrun);
598        std::sort(tot.begin(), tot.end(), [](auto& a, auto& b) { return a.second > b.second; });
599        for (auto& t : tot) std::printf("  %-16s %7.3f\n", t.first.c_str(), t.second / nrun);
600    }
601    return 0;
602}
603
604// ---------------------------------------------------------------- per-kernel micro timings
605template <class F> double time_us(F&& f, int iters) {
606    for (int i = 0; i < 5; ++i) f();
607    Q->wait();
608    const auto t0 = Clock::now();
609    for (int i = 0; i < iters; ++i) f();
610    Q->wait();
611    return std::chrono::duration<double, std::micro>(Clock::now() - t0).count() / iters;
612}
613
614int run_gemm_bench() {
615    if (envi("DF2B_HOSTALLOC", 0) == 1) (void) strata::host_malloc_polled(4096, *Q);   // uncached host memory (zeMemAllocHost), as the engine's doorbells
616    if (envi("DF2B_HOSTALLOC", 0) == 2) (void) sycl::malloc_host(4096, *Q);
617    if (envi("DF2B_BIGALLOC_MB", 0)) { void* bp = sycl::malloc_device((size_t) envi("DF2B_BIGALLOC_MB", 0) << 20, *Q); Q->memset(bp, 1, (size_t) envi("DF2B_BIGALLOC_MB", 0) << 20).wait(); }   // touched
618    for (int i = 0; i < envi("DF2B_NALLOC", 0); ++i) (void) sycl::malloc_device(4096 + 64 * i, *Q);
619    for (int i = 0; i < envi("DF2B_NMED", 0); ++i) { void* mp = sycl::malloc_device(8 << 20, *Q); Q->memset(mp, 1, 8 << 20); }
620    Q->wait();
621    if (const char* pre = std::getenv("DF2B_PRE")) {
622        const std::string ps = pre;
623        if (ps.find("rope") != std::string::npos) { double* r = dev<double>(64); df2_rope_inv(10000.0f, r, Q); Q->wait(); }
624        if (ps.find("head") != std::string::npos) (void) make_head();
625        if (ps.find("cb") != std::string::npos) { std::vector<uint16_t> cb((size_t) VOCAB * RANK, 0x3c00); uint16_t* d = dev<uint16_t>(cb.size()); up(d, cb); }
626        if (ps.find("ring") != std::string::npos) for (int l = 0; l < 10; ++l) (void) make_f32((size_t) RING * NKV * HD, 2.f);
627        if (ps.find("layers") != std::string::npos) for (int l = 0; l < 5; ++l) { make_mat(I, N); make_mat(I, N); make_mat(N, I); make_mat(4096, N); }
628        if (ps.find("host") != std::string::npos) { (void) strata::host_malloc_polled(128, *Q); (void) strata::host_malloc_polled(256, *Q); }
629        Q->wait();
630    }
631    std::printf("device: %s\n", Q->get_device().get_info<sycl::info::device::name>().c_str());
632    struct Shape { const char* name; int n_out, n_in, nmat; };
633    const Shape shapes[] = {{"fc", N, 5 * N, 1}, {"qkv(3)", NH * HD, N, 3}, {"o", N, NH * HD, 1}, {"gate+up(2)", I, N, 2}, {"down", N, I, 1}, {"akp/mkp", 640, N, 1}, {"hsel", RANK, N, 1}};
634    int vs[16], nv = 0;
635    if (const char* e = std::getenv("DF2B_VARIANTS")) { for (const char* c = e; *c;) { vs[nv++] = std::atoi(c); while (*c && *c != ',') ++c; if (*c == ',') ++c; } }
636    else { const int d[] = {28, 12, 40, 41, 42, 43}; for (int v : d) vs[nv++] = v; }
637    const int ncs[] = {8, 4, 1};
638    std::printf("us per launch (200 back-to-back), GB/s of weights+scales; columns 8 / 4 / 1\n%-12s %8s", "shape", "MB");
639    for (int i = 0; i < nv; ++i) std::printf(" | v%-2d 8col  4col  1col", vs[i]);
640    std::printf("\n");
641    const int ncopy = envi("DF2B_COLD", 0) ? 8 : 1;   // cold: the launches rotate over 8 copies of the matrices (> L2), as a round's 5 layers do
642    for (const Shape& s : shapes) {
643        std::vector<std::vector<Mat4>> mats(ncopy); std::vector<float*> ys;
644        for (int k = 0; k < ncopy; ++k)
645            for (int m = 0; m < s.nmat; ++m) { mats[k].push_back(make_mat(s.n_out, s.n_in)); if (k == 0) ys.push_back(dev<float>((size_t) 8 * s.n_out)); }
646        int8_t* xq = dev<int8_t>((size_t) 8 * s.n_in); float* xd = dev<float>((size_t) 8 * s.n_in / 32);
647        {   // random activations (the bit toggling of real data), scales 0.01
648            std::vector<int8_t> hq((size_t) 8 * s.n_in); for (auto& v : hq) v = (int8_t) rnd();
649            std::vector<float> hd((size_t) 8 * s.n_in / 32, 0.01f);
650            Q->memcpy(xq, hq.data(), hq.size()).wait(); Q->memcpy(xd, hd.data(), hd.size() * 4).wait();
651        }
652        const double mb = (double) s.nmat * s.n_out * s.n_in * 0.5625 / 1e6;
653        std::printf("%-12s %8.1f", s.name, mb);
654        for (int i = 0; i < nv; ++i) {
655            df2_gemm_variant(vs[i]);
656            std::printf(" |     ");
657            for (int nc : ncs) {
658                std::vector<std::vector<Df2Mat>> dm(ncopy);
659                for (int k = 0; k < ncopy; ++k) for (int m = 0; m < s.nmat; ++m) dm[k].push_back(Df2Mat{mats[k][m].w, ys[m], s.n_out, true});
660                int it = 0;
661                const double us = time_us([&] { df2_gemm(dm[it++ % ncopy].data(), s.nmat, xq, xd, s.n_in, nc, Q); }, 200);
662                std::printf(" %5.0f(%3.0f)", us, mb * 1e3 / us);
663            }
664        }
665        std::printf("\n");
666        for (auto& v : mats) for (auto& m : v) sycl::free(m.w, *Q);
667        for (auto y : ys) sycl::free(y, *Q);
668        sycl::free(xq, *Q); sycl::free(xd, *Q);
669    }
670    df2_gemm_variant(0);
671    return 0;
672}
673
674int run_attn_bench() {
675    std::printf("device: %s\n", Q->get_device().get_info<sycl::info::device::name>().c_str());
676    float* kr = make_f32((size_t) RING * NKV * HD, 2.f); float* vr = make_f32((size_t) RING * NKV * HD, 2.f);
677    int64_t* tag = dev<int64_t>(RING); float* part = dev<float>((size_t) kDf2PartChunks * 8 * 32 * 130);
678    float* q = make_f32((size_t) 8 * 32 * HD, 2.f); float* kb = make_f32((size_t) 8 * NKV * HD, 2.f); float* vb = make_f32((size_t) 8 * NKV * HD, 2.f);
679    int32_t* ap = dev<int32_t>(1); int32_t* ep = dev<int32_t>(1); int32_t e1 = 1; Q->memcpy(ep, &e1, 4).wait();
680    int8_t* xq = dev<int8_t>(8 * 32 * HD); float* xd = dev<float>(8 * 32 * HD / 32);
681    for (int avk : {1064, 2064, 1128, 2128}) for (int pos : {100, 600, 1200, 2100}) {
682        const int av = avk / 1000, ck = avk % 1000;
683        df2_attention_variant(av); df2_attention_chunk(ck);
684        std::vector<int64_t> t(RING, -1); for (int p = std::max(0, pos - 2047); p < pos; ++p) t[p % RING] = ((int64_t) 1 << 32) | p;
685        Q->memcpy(tag, t.data(), RING * 8).wait();
686        int32_t sv = pos; Q->memcpy(ap, &sv, 4).wait();
687        const double us = time_us([&] { df2_attention(q, kr, vr, kb, vb, ap, RING, WINDOW, part, xq, xd, Q, tag, ep); }, 100);
688        std::printf("attention v%d chunk %d, s = %d: %.0f us per layer\n", av, ck, pos, us);
689    }
690    return 0;
691}
692
693// ---------------------------------------------------------------- dp4a throughput
694extern "C" SYCL_EXTERNAL int __builtin_IB_dp4a_ss(int c, int a, int b);
695template <int MODE> int dp4a_mode(int a, int b, int c) {
696    if constexpr (MODE == 0) return dpct::dp4a(a, b, c);
697    else if constexpr (MODE == 1) return __builtin_IB_dp4a_ss(c, a, b);
698    else {   // sycl vec of int8
699        const auto va = sycl::bit_cast<sycl::vec<int8_t, 4>>(a), vb = sycl::bit_cast<sycl::vec<int8_t, 4>>(b);
700        return c + (int) va[0] * vb[0] + (int) va[1] * vb[1] + (int) va[2] * vb[2] + (int) va[3] * vb[3];
701    }
702}
703template <int MODE> double dp4a_rate(int iters) {
704    const size_t items = 28 * 128 * 64;
705    int* out = dev<int>(items);
706    int* in = dev<int>(items); std::vector<int> h(items); for (auto& v : h) v = (int) rnd() * 2654435761u; up(in, h);
707    auto run = [&] {
708        Q->parallel_for(sycl::nd_range<1>(items, 64), [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(32)]] {
709            const size_t i = it.get_global_id(0);
710            int a0 = in[i], a1 = a0 ^ 0x1234567, a2 = a0 * 3, a3 = a0 + 77, b = in[(i + 1) % items];
711            int s0 = 0, s1 = 0, s2 = 0, s3 = 0;
712            for (int k = 0; k < iters; ++k) {
713                s0 = dp4a_mode<MODE>(a0, b, s0); s1 = dp4a_mode<MODE>(a1, b, s1); s2 = dp4a_mode<MODE>(a2, b, s2); s3 = dp4a_mode<MODE>(a3, b, s3);
714                a0 += 0x01010101; a1 += 0x01010101; a2 += 0x01010101; a3 += 0x01010101;
715            }
716            out[i] = s0 + s1 + s2 + s3;
717        });
718    };
719    run(); Q->wait();
720    const auto t0 = Clock::now(); run(); Q->wait();
721    const double sec = ms_since(t0) * 1e-3;
722    sycl::free(out, *Q); sycl::free(in, *Q);
723    return (double) items * iters * 4 / sec / 1e9;
724}
725int run_dp4a() {
726    std::printf("dp4a throughput, Gdp4a/s (4 independent chains): dpct::dp4a %.0f   __builtin_IB_dp4a_ss %.0f   vec<int8,4> %.0f\n", dp4a_rate<0>(20000), dp4a_rate<1>(20000), dp4a_rate<2>(20000));
727    return 0;
728}
729
730int run_bubble() {
731    float* a = dev<float>(4096); Q->memset(a, 0, 4096 * 4).wait();
732    const double us1 = time_us([&] { Q->single_task([] {}); }, 2000);
733    const double us2 = time_us([&] { Q->parallel_for(sycl::nd_range<1>(256, 64), [=](sycl::nd_item<1> it) { a[it.get_global_id(0)] += 1.f; }); }, 2000);
734    const double us3 = time_us([&] { Q->parallel_for(sycl::nd_range<1>(32 * 28 * 64, 64), [=](sycl::nd_item<1> it) { a[it.get_global_id(0) & 4095] += 1.f; }); }, 2000);
735    std::printf("back-to-back in-order launches, us each: empty single_task %.1f   256-item kernel %.1f   57344-item kernel %.1f\n", us1, us2, us3);
736    return 0;
737}
738
739int run_small() {
740    Drafter D; D.nd = 7; build(D); reset_ring(D, 600);
741    set_meta(D, 3, 600, 5);
742    Layer& y = D.ly[0];
743    {
744        const double d0 = time_us([&] { gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp); }, 200);
745        std::printf("gate_up right after build (before any round): %.1f us\n", d0);
746        record_round(D, 3); Q->wait();
747        const double d1 = time_us([&] { gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp); }, 200);
748        std::printf("gate_up after one round: %.1f us\n", d1);
749        const double d2 = time_us([&] { df2_attention(D.qn, D.kring[0], D.vring[0], D.kblk, D.vv, D.d_meta + 17, RING, WINDOW, D.part, D.xqp, D.xdp, Q, D.tag, D.d_meta + 18); }, 200);
750        std::printf("attention after one round: %.1f us\n", d2);
751    }
752    auto t_all = [&](const char* tag) {
753        const double a = time_us([&] { df2_conv_quant(D.xn, D.dyn, 640, y.base_m, B, N, GROUP, D.xqp, D.xdp, Q); }, 1000);
754        const double b = time_us([&] { df2_conv_rms_quant(D.o, D.dyn + 320, 640, y.base_a + 2 * N, D.h, y.ln_post, D.xn, B, N, GROUP, EPS, D.xqp, D.xdp, Q); }, 1000);
755        const double c = time_us([&] { df2_swiglu_quant(D.gate, D.up, B, I, D.xqp, D.xdp, Q); }, 1000);
756        const double d = time_us([&] { gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp); }, 200);
757        const double e = time_us([&] { df2_attention(D.qn, D.kring[0], D.vring[0], D.kblk, D.vv, D.d_meta + 17, RING, WINDOW, D.part, D.xqp, D.xdp, Q, D.tag, D.d_meta + 18); }, 200);
758        std::printf("%-26s conv_quant %6.1f  conv_rms_quant %6.1f  swiglu_quant %6.1f  gate_up %6.1f  attention %6.1f  (us)\n", tag, a, b, c, d, e);
759    };
760    t_all("in-round data");
761    Q->memset(D.xn, 0, (size_t) B * N * 4); Q->memset(D.dyn, 0, (size_t) B * 640 * 4); Q->memset(D.o, 0, (size_t) B * N * 4); Q->memset(D.h, 0, (size_t) B * N * 4);
762    Q->memset(D.gate, 0, (size_t) B * I * 4); Q->memset(D.up, 0, (size_t) B * I * 4); Q->memset(D.xqp, 0, (size_t) 5 * N * 8); Q->memset(D.xdp, 0, 5 * N / 32 * 8 * 4); Q->wait();
763    t_all("zeroed activations");
764    std::vector<float> hx((size_t) B * N); for (auto& v : hx) v = frand();
765    Q->memcpy(D.xn, hx.data(), hx.size() * 4); Q->memcpy(D.o, hx.data(), hx.size() * 4); Q->memcpy(D.h, hx.data(), hx.size() * 4);
766    std::vector<float> hg((size_t) B * I); for (auto& v : hg) v = frand();
767    Q->memcpy(D.gate, hg.data(), hg.size() * 4); Q->memcpy(D.up, hg.data(), hg.size() * 4);
768    std::vector<float> hd((size_t) B * 640); for (auto& v : hd) v = frand();
769    Q->memcpy(D.dyn, hd.data(), hd.size() * 4); Q->wait();
770    t_all("small random activations");
771    return 0;
772}
773
774int run_poison() {
775    Drafter D; D.nd = 7; build(D); reset_ring(D, 600);
776    set_meta(D, 3, 600, 5);
777    Q->memcpy(D.d_meta, D.h_meta, 128).wait();
778    Layer& y = D.ly[0];
779    auto probe = [&](const char* after) {
780        const double e = time_us([&] { df2_attention(D.qn, D.kring[0], D.vring[0], D.kblk, D.vv, D.d_meta + 17, RING, WINDOW, D.part, D.xqp, D.xdp, Q, D.tag, D.d_meta + 18); }, 100);
781        const double g = time_us([&] { gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp); }, 100);
782        const double c = time_us([&] { df2_conv_quant(D.xn, D.dyn, 640, y.base_m, B, N, GROUP, D.xqp, D.xdp, Q); }, 300);
783        std::printf("after %-26s attention %7.1f  gate_up %7.1f  conv_quant %6.1f us\n", after, e, g, c);
784    };
785    probe("build");
786    df2_quant_rows(D.taps, 5 * N, 3, D.xqc, D.xdc, Q); Q->wait(); probe("quant_rows");
787    df2_rms_quant(D.fcy, D.hidden_norm, nullptr, 3, N, EPS, D.xqc, D.xdc, Q); Q->wait(); probe("rms_quant");
788    df2_kv_ring(D.kc, D.vc, D.ly[0].kn, D.d_meta, D.rt_ctx, D.kring[0], D.vring[0], RING, 3, NKV, EPS, Q, D.tag, D.d_meta + 18); Q->wait(); probe("kv_ring");
789    embed_stub(D); Q->wait(); probe("embed");
790    df2_conv_rms_quant(D.o, D.dyn + 320, 640, y.base_a + 2 * N, D.h, y.ln_post, D.xn, B, N, GROUP, EPS, D.xqp, D.xdp, Q); Q->wait(); probe("conv_rms_quant");
791    df2_swiglu_quant(D.gate, D.up, B, I, D.xqp, D.xdp, Q); Q->wait(); probe("swiglu_quant");
792    df2_norm_rope_qk(D.qv, D.kv, y.qn, y.kn, D.rt_blk, D.qn, D.kblk, B, NH, NKV, EPS, Q); Q->wait(); probe("norm_rope_qk");
793    native_quantize_q8_1(D.fn + N, D.xq_head, N, 7, Q); Q->wait(); probe("native_quantize_q8_1");
794    native_mmvq(GGML_Q8_0, D.head_w, D.xq_head, D.logits, N, NDV, 7, Q); Q->wait(); probe("native_mmvq head");
795    df2_topk(D.logits, NDV, D.dvocab, 7, D.topscratch, D.topval, D.toptok, Q); Q->wait(); probe("topk");
796    df2_select(D.hsel_out, D.topval, D.toptok, D.pred_cb, D.succ_cb, RANK, 7, D.d_meta + 16, D.d_out, (float*) D.d_out + 16, Q); Q->wait(); probe("select");
797    return 0;
798}
799
800int run_poison2() {
801    Drafter D; D.nd = 7; build(D); reset_ring(D, 600);
802    Layer& y = D.ly[0];
803    auto probe = [&](const char* after) {
804        const double e = time_us([&] { df2_attention(D.qn, D.kring[0], D.vring[0], D.kblk, D.vv, D.d_meta + 17, RING, WINDOW, D.part, D.xqp, D.xdp, Q, D.tag, D.d_meta + 18); }, 100);
805        const double g = time_us([&] { gemm2(y.gate, y.up, D.gate, D.up, B, D.xqp, D.xdp); }, 100);
806        const double c = time_us([&] { df2_conv_quant(D.xn, D.dyn, 640, y.base_m, B, N, GROUP, D.xqp, D.xdp, Q); }, 300);
807        std::printf("after %-44s attention %7.1f  gate_up %7.1f  conv_quant %6.1f us\n", after, e, g, c);
808    };
809    const int mode = envi("DF2B_P2", 0);
810    probe("nothing");
811    int32_t* hp = (int32_t*) strata::host_malloc_polled(4096, *Q);   // uncached host
812    int32_t* hc = (int32_t*) sycl::malloc_host(4096, *Q);             // cached host
813    int32_t* sh = (int32_t*) sycl::malloc_shared(4096, *Q);
814    int32_t* dv = dev<int32_t>(1024);
815    std::memset(hp, 0, 4096); std::memset(hc, 0, 4096); Q->memset(dv, 0, 4096).wait();
816    if (mode == 0 || mode == 1) { Q->parallel_for(sycl::range<1>(64), [=](sycl::id<1> i) { dv[i] = hp[i]; }).wait(); probe("kernel READ of uncached host USM"); }
817    if (mode == 0 || mode == 2) { Q->parallel_for(sycl::range<1>(64), [=](sycl::id<1> i) { dv[i] = hc[i]; }).wait(); probe("kernel READ of cached host USM"); }
818    if (mode == 0 || mode == 3) { Q->parallel_for(sycl::range<1>(64), [=](sycl::id<1> i) { hp[i] = (int) i; }).wait(); probe("kernel WRITE of uncached host USM"); }
819    if (mode == 0 || mode == 4) { Q->parallel_for(sycl::range<1>(64), [=](sycl::id<1> i) { hc[i] = (int) i; }).wait(); probe("kernel WRITE of cached host USM"); }
820    if (mode == 0 || mode == 5) { Q->parallel_for(sycl::range<1>(64), [=](sycl::id<1> i) { sh[i] = (int) i; }).wait(); probe("kernel WRITE of shared USM"); }
821    return 0;
822}
823
824// ---------------------------------------------------------------- the prompt's context update (the drafter's tap rows of the last 2047 prompt positions)
825int run_ctx() {
826    Drafter D; D.nd = 7; build(D); reset_ring(D, 0);
827    const int NCH = envi("DF2B_CHUNKS", 256), ROWS = 8;
828    int8_t* hq = (int8_t*) sycl::malloc_host((size_t) ROWS * N, *Q);
829    float* hd = (float*) sycl::malloc_host((size_t) ROWS * (N / 32) * 4, *Q);
830    std::vector<int32_t*> metas;   // per chunk, its own host staging (positions + rope tables), for the modes that do not wait between chunks
831    for (int k = 0; k < NCH; ++k) { set_meta(D, ROWS, (int64_t) k * ROWS, 0); int32_t* m = (int32_t*) sycl::malloc_host((32 + 2048) * 4, *Q); std::memcpy(m, D.h_meta, (32 + 2048) * 4); metas.push_back(m); }
832    auto warm = [&] { for (int i = 0; i < 4; ++i) { set_meta(D, ROWS, 0, 0); Q->memcpy(D.d_meta, D.h_meta, (32 + 2048) * 4); record_context(D, ROWS); } Q->wait(); };
833    warm();
834    auto per = [&](const char* name, double total_ms, int nch) { std::printf("  %-62s %8.1f ms total, %6.3f ms per 8-row chunk (%d chunks = %d rows)\n", name, total_ms, total_ms / nch, nch, nch * ROWS); };
835    std::printf("context update of %d rows in chunks of %d, the Q4_0 pack's shapes (fc 12800 -> 2560, K/V of 5 layers):\n", NCH * ROWS, ROWS);
836    { // (a) as Dflash2Drafter::context does it: host staging + meta copy + the update's kernels + the log's copy to the host + a wait, per chunk
837        const auto t0 = Clock::now();
838        for (int k = 0; k < NCH; ++k) {
839            set_meta(D, ROWS, (int64_t) k * ROWS, 0);
840            Q->memcpy(D.d_meta, D.h_meta, (32 + 2048) * 4);
841            record_context(D, ROWS);
842            Q->memcpy(hq, D.xqc, (size_t) ROWS * N); Q->memcpy(hd, D.xdc, (size_t) ROWS * (N / 32) * 4);
843            Q->wait();
844        }
845        per("(a) the engine's loop: stage, 13 kernels, log copy, wait per chunk", ms_since(t0), NCH);
846    }
847    { // (b) host staging alone
848        const auto t0 = Clock::now();
849        for (int k = 0; k < NCH; ++k) set_meta(D, ROWS, (int64_t) k * ROWS, 0);
850        per("(b) the host staging alone (positions + 16 rope tables)", ms_since(t0), NCH);
851    }
852    { // (c) the kernels alone, queued back to back, one wait
853        const auto t0 = Clock::now();
854        for (int k = 0; k < NCH; ++k) record_context(D, ROWS);
855        Q->wait();
856        per("(c) the 13 kernels alone, queued back to back, one wait", ms_since(t0), NCH);
857    }
858    { // (d) staging copies + kernels queued, no wait per chunk (each chunk's staging from its own buffer)
859        const auto t0 = Clock::now();
860        for (int k = 0; k < NCH; ++k) { Q->memcpy(D.d_meta, metas[(size_t) k], (32 + 2048) * 4); record_context(D, ROWS); }
861        Q->wait();
862        per("(d) meta copy + 13 kernels per chunk, pre-staged, one wait", ms_since(t0), NCH);
863    }
864    { // (e) one chunk, a marker after every launch: GPU ms per kernel of the update
865        D.profile = true;
866        std::vector<std::pair<std::string, double>> tot; int nrun = 0;
867        for (int i = 0; i < 12; ++i) {
868            D.marks.clear(); Q->wait();
869            D.marks.emplace_back("start", Q->single_task([] {}));
870            set_meta(D, ROWS, 0, 0); Q->memcpy(D.d_meta, D.h_meta, (32 + 2048) * 4); record_context(D, ROWS); Q->wait();
871            if (i < 2) continue;
872            ++nrun;
873            for (size_t k = 1; k < D.marks.size(); ++k) {
874                const double a = (double) D.marks[k - 1].second.get_profiling_info<sycl::info::event_profiling::command_end>();
875                const double b = (double) D.marks[k].second.get_profiling_info<sycl::info::event_profiling::command_end>();
876                auto it = std::find_if(tot.begin(), tot.end(), [&](auto& t) { return t.first == D.marks[k].first; });
877                if (it == tot.end()) tot.emplace_back(D.marks[k].first, (b - a) * 1e-6); else it->second += (b - a) * 1e-6;
878            }
879        }
880        D.profile = false;
881        double sum = 0; for (auto& t : tot) sum += t.second;
882        std::printf("  (e) one 8-row chunk, a marker after every launch (each includes its launch gap), ms: total %.3f over %d launches:", sum / nrun, (int) D.marks.size() - 1);
883        for (auto& t : tot) std::printf(" %s %.3f", t.first.c_str(), t.second / nrun);
884        std::printf("\n");
885    }
886    { // (f) weight bytes read per 8-row chunk and the bandwidth the best case would need
887        const double mb = (18.4 + 5 * 3.54);
888        std::printf("  (f) weight bytes per chunk: fc 18.4 MB + 5 x (k,v) 3.5 MB = %.1f MB; for %d chunks %.2f GB; at 400 GB/s that is %.1f ms per 8-row chunk\n", mb, NCH, mb * NCH / 1000.0, mb / 400.0);
889    }
890    return 0;
891}
892}  // namespace
893
894int main(int argc, char** argv) {
895    if (const char* d = std::getenv("DF2B_DUP")) g_dup = d;
896    g_dupn = envi("DF2B_DUPN", 1);
897    g_host_out = envi("DF2B_HOSTOUT", 0); g_meta_kernel = envi("DF2B_METAKERNEL", 0);
898    static sycl::queue q(sycl::gpu_selector_v, sycl::property_list{sycl::property::queue::in_order(), sycl::property::queue::enable_profiling()});
899    Q = &q;
900    const std::string mode = argc > 1 ? argv[1] : "bench";
901    return mode == "test" ? run_tests() : mode == "gemm" ? run_gemm_bench() : mode == "attn" ? run_attn_bench() : mode == "dp4a" ? run_dp4a() : mode == "bubble" ? run_bubble() : mode == "small" ? run_small() : mode == "poison" ? run_poison() : mode == "poison2" ? run_poison2() : mode == "ctx" ? run_ctx() : run_bench();
902}