Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions bench/cuda/speed/bench_qwen_prefill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ int main(int argc, char** argv) {
std::printf("=== 2a. projection GEMV(M=1) vs batched GEMM (per prompt token) ===\n");
const int64_t MB = qm::PREFILL_CHUNK; // the chunk production runs
struct Shape { const char* name; int64_t K, N; const qm::wmat* w; }; // one bf16 run
qm::Layer& L0 = M.layers[0];
qm::Layer& L0 = M.layers[0]; // layer 0's own widths (a family's may vary by layer)
const Shape shapes[] = {
{"QKV.fused", C.NE, (C.NH + 2 * C.NKV) * C.HD, &L0.wqkv_row},
{"wo", C.NH * C.HD, C.NE, &L0.wo_row},
{"gateup.fused", C.NE, 2 * C.FF, &L0.wgu_row},
{"wd", C.FF, C.NE, &L0.wd_row},
{"QKV.fused", C.NE, C.attn_proj(0), &L0.wqkv_row},
{"wo", C.NH * C.hd(0), C.NE, &L0.wo_row},
{"gateup.fused", C.NE, 2 * C.ff(0), &L0.wgu_row},
{"wd", C.ff(0), C.NE, &L0.wd_row},
};
int64_t maxK = 0, maxN = 0;
for (const Shape& s : shapes) {
Expand Down Expand Up @@ -284,12 +284,13 @@ int main(int argc, char** argv) {
"speedup");
for (int64_t T : {(int64_t)128, (int64_t)512, (int64_t)2048}) {
if (T > max_T) break;
tl::kv_cache c;
if (!c.init(C.NKV, C.MAXC, C.HD)) { std::printf(" cache init failed\n"); return 1; }
void* ks = zeros(C.NKV * T * C.HD);
void* vs = zeros(C.NKV * T * C.HD);
void* qs = zeros(C.NH * T * C.HD);
void* os = zeros(C.NH * T * C.HD);
tl::kv_cache c; // layer 0's heads
const int64_t nkv = C.nkv(0), hd = C.hd(0);
if (!c.init(nkv, C.MAXC, hd)) { std::printf(" cache init failed\n"); return 1; }
void* ks = zeros(nkv * T * hd);
void* vs = zeros(nkv * T * hd);
void* qs = zeros(C.NH * T * hd);
void* os = zeros(C.NH * T * hd);
if (!ks || !vs || !qs || !os) { std::printf(" alloc failed\n"); return 1; }

// Per-token: append + attend at every position 0..T-1 (ctx grows).
Expand Down
2 changes: 1 addition & 1 deletion bench/cuda/speed/check_quant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char** argv) {
const std::vector<float> got(lg, lg + M.c.VOCAB);
const qm::logits_vs c = qm::compare_logits(oracle_prompt, got, bound);
const double kl = qm::kl_div(oracle_prompt, got);
const bool ok = (c.same_top || c.near_tie) && (exact ? c.err < bound : kl < 0.3);
const bool ok = exact ? (c.same_top || c.near_tie) && c.err < bound : qm::close_by_kl(c, kl);
ok_all = ok_all && ok;
std::printf("prefill 200 tokens (%s) | first token %lld vs %lld | logits worst |d|/(1+max) %.2e, "
"KL %.2e %s\n",
Expand Down
19 changes: 16 additions & 3 deletions bench/models/chat_qwen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ static double ms_since(clk::time_point t) {

namespace qm = qwenmodel;

// What a Gemma 4 file's chat template puts after the model's turn when it is not
// thinking: an empty thought channel (12B's and 31B's), or nothing (E2B's). There is
// no jinja here, so the template is read by what it contains, as llama.cpp's
// llm_chat_detect_template tells templates apart by substrings: the channel as the
// template's source spells it, its newline escaped (whatever quotes it is in; E2B's
// other channel strings never run thought and its close together).
static std::string gemma4_reply_prefix(const qm::gg::model& m) {
const std::string channel = "<|channel>thought\n<channel|>", key = "tokenizer.chat_template";
std::string spelled;
for (char ch : channel) spelled += ch == '\n' ? std::string("\\n") : std::string(1, ch);
return m.has(key) && m.kv(key).as_str().find(spelled) != std::string::npos ? channel : "";
}

int main(int argc, char** argv) {
if (!tl::gpu_available()) {
std::printf("no GPU device — skipping chat\n");
Expand Down Expand Up @@ -67,8 +80,8 @@ int main(int argc, char** argv) {
// front, exactly what the template's enable_thinking=false does.
// Qwen3.5's template opens the <think> block itself. Gemma 3's opens with
// <bos> and has no system turn; so does Gemma 4's, its turns <|turn> ... <turn|>,
// and without thinking (the default) E2B's adds nothing after the model's
// opening. Llama 3's is llama.cpp's built-in one
// then what its template adds without thinking (gemma4_reply_prefix). Llama 3's
// is llama.cpp's built-in one
// (LLM_CHAT_TEMPLATE_LLAMA_3), opening with <|begin_of_text|>: 3.1's and 3.2's
// own jinja adds a system turn with a knowledge cutoff and today's date, left
// out here.
Expand Down Expand Up @@ -98,7 +111,7 @@ int main(int argc, char** argv) {
if (gemma3) {
templated = "<bos><start_of_turn>user\n" + user + "<end_of_turn>\n<start_of_turn>model\n";
} else if (gemma4) {
templated = "<bos><|turn>user\n" + user + "<turn|>\n<|turn>model\n";
templated = "<bos><|turn>user\n" + user + "<turn|>\n<|turn>model\n" + gemma4_reply_prefix(m);
} else if (llama) {
templated = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n" + user +
"<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n";
Expand Down
130 changes: 89 additions & 41 deletions bench/models/check_qwen.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// M9 "actually chat" — numeric proof. Load a real Qwen2.5-0.5B-Instruct, Qwen3
// or Qwen3.5 GGUF (F16 or BF16 weights, or Qwen3.5 0.8B's Q4_K_M, a fifth of it
// Q5_K), Gemma 3 4B or 1B (Q4_K_M), Gemma 4 E2B (Q4_K_M), or Llama 3.2 1B (F16,
// Q4_K_M, or Q4_0 with Q4_1) or 3B (Q4_K_M), and run the decoder end to end, verified
// Q5_K), Gemma 3 4B or 1B (Q4_K_M), Gemma 4 E2B or 12B (Q4_K_M), or Llama 3.2 1B
// (F16, Q4_K_M, or Q4_0 with Q4_1) or 3B (Q4_K_M), and run the decoder end to end, verified
// against a tight numpy reference that reads the SAME weights (tools/qwen_oracle.py
// wrote the *_oracle_data.h headers: Gemma 3 1B's and Gemma 4 E2B's over a prompt
// wrote the *_oracle_data.h headers: Gemma 3 1B's and Gemma 4 E2B's and 12B's over a prompt
// past their 512-position window, Llama 3.2 1B's over one long enough that its RoPE factors
// turn the low-frequency pairs well apart; the model's arch and shape pick one,
// and of several, the one that read the file's weights). Identical
Expand All @@ -20,6 +20,7 @@

#include "gemma3_1b_oracle_data.h"
#include "gemma3_oracle_data.h"
#include "gemma4_12b_oracle_data.h"
#include "gemma4_e2b_oracle_data.h"
#include "llama32_1b_oracle_data.h"
#include "llama32_1b_q40_oracle_data.h"
Expand Down Expand Up @@ -51,7 +52,21 @@ struct Oracle {
// and no other's: a Q4_K_M from another quantizer (an imatrix, another choice
// of Q6_K tensors) carries the same label but other blocks.
uint64_t weights = 0;
// A model whose forward has rows that rounding turns over (Gemma 4 12B: about 10 of
// its prompt's 1534 change entirely at layer 29 between numpy in f32 and numpy with
// an f16 KV cache, their neighbours within 1e-4): two paths that round differently
// disagree there, and on every logit that reads them, by far more than the bounds
// below allow a wiring error. Its paths are held as the chat's defaults are
// (agree), and its KV cache's error, which those rows decide, is shown only.
bool sensitive = false;
// Whether two paths' logits for the same tokens agree, as this model is held:
// the top-5 logits within `bound`, or a sensitive model's by their distributions.
bool agree(const std::vector<float>& want, const std::vector<float>& got, double bound) const;
};
static Oracle sensitive(Oracle o) {
o.sensitive = true;
return o;
}

// A file's weights by their bytes: FNV-1a over each tensor's name, ggml type and
// first 64 bytes of data, in file order. What a block oracle names (Oracle.weights),
Expand Down Expand Up @@ -120,6 +135,25 @@ static std::vector<float> logits_of(qm::Model& M) {
// GEMM before it kept them f32, on the repeated prompt of Qwen3.5's Q4_K_M).
constexpr double batched_bound = 1e-2;

// Two paths' logits for the same tokens, each check printing the measure it holds:
// the top-5 logits within `bound`, or the distributions as qm::close_by_kl holds
// them (the chat's defaults); Oracle::agree takes the one a model is held to.
static bool top5_agree(const std::vector<float>& want, const std::vector<float>& got, double bound) {
const double t5 = qm::top5_maxrel(got.data(), want.data(), (int64_t)want.size());
std::printf("top-5 logits maxrel %.2e", t5);
return t5 < bound;
}
static bool kl_agree(const std::vector<float>& want, const std::vector<float>& got) {
const double kl = qm::kl_div(want, got);
const qm::logits_vs c = qm::compare_logits(want, got, qm::BOUND_Q8);
std::printf("KL %.2e, top token %s", kl,
c.same_top ? "same" : c.near_tie ? "turned over at a near tie" : "different");
return qm::close_by_kl(c, kl);
}
bool Oracle::agree(const std::vector<float>& want, const std::vector<float>& got, double bound) const {
return sensitive ? kl_agree(want, got) : top5_agree(want, got, bound);
}

// The imperative forward's logits after `ids`, from position 0.
static std::vector<float> imperative_prompt(qm::Model& M, const std::vector<int>& ids) {
qm::reset_cache(M);
Expand All @@ -139,16 +173,17 @@ static void fixed_sums(bool on) {
}

// The batched prefill's f16 sums (the default where the backend has them),
// against `ref`, an f32 forward's logits for the same prompt: the top-5
// logits to 2e-2 and the same greedy token. Then the safety net: with the
// first layer's FFN norm scaled by 1e5 the f16 sums overflow (the MLP's input
// against `ref`, an f32 forward's logits for the same prompt: the top-5 logits to
// 2e-2 and the same greedy token (a sensitive model's as Oracle::agree holds
// it). Then the safety net: with the first layer's FFN norm scaled by 1e5 the
// f16 sums overflow (the MLP's input
// passes f16's range), and the prompt must be redone once, exactly, from the
// state it started at: an exact prefill of the same model, both with their
// sums in a fixed order (fixed_sums). Split-K's atomics would move them apart
// by their own rounding, which Gemma 4 E2B grows to 2e-3 to 6e-3; a missed
// restore is off by far more.
static bool check_f16_sums(qm::Model& B, const std::vector<int>& ids,
const std::vector<float>& ref) {
const std::vector<float>& ref, const Oracle& O) {
const bool was = B.f16_acc;
B.f16_acc = true;
if (!qm::prefill_sums_f16(B, false)) { // nothing to hold: its prefill sums in f32 still
Expand All @@ -158,10 +193,10 @@ static bool check_f16_sums(qm::Model& B, const std::vector<int>& ids,
}
qm::reset_cache(B);
const int64_t tok = qm::prefill_batched(B, ids);
const double t5 = qm::top5_maxrel(logits_of(B).data(), ref.data(), B.c.VOCAB);
const bool close = t5 < 2e-2 && tok == qm::argmax(ref);
std::printf("\nf16 sums: top-5 logits maxrel %.2e vs f32, token %lld %s\n", t5, (long long)tok,
close ? "OK" : "FAIL");
std::printf("\nf16 sums vs f32: ");
const bool close =
O.agree(ref, logits_of(B), 2e-2) && (O.sensitive || tok == qm::argmax(ref));
std::printf(", token %lld %s\n", (long long)tok, close ? "OK" : "FAIL");
const tl::array fn = B.layers[0].fn;
B.layers[0].fn = (fn * 1e5f).clone();
B.layers[0].fn.eval();
Expand Down Expand Up @@ -360,9 +395,17 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held)
}
// The prompt again, token by token: the logits, cache and first step the
// decoder is held to below, and the greedy opening, the reference's (a
// quantized file's own, run again from a reset cache).
// quantized file's own, run again from a reset cache). The long prompt's
// logits (below) are taken on the way where it is the prompt's head.
std::vector<int> longp;
const int64_t n_long = std::max<int64_t>(96, B.c.SWA > 0 ? B.c.SWA + 64 : 0);
for (int64_t i = 0; i < n_long; i++) longp.push_back(O.prompt_ids[i % NP]);
std::vector<float> imp_long;
int64_t tok = 0, p = 0;
for (int64_t i = 0; i < NP; i++) tok = qm::step_imperative(B, O.prompt_ids[i], p++);
for (int64_t i = 0; i < NP; i++) {
tok = qm::step_imperative(B, O.prompt_ids[i], p++);
if (i + 1 == n_long) imp_long = logits_of(B);
}
const std::vector<float> imp_prompt = logits_of(B); // the last prompt token's
const auto imp_cache = cache_rows(B, NP); // and the k / v the prompt left
std::vector<float> imp_step; // and the first decode step's
Expand Down Expand Up @@ -396,7 +439,8 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held)
// first, 0.17 at the last), where the logits see it only through the attention
// it feeds. The two paths' roundings leave every layer within ~5e-4 (every model
// here), about as much deep as shallow; a single element can
// differ far more (a large v), which a norm does not let decide.
// differ far more (a large v), which a norm does not let decide. A sensitive
// model's few turned-over rows decide it (Gemma 4 12B 5e-2): shown, not held.
const auto got_cache = cache_rows(B, NP);
double cache_err = 0;
int64_t cache_worst = -1;
Expand All @@ -410,23 +454,22 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held)
const double err = std::sqrt(dd / ww);
if (err > cache_err) cache_err = err, cache_worst = (int64_t)l;
}
const bool cache_ok = cache_err < 3e-2;
const bool cache_ok = O.sensitive || cache_err < 3e-2;
std::printf("\ndecoder (%s prefill) k / v cache vs imperative: relative error %.2e (attention "
"layer %lld) %s",
dec.batched ? "batched" : "token-by-token", cache_err, (long long)cache_worst,
cache_ok ? "OK" : "FAIL");
O.sensitive ? "(not held: a sensitive model)" : cache_ok ? "OK" : "FAIL");
const double pmr = maxrel(got_prompt, imp_prompt.data(), B.c.VOCAB);
const int64_t first = tok;
const int64_t second = dec.step(B, first);
const std::vector<float> got_step = logits_of(B);
const double smr = maxrel(got_step, imp_step.data(), B.c.VOCAB);
const double p5 = qm::top5_maxrel(got_prompt.data(), imp_prompt.data(), B.c.VOCAB),
s5 = qm::top5_maxrel(got_step.data(), imp_step.data(), B.c.VOCAB);
const bool logits_ok = p5 < batched_bound && s5 < batched_bound;
std::printf("\ndecoder (%s prefill) top-5 logits vs imperative: prompt maxrel %.2e, "
"first step %.2e %s (whole vocabulary %.2e, %.2e)",
dec.batched ? "batched" : "token-by-token", p5, s5, logits_ok ? "OK" : "FAIL",
pmr, smr);
std::printf("\ndecoder (%s prefill) vs imperative, prompt: ",
dec.batched ? "batched" : "token-by-token");
bool logits_ok = O.agree(imp_prompt, got_prompt, batched_bound);
std::printf("; first step: ");
logits_ok &= O.agree(imp_step, got_step, batched_bound);
std::printf(" %s (whole vocabulary maxrel %.2e, %.2e)", logits_ok ? "OK" : "FAIL", pmr, smr);
bool dec_ok = true;
std::printf("\ndecoder (%s), greedy %lld:\n cpp:", dec.ok() ? "graph capture" : "imperative",
(long long)NB);
Expand Down Expand Up @@ -466,21 +509,26 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held)
// A prompt past one chunk of the delta rule (32 tokens), so the batched
// prefill takes the backend's chunked form where it has one, and past a
// sliding window (Gemma 3's local layers), so both paths read one: its last
// token's top-5 logits against the imperative path's.
std::vector<int> longp;
const int64_t n_long = std::max<int64_t>(96, B.c.SWA > 0 ? B.c.SWA + 64 : 0);
for (int64_t i = 0; i < n_long; i++) longp.push_back(O.prompt_ids[i % NP]);
const std::vector<float> imp_long = imperative_prompt(B, longp);
// token's logits against the imperative path's (Oracle::agree), run here where
// the prompt is shorter than it (the prompt repeated).
if (imp_long.empty()) imp_long = imperative_prompt(B, longp);
qm::reset_cache(B);
bool long_ok = true;
if (qm::prefill_batched(B, longp) >= 0) {
const double l5 = qm::top5_maxrel(logits_of(B).data(), imp_long.data(), B.c.VOCAB);
long_ok = l5 < batched_bound;
std::printf("\n%lld-token prompt, batched vs imperative: top-5 logits maxrel %.2e %s\n",
(long long)longp.size(), l5, long_ok ? "OK" : "FAIL");
std::printf("\n%lld-token prompt, batched vs imperative: ", (long long)longp.size());
long_ok = O.agree(imp_long, logits_of(B), batched_bound);
std::printf(" %s\n", long_ok ? "OK" : "FAIL");
// Again with its last chunk one token (as a 513-token prompt's is), which runs
// on the decode's GEMVs.
qm::reset_cache(B);
qm::prefill_batched(B, longp, (int64_t)longp.size() - 1);
std::printf("the same in chunks of %lld and 1: ", (long long)longp.size() - 1);
const bool one_ok = O.agree(imp_long, logits_of(B), batched_bound);
long_ok &= one_ok;
std::printf(" %s\n", one_ok ? "OK" : "FAIL");
}
const bool f16_ok = !qm::can_prefill_batched(B, (int64_t)longp.size()) ||
check_f16_sums(B, longp, imp_long);
check_f16_sums(B, longp, imp_long, O);

// The chat's own defaults, its activation forms and f16 sums: the decoder's logits
// after a spread-out prompt, held to the f32 forward's by the KL of their
Expand All @@ -504,14 +552,11 @@ static bool check_imperative(const qm::gg::model& m, const Oracle& O, bool held)
d.begin(B, spread);
const std::vector<float> got = logits_of(B);
d.destroy();
const double kl = qm::kl_div(ref_spread, got);
const qm::logits_vs c = qm::compare_logits(ref_spread, got, qm::BOUND_Q8);
defaults_ok = (c.same_top || c.near_tie) && kl < 0.3;
std::printf("\nthe chat's defaults (decode GEMV activations %s, prefill MMQ %s, sums in %s), a "
"spread-out prompt: KL %.2e from the f32 forward, top token %s %s\n",
qm::act_form_name(chat_form), mmq ? "on" : "off", f16 ? "f16" : "f32", kl,
c.same_top ? "same" : c.near_tie ? "turned over at a near tie" : "different",
defaults_ok ? "OK" : "FAIL");
"spread-out prompt, from the f32 forward: ",
qm::act_form_name(chat_form), mmq ? "on" : "off", f16 ? "f16" : "f32");
defaults_ok = kl_agree(ref_spread, got);
std::printf(" %s\n", defaults_ok ? "OK" : "FAIL");
}
return ok && bf16_ok && logits_ok && cache_ok && dec_ok && turn2_ok && sample_ok && long_ok && f16_ok &&
defaults_ok;
Expand Down Expand Up @@ -542,6 +587,9 @@ int main(int argc, char** argv) {
{"gemma3", 26, 1152, TL_ORACLE(gemma3_1boracle, 0x1f0d93832cf2bb46ull)}, // Gemma 3 1B
// unsloth/gemma-4-E2B-it-GGUF's gemma-4-E2B-it-Q4_K_M.gguf, past its 512 window
{"gemma4", 35, 1536, TL_ORACLE(gemma4_e2b_oracle, 0xfd1769fcfb2b2478ull)},
// and gemma-4-12b-it-GGUF's Q4_K_M: kv heads a layer's own, a global layer's v its k,
// its prompt twice LONG_USER, past its 1024 window
{"gemma4", 48, 3840, sensitive(TL_ORACLE(gemma4_12b_oracle, 0x6d183d8cd40a4e55ull))},
// unsloth/Llama-3.2-{1B,3B}-Instruct-GGUF's F16, Q4_K_M and Q4_0
{"llama", 16, 2048, TL_ORACLE(llama32_1boracle, 0)}, // Llama 3.2 1B, F16
{"llama", 16, 2048, TL_ORACLE(llama32_1b_q4oracle, 0x00f056cb14a01068ull)}, // 1B Q4_K_M
Expand Down Expand Up @@ -707,7 +755,7 @@ int main(int argc, char** argv) {
for (auto t : got) std::printf(" %lld", (long long)t);
bf16_ok = got == want;
std::printf("\n greedy %s\n", bf16_ok ? "MATCH" : "DIVERGE");
bf16_ok &= check_f16_sums(B, ids, array_prompt);
bf16_ok &= check_f16_sums(B, ids, array_prompt, O);
bf16_ok &= check_sampled(B, ids);
}
}
Expand Down
13 changes: 13 additions & 0 deletions bench/models/gemma4_12b_oracle_data.h

Large diffs are not rendered by default.

Loading
Loading