LELEKA_SM120_HITS_IN_COMBINE: one leleka_verify_combine_k10_vec4_hits launch per layer replaces moe_hit_add + the combine (mtp_full_combine or the COMBINE_VEC4 kernel), -48 launches per graph. New sealed asset sm120_combine_hits.cu/.ptx (nvcc 13.2.86, sm_120, --fmad=true --ftz=false; pinned in extra-seal-plan.json and ptx-sm86/MANIFEST.json), planner and ffn_hits_combine in original_verify_window_ops/hits_combine*, asset preloaded via required_assets. Shapes the kernel does not fit record add_hits + the scalar combine. Model-free probe tools/decode-sm120/sm120_probe_hits.cpp (+ build_hits.sh): add_hits + combine against the folded kernel byte for byte, T 1-8; built, not run (no GPU). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
174 lines
11 KiB
C++
174 lines
11 KiB
C++
// claude/ffn-hits-combine: model-free probe of LELEKA_SM120_HITS_IN_COMBINE (docs/experiments/ffn-phase-loss-design.md, D2).
|
|
// The window's two launches per layer
|
|
// add_hits_kernel parts[row] += hit_out[row] for the rows listed in dst[0..count) (mtp_original_full_s2.ptx)
|
|
// combine mtp_full_combine (scalar, the literal Original) or leleka_verify_combine_k10_vec4
|
|
// are run against the one launch of leleka_verify_combine_k10_vec4_hits (sm120_combine_hits.ptx) on real-size random inputs
|
|
// and EVERY output byte of the combine is compared, for T = 1..8 rows from a first row 0 and from the last possible first
|
|
// row (the hit list is relative to the group's first row), with hit lists that are empty, partial, full, in any order, at
|
|
// the edges of the expert range, with the CPU leg convention (listed parts rows zeroed to +0.0) and without it, and with
|
|
// -0.0, denormal, inf and nan values in the hit and part rows. The folded kernel must also leave `parts` unchanged.
|
|
// Prints "RESULT name key=value" lines; exits 1 if any comparison differs. Never touches the model. Written without a GPU:
|
|
// compiled (tools/decode-sm120/build_hits.sh), not run.
|
|
// sm120_probe_hits PTXDIR PTXDIR holds mtp_original_full_s2.ptx, mtp_original_full_combine.ptx, sm120_combine_vec4.ptx,
|
|
// sm120_combine_hits.ptx (build_hits.sh [OUTDIR] collects them).
|
|
#include <cuda.h>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#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 const int N = 2560, K = 10, MAXT = 8;
|
|
static const char* ADD_HITS = "_ZN6strata7kernels56_GLOBAL__N__2b3d023b_23_mtp_original_full_s2_cu_9e2602a815add_hits_kernelEPfPKfPKiS6_i";
|
|
|
|
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 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<uint8_t> get(size_t off, size_t bytes) const {
|
|
std::vector<uint8_t> v(bytes); CU(cuCtxSynchronize()); CU(cuMemcpyDtoH(v.data(), p + off, bytes)); return v;
|
|
}
|
|
};
|
|
static bool same(const char* what, const std::vector<uint8_t>& a, const std::vector<uint8_t>& 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;
|
|
}
|
|
static void launch(CUfunction f, unsigned gx, unsigned gy, unsigned bx, void** args) {
|
|
CU(cuLaunchKernel(f, gx, gy, 1, bx, 1, 1, 0, nullptr, args, nullptr));
|
|
}
|
|
|
|
// What a case puts in the inputs.
|
|
struct Case {
|
|
const char* name;
|
|
int hits; // 0 = none, 1 = a random half of the T*K rows, 2 = every row, 3 = expert 0 and 9 of the last token only
|
|
bool zero_listed; // the CPU leg convention: the listed parts rows are +0.0 before the hit is added
|
|
bool specials; // -0.0, denormal, inf and nan in the parts and hit rows
|
|
};
|
|
static const Case CASES[] = {
|
|
{"none", 0, true, false}, {"none_specials", 0, false, true}, {"half_zeroed", 1, true, false},
|
|
{"half_free", 1, false, false}, {"all_zeroed", 2, true, true}, {"all_free_specials", 2, false, true},
|
|
{"edges_zeroed", 3, true, true}, {"half_zeroed_specials", 1, true, true},
|
|
};
|
|
|
|
static float special(uint32_t pick) {
|
|
switch (pick % 6) { case 0: return -0.0f; case 1: return 0.0f; case 2: return 1e-40f; case 3: return -1e-40f; case 4: return INFINITY; default: return NAN; }
|
|
}
|
|
// The hit list of a case for `tokens` rows (relative row = token * K + expert), in a random order.
|
|
static std::vector<int32_t> hit_list(const Case& c, int tokens) {
|
|
std::vector<int32_t> rows;
|
|
for (int r = 0; r < tokens * K; ++r) {
|
|
const bool listed = c.hits == 2 || (c.hits == 1 && (rnd() & 1)) || (c.hits == 3 && r >= (tokens - 1) * K && (r % K == 0 || r % K == K - 1));
|
|
if (listed) rows.push_back(r);
|
|
}
|
|
for (size_t i = rows.size(); i > 1; --i) std::swap(rows[i - 1], rows[rnd() % i]);
|
|
return rows;
|
|
}
|
|
|
|
static void run_case(const Case& c, int seed, int tokens, int first, CUfunction f_add, CUfunction f_scalar, CUfunction f_vec4, CUfunction f_hits) {
|
|
const size_t rows = (size_t)MAXT * K * N;
|
|
std::vector<float> parts(rows), hit_out(rows), w((size_t)MAXT * K), shared((size_t)MAXT * N);
|
|
for (auto& v : parts) v = rndf() * (seed == 1 ? 1e6f : 3.0f);
|
|
for (auto& v : hit_out) v = rndf() * (seed == 1 ? 1e6f : 3.0f);
|
|
for (auto& v : w) v = rndf();
|
|
for (auto& v : shared) v = rndf();
|
|
if (c.specials) {
|
|
for (size_t i = 0; i < rows - 8; i += 61) { parts[i] = special(rnd()); hit_out[i + (i % 7)] = special(rnd()); }
|
|
for (size_t i = 0; i < shared.size(); i += 131) shared[i] = -0.0f;
|
|
}
|
|
std::vector<int32_t> list = hit_list(c, tokens);
|
|
if (c.zero_listed) // the CPU leg zeroes the rows whose expert ran on the hit path
|
|
for (int32_t r : list) std::fill_n(parts.begin() + ((size_t)first * K + r) * N, N, 0.0f);
|
|
int32_t count = (int32_t)list.size();
|
|
list.resize((size_t)MAXT * K, -1);
|
|
const size_t at = (size_t)first * K * N * 4, bytes = (size_t)tokens * K * N * 4, row_at = (size_t)first * N * 4, row_bytes = (size_t)tokens * N * 4;
|
|
Dev dp_a, dp_b, dp_c, dh, dw, ds, dd, dn, o_a, o_b, o_c;
|
|
for (Dev* d : {&dp_a, &dp_b, &dp_c, &dh}) d->alloc(rows * 4);
|
|
dw.alloc(w.size() * 4); ds.alloc(shared.size() * 4); dd.alloc(list.size() * 4); dn.alloc(4);
|
|
for (Dev* d : {&o_a, &o_b, &o_c}) d->alloc(shared.size() * 4);
|
|
for (Dev* d : {&dp_a, &dp_b, &dp_c}) d->put(parts.data(), rows * 4);
|
|
dh.put(hit_out.data(), rows * 4); dw.put(w.data(), w.size() * 4); ds.put(shared.data(), shared.size() * 4);
|
|
dd.put(list.data(), list.size() * 4); dn.put(&count, 4);
|
|
int n_embd = N; int64_t n = N, n4 = N / 4; int k = K;
|
|
// add_hits_kernel over the group's parts and hit_out base (grid (min(ceil(N/256), 8), cap = tokens * K), 256 threads)
|
|
for (Dev* dp : {&dp_a, &dp_b}) {
|
|
CUdeviceptr pa = dp->p + at, ha = dh.p + at;
|
|
void* args[] = {&pa, &ha, &dd.p, &dn.p, &n_embd};
|
|
launch(f_add, std::min((N + 255) / 256, 8), (unsigned)(tokens * K), 256, args);
|
|
}
|
|
CUdeviceptr w0 = dw.p + (size_t)first * K * 4, s0 = ds.p + row_at;
|
|
CUdeviceptr pa = dp_a.p + at, pb = dp_b.p + at, pc = dp_c.p + at, hc = dh.p + at;
|
|
CUdeviceptr oa = o_a.p + row_at, ob = o_b.p + row_at, oc = o_c.p + row_at;
|
|
void* a_scalar[] = {&pa, &w0, &s0, &oa, &n, &k};
|
|
void* a_vec4[] = {&pb, &w0, &s0, &ob, &n4};
|
|
void* a_hits[] = {&pc, &hc, &dd.p, &dn.p, &w0, &s0, &oc, &n4};
|
|
launch(f_scalar, (N + 255) / 256, (unsigned)tokens, 256, a_scalar);
|
|
launch(f_vec4, (unsigned)((n4 + 127) / 128), (unsigned)tokens, 128, a_vec4);
|
|
launch(f_hits, (unsigned)((n4 + 127) / 128), (unsigned)tokens, 128, a_hits);
|
|
const auto out_a = o_a.get(row_at, row_bytes), out_b = o_b.get(row_at, row_bytes), out_c = o_c.get(row_at, row_bytes);
|
|
const bool scalar_ok = same("add_hits + mtp_full_combine vs folded", out_a, out_c);
|
|
const bool vec4_ok = same("add_hits + combine_vec4 vs folded", out_b, out_c);
|
|
// rows outside the launch are untouched (0xCD) by the folded kernel, and it does not write `parts` back
|
|
const size_t after = shared.size() * 4 - row_at - row_bytes;
|
|
const bool tail_ok = same("outside the rows", o_c.get(0, row_at), std::vector<uint8_t>(row_at, 0xCD)) &&
|
|
same("after the rows", o_c.get(row_at + row_bytes, after), std::vector<uint8_t>(after, 0xCD));
|
|
std::vector<uint8_t> before(bytes); memcpy(before.data(), (const uint8_t*)parts.data() + at, bytes);
|
|
const bool parts_ok = same("parts after the folded kernel", dp_c.get(at, bytes), before);
|
|
printf("RESULT hits case=%s seed=%d T=%d first=%d count=%d identical_scalar=%d identical_vec4=%d tail=%d parts_unchanged=%d\n", c.name, seed, tokens, first,
|
|
(int)count, scalar_ok, vec4_ok, tail_ok, parts_ok);
|
|
for (Dev* d : {&dp_a, &dp_b, &dp_c, &dh, &dw, &ds, &dd, &dn, &o_a, &o_b, &o_c}) CU(cuMemFree(d->p));
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) { fprintf(stderr, "usage: %s PTXDIR\n", argv[0]); return 2; }
|
|
const std::string dir = argv[1];
|
|
CU(cuInit(0)); CUdevice dev; CU(cuDeviceGet(&dev, 0)); CUcontext ctx; CU(cuDevicePrimaryCtxRetain(&ctx, dev)); CU(cuCtxSetCurrent(ctx));
|
|
CUfunction f_add = get_fn(load_module(dir + "/mtp_original_full_s2.ptx"), ADD_HITS);
|
|
CUfunction f_scalar = get_fn(load_module(dir + "/mtp_original_full_combine.ptx"), "mtp_full_combine");
|
|
CUfunction f_vec4 = get_fn(load_module(dir + "/sm120_combine_vec4.ptx"), "leleka_verify_combine_k10_vec4");
|
|
CUfunction f_hits = get_fn(load_module(dir + "/sm120_combine_hits.ptx"), "leleka_verify_combine_k10_vec4_hits");
|
|
for (const Case& c : CASES)
|
|
for (int seed = 0; seed < 3; ++seed)
|
|
for (int tokens = 1; tokens <= MAXT; ++tokens) {
|
|
run_case(c, seed, tokens, 0, f_add, f_scalar, f_vec4, f_hits);
|
|
if (tokens < MAXT) run_case(c, seed, tokens, MAXT - tokens, f_add, f_scalar, f_vec4, f_hits);
|
|
}
|
|
printf("SUMMARY failures=%d\n", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|