// tools/q8-probe/q8_probe.cpp - identity and extreme-activation probe of the sealed q8_1 quantizers (docs/upstream-port-plan.md row 1.1). // // Model-free, one GPU, driver API only. It loads the sealed PTX before the change (OLD) and after it (NEW), runs the same quantizer entry of // both on the same activations and compares every output byte: // normal random activations (a spread of scales, zero blocks, subnormals, ties): OLD and NEW must agree on EVERY byte; // edge blocks around the overflow thresholds (|sum| 65504, amax / 127 = 65504): where the block was finite before it is the same bytes; // extreme amax up to FLT_MAX, +-inf, NaN: NEW's scale and sum are finite (a NaN input keeps the sum NaN), the codes stay inside +-127 // and equal the host's clamp(round(x / d)) (within one step for the fast-math builds, which divide approximately); // OLD's overflow blocks are counted (inf/NaN scale or sum). // Usage: q8_probe OLD.ptx NEW.ptx KIND [ENTRY_SUFFIX] KIND = head | tail | iq | chain exit 0 only if every check holds. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define CK(x) do { CUresult r_ = (x); if (r_ != CUDA_SUCCESS) { const char* s_ = nullptr; cuGetErrorString(r_, &s_); std::fprintf(stderr, "%s:%d %s: %s\n", __FILE__, __LINE__, #x, s_); std::exit(2); } } while (0) struct Blk { uint16_t d, s; int8_t q[32]; }; static_assert(sizeof(Blk) == 36, "q8_1 block"); static int g_fail = 0; #define CHECK(c, ...) do { if (!(c)) { ++g_fail; std::printf("FAIL: "); std::printf(__VA_ARGS__); std::printf("\n"); } } while (0) static std::string slurp(const char* p) { std::ifstream f(p, std::ios::binary); return std::string(std::istreambuf_iterator(f), {}); } static CUmodule load(const char* path, std::string* text) { std::string t = slurp(path); while (!t.empty() && t.back() == '\0') t.pop_back(); if (text) *text = t; CUmodule m; CK(cuModuleLoadData(&m, t.c_str())); return m; } static std::string entry_by_suffix(const std::string& ptx, const std::string& suffix) { std::string found; size_t at = 0; while ((at = ptx.find(".entry ", at)) != std::string::npos) { at += 7; const size_t e = ptx.find('(', at); const std::string name = ptx.substr(at, e - at); if (name.size() >= suffix.size() && name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0) { if (!found.empty()) { std::fprintf(stderr, "ambiguous suffix %s\n", suffix.c_str()); std::exit(2); } found = name; } } if (found.empty()) { std::fprintf(stderr, "no entry ends with %s\n", suffix.c_str()); std::exit(2); } return found; } struct Kernel { CUfunction f; std::string kind; }; static void run(const Kernel& k, CUdeviceptr gate, CUdeviceptr up, CUdeviceptr h, CUdeviceptr y, long long n) { int n32 = (int) n; void* a_int[] = {&gate, &y, &n32}; void* a_ll[] = {&gate, &y, &n}; void* a_chain[] = {&gate, &up, &h, &y, &n}; void** a = k.kind == "chain" ? a_chain : k.kind == "iq" ? a_ll : a_int; CK(cuLaunchKernel(k.f, (unsigned) ((n + 255) / 256), 1, 1, 256, 1, 1, 0, nullptr, a, nullptr)); CK(cuCtxSynchronize()); } static float hf(uint16_t b) { __half h; std::memcpy(&h, &b, 2); return __half2float(h); } static bool fin(float v) { return std::isfinite(v); } struct Out { std::vector y; std::vector h; }; static Out exec(const Kernel& k, const std::vector& x) { const long long n = (long long) x.size(); CUdeviceptr dx, dup = 0, dh = 0, dy; CK(cuMemAlloc(&dx, n * 4)); CK(cuMemAlloc(&dy, n / 32 * 36)); CK(cuMemsetD8(dy, 0xA5, n / 32 * 36)); if (k.kind == "chain") { // xi = silu(g) * up with g = 40: silu(40) is 40 in float (expf(-40) vanishes next to 1), so up = x / 40 gives xi ~ x std::vector g(n, 40.0f), u(n); for (long long i = 0; i < n; ++i) u[i] = x[i] / 40.0f; CK(cuMemcpyHtoD(dx, g.data(), n * 4)); CK(cuMemAlloc(&dup, n * 4)); CK(cuMemcpyHtoD(dup, u.data(), n * 4)); CK(cuMemAlloc(&dh, n * 4)); } else { CK(cuMemcpyHtoD(dx, x.data(), n * 4)); } run(k, dx, dup, dh, dy, n); Out o; o.y.resize(n / 32); CK(cuMemcpyDtoH(o.y.data(), dy, n / 32 * 36)); if (k.kind == "chain") { o.h.resize(n); CK(cuMemcpyDtoH(o.h.data(), dh, n * 4)); } cuMemFree(dx); cuMemFree(dy); if (dup) cuMemFree(dup); if (dh) cuMemFree(dh); return o; } int main(int argc, char** argv) { if (argc < 4) { std::fprintf(stderr, "usage: %s OLD.ptx NEW.ptx KIND [ENTRY_SUFFIX]\n", argv[0]); return 2; } const std::string kind = argv[3]; CK(cuInit(0)); CUdevice dev; CK(cuDeviceGet(&dev, 0)); CUcontext ctx; CK(cuDevicePrimaryCtxRetain(&ctx, dev)); CK(cuCtxSetCurrent(ctx)); std::string to, tn; CUmodule mo = load(argv[1], &to), mn = load(argv[2], &tn); const std::string suffix = argc > 4 ? argv[4] : ""; const std::string ename = kind == "head" ? "head_quant" : kind == "tail" ? "mtp_original_quantize" : kind == "chain" ? "chain_swiglu_quant_kernel" : ""; Kernel ko{nullptr, kind}, kn{nullptr, kind}; CK(cuModuleGetFunction(&ko.f, mo, (ename.empty() ? entry_by_suffix(to, suffix) : ename).c_str())); CK(cuModuleGetFunction(&kn.f, mn, (ename.empty() ? entry_by_suffix(tn, suffix) : ename).c_str())); std::printf("probe %s: %s\n", kind.c_str(), argv[2]); std::mt19937_64 rng(0x5eed0001); std::normal_distribution nd(0.f, 1.f); std::uniform_real_distribution ud(0.f, 1.f); // ---- normal: every byte equal { const long long blocks = 1 << 16; std::vector x(blocks * 32); long long zero = 0, sub = 0; for (long long b = 0; b < blocks; ++b) { const int mode = (int) (rng() % 8); const float scale = std::pow(10.f, -6.f + 9.f * ud(rng)); for (int i = 0; i < 32; ++i) { float v = nd(rng) * scale; if (mode == 0) v = 0.f; // zero block else if (mode == 1) v = (float) ((int) (nd(rng) * 20)) * 0.5f; // ties on a small grid else if (mode == 2) v = nd(rng) * 1e-39f; // subnormals else if (mode == 3 && i == 7) v *= 1000.f; // one big lane x[b * 32 + i] = v; } zero += mode == 0; sub += mode == 2; } const Out a = exec(ko, x), b = exec(kn, x); long long diff = 0, hdiff = 0, tiny_diff = 0, shown = 0; for (long long i = 0; i < blocks; ++i) { if (std::memcmp(&a.y[i], &b.y[i], 36) == 0) continue; ++diff; // a block whose scale underflows (amax / 127 below the smallest normal float) divides by a flushed or denormal scale: its // old codes were not meaningful (x / 0 = inf wrapped to an int8), the new ones are the clamp; not an activation of a real model float amax = 0; double bsum = 0; for (int j = 0; j < 32; ++j) { const float v = a.h.empty() ? x[i * 32 + j] : b.h[i * 32 + j]; amax = std::fmax(amax, std::fabs(v)); bsum += v; } // the other regime of change: the block's sum (or scale) is past the largest half, where OLD stored inf const bool tiny = amax / 127.0f < FLT_MIN || std::fabs(bsum) > 65000.0 || amax / 127.0f > 65000.0; tiny_diff += tiny; if (!tiny && shown++ < 5) std::printf(" differs: block %lld amax %g OLD d=%g s=%g q0=%d | NEW d=%g s=%g q0=%d\n", i, amax, hf(a.y[i].d), hf(a.y[i].s), a.y[i].q[0], hf(b.y[i].d), hf(b.y[i].s), b.y[i].q[0]); } for (size_t i = 0; i < a.h.size(); ++i) hdiff += std::memcmp(&a.h[i], &b.h[i], 4) != 0; std::printf("normal : %lld blocks (%lld zero, %lld subnormal): %lld blocks differ (%lld of them with an underflowing scale or a sum/scale past the half range, where OLD stored inf), %lld h words differ\n", blocks, zero, sub, diff, tiny_diff, hdiff); CHECK(diff == tiny_diff && hdiff == 0, "normal activations are not bit-identical"); } // ---- edge and extreme: blocks built one by one struct Case { const char* name; std::vector v; }; std::vector cases; auto fill = [&](const char* name, std::function f) { Case c{name, std::vector(32)}; for (int i = 0; i < 32; ++i) c.v[i] = f(i); cases.push_back(c); }; fill("sum 65504 (32 x 2047)", [](int) { return 2047.0f; }); fill("sum 65520 (32 x 2047.5)", [](int) { return 2047.5f; }); fill("sum 4e6 (32 x 125000), d finite", [](int) { return 125000.0f; }); fill("amax 8.0e6 (d = 62992)", [](int i) { return i == 3 ? 8.0e6f : 1.0f; }); fill("amax 8.4e6 (d past half max)", [](int i) { return i == 3 ? 8.4e6f : 1.0f; }); fill("amax 1e9, lane 5", [](int i) { return i == 5 ? 1e9f : -3.0f; }); fill("amax 1e30 and -1e30", [](int i) { return i == 0 ? 1e30f : i == 1 ? -1e30f : 0.5f; }); fill("FLT_MAX x 32 (sum overflows)", [](int) { return FLT_MAX; }); fill("massive activation among O(1)", [](int i) { return i == 12 ? 7.5e7f : (float) (i - 16) * 0.37f; }); fill("+inf lane", [](int i) { return i == 4 ? INFINITY : 1.0f; }); fill("-inf lane", [](int i) { return i == 4 ? -INFINITY : 1.0f; }); fill("+inf and -inf (sum NaN)", [](int i) { return i == 0 ? INFINITY : i == 1 ? -INFINITY : 2.0f; }); fill("NaN lane", [](int i) { return i == 9 ? NAN : 1.0f; }); fill("all NaN", [](int) { return NAN; }); fill("all zero", [](int) { return 0.0f; }); const int reps = 64; // every case in many blocks, so many warps run it std::vector x; for (int r = 0; r < reps; ++r) for (auto& c : cases) x.insert(x.end(), c.v.begin(), c.v.end()); const Out a = exec(ko, x), b = exec(kn, x); const bool chain = kind == "chain"; long long old_bad_scale = 0, old_bad_sum = 0; for (size_t ci = 0; ci < cases.size(); ++ci) { const Blk& po = a.y[ci]; const Blk& pn = b.y[ci]; // the block's own float inputs (the chain kernel stores xi in h) std::vector xi(32); for (int i = 0; i < 32; ++i) xi[i] = chain ? b.h[ci * 32 + i] : cases[ci].v[i]; double amax = 0; bool has_nan = false, has_inf = false; for (float v : xi) { if (std::isnan(v)) has_nan = true; else if (std::isinf(v)) has_inf = true; else amax = std::fmax(amax, std::fabs((double) v)); } const float ds = hf(pn.d), ss = hf(pn.s); const float do_ = hf(po.d), so_ = hf(po.s); old_bad_scale += !fin(do_); old_bad_sum += !fin(so_); double fsum = 0; for (float v : xi) fsum += v; // double: inf + -inf = NaN, the same condition as the float sum const bool sum_nan = std::isnan(fsum) || has_nan; bool all_nan = true; for (float v : xi) all_nan = all_nan && std::isnan(v); CHECK(all_nan ? std::isnan(ds) : fin(ds), "%s: NEW scale %g (a block of NaNs keeps a NaN scale, any other is finite)", cases[ci].name, ds); CHECK(sum_nan ? std::isnan(ss) : fin(ss), "%s: NEW sum %g (expected %s)", cases[ci].name, ss, sum_nan ? "NaN" : "finite"); for (int i = 0; i < 32; ++i) { if (std::isnan(xi[i])) continue; if (amax == 0 || has_inf) { CHECK(std::abs((int) pn.q[i]) <= 127, "%s: code out of range", cases[ci].name); continue; } const double d = std::fmin((double) (float) (amax / 127.0), 65504.0); const double ex = std::fmax(-127.0, std::fmin(127.0, std::round((double) xi[i] / d))); CHECK(std::fabs((double) pn.q[i] - ex) <= 1.0, "%s: lane %d code %d, expected %g", cases[ci].name, i, pn.q[i], ex); } // OLD finite and in range: NEW must equal it byte for byte bool old_fine = fin(do_) && fin(so_) && !has_nan && !has_inf && amax / 127.0 <= 65504.0 && std::fabs(fsum) <= 65000.0; if (old_fine) CHECK(std::memcmp(&po, &pn, 36) == 0, "%s: a block that was finite before changed", cases[ci].name); std::printf(" %-36s OLD d=%-9g sum=%-10g | NEW d=%-8g sum=%-10g q[0..2]=%d,%d,%d%s\n", cases[ci].name, do_, so_, ds, ss, pn.q[0], pn.q[1], pn.q[2], old_fine ? " (finite before: identical)" : ""); } for (int r = 1; r < reps; ++r) for (size_t ci = 0; ci < cases.size(); ++ci) CHECK(std::memcmp(&b.y[ci], &b.y[r * cases.size() + ci], 36) == 0 && std::memcmp(&a.y[ci], &a.y[r * cases.size() + ci], 36) == 0, "case %zu differs between repetitions", ci); std::printf("extreme: OLD stored a non-finite scale in %lld and a non-finite sum in %lld of %zu cases; NEW none (sums of NaN inputs stay NaN)\n", old_bad_scale, old_bad_sum, cases.size()); CHECK(old_bad_scale + old_bad_sum > 0, "the probe never produced an overflow in OLD: it does not exercise the change"); std::printf(g_fail ? "q8_probe: FAILED (%d)\n" : "q8_probe: OK\n", g_fail); return g_fail ? 1 : 0; }