// claude/s11-s9-combine-headmix: model-free probe of LELEKA_SM120_COMBINE_VEC4 and LELEKA_SM120_HEAD_MIX_MULTI. Each switch launches are run next to the // launches of the current window that they replace, on real-size random inputs (including non-finite values), and EVERY output byte is compared. Prints // "RESULT name key=value" lines; exits 1 if any comparison differs. Never touches the model. Written without a GPU: compiled, not run. // combine_head_probe PTXDIR [combine|head ...] default: both. PTXDIR holds mtp_original_full_combine.ptx, sm120_combine_vec4.ptx, // mtp_original_full_gr_norm.ptx, mtp_original_full_gr_post.ptx, mtp_original_full_bf16.ptx, mtp_original_full_fused_gr.ptx, chain_window.ptx (tools/combine-head/build.sh). #include #include #include #include #include #include #include #include #define CU(x) do { CUresult r_ = (x); if (r_ != CUDA_SUCCESS) { const char* s_ = nullptr; cuGetErrorString(r_, &s_); fprintf(stderr, "CUDA error %s at %s:%d: %s\n", #x, __FILE__, __LINE__, s_ ? s_ : "?"); exit(2); } } while (0) static int g_fail = 0; static uint64_t g_rng = 0x9e3779b97f4a7c15ull; static uint32_t rnd() { g_rng ^= g_rng << 13; g_rng ^= g_rng >> 7; g_rng ^= g_rng << 17; return (uint32_t)(g_rng >> 16); } static float rndf() { return (float)(rnd() & 0xffffff) / 16777216.0f * 2.0f - 1.0f; } // [-1, 1) static uint16_t bf16_of(float x) { uint32_t u; memcpy(&u, &x, 4); return (uint16_t)(u >> 16); } static std::string read_file(const std::string& p) { FILE* f = fopen(p.c_str(), "rb"); if (!f) { fprintf(stderr, "cannot open %s\n", p.c_str()); exit(2); } std::string s; char buf[1 << 16]; size_t n; while ((n = fread(buf, 1, sizeof buf, f)) > 0) s.append(buf, n); fclose(f); return s; } static CUmodule load_module(const std::string& p) { std::string s = read_file(p); s.push_back('\0'); CUmodule m; char log[8192] = {}; CUjit_option opt[] = {CU_JIT_ERROR_LOG_BUFFER, CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES}; void* val[] = {log, (void*)(uintptr_t)sizeof log}; CUresult r = cuModuleLoadDataEx(&m, s.data(), 2, opt, val); if (r != CUDA_SUCCESS) { fprintf(stderr, "load %s failed: %d %s\n", p.c_str(), (int)r, log); exit(2); } return m; } static CUfunction get_fn(CUmodule m, const char* name) { CUfunction f; CUresult r = cuModuleGetFunction(&f, m, name); if (r != CUDA_SUCCESS) { fprintf(stderr, "no entry %s\n", name); exit(2); } return f; } struct Dev { CUdeviceptr p = 0; size_t n = 0; void alloc(size_t bytes) { n = bytes; CU(cuMemAlloc(&p, bytes ? bytes : 1)); CU(cuMemsetD8(p, 0xCD, bytes ? bytes : 1)); } void put(const void* h, size_t bytes) { CU(cuMemcpyHtoD(p, h, bytes)); } std::vector get(size_t off = 0, size_t bytes = (size_t)-1) const { if (bytes == (size_t)-1) bytes = n - off; std::vector v(bytes); CU(cuCtxSynchronize()); CU(cuMemcpyDtoH(v.data(), p + off, bytes)); return v; } }; static bool same(const char* what, const std::vector& a, const std::vector& b) { if (a.size() != b.size()) { printf("MISMATCH %s: size %zu vs %zu\n", what, a.size(), b.size()); g_fail++; return false; } size_t bad = 0, first = (size_t)-1; for (size_t i = 0; i < a.size(); ++i) if (a[i] != b[i]) { if (first == (size_t)-1) first = i; ++bad; } if (bad) { printf("MISMATCH %s: %zu of %zu bytes differ (first at %zu)\n", what, bad, a.size(), first); g_fail++; return false; } return true; } // one launch; args points at the kernel parameters in declaration order static void launch(CUfunction f, unsigned gx, unsigned gy, unsigned bx, unsigned shared, void** args) { CU(cuLaunchKernel(f, gx, gy, 1, bx, 1, 1, shared, nullptr, args, nullptr)); } // ---- combine: mtp_full_combine (grid (n/256, T), 256 threads, one thread a column) against leleka_verify_combine_k10_vec4 (grid (n/4/128, T), 128 threads) static void test_combine(const std::string& dir) { CUmodule mo = load_module(dir + "/mtp_original_full_combine.ptx"), mv = load_module(dir + "/sm120_combine_vec4.ptx"); CUfunction fo = get_fn(mo, "mtp_full_combine"), fv = get_fn(mv, "leleka_verify_combine_k10_vec4"); const int N = 2560, K = 10, MAXT = 8; for (int seed = 0; seed < 4; ++seed) { std::vector parts((size_t)MAXT * K * N), w((size_t)MAXT * K), shared((size_t)MAXT * N); for (auto& v : parts) v = rndf() * (seed == 1 ? 1e6f : seed == 2 ? 1e-30f : 3.0f); for (auto& v : w) v = rndf(); for (auto& v : shared) v = rndf() * (seed == 1 ? 1e6f : 1.0f); if (seed == 3) { // non-finite and signed-zero values: the same bytes must come out of both kernels for (size_t i = 0; i < parts.size(); i += 97) parts[i] = (i % 3 == 0) ? INFINITY : (i % 3 == 1) ? NAN : -0.0f; for (size_t i = 0; i < shared.size(); i += 131) shared[i] = -0.0f; } Dev dp, dw, ds, o_orig, o_vec; dp.alloc(parts.size() * 4); dw.alloc(w.size() * 4); ds.alloc(shared.size() * 4); o_orig.alloc(shared.size() * 4); o_vec.alloc(shared.size() * 4); dp.put(parts.data(), parts.size() * 4); dw.put(w.data(), w.size() * 4); ds.put(shared.data(), shared.size() * 4); for (int T = 1; T <= MAXT; ++T) { CU(cuMemsetD8(o_orig.p, 0xCD, o_orig.n)); CU(cuMemsetD8(o_vec.p, 0xCD, o_vec.n)); int64_t n = N, n4 = N / 4; int k = K; void* ao[] = {&dp.p, &dw.p, &ds.p, &o_orig.p, &n, &k}; void* av[] = {&dp.p, &dw.p, &ds.p, &o_vec.p, &n4}; launch(fo, (N + 255) / 256, T, 256, 0, ao); launch(fv, (unsigned)((n4 + 127) / 128), T, 128, 0, av); const bool ok = same("combine", o_orig.get(0, (size_t)T * N * 4), o_vec.get(0, (size_t)T * N * 4)); // rows past T are untouched (0xCD) by both const bool tail = same("combine tail", o_orig.get((size_t)T * N * 4), o_vec.get((size_t)T * N * 4)); printf("RESULT combine seed=%d T=%d identical=%d\n", seed, T, ok && tail); } } } // ---- head mix: the five launches per token of plan_head_mix against one multi-token hc read (plan_head_mix_multi), chain kernels and the fused ones of the Original static const int N = 2560, HC = 4, D = N * HC, LR = 320, MAXT = 8; struct Fused { uint64_t a[14 * MAXT + 2]; }; // GrMulti: eight 14-word FusedGrArgs, then xn and T (912 bytes) static void set_row(Fused& m, int t, CUdeviceptr R, CUdeviceptr norm, CUdeviceptr down, CUdeviceptr up, CUdeviceptr lo, CUdeviceptr rs, CUdeviceptr mixed) { uint64_t* a = m.a + 14 * t; a[0] = R; a[1] = R; a[2] = 0; // apply = false: the final write is its own launch a[3] = 0; a[4] = 0; // bo_prev, inj_prev a[5] = norm; a[6] = down; a[7] = up; a[8] = 0; // no inject projection float eps = 1e-6f; uint32_t eb; memcpy(&eb, &eps, 4); a[9] = eb; a[10] = lo; a[11] = rs; a[12] = 0; a[13] = mixed; } static void test_head(const std::string& dir) { CUmodule mn = load_module(dir + "/mtp_original_full_gr_norm.ptx"), mp = load_module(dir + "/mtp_original_full_gr_post.ptx"); CUmodule mb = load_module(dir + "/mtp_original_full_bf16.ptx"), mf = load_module(dir + "/mtp_original_full_fused_gr.ptx"); CUmodule mc = load_module(dir + "/chain_window.ptx"); CUfunction f_norm = get_fn(mn, "mtp_full_gr_norm"), f_silu = get_fn(mp, "mtp_full_down_silu"), f_pre = get_fn(mp, "mtp_full_pre_final"); CUfunction f_mm256 = get_fn(mb, "mtp_full_mmvf256"), f_mm160 = get_fn(mb, "mtp_full_mmvf160"); CUfunction f_csplit = get_fn(mc, "chain_gr_split"), f_cdown = get_fn(mc, "chain_gr_down_t2w8_nb4"), f_cup = get_fn(mc, "chain_gr_up8"); CUfunction f_ssplit = get_fn(mf, "mtp_scalar_gr_split"), f_sdown = get_fn(mf, "mtp_scalar_gr_staged"), f_sup = get_fn(mf, "mtp_scalar_gr_up"); CU(cuFuncSetAttribute(f_sdown, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, MAXT * 2 * 1280 * 4)); for (int seed = 0; seed < 3; ++seed) { std::vector R((size_t)MAXT * D), norm(D); std::vector down((size_t)LR * D), up((size_t)D * LR); for (auto& v : R) v = rndf() * (seed == 1 ? 40.0f : 2.0f); for (auto& v : norm) v = 1.0f + 0.5f * rndf(); for (auto& v : down) v = bf16_of(rndf() * 0.03f); for (auto& v : up) v = bf16_of(rndf() * 0.08f); Dev dR, dnorm, ddown, dup, xn_a, lo_a, gated_a, mixed_a, xn_b, lo_b, rs_b, mixed_b; dR.alloc(R.size() * 4); dnorm.alloc(norm.size() * 4); ddown.alloc(down.size() * 2); dup.alloc(up.size() * 2); for (Dev* d : {&xn_a, &xn_b}) d->alloc((size_t)MAXT * D * 4); for (Dev* d : {&lo_a, &lo_b}) d->alloc((size_t)MAXT * LR * 4); gated_a.alloc((size_t)MAXT * D * 4); rs_b.alloc((size_t)MAXT * HC * 4); for (Dev* d : {&mixed_a, &mixed_b}) d->alloc((size_t)MAXT * N * 4); dR.put(R.data(), R.size() * 4); dnorm.put(norm.data(), norm.size() * 4); ddown.put(down.data(), down.size() * 2); dup.put(up.data(), up.size() * 2); for (int T = 1; T <= MAXT; ++T) { // reference: five launches per token (plan_head_mix) CU(cuMemsetD8(mixed_a.p, 0xCD, mixed_a.n)); for (int t = 0; t < T; ++t) { CUdeviceptr r = dR.p + (size_t)t * D * 4, xn = xn_a.p + (size_t)t * D * 4, lo = lo_a.p + (size_t)t * LR * 4; CUdeviceptr gated = gated_a.p + (size_t)t * D * 4, mixed = mixed_a.p + (size_t)t * N * 4; int n = N, hcn = HC, k_in = D, k_up = LR, lr = LR; float eps = 1e-6f, quarter = 0.25f; void* a1[] = {&r, &dnorm.p, &xn, &n, &eps}; launch(f_norm, HC, 1, 1024, 0, a1); void* a2[] = {&xn, &ddown.p, &lo, &k_in}; launch(f_mm256, LR, 1, 256, 0, a2); void* a3[] = {&lo, &lr, &quarter}; launch(f_silu, (LR + 255) / 256, 1, 256, 0, a3); void* a4[] = {&lo, &dup.p, &gated, &k_up}; launch(f_mm160, D, 1, 160, 0, a4); void* a5[] = {&xn, &gated, &mixed, &n, &hcn, &quarter}; launch(f_pre, (N + 255) / 256, 1, 256, 0, a5); } auto ref_mixed = mixed_a.get(0, (size_t)T * N * 4), ref_lo = lo_a.get(0, (size_t)T * LR * 4), ref_xn = xn_a.get(0, (size_t)T * D * 4); for (int variant = 0; variant < 2; ++variant) { // 0: the chain kernels, 1: the fused ones of the Original CU(cuMemsetD8(mixed_b.p, 0xCD, mixed_b.n)); CU(cuMemsetD8(lo_b.p, 0xCD, lo_b.n)); CU(cuMemsetD8(xn_b.p, 0xCD, xn_b.n)); Fused m; memset(&m, 0, sizeof m); for (int t = 0; t < T; ++t) set_row(m, t, dR.p + (size_t)t * D * 4, dnorm.p, ddown.p, dup.p, lo_b.p + (size_t)t * LR * 4, rs_b.p + (size_t)t * HC * 4, mixed_b.p + (size_t)t * N * 4); m.a[14 * MAXT] = xn_b.p; m.a[14 * MAXT + 1] = T; void* args[] = {&m}; const unsigned tiles = (unsigned)((T + 1) / 2), tok2 = T < 2 ? T : 2; launch(variant == 0 ? f_csplit : f_ssplit, T, HC, 256, 0, args); if (variant == 0) launch(f_cdown, 41, tiles, 256, 4 * tok2 * 1280 * 4, args); else launch(f_sdown, 41, 1, 256, T * 2 * 1280 * 4, args); launch(variant == 0 ? f_cup : f_sup, variant == 0 ? 320 : 160, 1, 256, 0, args); const bool a = same("head mixed", ref_mixed, mixed_b.get(0, (size_t)T * N * 4)); const bool b = same("head lo", ref_lo, lo_b.get(0, (size_t)T * LR * 4)); const bool c = same("head xn", ref_xn, xn_b.get(0, (size_t)T * D * 4)); const bool d = same("head tail", mixed_a.get((size_t)T * N * 4), mixed_b.get((size_t)T * N * 4)); printf("RESULT head seed=%d T=%d variant=%s identical=%d\n", seed, T, variant == 0 ? "chain" : "fused", a && b && c && d); } } } } int main(int argc, char** argv) { if (argc < 2) { fprintf(stderr, "usage: %s PTXDIR [combine|head ...]\n", argv[0]); return 2; } CU(cuInit(0)); CUdevice dev; CU(cuDeviceGet(&dev, 0)); CUcontext ctx; CU(cuDevicePrimaryCtxRetain(&ctx, dev)); CU(cuCtxSetCurrent(ctx)); bool all = argc == 2, combine = all, head = all; for (int i = 2; i < argc; ++i) { combine |= !strcmp(argv[i], "combine"); head |= !strcmp(argv[i], "head"); } if (combine) test_combine(argv[1]); if (head) test_head(argv[1]); printf("SUMMARY failures=%d\n", g_fail); return g_fail ? 1 : 0; }