Something went wrong. Try again.
A single-model (Bonsai-27B) inference engine for Intel Arc GPUs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182/* qxmx_run: decode driver for Ternary-Bonsai-27B. * * ./qxmx_run [--cpu|--gpu] [-ctk fp16|q8_0|fp8] model.gguf "prompt" [-n N] * * --gpu (default): the B70 SYCL engine — matmuls via XMX DPAS (240-468 GB/s), * DeltaNet + elementwise on GPU/host mix. * --cpu: the frozen fp32 CPU oracle (~12 s/fwd, obviously correct). * -ctk: K cache format (GPU only; default q8_0). fp16 = conservative, * q8_0 = llama.cpp's 8-bit (per-32-block fp16 scale + int8), * fp8 = e4m3 + per-32-block fp16 scale. V cache is always * TurboQuant 4-bit (WHT + Lloyd-Max) regardless of -ctk. * * Both backends implement qx::engine_i; the flag just picks one. This lets * every GPU-path change be A/B-validated on the same prompt by flipping a flag. */#include "qxmx_engine.h"#include "qxmx_cpu.h"#include "qxmx_gpu.h"#include "qxmx_model.h"#include "qxmx_stop.h"#include "qxmx_tokenizer.h"#include "qxmx_device.h"
#include <chrono>#include <cstdio>#include <cstring>#include <fstream>#include <iostream>#include <memory>#include <sstream>#include <string>#include <vector>
namespace qx { extern qx_model* g_model; } // for embedding access (GPU path)
int main(int argc, char** argv) { bool use_cpu = false; int n_gen = 64; std::vector<const char*> pos; qx::engine::KCT k_ctk = qx::engine::KCT::q8_0; const char* prompt_file = nullptr; qx::sample_params sp; /* M2.2: greedy by default */ uint64_t sp_seed = 0; bool sp_seed_set = false; qx::stop_policy stopp; /* M2.3: stop conditions */ std::vector<std::string> stop_strs; bool max_tokens_set = false;
for (int i = 1; i < argc; i++) { std::string a = argv[i]; if (a == "--cpu") use_cpu = true; else if (a == "--gpu") use_cpu = false; else if (a == "-n" && i + 1 < argc) n_gen = std::atoi(argv[++i]); else if (a == "-ctk" && i + 1 < argc) { std::string v = argv[++i]; if (v == "fp16") k_ctk = qx::engine::KCT::fp16; else if (v == "q8_0") k_ctk = qx::engine::KCT::q8_0; else if (v == "fp8") k_ctk = qx::engine::KCT::fp8; else { std::fprintf(stderr, "-ctk: unknown type '%s' (fp16|q8_0|fp8)\n", v.c_str()); return 1; } } else if (a == "-f" && i + 1 < argc) prompt_file = argv[++i]; else if (a == "--temp" && i + 1 < argc) sp.temperature = std::strtof(argv[++i], nullptr); else if (a == "--top-k" && i + 1 < argc) sp.top_k = std::atoi(argv[++i]); else if (a == "--top-p" && i + 1 < argc) sp.top_p = std::strtof(argv[++i], nullptr); else if (a == "--seed" && i + 1 < argc) { sp_seed = std::strtoull(argv[++i], nullptr, 0); sp_seed_set = true; } else if (a == "--stop" && i + 1 < argc) stop_strs.push_back(argv[++i]); else if (a == "--max-tokens" && i + 1 < argc) { stopp.max_tokens = std::atoi(argv[++i]); max_tokens_set = true; } else pos.push_back(argv[i]); }
if (pos.size() < 2 && !prompt_file) { std::fprintf(stderr, "usage: %s [--cpu|--gpu] [-ctk fp16|q8_0|fp8] model.gguf \"prompt\" [-n N]\n", argv[0]); std::fprintf(stderr, " %s [--cpu|--gpu] [-ctk ...] -f prompt.txt model.gguf [-n N]\n", argv[0]); return 1; }
std::string prompt_str; if (prompt_file) { std::ifstream f(prompt_file); if (!f) { std::fprintf(stderr, "prompt file '%s': cannot open\n", prompt_file); return 1; } std::stringstream ss; ss << f.rdbuf(); prompt_str = ss.str(); } else { prompt_str = pos[1]; } /* model.gguf is pos[0] if prompt on cmdline, or pos[0] if -f used (order- * independent: the non-flag positional arg is the model). */ const char* model_path = pos[0];
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; }
/* M2.3: build stop_policy from GGUF EOS + caller flags. */ stopp.add_eos(tok.eos_id); for (auto& s : stop_strs) stopp.add_stop_string(std::move(s)); if (!max_tokens_set) stopp.max_tokens = n_gen; /* -n still caps by default */
std::vector<int32_t> ids; tok.encode(prompt_str, &ids); std::printf("prompt: %zu tokens\n", ids.size());
int max_ctx = std::max((int)ids.size() + n_gen + 16, 1 << 14); qx::g_model = &m; std::unique_ptr<qx::engine_i> eng = use_cpu ? std::unique_ptr<qx::engine_i>(std::make_unique<qx::cpu_engine>()) : std::unique_ptr<qx::engine_i>(std::make_unique<qx::engine>()); if (!use_cpu) { qx::engine* gpu = static_cast<qx::engine*>(eng.get()); gpu->k_ctk = k_ctk; const char* ctk_name = (k_ctk == qx::engine::KCT::fp16) ? "fp16" : (k_ctk == qx::engine::KCT::q8_0) ? "q8_0" : "fp8"; std::printf("K cache: %s (V cache: TurboQuant 4-bit WHT+LM)\n", ctk_name); } eng->init(&m, max_ctx); int slot = eng->open_slot(); if (use_cpu) { std::printf("device: CPU (fp32 oracle)\n"); } else { qx::engine* gpu = static_cast<qx::engine*>(eng.get()); std::printf("device: %s\n", qx::device_name(gpu->q).c_str()); } if (sp_seed_set) eng->seed(slot, sp_seed);
auto t0 = std::chrono::steady_clock::now();
/* prefill: GPU engine uses the batched _b path (chunks of 256); CPU engine * runs per-token forward(). Leaves logits for the last prompt token. */ eng->prefill(slot, ids.data(), (int)ids.size());
/* generate. stop_policy governs termination (M2.3): EOS id, stop-string * suffix match over the decoded text tail, max_tokens, ctx exhaustion. * M2.4: incremental_decoder feeds the stop matcher a UTF-8-boundary-safe * buffer (never a partial codepoint) and streams whole codepoints only. * Loop invariant: `next` is always pushed to the decoder BEFORE the stop * check, so no sampled token is dropped from the stream. */ std::vector<int32_t> gen; qx::incremental_decoder dec(tok); std::string streamed; int next = eng->sample(slot, sp); gen.push_back(next); for (int step = 1; step < n_gen; step++) { streamed += dec.push(next); auto r = stopp.check((uint32_t)next, dec.buffer(), (int)gen.size(), (int64_t)ids.size() + (int64_t)gen.size()); if (r != qx::stop_policy::reason::none) break; eng->forward(slot, next); next = eng->sample(slot, sp); gen.push_back(next); } /* Push the final sampled token (the loop body pushes the previous one * before sampling the next, so the last needs an explicit push). */ streamed += dec.push(next); streamed += dec.finish(); /* flush any remainder (empty at EOS) */
auto t1 = std::chrono::steady_clock::now(); double sec = std::chrono::duration<double>(t1 - t0).count();
/* `streamed` is the UTF-8-safe decoded output (whole codepoints only); * the bulk decode below is kept as a cross-check that they match. */ std::string text = tok.decode(gen.data(), gen.size(), true); std::printf("generated (%d tokens):\n%s\n", (int)gen.size(), text.c_str()); if (streamed != text) std::fprintf(stderr, "M2.4 mismatch: incremental vs bulk decode differ (%zu vs %zu bytes)\n", streamed.size(), text.size()); if (auto fr = qx::stop_policy::finish_reason( stopp.check((uint32_t)next, streamed, (int)gen.size(), (int64_t)ids.size() + (int64_t)gen.size()))) std::printf("finish_reason: %s\n", fr); int total_fwd = ids.size() + gen.size(); std::printf("\n%.2f s, %d fwd, %.1f ms/fwd, ~%.1f tok/s\n", sec, total_fwd, sec * 1e3 / total_fwd, gen.size() / (sec > 0 ? sec : 1)); qx_model_close(&m); return 0;}