Analysis (docs/experiments/verify-tail-and-gaps.md, CUPTI windows CP.1 / CL.2 / CY.1 against VANL): R6 is the single-stream control (2035 gaps, 211 us) against VANL (1194, 119 us); with the side streams LEL has 1124 / 123.7 us, residual +0.006 ms. R7 has no gap. R8 (+86.5 us at LEL's T mix) is the hyper-connection chain 31.2, the head matmul at T >= 3 38.4 and the greedy pick 18.0. The switch batches the 6 T launches of the tail (mtp_full_native_write plus the five lm_head_mix kernels a row) into 6: new sealed sm120_head_chain.cu/.ptx (final write, head norm, pre-final mix with the row as grid.y, compiled with the --use_fast_math flags of the originals), the existing multi-column bf16 gemv, mtp_full_down_silu over the contiguous rows. Bitwise by construction (same bodies, same per-column fma chain). Flag in GrConfig::head_chain_batch like U5a; HEAD_MIX_MULTI untouched. Model-free probe tools/decode-sm120/sm120_probe_head_chain.cpp (built, not run), report script scripts/e2e/codex/speed/verify_tail_report.py, 15 unit tests (288 original_verify tests pass). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
199 lines
12 KiB
C++
199 lines
12 KiB
C++
// claude/tail-fusion: model-free probe of LELEKA_SM120_HEAD_CHAIN_BATCH (docs/experiments/verify-tail-and-gaps.md).
|
|
// The literal tail of the verify graph, per window row t,
|
|
// mtp_full_native_write R_t = bo_t * (2 sigmoid(inject)) + R_t (mtp_original_full_gr_post.ptx)
|
|
// mtp_full_gr_norm xn_t = norm(R_t) * gamma (mtp_original_full_gr_norm.ptx)
|
|
// mtp_full_mmvf256 lo_t = down . xn_t (mtp_original_full_bf16.ptx)
|
|
// mtp_full_down_silu lo_t = silu(lo_t / 4) (mtp_original_full_gr_post.ptx)
|
|
// mtp_full_mmvf160 gated_t = up . lo_t (mtp_original_full_bf16.ptx)
|
|
// mtp_full_pre_final gated_t = xn_t * sigmoid(gated_t); mixed_t = sum / 4
|
|
// is run against the batched launches of the switch (sm120_head_chain.ptx: leleka_verify_final_write_rows,
|
|
// leleka_verify_head_norm_rows, leleka_verify_head_pre_final_rows; the sealed bf16_f32_mmvf_multi_kernel<256|160, 4|8>;
|
|
// mtp_full_down_silu over the T * 320 contiguous floats) on real-size random inputs, for T = 1..8 rows, three seeds
|
|
// (small, large and special values: -0.0, denormals, inf, nan in the residual, block output and weights), the write also
|
|
// for a group that starts at row 1..7 of the arena. EVERY byte of R, xn, lo, gated (the parts slots) and mixed is
|
|
// compared, so a stray write outside the rows shows too (buffers start as 0xCD). 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_head_chain.sh), not run.
|
|
// sm120_probe_head_chain PTXDIR PTXDIR holds mtp_original_full_gr_post.ptx, mtp_original_full_gr_norm.ptx,
|
|
// mtp_original_full_bf16.ptx and sm120_head_chain.ptx (build_head_chain.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, HC = 4, LR = 320, D = N * HC, K = 10, MAXT = 8;
|
|
static const float QUARTER = 0.25f, EPS = 1e-6f;
|
|
static const char* MULTI_FMT = "_ZN6strata7kernels58_GLOBAL__N__d5e6e908_25_mtp_original_full_bf16_cu_55b113d026bf16_f32_mmvf_multi_kernelILi%dELi%dEEEvPKflPKtPflii";
|
|
|
|
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)); CU(cuMemsetD8(p, 0xCD, bytes)); }
|
|
void put(const void* h, size_t bytes) { CU(cuMemcpyHtoD(p, h, bytes)); }
|
|
std::vector<uint8_t> get() const { std::vector<uint8_t> v(n); CU(cuCtxSynchronize()); CU(cuMemcpyDtoH(v.data(), p, n)); return v; }
|
|
};
|
|
static bool same(const char* what, const std::vector<uint8_t>& a, const std::vector<uint8_t>& b) {
|
|
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));
|
|
}
|
|
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; }
|
|
}
|
|
|
|
struct Fns {
|
|
CUfunction write, norm, mmvf256, mmvf160, silu, pre_final; // the literal kernels
|
|
CUfunction write_rows, norm_rows, pre_final_rows, multi256[2], multi160[2]; // the batched ones (multi: NT 4, 8)
|
|
};
|
|
|
|
// One case: T rows, the write for rows [first, first + T) of an 8-row arena, the head chain for rows [0, T).
|
|
static void run_case(const Fns& f, int seed, int tokens, int first) {
|
|
const float mag = seed == 1 ? 1e3f : 1.0f;
|
|
const bool specials = seed == 2;
|
|
std::vector<float> r((size_t)MAXT * D), bo((size_t)MAXT * N), inj((size_t)MAXT * HC), gamma(D);
|
|
std::vector<uint16_t> down((size_t)LR * D), up((size_t)D * LR);
|
|
for (auto& v : r) v = rndf() * mag;
|
|
for (auto& v : bo) v = rndf() * mag;
|
|
for (auto& v : inj) v = rndf() * 4.0f;
|
|
for (auto& v : gamma) v = rndf() * 2.0f;
|
|
for (auto& v : down) v = (uint16_t)(((rnd() & 0x7f00) | (rnd() & 0xff)) | ((rnd() & 1) << 15)); // bf16, small exponents
|
|
for (auto& v : up) v = (uint16_t)(((rnd() & 0x3f00) | (rnd() & 0xff)) | ((rnd() & 1) << 15));
|
|
if (specials) {
|
|
for (size_t i = 0; i < r.size(); i += 997) r[i] = special(rnd());
|
|
for (size_t i = 0; i < bo.size(); i += 389) bo[i] = special(rnd());
|
|
for (size_t i = 0; i < gamma.size(); i += 211) gamma[i] = special(rnd());
|
|
for (size_t i = 0; i < inj.size(); i += 5) inj[i] = special(rnd()) * 3.0f;
|
|
}
|
|
Dev dr[2], dbo, dinj, dgamma, ddown, dup, dxn[2], dlo[2], dgate[2], dmix[2];
|
|
for (int a = 0; a < 2; ++a) {
|
|
dr[a].alloc(r.size() * 4); dxn[a].alloc((size_t)MAXT * D * 4); dlo[a].alloc((size_t)MAXT * LR * 4);
|
|
dgate[a].alloc((size_t)MAXT * K * N * 4); dmix[a].alloc((size_t)MAXT * N * 4);
|
|
dr[a].put(r.data(), r.size() * 4);
|
|
}
|
|
dbo.alloc(bo.size() * 4); dbo.put(bo.data(), bo.size() * 4);
|
|
dinj.alloc(inj.size() * 4); dinj.put(inj.data(), inj.size() * 4);
|
|
dgamma.alloc(gamma.size() * 4); dgamma.put(gamma.data(), gamma.size() * 4);
|
|
ddown.alloc(down.size() * 2); ddown.put(down.data(), down.size() * 2);
|
|
dup.alloc(up.size() * 2); dup.put(up.data(), up.size() * 2);
|
|
int n = N, hc = HC, lr = LR, d = D, count = tokens * LR;
|
|
float quarter = QUARTER, eps = EPS;
|
|
|
|
// literal arm: the write of the group's rows, then the five kernels per row from row 0
|
|
for (int t = first; t < first + tokens; ++t) {
|
|
CUdeviceptr R = dr[0].p + (size_t)t * D * 4, B = dbo.p + (size_t)t * N * 4, I = dinj.p + (size_t)t * HC * 4;
|
|
void* a[] = {&R, &B, &I, &R, &n, &hc, &quarter};
|
|
launch(f.write, (D + 255) / 256, 1, 256, a);
|
|
}
|
|
for (int t = 0; t < tokens; ++t) {
|
|
CUdeviceptr R = dr[0].p + (size_t)t * D * 4, XN = dxn[0].p + (size_t)t * D * 4, LO = dlo[0].p + (size_t)t * LR * 4;
|
|
CUdeviceptr G = dgate[0].p + (size_t)t * K * N * 4, M = dmix[0].p + (size_t)t * N * 4;
|
|
void* a_norm[] = {&R, &dgamma.p, &XN, &n, &eps};
|
|
launch(f.norm, HC, 1, 1024, a_norm);
|
|
void* a_down[] = {&XN, &ddown.p, &LO, &d};
|
|
launch(f.mmvf256, LR, 1, 256, a_down);
|
|
void* a_silu[] = {&LO, &lr, &quarter};
|
|
launch(f.silu, (LR + 255) / 256, 1, 256, a_silu);
|
|
void* a_up[] = {&LO, &dup.p, &G, &lr};
|
|
launch(f.mmvf160, D, 1, 160, a_up);
|
|
void* a_pre[] = {&XN, &G, &M, &n, &hc, &quarter};
|
|
launch(f.pre_final, (N + 255) / 256, 1, 256, a_pre);
|
|
}
|
|
|
|
// batched arm
|
|
{
|
|
CUdeviceptr R = dr[1].p + (size_t)first * D * 4, B = dbo.p + (size_t)first * N * 4, I = dinj.p + (size_t)first * HC * 4;
|
|
int64_t rs = D, bs = N, is = HC;
|
|
void* a[] = {&R, &B, &I, &n, &hc, &quarter, &rs, &bs, &is};
|
|
launch(f.write_rows, (D + 255) / 256, (unsigned)tokens, 256, a);
|
|
}
|
|
{
|
|
CUdeviceptr R = dr[1].p, XN = dxn[1].p, LO = dlo[1].p, G = dgate[1].p, M = dmix[1].p;
|
|
int64_t ds = D, ls = LR, gs = (int64_t)K * N, ms = N;
|
|
int nt = tokens;
|
|
const int slot = tokens <= 4 ? 0 : 1;
|
|
void* a_norm[] = {&R, &dgamma.p, &XN, &n, &eps, &ds, &ds};
|
|
launch(f.norm_rows, HC, (unsigned)tokens, 1024, a_norm);
|
|
void* a_down[] = {&XN, &ds, &ddown.p, &LO, &ls, &d, &nt};
|
|
launch(f.multi256[slot], LR, 1, 256, a_down);
|
|
void* a_silu[] = {&LO, &count, &quarter};
|
|
launch(f.silu, (unsigned)((count + 255) / 256), 1, 256, a_silu);
|
|
void* a_up[] = {&LO, &ls, &dup.p, &G, &gs, &lr, &nt};
|
|
launch(f.multi160[slot], D, 1, 160, a_up);
|
|
void* a_pre[] = {&XN, &G, &M, &n, &hc, &quarter, &ds, &gs, &ms};
|
|
launch(f.pre_final_rows, (N + 255) / 256, (unsigned)tokens, 256, a_pre);
|
|
}
|
|
const bool ok_r = same("R (residual write)", dr[0].get(), dr[1].get());
|
|
const bool ok_xn = same("xn (head norm)", dxn[0].get(), dxn[1].get());
|
|
const bool ok_lo = same("lo (down gemv + silu)", dlo[0].get(), dlo[1].get());
|
|
const bool ok_g = same("gated (up gemv + pre-final gate)", dgate[0].get(), dgate[1].get());
|
|
const bool ok_m = same("mixed", dmix[0].get(), dmix[1].get());
|
|
printf("RESULT head_chain seed=%d T=%d first=%d identical_R=%d identical_xn=%d identical_lo=%d identical_gated=%d identical_mixed=%d\n", seed, tokens, first,
|
|
ok_r, ok_xn, ok_lo, ok_g, ok_m);
|
|
for (int a = 0; a < 2; ++a) for (Dev* x : {&dr[a], &dxn[a], &dlo[a], &dgate[a], &dmix[a]}) CU(cuMemFree(x->p));
|
|
for (Dev* x : {&dbo, &dinj, &dgamma, &ddown, &dup}) CU(cuMemFree(x->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));
|
|
CUmodule post = load_module(dir + "/mtp_original_full_gr_post.ptx"), norm = load_module(dir + "/mtp_original_full_gr_norm.ptx");
|
|
CUmodule bf16 = load_module(dir + "/mtp_original_full_bf16.ptx"), chain = load_module(dir + "/sm120_head_chain.ptx");
|
|
Fns f;
|
|
f.write = get_fn(post, "mtp_full_native_write"); f.silu = get_fn(post, "mtp_full_down_silu"); f.pre_final = get_fn(post, "mtp_full_pre_final");
|
|
f.norm = get_fn(norm, "mtp_full_gr_norm");
|
|
f.mmvf256 = get_fn(bf16, "mtp_full_mmvf256"); f.mmvf160 = get_fn(bf16, "mtp_full_mmvf160");
|
|
f.write_rows = get_fn(chain, "leleka_verify_final_write_rows"); f.norm_rows = get_fn(chain, "leleka_verify_head_norm_rows");
|
|
f.pre_final_rows = get_fn(chain, "leleka_verify_head_pre_final_rows");
|
|
for (int s = 0; s < 2; ++s) {
|
|
char name[256];
|
|
snprintf(name, sizeof name, MULTI_FMT, 256, s ? 8 : 4); f.multi256[s] = get_fn(bf16, name);
|
|
snprintf(name, sizeof name, MULTI_FMT, 160, s ? 8 : 4); f.multi160[s] = get_fn(bf16, name);
|
|
}
|
|
for (int seed = 0; seed < 3; ++seed)
|
|
for (int tokens = 1; tokens <= MAXT; ++tokens) {
|
|
run_case(f, seed, tokens, 0);
|
|
if (tokens < MAXT) run_case(f, seed, tokens, MAXT - tokens);
|
|
}
|
|
printf("SUMMARY failures=%d\n", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|