// tests/cuda/qwen36_verify_test.cpp - the verify window on a qwen35moe NATIVE PACK, held to the oracle and to the // single-token path (Qwen3.6 port, step 4; docs/qwen36-port/step4-runner.md). // // qwen36_verify_test (tests/python/qwen36_runner_fixture.py: full4.gguf, pack/, o8, o11, o40) // // The step-3 review's HIGH finding: no test drove `Verifier` on a qwen35moe geometry, and the window's hand-written // attention shapes would have exited on the first attention layer. This drives it the way `strata generate` does on // the pack (the load order of docs/qwen36-port/step4-pack.md section 6, `check_all` included; the session at hc = 1 // with no PLE and no indexer; an expert cache holding every expert of the 4 x 16 fixture), and holds // // 1. one window of 8 tokens ("hello wo"): every row's residual after every layer (the stage hand-off of a verifier // ending at that layer) against the oracle's `l_out-N`, and every row's head logits against `result_output_all`; // 2. the same rows from eight one-token windows (commit after each): BIT FOR BIT the 8-row window's (verify.hpp's // discipline: row t of a T-row window is the single-token path after token t); // 3. two windows of 8 + 3 ("hello world", a commit between them) against the 11-token dumps; // 4. the 40-token sequence one token per window (the oracle's `base_ar` case through the decode path) against // `result_output_all` and `l_out-3` at every position, with the engine's argmax against the oracle's. // // Tolerances: the oracle is F32 weights x F32 activations; the engine runs Q8_0 projections (FP16 dequantized GEMV // with q8_1 activations) and Q8_0 experts on int8 dot products, so the per-position gates are the prompt-path // harness's measured classes with margin (step3-prefill.md 2.2): `l_out` 1e-2 relative L2 per row, the logits 2e-2; // the window-vs-single-token comparison is exact. Exit 77 without a CUDA device or when the fixture was not written // (no numpy), 1 on a failure. #include "strata/artifact/gguf_reader.hpp" #include "strata/artifact/gguf_split.hpp" #include "strata/core/expert_cache.hpp" #include "strata/core/expert_source.hpp" #include "strata/core/layer.hpp" #include "strata/core/layout.hpp" #include "strata/core/model_geometry.hpp" #include "strata/core/native_dense.hpp" #include "strata/core/native_head.hpp" #include "strata/core/session.hpp" #include "strata/core/verify.hpp" #include "strata/core/weights.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "strata/kernels/native_gdn.hpp" #include "strata/kernels/native_moe.hpp" #include "strata/kernels/native_qsa.hpp" #include "strata/kernels/native_qsa_indexer.hpp" #include "strata/kernels/native_rope.hpp" #include "strata/kernels/native_router.hpp" #include "strata/kernels/rope_scaling.hpp" #include "strata/kernels/shared_expert.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; namespace core = strata::core; namespace { int g_fail = 0; void check(bool ok, const std::string& what) { std::printf(" %-108s %s\n", what.c_str(), ok ? "ok" : "FAIL"); if (!ok) ++g_fail; } std::string fmt(double v) { char b[32]; std::snprintf(b, sizeof b, "%.2e", v); return b; } [[noreturn]] void die(const std::string& what) { std::fprintf(stderr, "qwen36_verify_test: %s\n", what.c_str()); std::exit(1); } void ck(cudaError_t e, const char* what) { if (e != cudaSuccess) die(std::string(what) + ": " + cudaGetErrorString(e)); } // ---- the oracle's dumps: .f32 in C order, shapes from manifest.txt (GGUF order, ne0 fastest) struct Dumps { fs::path dir; std::map> ne; bool open(const fs::path& d) { dir = d; std::ifstream m(d / "manifest.txt"); if (!m) return false; std::string line; while (std::getline(m, line)) { std::istringstream is(line); std::string stem, kind, dims; if (!(is >> stem >> kind >> dims)) continue; std::vector v; std::istringstream ds(dims); std::string tok; while (std::getline(ds, tok, ',')) v.push_back(std::stoll(tok)); ne[stem] = v; } return !ne.empty(); } std::vector f(const std::string& stem) const { std::ifstream in(dir / (stem + ".f32"), std::ios::binary); if (!in) die("missing dump " + (dir / (stem + ".f32")).string()); in.seekg(0, std::ios::end); const std::streamoff n = in.tellg(); in.seekg(0); std::vector v((size_t) n / 4); in.read((char*) v.data(), n); return v; } }; double rel_l2(const float* a, const float* b, size_t n) { double d = 0, m = 0; for (size_t i = 0; i < n; ++i) { if (!std::isfinite(a[i]) || !std::isfinite(b[i])) return INFINITY; d += (double) (a[i] - b[i]) * (a[i] - b[i]); m += (double) b[i] * b[i]; } return m > 0 ? std::sqrt(d / m) : (d > 0 ? INFINITY : 0.0); } size_t argmax(const float* v, size_t n) { return (size_t) (std::max_element(v, v + n) - v); } std::vector down(const float* dev, size_t n) { std::vector h(n); ck(cudaMemcpy(h.data(), dev, n * 4, cudaMemcpyDeviceToHost), "download"); return h; } // the CPU pool for a window: every expert of the fixture is resident in VRAM, so the pool has nothing to compute // (the window's own hit path serves them); zeroing keeps a stray call from reading uninitialised rows int64_t g_n_embd = 0; void pool_none(void*, const float*, const int32_t*, int64_t n_tok, int64_t k, float* out, int64_t) { std::memset(out, 0, (size_t) (n_tok * k) * (size_t) g_n_embd * sizeof(float)); } /// The engine's loads, in generate.cpp's order after the plan (docs/qwen36-port/step4-pack.md section 6). struct Engine { std::string gguf, pack; std::vector shards; core::ModelGeometry g; core::WeightTable wt; void* arena = nullptr; core::NativeDense dense; core::NativeHead head; core::NativeEmbed embed; std::vector> profile; int64_t n_vocab = 0; // the experts: the GGUF in place through the engine's arena source, every blob in a VRAM slot core::ArenaExpertSource src; core::ExpertCache cache; std::vector host_res; int32_t* d_res = nullptr; core::VerifyHits hits; void load(const std::string& gguf_in, const std::string& pack_in) { gguf = gguf_in; pack = pack_in; std::string err; if (!core::read_model_geometry(gguf, g, err)) die("read_model_geometry: " + err); if (g.arch != core::ModelArch::Qwen35Moe) die("the fixture is not a qwen35moe file"); g_n_embd = g.n_embd; shards = strata::gguf_split_paths(gguf); { const strata::GgufModel model(shards); if (!core::check_model_tensor_forms(model, g, err)) die("check_model_tensor_forms: " + err); } if (!strata::kernels::cpu::expert_layout_load(pack, g.n_layers, g.n_expert, g.n_embd, g.n_ff, err)) die("expert_layout_load: " + err); const auto& lay = strata::kernels::cpu::expert_layout(); if (!lay.native) die("not a native pack"); std::set skip; if (!core::NativeDense::served_names(shards, false, skip, err)) die("served_names: " + err); skip.insert("output.weight"); skip.insert("token_embd.weight"); uint64_t pool = 0; if (!core::WeightTable::pool_bytes(pack, pool, err, &skip)) die("pool_bytes: " + err); ck(cudaMalloc(&arena, pool), "arena"); if (!wt.load(pack, arena, pool, err, &skip)) die("WeightTable::load: " + err); if (!dense.load(shards, wt, err, false, 0, g.n_layers)) die("NativeDense::load: " + err); if (!core::check_all(wt, g, err)) die("check_all: " + err); const core::WeightRef* wo = wt.find("output.weight"); if (wo == nullptr) die("output.weight missing"); n_vocab = wo->ne1; if (!head.load(shards, g.n_embd, n_vocab, err)) die("NativeHead::load: " + err); if (!embed.load(shards, g.n_embd, n_vocab, err)) die("NativeEmbed::load: " + err); core::set_native_embed(&embed); int64_t slots = 0; if (!core::read_expert_profile(pack + "/expert-profile.bin", g.n_layers, g.n_expert, profile, slots, err)) die("read_expert_profile: " + err); std::printf("loaded %s with %s: %lld layers, n_embd %lld, %lld experts top-%lld, n_vocab %lld, arena %llu B, " "%zu native projections, head %s, embedding %s, profile %zu pairs\n", gguf.c_str(), pack.c_str(), (long long) g.n_layers, (long long) g.n_embd, (long long) g.n_expert, (long long) g.n_expert_used, (long long) n_vocab, (unsigned long long) pool, dense.tensor_count(), strata::ggml_type_name((uint32_t) head.type()), strata::ggml_type_name((uint32_t) embed.type()), profile.size()); // the decode configuration the window reproduces (generate.cpp's --native defaults) core::layer_set_native_bf16(true); core::layer_set_fused_gdn(true); core::layer_set_fast_attn(true); core::layer_set_fast_select(true); core::layer_set_fused_gr(false); // hc = 1: no hyper-connection read exists to fuse strata::kernels::native_gdn_set_enabled(true); strata::kernels::native_router_set_enabled(true); strata::kernels::native_qsa_set_enabled(true); strata::kernels::native_qsa_indexer_set_enabled(true); strata::kernels::native_rope_set_enabled(true); strata::kernels::native_moe_combine_set_enabled(true); strata::kernels::shared_expert_set_native_bf16(true); strata::kernels::RopeScaling rope; rope.freq_base = g.rope_base; strata::kernels::rope_scaling_set(rope); core::qsa_set_kv_resident(0); std::string why; if (!core::layer_verify_compatible(g, why)) die("layer_verify_compatible: " + why); // the experts: every (layer, expert) pair in its own slot, from the GGUF in place src.set_gguf(gguf); if (!src.open(pack, g.n_layers, g.n_expert, 2, err)) die("ArenaExpertSource::open: " + err); const int64_t n_pairs = g.n_layers * g.n_expert; if (!cache.open(n_pairs, g.n_layers, g.n_expert, (int64_t) lay.max_blob, err)) die("ExpertCache::open: " + err); host_res.assign((size_t) n_pairs, core::kNotResident); for (int64_t l = 0; l < g.n_layers; ++l) for (int64_t e = 0; e < g.n_expert; ++e) { const int32_t slot = cache.admit(l, e); if (slot == core::kNotResident) die("the cache refused a slot"); const uint8_t* b = src.blob(l, e); if (b == nullptr || !cache.fill_slot_blocking(slot, b, err, (int64_t) lay.blob_bytes(l))) die("fill_slot: " + err); host_res[(size_t) (l * g.n_expert + e)] = slot; } if (!cache.verify_slot(cache.slot_of(0, 0), src.blob(0, 0), err, (int64_t) lay.blob_bytes(0))) die("verify_slot: " + err); ck(cudaMalloc((void**) &d_res, host_res.size() * 4), "d_res"); ck(cudaMemcpy(d_res, host_res.data(), host_res.size() * 4, cudaMemcpyHostToDevice), "d_res upload"); hits.d_res = d_res; hits.h_res = host_res.data(); hits.cache_base = cache.device_slot(0); hits.blob = (int64_t) lay.max_blob; hits.slot_off = cache.slot_offsets(); hits.n_slots = cache.slots(); std::printf("expert cache: %lld slots of %llu B, every expert resident (slot 0 verified against the file)\n", (long long) cache.slots(), (unsigned long long) lay.max_blob); } }; /// A session and a verifier over layers [lb, le) of it. lb > 0: the rows' residuals come from the hand-in (the /// oracle's `l_out-(lb-1)`, teacher forcing a layer); le < n_layers: the residual after layer le - 1 lands in the /// hand-out, per row; le = n_layers: the whole tail with the head. struct Window { const Engine& en; core::SessionState ss{}; void* sbuf = nullptr; float* in_h = nullptr; // mapped host hand-in and its device alias float* in_d = nullptr; float* out_h = nullptr; // mapped host hand-out and its device alias float* out_d = nullptr; int64_t lb = 0, le = 0; int max_t = 0; core::Verifier ver; Window(const Engine& e, int64_t layer_begin, int64_t layer_end, int max_tokens, int64_t max_cells = 64) : en(e), lb(layer_begin), le(layer_end), max_t(max_tokens) { std::string err; const int64_t K = en.g.n_expert_used; ck(cudaMalloc(&sbuf, core::session_bytes(en.g, max_cells, K, 0, -1)), "session"); if (core::session_init(en.g, max_cells, K, sbuf, ss, 0, -1) == 0) die("session_init failed"); const size_t hb = (size_t) max_t * (size_t) core::Verifier::handoff_floats(en.g) * sizeof(float); if (lb > 0) { ck(cudaHostAlloc((void**) &in_h, hb, cudaHostAllocMapped | cudaHostAllocPortable), "hand-in"); ck(cudaHostGetDevicePointer((void**) &in_d, in_h, 0), "hand-in device pointer"); std::memset(in_h, 0, hb); } if (le < en.g.n_layers) { ck(cudaHostAlloc((void**) &out_h, hb, cudaHostAllocMapped | cudaHostAllocPortable), "hand-out"); ck(cudaHostGetDevicePointer((void**) &out_d, out_h, 0), "hand-out device pointer"); std::memset(out_h, 0, hb); } if (lb > 0 || le < en.g.n_layers) ver.set_stage(lb, le, in_d, out_d); if (!ver.init(en.wt, en.g, ss, en.hits, le == en.g.n_layers ? &en.head : nullptr, max_t, err)) die("Verifier::init (layers " + std::to_string(lb) + ".." + std::to_string(le) + "): " + err); } /// teacher forcing: the T rows' residuals before layer lb (the hand-in holds every row's R first, then every /// row's pending write and inject, both zero: the plain block has no pending write) void force_in(const float* rows, int T) { std::memcpy(in_h, rows, (size_t) T * (size_t) en.g.n_embd * sizeof(float)); } bool run(int T, const int32_t* toks, int64_t pos0, int32_t* out, std::string& err) { return ver.run(T, toks, pos0, &pool_none, nullptr, out, err); } /// row t's residual after the last layer this window runs std::vector residual(int t) const { const int64_t N = en.g.n_embd; if (le < en.g.n_layers) { ck(cudaDeviceSynchronize(), "hand-off sync"); const float* r = out_h + (size_t) t * (size_t) en.g.hc * (size_t) N; return std::vector(r, r + N); } return down(ver.final_R(t), (size_t) N); } std::vector logits(int t) const { std::vector row((size_t) en.n_vocab); if (!ver.copy_logits(t, row.data())) die("copy_logits failed"); return row; } }; /// the worst relative L2 over rows [0, T) of `got` (row stride n) against rows p0.. of `ref` double worst_rows(const std::vector>& got, const std::vector& ref, int p0, size_t n) { double w = 0; for (size_t t = 0; t < got.size(); ++t) w = std::max(w, rel_l2(got[t].data(), ref.data() + ((size_t) p0 + t) * n, n)); return w; } int argmax_equal(const std::vector>& got, const std::vector& ref, int p0, size_t n, std::string& miss) { int eq = 0; for (size_t t = 0; t < got.size(); ++t) { const bool same = argmax(got[t].data(), n) == argmax(ref.data() + ((size_t) p0 + t) * n, n); eq += same; if (!same) miss += (miss.empty() ? "" : ", ") + std::to_string(p0 + (int) t); } return eq; } } // namespace int main(int argc, char** argv) { if (argc < 2) { std::fprintf(stderr, "usage: qwen36_verify_test \n"); return 2; } const fs::path fx = argv[1]; if (fs::exists(fx / "SKIP") || !fs::exists(fx / "full4.gguf") || !fs::exists(fx / "pack" / "index.txt") || !fs::exists(fx / "o8" / "manifest.txt")) { std::fprintf(stderr, "qwen36_verify_test: no fixture in %s (tests/python/qwen36_runner_fixture.py): skipped\n", argv[1]); return 77; } int devices = 0; if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) { std::fprintf(stderr, "qwen36_verify_test: no CUDA device: skipped\n"); return 77; } // the F32 oracle (the reference the real file was cross-checked with) and its Q8 mode (the activations rounded // to Q8 before every quantized matmul, as ggml and this engine's int8 paths do): the policy's reference for a // path that rounds (oracle-crosscheck.md, "Tolerance policy", items 3 and 4) Dumps o8, o8q, o11, o11q, o40, o40q; if (!o8.open(fx / "o8") || !o8q.open(fx / "o8_q8") || !o11.open(fx / "o11") || !o11q.open(fx / "o11_q8") || !o40.open(fx / "o40") || !o40q.open(fx / "o40_q8")) die("the oracle dumps are incomplete (o8, o8_q8, o11, o11_q8, o40, o40_q8)"); Engine en; en.load((fx / "full4.gguf").string(), (fx / "pack").string()); const int64_t L = en.g.n_layers, N = en.g.n_embd, V = en.n_vocab; // the gates, against the Q8 oracle: one layer with its input forced is the policy's per-layer protocol (the Q8 // column's l_out class 1.4e-3 max on the real file; this fixture's 16-expert MoE spreads more: 1e-2 is 4x its // measured worst); a whole sequence accumulates over the 4 layers and the recurrence (5e-2); the argmax must agree const double kForced = 1.5e-2, kAccum = 5e-2; std::string err, miss; const std::string lo_last = "l_out-" + std::to_string(L - 1); // ================================ 1. one window of 8 tokens, every layer, every row ================================ std::printf("1. one window of 8 tokens: the residual after every layer per row, against the oracle (F32 | Q8)\n"); const std::vector t8 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o'}; std::vector out8(8); std::vector> win_R(8), win_logits(8), rows(8); std::printf(" 1a. the layers from the embedding (accumulated):\n"); for (int64_t l = 0; l < L; ++l) { Window w(en, 0, l + 1, 8); if (!w.run(8, t8.data(), 0, out8.data(), err)) { check(false, "window of 8 through layers 0.." + std::to_string(l) + ": " + err); continue; } ck(cudaDeviceSynchronize(), "window sync"); for (int t = 0; t < 8; ++t) rows[(size_t) t] = w.residual(t); const std::string lo = "l_out-" + std::to_string(l); const double e32 = worst_rows(rows, o8.f(lo), 0, (size_t) N), eq8 = worst_rows(rows, o8q.f(lo), 0, (size_t) N); check(eq8 <= kAccum, lo + " per row: worst rel L2 F32 " + fmt(e32) + " | Q8 " + fmt(eq8) + " (Q8 <= " + fmt(kAccum) + ")"); if (l == L - 1) { win_R = rows; for (int t = 0; t < 8; ++t) win_logits[(size_t) t] = w.logits(t); } } // The per-layer protocol (oracle-crosscheck.md "Tolerance policy", item 2): start each layer from the oracle's // own l_out of the layer before, so each seam's error is that layer's own. The reference must match the forced // input: a path that rounds activations to Q8 is forced from, and compared to, the Q8 oracle. std::printf(" 1b. each layer alone, its input rows forced to the Q8 oracle's l_out of the layer before:\n"); for (int64_t l = 1; l < L; ++l) { Window w(en, l, l + 1, 8); const std::vector prevq = o8q.f("l_out-" + std::to_string(l - 1)); w.force_in(prevq.data(), 8); if (!w.run(8, t8.data(), 0, out8.data(), err)) { check(false, "layer " + std::to_string(l) + " forced: " + err); continue; } ck(cudaDeviceSynchronize(), "window sync"); for (int t = 0; t < 8; ++t) rows[(size_t) t] = w.residual(t); const std::string lo = "l_out-" + std::to_string(l); const double eq8 = worst_rows(rows, o8q.f(lo), 0, (size_t) N); check(eq8 <= kForced, lo + " forced from the Q8 oracle, per row: worst rel L2 " + fmt(eq8) + " (<= " + fmt(kForced) + ")"); } { const std::vector ro = o8.f("result_output_all"), roq = o8q.f("result_output_all"); const double e32 = worst_rows(win_logits, ro, 0, (size_t) V), eq8 = worst_rows(win_logits, roq, 0, (size_t) V); check(eq8 <= kAccum, "result_output_all per row: worst rel L2 F32 " + fmt(e32) + " | Q8 " + fmt(eq8) + " (Q8 <= " + fmt(kAccum) + ")"); miss.clear(); const int eq = argmax_equal(win_logits, roq, 0, (size_t) V, miss); std::string m32; const int eq32 = argmax_equal(win_logits, ro, 0, (size_t) V, m32); check(eq == 8, "argmax per row equals the Q8 oracle's: " + std::to_string(eq) + " of 8" + (miss.empty() ? "" : " (differs at " + miss + ")") + "; the F32 oracle's: " + std::to_string(eq32) + " of 8"); for (int t = 0; t < 8; ++t) if ((size_t) out8[(size_t) t] != argmax(win_logits[(size_t) t].data(), (size_t) V)) check(false, "row " + std::to_string(t) + ": the window's pick is not its own logits' argmax"); } // ================================ 2. the single-token path: eight windows of one token ================================ std::printf("2. eight one-token windows (commit after each) against the 8-row window: bit for bit\n"); { Window one(en, 0, L, 2); int32_t out1 = 0; double worst_R = 0, worst_lg = 0; bool bit_R = true, bit_lg = true; for (int t = 0; t < 8; ++t) { if (!one.run(1, &t8[(size_t) t], t, &out1, err)) { check(false, "one-token window at " + std::to_string(t) + ": " + err); break; } ck(cudaDeviceSynchronize(), "sync"); const std::vector r = one.residual(0), lg = one.logits(0); bit_R = bit_R && std::memcmp(r.data(), win_R[(size_t) t].data(), (size_t) N * 4) == 0; bit_lg = bit_lg && std::memcmp(lg.data(), win_logits[(size_t) t].data(), (size_t) V * 4) == 0; worst_R = std::max(worst_R, rel_l2(r.data(), win_R[(size_t) t].data(), (size_t) N)); worst_lg = std::max(worst_lg, rel_l2(lg.data(), win_logits[(size_t) t].data(), (size_t) V)); if (!one.ver.commit(1, err)) die("commit: " + err); } if (!one.ver.wait_commit(err)) die("wait_commit: " + err); check(bit_R, "residual after layer " + std::to_string(L - 1) + ": the single-token rows equal the window's rows bitwise (rel L2 " + fmt(worst_R) + ")"); check(bit_lg, "head logits: the single-token rows equal the window's rows bitwise (rel L2 " + fmt(worst_lg) + ")"); } // ================================ 3. 8 + 3: two windows with a commit between them ================================ std::printf("3. \"hello world\": a window of 8, commit, a window of 3 at position 8, against the 11-token dumps\n"); { const std::vector t11 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}; Window w(en, 0, L, 8); std::vector out(8); std::vector> R11, lg11; if (!w.run(8, t11.data(), 0, out.data(), err)) die("window 1: " + err); ck(cudaDeviceSynchronize(), "sync"); for (int t = 0; t < 8; ++t) { R11.push_back(w.residual(t)); lg11.push_back(w.logits(t)); } if (!w.ver.commit(8, err)) die("commit 8: " + err); if (!w.run(3, t11.data() + 8, 8, out.data(), err)) die("window 2: " + err); ck(cudaDeviceSynchronize(), "sync"); for (int t = 0; t < 3; ++t) { R11.push_back(w.residual(t)); lg11.push_back(w.logits(t)); } if (!w.ver.commit(3, err) || !w.ver.wait_commit(err)) die("commit 3: " + err); const double r32 = worst_rows(R11, o11.f(lo_last), 0, (size_t) N), rq8 = worst_rows(R11, o11q.f(lo_last), 0, (size_t) N); const double l32 = worst_rows(lg11, o11.f("result_output_all"), 0, (size_t) V), lq8 = worst_rows(lg11, o11q.f("result_output_all"), 0, (size_t) V); check(rq8 <= kAccum, lo_last + " per position (11): worst rel L2 F32 " + fmt(r32) + " | Q8 " + fmt(rq8) + " (Q8 <= " + fmt(kAccum) + ")"); check(lq8 <= kAccum, "result_output_all per position (11): worst rel L2 F32 " + fmt(l32) + " | Q8 " + fmt(lq8) + " (Q8 <= " + fmt(kAccum) + ")"); miss.clear(); const int eq = argmax_equal(lg11, o11q.f("result_output_all"), 0, (size_t) V, miss); std::string m32; const int eq32 = argmax_equal(lg11, o11.f("result_output_all"), 0, (size_t) V, m32); check(eq == 11, "argmax per position equals the Q8 oracle's: " + std::to_string(eq) + " of 11" + (miss.empty() ? "" : " (differs at " + miss + ")") + "; the F32 oracle's: " + std::to_string(eq32) + " of 11"); } // ================================ 4. 40 tokens, one per window: the autoregressive case ================================ std::printf("4. 40 tokens one per window (the oracle's base_ar shape) against result_output_all and %s at every position\n", lo_last.c_str()); { const std::string text = "the quick brown fox jumps over the lazy "; std::vector t40(text.begin(), text.end()); if (t40.size() != 40) die("the 40-token text"); Window w(en, 0, L, 2); int32_t out = 0; std::vector> R40, lg40; const std::vector lo = o40.f(lo_last), ro = o40.f("result_output_all"), loq = o40q.f(lo_last), roq = o40q.f("result_output_all"); std::printf(" pos: rel L2 of %s and the logits against the F32 oracle | the Q8 oracle\n", lo_last.c_str()); for (int t = 0; t < 40; ++t) { if (!w.run(1, &t40[(size_t) t], t, &out, err)) die("window at " + std::to_string(t) + ": " + err); ck(cudaDeviceSynchronize(), "sync"); R40.push_back(w.residual(0)); lg40.push_back(w.logits(0)); const float* r = R40.back().data(); const float* lg = lg40.back().data(); const bool same = argmax(lg, (size_t) V) == argmax(roq.data() + (size_t) t * V, (size_t) V); std::printf(" %2d: %s %s | %s %s%s\n", t, fmt(rel_l2(r, lo.data() + (size_t) t * N, (size_t) N)).c_str(), fmt(rel_l2(lg, ro.data() + (size_t) t * V, (size_t) V)).c_str(), fmt(rel_l2(r, loq.data() + (size_t) t * N, (size_t) N)).c_str(), fmt(rel_l2(lg, roq.data() + (size_t) t * V, (size_t) V)).c_str(), same ? "" : " (argmax differs from the Q8 oracle)"); if (!w.ver.commit(1, err)) die("commit: " + err); } if (!w.ver.wait_commit(err)) die("wait_commit: " + err); const double r32 = worst_rows(R40, lo, 0, (size_t) N), rq8 = worst_rows(R40, loq, 0, (size_t) N); const double l32 = worst_rows(lg40, ro, 0, (size_t) V), lq8 = worst_rows(lg40, roq, 0, (size_t) V); check(rq8 <= kAccum, lo_last + " per position (40): worst rel L2 F32 " + fmt(r32) + " | Q8 " + fmt(rq8) + " (Q8 <= " + fmt(kAccum) + ")"); check(lq8 <= kAccum, "result_output_all per position (40): worst rel L2 F32 " + fmt(l32) + " | Q8 " + fmt(lq8) + " (Q8 <= " + fmt(kAccum) + ")"); miss.clear(); const int eq = argmax_equal(lg40, roq, 0, (size_t) V, miss); std::string m32; const int eq32 = argmax_equal(lg40, ro, 0, (size_t) V, m32); // A near tie cannot discriminate the engine: a position where the two references disagree with each other // (the runner check, qwen36_runner_check.py, excludes the same positions), or where the Q8 oracle's top-two // margin, (top1 - top2) / |top1|, is below the engine row's own rel L2 error against that oracle (a flip // inside the measured error; on the 4090 position 4 of this fixture flips between runs with a 0.4% margin // under a 1.4% row error). The gate runs over the other positions and reports the ties with their margins. std::string ties; int unambiguous = 0, eq_un = 0; for (int t = 0; t < 40; ++t) { const float* rq = roq.data() + (size_t) t * V; const size_t a32 = argmax(ro.data() + (size_t) t * V, (size_t) V), aq8 = argmax(rq, (size_t) V); float top1 = rq[aq8], top2 = -INFINITY; for (size_t i = 0; i < (size_t) V; ++i) if (i != aq8 && rq[i] > top2) top2 = rq[i]; const double margin = (top1 - top2) / std::max(1e-30, (double) std::fabs(top1)); const double row_err = rel_l2(lg40[(size_t) t].data(), rq, (size_t) V); if (a32 != aq8 || margin < row_err) { ties += (ties.empty() ? "" : ", ") + std::to_string(t) + (a32 != aq8 ? " (oracles disagree" : " (margin ") + (a32 != aq8 ? std::string() : fmt(margin) + " < row error " + fmt(row_err)) + ")"; continue; } ++unambiguous; eq_un += argmax(lg40[(size_t) t].data(), (size_t) V) == aq8; } check(eq_un == unambiguous, "argmax per position equals the Q8 oracle's at " + std::to_string(eq_un) + " of " + std::to_string(unambiguous) + " unambiguous positions (all 40: Q8 " + std::to_string(eq) + (miss.empty() ? "" : ", differs at " + miss) + "; F32 " + std::to_string(eq32) + (m32.empty() ? "" : ", differs at " + m32) + ")" + (ties.empty() ? "" : "; near ties, not gated: [" + ties + "]")); } std::printf("%s: %d failure(s)\n", g_fail == 0 ? "PASS" : "FAIL", g_fail); return g_fail == 0 ? 0 : 1; }