// src/kernels/native_expert_parity.cpp - plan v0.3 P6: one native expert three ways, on real GGUF rows. // // build/native_expert_parity [layer ...] // // (a) float reference: ggml's own dequantizer (`to_float`) and a float SwiGLU expert, (b) the CPU path // (ggml-cpu vec_dot with its quantized activations), (c) the GPU path (`native_expert_grouped`, q8_1 // activations). (b) and (c) each differ from (a) by their activation rounding only (a few 1e-3 relative). #include "strata/artifact/gguf_reader.hpp" #include "strata/kernels/cpu/native_expert.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/cpu/iq_avx512.hpp" #include "strata/kernels/cpu/iq_avx2.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "ggml-cpu.h" #include "strata/kernels/iq_kernels.hpp" #include "ggml.h" #include #include // SYCL port: hand-ported (dpct needs ggml-cpu.h to parse this file) #include #include #include #include #include #include #include #include #include namespace cpu = strata::kernels::cpu; // Round 0214: --synthetic GU/DOWN ... : random weights quantized by ggml (ggml type names, e.g. iq2_xs/q5_0) in place of real // rows, as the CUDA/HIP tree's native_expert_parity does; the checks below are the same. static int type_of(std::string name) { for (auto& c : name) c = (char) std::tolower((unsigned char) c); for (int t = 0; t < GGML_TYPE_COUNT; ++t) { const char* n = ggml_type_name((ggml_type) t); if (n == nullptr) continue; std::string m(n); for (auto& c : m) c = (char) std::tolower((unsigned char) c); if (m == name) return t; } return -1; } static std::vector synthetic_blob(const cpu::NativeFmt& f, int seed, int64_t H, int64_t FF) { std::vector blob(f.bytes); std::mt19937 rng(seed); std::normal_distribution nd(0.f, 1.f); auto quant = [&](int type, int64_t rows, int64_t cols, uint8_t* dst) { std::vector w((size_t) (rows * cols)); for (int64_t r = 0; r < rows; ++r) { const float scale = 0.02f * (0.5f + (float) (r % 7) / 7.0f); for (int64_t c = 0; c < cols; ++c) w[(size_t) (r * cols + c)] = scale * nd(rng); } const std::vector imatrix((size_t) cols, 1.0f); ggml_quantize_chunk((ggml_type) type, w.data(), dst, 0, rows, cols, ggml_quantize_requires_imatrix((ggml_type) type) ? imatrix.data() : nullptr); }; quant(f.gu_type, FF, H, blob.data()); quant(f.gu_type, FF, H, blob.data() + f.up_off); quant(f.d_type, H, FF, blob.data() + f.down_off); return blob; } static double rel(const std::vector& a, const std::vector& b) { double n = 0, d = 0; for (size_t i = 0; i < a.size(); ++i) { n += std::fabs((double) a[i] - b[i]); d += std::fabs((double) b[i]); } return n / (d + 1e-30); } int main(int argc, char** argv) { setvbuf(stdout, nullptr, _IONBF, 0); // keep the trail on a crash if (argc < 2) { std::fprintf(stderr, "usage: native_expert_parity [layer ...] | --synthetic GU/DOWN ...\n"); return 2; } const bool synthetic = std::string(argv[1]) == "--synthetic"; std::unique_ptr gguf_p; if (!synthetic) gguf_p.reset(new strata::GgufFile(argv[1])); std::vector layers; std::vector pair_names; for (int i = 2; i < argc; ++i) { if (synthetic) { pair_names.push_back(argv[i]); layers.push_back((int) pair_names.size() - 1); } else layers.push_back(std::atoi(argv[i])); } if (layers.empty() && !synthetic) layers = {0, 1, 2, 3, 20, 47}; const int NT = 3, E = 7; int64_t H = 2560, FF = 640; // Q4KQ_PARITY_EMBD / Q4KQ_PARITY_FF override them (Q4-KQ, as the CUDA tree's test: expert widths 512 and 1024; Q4-SYCL: the q4_* builds) if (const char* e = std::getenv("Q4KQ_PARITY_EMBD")) H = std::atoll(e); if (const char* e = std::getenv("Q4KQ_PARITY_FF")) FF = std::atoll(e); int failures = 0; sycl::queue* s = &dpct::get_in_order_queue(); for (int l : layers) { cpu::NativeFmt f; std::string err; std::vector blob; if (synthetic) { const std::string arg = pair_names[(size_t) l]; const size_t slash = arg.find('/'); const int gu = slash == std::string::npos ? -1 : type_of(arg.substr(0, slash)); const int dn = slash == std::string::npos ? -1 : type_of(arg.substr(slash + 1)); if (gu < 0 || dn < 0 || !cpu::native_fmt(gu, dn, H, FF, f, err) || !strata::kernels::native_expert_supported(gu, dn, H, FF)) { std::printf("%s: %s\n", arg.c_str(), err.empty() ? "not a pair the GPU expert kernels take" : err.c_str()); ++failures; continue; } blob = synthetic_blob(f, l, H, FF); } else { strata::GgufFile& gguf = *gguf_p; const strata::TensorInfo* t[3] = {}; const char* roles[3] = {"gate", "up", "down"}; for (const auto& ti : gguf.tensors()) for (int r = 0; r < 3; ++r) if (ti.name == "blk." + std::to_string(l) + ".ffn_" + roles[r] + "_exps.weight") t[r] = &ti; if (!t[0] || !t[1] || !t[2]) { std::printf("layer %d: no expert tensors\n", l); ++failures; continue; } if (!cpu::native_fmt((int) t[0]->type, (int) t[2]->type, H, FF, f, err)) { std::printf("layer %d: %s\n", l, err.c_str()); ++failures; continue; } blob.resize(f.bytes); std::memcpy(blob.data(), gguf.tensor_data(*t[0]) + (size_t) E * f.up_off, f.up_off); std::memcpy(blob.data() + f.up_off, gguf.tensor_data(*t[1]) + (size_t) E * f.up_off, f.up_off); std::memcpy(blob.data() + f.down_off, gguf.tensor_data(*t[2]) + (size_t) E * (f.bytes - f.down_off), f.bytes - f.down_off); } // (a) the float reference const auto* tg = ggml_get_type_traits((ggml_type) f.gu_type); const auto* td = ggml_get_type_traits((ggml_type) f.d_type); std::vector G((size_t) FF * H), U((size_t) FF * H), D((size_t) H * FF); for (int64_t r = 0; r < FF; ++r) { tg->to_float(blob.data() + r * f.gu_row, G.data() + r * H, H); tg->to_float(blob.data() + f.up_off + r * f.gu_row, U.data() + r * H, H); } for (int64_t r = 0; r < H; ++r) td->to_float(blob.data() + f.down_off + r * f.d_row, D.data() + r * FF, FF); std::mt19937 rng(11 + l); std::normal_distribution nd(0.f, 1.f); std::vector x((size_t) NT * H); for (auto& v : x) v = nd(rng); std::vector ref((size_t) NT * H), got_c((size_t) NT * H), got_g((size_t) NT * H); for (int k = 0; k < NT; ++k) { std::vector h(FF); for (int64_t r = 0; r < FF; ++r) { double g = 0, u = 0; for (int64_t i = 0; i < H; ++i) { g += (double) G[r * H + i] * x[k * H + i]; u += (double) U[r * H + i] * x[k * H + i]; } h[r] = (float) (g / (1.0 + std::exp(-g)) * u); } for (int64_t r = 0; r < H; ++r) { double o = 0; for (int64_t i = 0; i < FF; ++i) o += (double) D[r * FF + i] * h[i]; ref[k * H + r] = (float) o; } } // (b) the CPU { std::vector> act(NT, std::vector(cpu::kNativeActBytes)); std::vector> hq(NT, std::vector(cpu::kNativeHBytes)); std::vector> ff(NT, std::vector(FF)); const void* a[NT]; float* ffp[NT]; const void* hp[NT]; float* op[NT]; for (int k = 0; k < NT; ++k) { cpu::native_quant_act(f, x.data() + k * H, act[k].data()); a[k] = act[k].data(); ffp[k] = ff[k].data(); } cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF); if (cpu::iq512_supported(f.gu_type)) { // ggml's own vec_dot, same Q8_K activations: the reference for both multi-token kernels // (float-order differences only) const auto* tc = ggml_get_type_traits_cpu((ggml_type) f.gu_type); std::vector gref((size_t) NT * FF); for (int k = 0; k < NT; ++k) for (int64_t r = 0; r < FF; ++r) tc->vec_dot((int) H, &gref[k * FF + r], 0, blob.data() + r * f.gu_row, 0, a[k], 0, 1); double usg = 0.0; { // ggml single-token throughput, the weights cache-resident (compute bound) const int it = 50; auto t0 = std::chrono::steady_clock::now(); for (int i = 0; i < it; ++i) for (int64_t r = 0; r < FF; ++r) { float gg, uu; tc->vec_dot((int) H, &gg, 0, blob.data() + r * f.gu_row, 0, a[0], 0, 1); tc->vec_dot((int) H, &uu, 0, blob.data() + f.up_off + r * f.gu_row, 0, a[0], 0, 1); } auto t1 = std::chrono::steady_clock::now(); usg = std::chrono::duration(t1 - t0).count() / it; } // one multi-token kernel: parity against ggml's vec_dot, then one-token and NT-token throughput auto check = [&](const char* tag, bool is512) { std::vector g((size_t) NT * FF); float* gp[NT]; for (int k = 0; k < NT; ++k) gp[k] = g.data() + k * FF; (is512 ? cpu::iq512_rows : cpu::iq256_rows)(f.gu_type, blob.data(), f.gu_row, (int) H, a, NT, gp, 0, (int) FF); const double rg = rel(g, gref); std::printf(" %s %s gate rows vs ggml vec_dot: rel %.2e\n", ggml_type_name((ggml_type) f.gu_type), tag, rg); if (rg > 1e-5) { // ggml's own vec_dot is the reference: above 1e-5 it is a real mismatch, std::printf(" %s %s gate rows MISMATCH (rel %.2e > 1e-5)\n", ggml_type_name((ggml_type) f.gu_type), tag, rg); // not rounding (measured 3e-8) ++failures; } float* f1[1] = {ffp[0]}; const int it = 50; const auto gu = is512 ? cpu::iq512_gu_rows : cpu::iq256_gu_rows; auto t0 = std::chrono::steady_clock::now(); for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, 1, f1, 0, (int) FF); auto t1 = std::chrono::steady_clock::now(); for (int i = 0; i < it; ++i) gu(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a, NT, ffp, 0, (int) FF); auto t2 = std::chrono::steady_clock::now(); const double us1 = std::chrono::duration(t1 - t0).count() / it; const double usn = std::chrono::duration(t2 - t1).count() / it; std::printf(" gate+up one token one thread: %s %.0f us (%.2f GB/s), ggml %.0f us (%.2f GB/s)\n", tag, us1, 2.0 * f.up_off / us1 / 1e3, usg, 2.0 * f.up_off / usg / 1e3); std::printf(" gate+up %d tokens one thread: %s %.0f us vs ggml %d x %.0f us\n", NT, tag, usn, NT, usg); }; if (cpu::cpu_avx512_ok()) check("avx512", true); // guarded: the binary runs on AVX-2 CPUs too check("avx2", false); } for (int k = 0; k < NT; ++k) { cpu::native_quant_h(f, ff[k].data(), hq[k].data()); hp[k] = hq[k].data(); op[k] = got_c.data() + k * H; } cpu::native_down_rows(f, blob.data(), hp, NT, op, 0, (int) H); if (f.d_type == 42) { // (b2) the GGUF-layout Q2_0 kernel the pool uses for Q2_0 down projections - the AVX-512 one // where the CPU has it, the AVX-2 one (q2_avx2.cpp) where it does not. Calling the AVX-512 // kernel unconditionally faults on a Zen 2/3 CPU. std::vector a2(NT); const cpu::ActQ* ap[NT]; std::vector alt((size_t) NT * H); float* altp[NT]; for (int k = 0; k < NT; ++k) { cpu::act_quant_any(ff[k].data(), (int) FF, a2[k]); // gated: AVX-512 or AVX-2 per CPU ap[k] = &a2[k]; altp[k] = alt.data() + k * H; } cpu::q2_rows_any(blob.data() + f.down_off, f.d_row, (int) (FF / 64), ap, NT, altp, 0, (int) H); std::printf(" q2_0 %s down vs ggml down: rel %.2e\n", cpu::cpu_avx512_ok() ? "AVX-512" : "AVX-2", rel(alt, got_c)); } if (f.d_type == 20) { // (b3) the IQ4_NL multi-token AVX-2 kernel the pool now uses for IQ4_NL down projections, // against ggml-cpu's single-token vec_dot on the SAME Q8_0 activations (h), plus timing. std::vector alt((size_t) NT * H), refd((size_t) NT * H); float* altp[NT]; for (int k = 0; k < NT; ++k) altp[k] = alt.data() + k * H; cpu::iq4nl256_down_rows(blob.data() + f.down_off, f.d_row, (int) FF, hp, NT, altp, 0, (int) H); const auto* tdc = ggml_get_type_traits_cpu((ggml_type) f.d_type); for (int k = 0; k < NT; ++k) for (int64_t r = 0; r < H; ++r) tdc->vec_dot((int) FF, refd.data() + k * H + r, 0, blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1); const double rdn = rel(alt, refd); std::printf(" iq4_nl AVX-2 multi-token down vs ggml down: rel %.2e\n", rdn); if (rdn > 1e-5) { // measured 1e-7; same reasoning as the gate/up rows above std::printf(" iq4_nl down MISMATCH (rel %.2e > 1e-5)\n", rdn); ++failures; } const int it = 50; const auto t0 = std::chrono::steady_clock::now(); for (int i = 0; i < it; ++i) cpu::iq4nl256_down_rows(blob.data() + f.down_off, f.d_row, (int) FF, hp, NT, altp, 0, (int) H); const auto t1 = std::chrono::steady_clock::now(); for (int i = 0; i < it; ++i) for (int k = 0; k < NT; ++k) for (int64_t r = 0; r < H; ++r) tdc->vec_dot((int) FF, refd.data() + k * H + r, 0, blob.data() + f.down_off + (size_t) r * f.d_row, 0, hp[k], 0, 1); const auto t2 = std::chrono::steady_clock::now(); const double usn = std::chrono::duration(t1 - t0).count() / it; const double usg = std::chrono::duration(t2 - t1).count() / it; std::printf(" down %d tokens one thread: avx2 %.0f us (%.2f GB/s) vs ggml %.0f us (%.2f GB/s)\n", NT, usn, (double) (f.bytes - f.down_off) / usn / 1e3, usg, (double) (f.bytes - f.down_off) / usg / 1e3); } } // (c) the GPU: one group holding the NT entries { const auto L = strata::kernels::native_expert_layout(f.gu_type, f.d_type, H, FF); void *dblob, *dx, *dxq, *dscr; float* dout; unsigned long long* dptr; int32_t *dstart, *dn, *ddst, *dtok; dblob = (decltype(dblob)) sycl::malloc_device(blob.size(), *s); dx = (decltype(dx)) sycl::malloc_device(x.size() * 4, *s); dxq = (decltype(dxq)) sycl::malloc_device((size_t) NT * H / 32 * 36, *s); dscr = (decltype(dscr)) sycl::malloc_device(strata::kernels::native_expert_scratch_bytes(NT, FF), *s); dout = (decltype(dout)) sycl::malloc_device((size_t) NT * H * 4, *s); dptr = (decltype(dptr)) sycl::malloc_device(8, *s); dstart = (decltype(dstart)) sycl::malloc_device(8, *s); dn = (decltype(dn)) sycl::malloc_device(4, *s); ddst = (decltype(ddst)) sycl::malloc_device(NT * 4, *s); dtok = (decltype(dtok)) sycl::malloc_device(NT * 4, *s); s->memcpy(dblob, blob.data(), blob.size()).wait(); s->memcpy(dx, x.data(), x.size() * 4).wait(); const unsigned long long p = (unsigned long long) dblob; const int32_t st[2] = {0, NT}, one = 1, idx[NT] = {0, 1, 2}; s->memcpy(dptr, &p, 8).wait(); s->memcpy(dstart, st, 8).wait(); s->memcpy(dn, &one, 4).wait(); s->memcpy(ddst, idx, NT * 4).wait(); s->memcpy(dtok, idx, NT * 4).wait(); strata::kernels::quantize_q8_1_rows((const float*) dx, NT, H, dxq, s); strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s); s->wait(); s->memcpy(got_g.data(), dout, got_g.size() * 4).wait(); if (std::getenv("NATIVE_BENCH") != nullptr) { // SYCL port: one layer's worth of hits, timed // G distinct experts (copies of this blob at different addresses), E entries each = a T-token window const int G = 10, E = 4, NE = G * E, iters = 20; std::vector blobs((size_t) G); std::vector ptrs((size_t) G); for (int g = 0; g < G; ++g) { blobs[(size_t) g] = sycl::malloc_device(blob.size(), *s); s->memcpy(blobs[(size_t) g], blob.data(), blob.size()).wait(); ptrs[(size_t) g] = (unsigned long long) blobs[(size_t) g]; } std::vector starts((size_t) G + 1), dst((size_t) NE), tokv((size_t) NE); for (int g = 0; g <= G; ++g) starts[(size_t) g] = g * E; for (int e = 0; e < NE; ++e) { dst[(size_t) e] = e; tokv[(size_t) e] = e % NT; } unsigned long long* bptr = sycl::malloc_device((size_t) G, *s); int32_t* bstart = sycl::malloc_device((size_t) G + 1, *s); int32_t* bn = sycl::malloc_device(1, *s); int32_t* bdst = sycl::malloc_device((size_t) NE, *s); int32_t* btok = sycl::malloc_device((size_t) NE, *s); void* bscr = sycl::malloc_device(strata::kernels::native_expert_scratch_bytes(NE, FF), *s); float* bout = sycl::malloc_device((size_t) NE * H, *s); const int32_t gn = G; s->memcpy(bptr, ptrs.data(), (size_t) G * 8).wait(); s->memcpy(bstart, starts.data(), ((size_t) G + 1) * 4).wait(); s->memcpy(bn, &gn, 4).wait(); s->memcpy(bdst, dst.data(), (size_t) NE * 4).wait(); s->memcpy(btok, tokv.data(), (size_t) NE * 4).wait(); strata::kernels::native_expert_grouped(L, bptr, bstart, bn, bdst, btok, G, NE, dxq, bscr, bout, s); s->wait(); const auto b0 = std::chrono::steady_clock::now(); for (int i = 0; i < iters; ++i) strata::kernels::native_expert_grouped(L, bptr, bstart, bn, bdst, btok, G, NE, dxq, bscr, bout, s); s->wait(); const double ms = std::chrono::duration(std::chrono::steady_clock::now() - b0).count() / iters; std::printf(" GPU grouped: %d experts x %d entries: %.3f ms per layer call (%.1f GB/s of expert bytes)\n", G, E, ms, (double) G * (double) blob.size() / ms / 1e6); for (int g = 0; g < G; ++g) sycl::free(blobs[(size_t) g], *s); sycl::free(bptr, *s); sycl::free(bstart, *s); sycl::free(bn, *s); sycl::free(bdst, *s); sycl::free(btok, *s); sycl::free(bscr, *s); sycl::free(bout, *s); } sycl::free(dblob, *s); sycl::free(dx, *s); sycl::free(dxq, *s); sycl::free(dscr, *s); sycl::free(dout, *s); sycl::free(dptr, *s); sycl::free(dstart, *s); sycl::free(dn, *s); sycl::free(ddst, *s); sycl::free(dtok, *s); } const double ec = rel(got_c, ref), eg = rel(got_g, ref), ecg = rel(got_c, got_g); const double gpu_limit = f.d_type == 6 ? 1.8e-2 : 3e-2; // Q5_0 down: the quantized-sum offset (round 0214) makes it as accurate as the rest const bool ok = ec < 3e-2 && eg < gpu_limit && std::isfinite(ec) && std::isfinite(eg); std::printf("%s %2d %-8s/%-7s blob %8zu cpu rel %.2e gpu rel %.2e cpu-gpu %.2e %s\n", synthetic ? "pair " : "layer", l, ggml_type_name((ggml_type) f.gu_type), ggml_type_name((ggml_type) f.d_type), f.bytes, ec, eg, ecg, ok ? "ok" : "FAIL"); if (!ok) ++failures; } std::printf("native_expert_parity: %d failures\n", failures); return failures ? 1 : 0; }