Something went wrong. Try again.
A single-model (Bonsai-27B) inference engine for Intel Arc GPUs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221/* qxmx_bench: llama-bench-style prompt-processing + decode matrix. * * ./qxmx_bench model.gguf [-f prefix_file] [-pp PP] [-depth DEPTH] * [-tg TG] [-n N] [-ctk fp16|q8_0|fp8] * * Two measurement modes, mirroring llama-bench's pp/tg axes: * * pp<PP>@depth<DEPTH> (-pp PP -depth DEPTH, default mode) * Measures prefill throughput of a PP-token chunk loaded on top of DEPTH * tokens of existing KV context. The "prompt matrix" axis llama-bench * reports as pp<PP>@depth<DEPTH>. Examples: * pp512@depth0, pp2048@depth0, pp512@depth8192, pp2048@depth8192 * * tg<TG>@depth<DEPTH> (-tg TG -depth DEPTH) * Measures decode (token-generation) throughput of TG greedy steps on top * of DEPTH tokens of existing KV context. Mirrors llama-bench's tg axis: * tg128@depth0, tg128@depth8192, ... * * Method: * 1. open_slot, init(max_ctx = depth + max(pp, tg) + slack). * 2. prefill `depth` prefix tokens (untimed warmup -- fills KV to `depth`). * For -tg: also prefill the PP chunk (untimed) so the first TG step has * the full depth+0 context, then time TG forward steps. * 3. Drain the queue (the engine is single in-order; the final prefill sync * in step 2 guarantees the prefix is complete before timing). * 4. Time the measured work: * pp: prefill(slot, chunk, PP) * tg: TG greedy decode steps (forward + sample), no stop conditions * 5. Repeat N runs (reset between runs), report median tok/s. * * Prefix + chunk token ids are read from a file (-f) if given, else sampled * uniformly from the model's vocab (deterministic LCG, never zero-filled). * Tokens are ids, not weights, so the value-dependence trap doesn't apply -- * but we use real text anyway to avoid any unforeseen degenerate path. */#include "qxmx_gpu.h"#include "qxmx_model.h"#include "qxmx_prefix_cache.h"#include "qxmx_tokenizer.h"#include "qxmx.h"#include "qxmx_device.h"
#include <algorithm>#include <chrono>#include <cstdio>#include <cstdlib>#include <cstring>#include <fstream>#include <memory>#include <string>#include <vector>
namespace qx { extern qx_model* g_model; }
int main(int argc, char** argv) { const char* model_path = nullptr; const char* prompt_file = nullptr; int pp = 0; /* pp chunk size; 0 = skip pp */ int tg = 0; /* tg step count; 0 = skip tg */ int depth = 0; int runs = 3; const char* ctk = "q8_0"; bool use_cached = false; /* route PP through prefill_cached with a fresh empty cache */ bool warm_cache = false; /* prime the cache on the first run so subsequent runs see a hit */
for (int i = 1; i < argc; i++) { std::string a = argv[i]; if (a == "-f" && i + 1 < argc) prompt_file = argv[++i]; else if (a == "-pp" && i + 1 < argc) pp = std::atoi(argv[++i]); else if (a == "-tg" && i + 1 < argc) tg = std::atoi(argv[++i]); else if (a == "-depth"&& i + 1 < argc) depth = std::atoi(argv[++i]); else if (a == "-n" && i + 1 < argc) runs = std::atoi(argv[++i]); else if (a == "-ctk" && i + 1 < argc) ctk = argv[++i]; else if (a == "--use-cached") use_cached = true; else if (a == "--warm-cache") warm_cache = true; else if (a == "-h" || a == "--help") { std::printf("usage: %s model.gguf [-f file] [-pp PP] [-tg TG] [-depth D] [-n N] [-ctk fp16|q8_0|fp8] [--use-cached] [--warm-cache]\n" " -pp PP measure prompt-processing of PP tokens (default 0 = skip)\n" " -tg TG measure TG decode of TG steps (default 0 = skip)\n" " -depth D existing KV context before the measured work\n" " --use-cached route PP through prefill_cached() (matches qxmx_serve)\n" " --warm-cache prime the cache on run 0 so runs 1..N-1 see a hit\n" " both -pp and -tg may be set in one invocation (runs each axis).\n", argv[0]); return 0; } else if (!model_path) model_path = argv[i]; else { std::fprintf(stderr, "unexpected arg: %s\n", a.c_str()); return 1; } } if (!model_path) { std::fprintf(stderr, "usage: %s model.gguf ...\n", argv[0]); return 1; } if (pp == 0 && tg == 0) { std::fprintf(stderr, "need at least one of -pp / -tg\n"); return 1; } if (pp && (pp < 16 || (pp % 16) != 0)) { std::fprintf(stderr, "-pp=%d must be a multiple of 16 (engine chunk alignment)\n", pp); return 1; } if (tg < 0) { std::fprintf(stderr, "-tg=%d must be >= 0\n", tg); return 1; }
qx_model m; char err[256]; if (qx_model_open(&m, model_path, err, sizeof err)) { std::fprintf(stderr, "load: %s\n", err); return 1; } qx::tokenizer tok; if (!tok.load(&m.gguf, err, sizeof err)) { std::fprintf(stderr, "tok: %s\n", err); return 1; }
/* Build the token stream: prefer a real file (realistic token spread), * else sample uniformly from the vocab. Need depth + pp tokens for the * prefix+chunk; TG continues from the pp chunk's last logits (greedy), so * TG tokens come from the model, not the stream. */ int need = depth + pp; std::vector<int32_t> stream; if (prompt_file) { std::ifstream f(prompt_file); std::string s((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); tok.encode(s, &stream); } if ((int)stream.size() < need) { /* Pad with uniform-random vocab ids (deterministic LCG, reproducible). * Tokens are ids, not weights -- safe regardless of value; we only * avoid zero-fill on principle. */ uint32_t st = 0x12345678u; while ((int)stream.size() < need) { st = st * 1103515245u + 12345u; stream.push_back((int)(st % QX_N_VOCAB)); } }
int max_ctx = std::max(need + tg + 64, 1 << 14); qx::g_model = &m; auto eng = std::make_unique<qx::engine>(); if (std::strcmp(ctk, "fp16") == 0) eng->k_ctk = qx::engine::KCT::fp16; else if (std::strcmp(ctk, "q8_0") == 0) eng->k_ctk = qx::engine::KCT::q8_0; else if (std::strcmp(ctk, "fp8") == 0) eng->k_ctk = qx::engine::KCT::fp8; else { std::fprintf(stderr, "-ctk: unknown '%s' (fp16|q8_0|fp8)\n", ctk); return 1; } eng->init(&m, max_ctx); int slot = eng->open_slot();
std::printf("device: %s\n", qx::device_name(eng->q).c_str()); std::printf("config: pp=%d tg=%d depth=%d runs=%d max_ctx=%d K=%s path=%s\n", pp, tg, depth, runs, max_ctx, ctk, use_cached ? (warm_cache ? "prefill_cached(warm)" : "prefill_cached(cold)") : "prefill(plain)");
/* Empty prefix cache, geometry matches qxmx_serve (qxmx_serve.cpp:715-718). * Only used when --use-cached. Freshly constructed -> every call is a * cold-cache run unless --warm-cache primed it on the first iteration. */ qx::prefix_cache cache; if (use_cached) { size_t s_state = (size_t)QX_SSM_DT_RANK * QX_SSM_STATE * QX_SSM_STATE * sizeof(float); size_t s_conv = (size_t)(QX_SSM_CONV_K - 1) * QX_SSM_CONVD * sizeof(float); size_t k_row = (eng->k_ctk == qx::engine::KCT::fp16) ? 2048 : 1088; cache.init(eng->n_attn, eng->n_ssm, k_row, 640, s_state, s_conv); }
/* Greedy sampling (temperature=0) for TG -- argmax over logits. */ qx::sample_params sp;
/* ---- PP axis: timed prefill of PP tokens on top of DEPTH. ---- */ if (pp) { std::vector<double> tps; for (int r = 0; r < runs; r++) { eng->reset(slot); if (depth > 0) eng->prefill(slot, stream.data(), depth); auto t0 = std::chrono::steady_clock::now(); if (use_cached) { int computed = 0; int restored = eng->prefill_cached(slot, stream.data() + depth, pp, &cache, &computed); (void)restored; (void)computed; } else { eng->prefill(slot, stream.data() + depth, pp); } auto t1 = std::chrono::steady_clock::now(); double ms = std::chrono::duration<double, std::milli>(t1 - t0).count(); double t = pp / (ms / 1e3); tps.push_back(t); std::printf(" [pp run %d] %.1f ms, %.0f tok/s\n", r + 1, ms, t); } std::sort(tps.begin(), tps.end()); double med = tps[tps.size() / 2]; std::printf("pp%d@depth%d: median %.0f tok/s (over %d runs)\n", pp, depth, med, runs); }
/* ---- TG axis: timed greedy decode of TG steps on top of DEPTH. ---- * The slot is preloaded with `depth` prefix tokens (untimed), then the * first token is sampled from the prefix's final logits and TG steps * are timed. forward() advances the KV cache by one; sample() reads * logits (pure host). No stop conditions -- we want exactly TG steps. */ if (tg) { std::vector<double> tps; for (int r = 0; r < runs; r++) { eng->reset(slot); if (depth > 0) eng->prefill(slot, stream.data(), depth); /* Seed the first generated token from the prefix's final logits. * Then time TG-1 forward+sample steps (the first sample is the * "transition" into TG and not counted, mirroring llama-bench). */ int next = eng->sample(slot, sp); auto t0 = std::chrono::steady_clock::now(); for (int step = 1; step < tg; step++) { eng->forward(slot, next); next = eng->sample(slot, sp); } auto t1 = std::chrono::steady_clock::now(); double ms = std::chrono::duration<double, std::milli>(t1 - t0).count(); double t = (tg - 1) / (ms / 1e3); tps.push_back(t); std::printf(" [tg run %d] %.1f ms, %.0f tok/s\n", r + 1, ms, t); } std::sort(tps.begin(), tps.end()); double med = tps[tps.size() / 2]; std::printf("tg%d@depth%d: median %.0f tok/s (over %d runs)\n", tg, depth, med, runs); } return 0;}