Something went wrong. Try again.
A single-model (Bonsai-27B) inference engine for Intel Arc GPUs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231/* gemm_tune: sweep tiling params for EVERY engine GEMM shape, under REAL * activation values, to find the best config. * * CRITICAL: the GEMM is value-dependent on Xe2. An earlier version of this * tool used trivial all-zero B + 0x55 (ternary-0) A + 1.0 scales, which ran * ~65% FASTER than real values (degenerate DPAS/FMA chain -> the loop-carried * accumulator acc = acc + d*f stays near zero, less operand-switch / RF * pressure). That made the sweep pick configs that were NOT optimal under * real load. Now: A = random ternary repacked to upload_q2 layout, B = * gpu_rmsnorm_quant on random x with varied row scales. * * Engine GEMM shapes (N=256, transpose_out=true) + per-chunk call counts: * down M=5120 K=17408 (FFN, 64 layers, accum=x) * gate+up M=17408 K=5120 (FFN dual, 64 layers) * qkv M=10240 K=5120 (SSM, 48 layers) * alpha/beta M=48->128 K=5120 (SSM, 48 layers each; weight M padded to 128) * ssm gate M=6144 K=6144 (SSM, 48 layers) * ssm out M=5120 K=6144 (SSM, 48 layers, accum=x) * attn q M=6144 K=5120 (attn, 16 layers) * attn k/v M=1024 K=5120 (attn, 16 layers each) * attn o M=5120 K=6144 (attn, 16 layers, accum=x) [== ssm out] * * Build: meson compile -C build gemm_tune && ./build/gemm_tune */#include "qxmx_gemm.h"#include "qxmx_gpu.h"#include "qxmx.h"#include <sycl/sycl.hpp>#include <chrono>#include <cstdio>#include <cstdlib>#include <cstring>#include <random>#include <vector>
using namespace qx;
static std::mt19937_64 rng(0xA17ULL);static uint16_t f32_to_f16_bits(float f) { return sycl::bit_cast<uint16_t>(sycl::half(f)); }static float f16_to_f32_local(uint16_t b) { return float(sycl::bit_cast<sycl::half>(b)); }
static sycl::queue q(sycl::gpu_selector_v, sycl::property::queue::in_order{});
/* Random ternary Q2 weight, repacked to A_codes [Mp/8][ngrp][2][64B] + * A_sc_t [ngrp][Mp], M padded to 256 (WGM8=32 default loads a 256-row tile * unguarded). Matches engine upload_q2 layout. */struct DevA { uint8_t* codes; float* sc; int Mp, M, K; /* M = original (sc stride + GEMM M param); Mp = pad (codes alloc) */};/* Random ternary Q2 weight, repacked to A_codes [Mp/8][ngrp][2][64B] + * A_sc_t [ngrp][M] (+256-float tail pad), M padded to 256 for the codes alloc * (WGM8=32 loads a 256-row tile unguarded; the sc stride stays the ORIGINAL M * and the tail pad covers the last group's OOB -- matches engine upload_q2). * The GEMM is called with the ORIGINAL M as its M param (the kernel uses M as * the sc stride), NOT Mp. */static DevA synth_real_a(int M, int K) { int ngrp = K / QX_QK2; int Mp = (M + 255) & ~255; std::vector<qx_block_q2> W((size_t)M * ngrp); std::normal_distribution<float> g(0, 1); for (int m = 0; m < M; m++) for (int gg = 0; gg < ngrp; gg++) { W[(size_t)m * ngrp + gg].d = f32_to_f16_bits(0.05f * std::fabs(g(rng)) + 0.001f); for (int b = 0; b < QX_QK2 / 4; b++) { uint8_t p = 0; for (int k = 0; k < 4; k++) p |= (uint8_t)((rng() % 3) << (k * 2)); W[(size_t)m * ngrp + gg].qs[b] = p; } } size_t csz = (size_t)Mp * ngrp * 16; size_t ssz = (size_t)ngrp * M + 256; std::vector<uint8_t> codes_h(csz, 0); std::vector<float> sc_h(ssz, 0); for (int m8 = 0; m8 < M / 8; m8++) for (int gg = 0; gg < ngrp; gg++) for (int hf = 0; hf < 2; hf++) for (int r = 0; r < 8; r++) memcpy(codes_h.data() + (((size_t)(m8 * ngrp + gg) * 2 + hf) * 64 + r * 8), W[(m8 * 8 + r) * ngrp + gg].qs + hf * 8, 8); for (int m = 0; m < M; m++) for (int gg = 0; gg < ngrp; gg++) sc_h[gg * M + m] = f16_to_f32_local(W[m * ngrp + gg].d); DevA d; d.codes = sycl::malloc_shared<uint8_t>(csz, q); d.sc = sycl::malloc_shared<float>(ssz, q); q.memcpy(d.codes, codes_h.data(), csz).wait(); q.memcpy(d.sc, sc_h.data(), ssz * 4).wait(); d.Mp = Mp; d.M = M; d.K = K; return d;}
/* Real B from gpu_rmsnorm_quant on random x[N][K] with varied row scales. * Produces bvnni [K/4][N] int32, sum_a [K/64][N], act_scale [N] — the exact * layout the GEMM consumes. */struct DevB { int8_t* bvn; int32_t* sum; float* asc; int K, N;};static DevB synth_real_b(int K, int N) { DevB r; r.K = K; r.N = N; r.bvn = sycl::malloc_device<int8_t>((size_t)K * N, q); r.sum = sycl::malloc_device<int32_t>((size_t)(K / 64) * N, q); r.asc = sycl::malloc_shared<float>(N, q); float* x = sycl::malloc_shared<float>((size_t)N * K, q); std::normal_distribution<float> g(0, 1); std::vector<float> hx((size_t)N * K); for (int t = 0; t < N; t++) { float sc = (t % 17 == 3) ? 0.0f : powf(10, -2 + 3 * ((t % 13) / 12.0f)); for (int i = 0; i < K; i++) hx[(size_t)t * K + i] = g(rng) * sc; } q.memcpy(x, hx.data(), (size_t)N * K * 4).wait(); /* rmsnorm needs a weight vector of length K (use all-1.0 — values don't * affect the GEMM's view of B, only the activation distribution). */ std::vector<float> w(K, 1.0f); float* d_w = sycl::malloc_shared<float>(K, q); q.memcpy(d_w, w.data(), K * 4).wait(); gpu_rmsnorm_quant(q, x, d_w, K, r.bvn, r.sum, r.asc, N); q.wait(); sycl::free(x, q); sycl::free(d_w, q); return r;}
struct Cfg { int w, n, g; };static Cfg single_cfgs[] = { {16, 4, 6}, {16, 4, 8}, {16, 4, 12}, {16, 4, 16}, {16, 8, 6}, {16, 8, 8}, {32, 4, 6}, {32, 4, 8}, { 8, 4, 6}, { 8, 4, 8}, { 8, 8, 6},};static Cfg dual_cfgs[] = { {16, 4, 6}, {16, 4, 8}, {16, 4, 16}, {16, 8, 6}, {16, 8, 8}, {32, 4, 6}, {32, 4, 8},};
static void set_cfg(const Cfg& c) { char buf[64]; std::snprintf(buf, sizeof buf, "%d", c.w); setenv("QXMX_GEMM_WGM8", buf, 1); std::snprintf(buf, sizeof buf, "%d", c.n); setenv("QXMX_GEMM_WGN16", buf, 1); std::snprintf(buf, sizeof buf, "%d", c.g); setenv("QXMX_GEMM_GPP", buf, 1);}
template <class Fn>static double bench(Fn fn, int reps) { fn(); q.wait(); // warm auto t0 = std::chrono::steady_clock::now(); for (int r = 0; r < reps; r++) fn(); q.wait(); auto t1 = std::chrono::steady_clock::now(); return std::chrono::duration<double, std::milli>(t1 - t0).count() / reps;}
int main() { int REPS = 40, N = 256;
struct Shape { const char* name; int M, K; bool accum; }; Shape singles[] = { {"down (FFN, accum)", 5120, 17408, true}, {"qkv (SSM)", 10240, 5120, false}, {"alpha/beta (SSM, pad128)", 128, 5120, false}, {"ssm gate", 6144, 6144, false}, {"ssm out / attn o (accum)", 5120, 6144, true}, {"attn q", 6144, 5120, false}, {"attn k/v", 1024, 5120, false}, };
printf("=== single GEMM sweep (N=256, transpose_out=true, REAL A+B) ===\n"); for (auto& sh : singles) { DevA A = synth_real_a(sh.M, sh.K); DevB B = synth_real_b(sh.K, N); float* C = sycl::malloc_shared<float>((size_t)A.Mp * N, q); printf("\n[%s] M=%d (pad %d) K=%d N=%d (A.codes=%zu MB, B.bvn=%zu MB)\n", sh.name, sh.M, A.Mp, sh.K, N, (size_t)A.Mp * (sh.K/64) * 16 / (1024*1024), (size_t)sh.K * N / (1024*1024)); fflush(stdout); double best = 1e9; Cfg bestc{}; for (auto c : single_cfgs) { set_cfg(c); double ms = bench([&]{ gemm_gpu_v4(q, A.codes, A.sc, B.bvn, B.sum, B.asc, C, A.M, sh.K, N, /*accum=*/sh.accum ? C : nullptr, false, /*transpose_out=*/true); }, REPS); const char* tag = (c.w==16 && c.n==4 && c.g==16) ? " (old default)" : ""; printf(" WGM8=%2d WGN16=%d GPP=%2d: %.3f ms/call%s\n", c.w, c.n, c.g, ms, tag); if (ms < best) { best = ms; bestc = c; } } printf(" BEST: WGM8=%2d WGN16=%d GPP=%2d %.3f ms/call\n", bestc.w, bestc.n, bestc.g, best); sycl::free(A.codes, q); sycl::free(A.sc, q); sycl::free(B.bvn, q); sycl::free(B.sum, q); sycl::free(B.asc, q); sycl::free(C, q); }
/* Dual (gate+up). */ { int M = 17408, K = 5120; DevA A0 = synth_real_a(M, K); DevA A1 = synth_real_a(M, K); DevB B = synth_real_b(K, N); float* C0 = sycl::malloc_shared<float>((size_t)A0.Mp * N, q); float* C1 = sycl::malloc_shared<float>((size_t)A0.Mp * N, q); printf("\n=== dual GEMM sweep (gate+up, N=256, REAL A+B) ===\n"); printf("[gate+up (FFN dual)] M=%d (pad %d) K=%d N=%d\n", M, A0.Mp, K, N); double best = 1e9; Cfg bestc{}; for (auto c : dual_cfgs) { set_cfg(c); double ms = bench([&]{ gemm_gpu_v4_dual(q, A0.codes, A0.sc, A1.codes, A1.sc, B.bvn, B.sum, B.asc, C0, C1, A0.M, K, N, nullptr, nullptr, false, true); }, REPS); const char* tag = (c.w==16 && c.n==4 && c.g==16) ? " (old default)" : ""; printf(" WGM8=%2d WGN16=%d GPP=%2d: %.3f ms/call%s\n", c.w, c.n, c.g, ms, tag); if (ms < best) { best = ms; bestc = c; } } printf(" BEST: WGM8=%2d WGN16=%d GPP=%2d %.3f ms/call\n", bestc.w, bestc.n, bestc.g, best); sycl::free(A0.codes, q); sycl::free(A0.sc, q); sycl::free(A1.codes, q); sycl::free(A1.sc, q); sycl::free(B.bvn, q); sycl::free(B.sum, q); sycl::free(B.asc, q); sycl::free(C0, q); sycl::free(C1, q); } return 0;}