Files
Pavlo RatushnyiandClaude Sonnet 5.5 28d74a2efd draft-round: fused verifier commit graph (LELEKA_DRAFT_FUSED_COMMIT=on, opt-in, sm_120 only): 182 nodes -> 4
The accepted-prefix commit graph replayed each of the 36 GDN layers (conv history commit + recurrence replay through the accepted tokens) and each of the 12 QSA layers (tail restore +
8 row appends) as its own kernels. Two sealed kernels do all layers in one launch each: draft_commit_gdn_layers (precise math like original_verify.cu; the replay's output path,
which nothing reads, is not computed; the state-independent loads of all tokens are issued first) and draft_commit_qsa_layers (fast math like the resealed original_verify_qsa.cu;
one block per layer, rows in order). Model-free probe tools/draft-round/commit_probe.cpp: every state byte identical to the Original's kernels on 36 GDN and 60 QSA random
cases (n_keep 0..8, three RoPE settings, positions around block edges, holes); in a graph the GDN part is 346 -> 156 us, the QSA part 72 -> 4.5 us. Replay gate: draft-round-id1
(fused alone, DAILY alone and both): 2057/2057 top-16 logprob rows and 1036/1036 draft traces bit-identical, A/A exact.

Sealed with scripts/gpu-bringup (extra-seal-plan.json group draft-round, nvcc 13.2.86); ptx-sm86/MANIFEST.json records them as not built for sm_86 (record_commit fuses only on cc 12).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 13:00:32 +01:00

245 lines
14 KiB
C++

// draft-round: model-free probe of the fused verifier commit kernels (crates/cuda-driver/src/draft_commit.ptx). Each fused kernel is run next to the sequence of kernels of
// the commit graph it replaces (original_verify.ptx / original_verify_qsa.ptx) on real-size random inputs and EVERY output byte is compared; the two are also timed inside a
// CUDA graph. Prints "RESULT name key=value" lines; exits 1 if any comparison differs. Never touches the model.
// commit_probe PTXDIR PTXDIR holds original_verify.ptx, original_verify_qsa.ptx, draft_commit.ptx and draft_commit_qsa.ptx (copies of crates/cuda-driver/src/)
#include <cuda.h>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#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 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, 0, bytes ? bytes : 1)); CU(cuCtxSynchronize()); }
void put(const void* h, size_t bytes) { CU(cuMemcpyHtoD(p, h, bytes)); CU(cuCtxSynchronize()); }
std::vector<uint8_t> get() const { std::vector<uint8_t> v(n); 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) {
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 CUstream g_s;
static double time_graph(const std::function<void()>& body, int iters = 20) {
CUgraph g; CUgraphExec ge;
CU(cuStreamBeginCapture(g_s, CU_STREAM_CAPTURE_MODE_GLOBAL));
body();
CU(cuStreamEndCapture(g_s, &g));
CU(cuGraphInstantiateWithFlags(&ge, g, 0));
for (int i = 0; i < 3; ++i) CU(cuGraphLaunch(ge, g_s));
CU(cuStreamSynchronize(g_s));
CUevent a, b; CU(cuEventCreate(&a, 0)); CU(cuEventCreate(&b, 0));
std::vector<double> v;
for (int i = 0; i < iters; ++i) {
CU(cuEventRecord(a, g_s)); CU(cuGraphLaunch(ge, g_s)); CU(cuEventRecord(b, g_s)); CU(cuEventSynchronize(b));
float ms; CU(cuEventElapsedTime(&ms, a, b)); v.push_back(ms * 1000.0);
}
std::sort(v.begin(), v.end());
return v[v.size() / 2];
}
static void launch(CUfunction f, unsigned gx, unsigned gy, unsigned bx, unsigned by, void** args) {
CU(cuLaunchKernel(f, gx, gy, 1, bx, by, 1, 0, g_s, args, nullptr));
}
constexpr int H = 48, C = 10240, T = 8;
constexpr size_t STATE_WORDS = (size_t)128 * 48 * 128, HIST_WORDS = (size_t)10240 * 3;
constexpr size_t LAYER_BYTES = (STATE_WORDS + HIST_WORDS) * 4;
struct GdnOperands {
Dev A, B, dqkv, dh, dg, db, dz, dgam, ydummy, commit;
};
// the Original's commit graph for the GDN layers: per layer conv commit + recurrence replay
static void gdn_original(GdnOperands& o, CUfunction conv, CUfunction step) {
const float eps = 1e-6f;
for (int i = 0; i < 36; ++i) {
CUdeviceptr hist = o.A.p + (size_t)i * LAYER_BYTES + STATE_WORDS * 4, state = o.A.p + (size_t)i * LAYER_BYTES;
CUdeviceptr q = o.dqkv.p + (size_t)i * T * C * 4, hh = o.dh.p + (size_t)i * T * C * 4, g = o.dg.p + (size_t)i * T * H * 4, b = o.db.p + (size_t)i * T * H * 4, gm = o.dgam.p + (size_t)i * 128 * 4;
int c = C; CUdeviceptr nk = o.commit.p;
void* a1[] = {&hist, &q, &c, &nk};
launch(conv, (C + 255) / 256, 1, 256, 1, a1);
CUdeviceptr zz = o.dz.p, yd = o.ydummy.p; int ci = C, hk = 16, hv = 48, tt = T, tob = 0; float e = eps;
void* a2[] = {&state, &hh, &ci, &g, &b, &zz, &gm, &e, &yd, &hk, &hv, &tt, &nk, &tob};
launch(step, 48, 1, 128, 4, a2);
}
}
static void gdn_fused(GdnOperands& o, CUfunction fused) {
uint64_t w[9] = {o.B.p, (uint64_t)LAYER_BYTES, (uint64_t)(STATE_WORDS * 4), o.dqkv.p, o.dh.p, o.dg.p, o.db.p, o.commit.p, (uint64_t)T};
void* af[] = {w};
launch(fused, 48 + 20, 36, 128, 4, af);
}
static void test_gdn(CUmodule mo, CUmodule md) {
CUfunction conv = get_fn(mo, "verify_gdn_conv_commit_kernel"), step = get_fn(mo, "verify_gdn_step_norm_multi_kernel"), fused = get_fn(md, "draft_commit_gdn_layers");
const size_t canon = 36 * LAYER_BYTES;
std::vector<float> st(canon / 4), qkv((size_t)36 * T * C), h((size_t)36 * T * C), gate((size_t)36 * T * H), beta((size_t)36 * T * H), z((size_t)T * 6144), gamma(36 * 128);
for (auto& x : qkv) x = rndf();
for (auto& x : h) x = rndf() * 0.3f;
for (auto& x : gate) x = -(rndf() * 0.5f + 0.5f) * 2.0f;
for (auto& x : beta) x = rndf() * 0.5f + 0.5f;
for (auto& x : z) x = rndf();
for (auto& x : gamma) x = rndf() * 0.5f + 1.0f;
GdnOperands o;
o.A.alloc(canon); o.B.alloc(canon); o.dqkv.alloc(qkv.size() * 4); o.dh.alloc(h.size() * 4); o.dg.alloc(gate.size() * 4); o.db.alloc(beta.size() * 4); o.dz.alloc(z.size() * 4); o.dgam.alloc(gamma.size() * 4);
o.ydummy.alloc((size_t)T * 6144 * 4); o.commit.alloc((T + 2) * 4);
o.dqkv.put(qkv.data(), qkv.size() * 4); o.dh.put(h.data(), h.size() * 4); o.dg.put(gate.data(), gate.size() * 4); o.db.put(beta.data(), beta.size() * 4); o.dz.put(z.data(), z.size() * 4); o.dgam.put(gamma.data(), gamma.size() * 4);
int cases = 0;
for (int seed = 0; seed < 4; ++seed) {
for (int n = 0; n <= T; ++n) {
for (auto& x : st) x = rndf() * (seed ? 0.5f : 2.0f);
o.A.put(st.data(), canon); o.B.put(st.data(), canon);
int32_t cm[T + 2] = {n, 0, 0, 0, 0, 0, 0, 0, 0, 0};
o.commit.put(cm, sizeof cm);
gdn_original(o, conv, step);
gdn_fused(o, fused);
CU(cuStreamSynchronize(g_s));
char what[96]; snprintf(what, sizeof what, "gdn seed %d n_keep %d (state+history of 36 layers)", seed, n);
same(what, o.A.get(), o.B.get());
++cases;
}
}
printf("RESULT gdn cases=%d\n", cases);
int32_t cm[T + 2] = {3, 0, 0, 0, 0, 0, 0, 0, 0, 0};
o.commit.put(cm, sizeof cm);
double t_orig = time_graph([&] { gdn_original(o, conv, step); });
double t_fused = time_graph([&] { gdn_fused(o, fused); });
printf("RESULT gdn_time n_keep=3 original_us=%.1f fused_us=%.1f\n", t_orig, t_fused);
}
constexpr int D = 128;
struct QsaArgs {
uint64_t tail[12], dead[12], pooled[12], block_pos[12], gamma[12];
uint64_t raw_base, snap_base, commit, mtab, rows, max_cells, pos_base;
float epsilon, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale, pad;
};
struct Rope { float theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale; };
struct QsaOperands {
Dev tail[2][12], dead[2][12], pooled[2][12], bpos[2][12], gam[12], raw, snap, commit;
};
static const int kMaxCells = 64 * 4;
static void qsa_original(QsaOperands& q, CUfunction app, CUfunction cpy, const Rope& r) {
for (int l = 0; l < 12; ++l) {
CUdeviceptr dst = q.tail[0][l].p, src = q.snap.p + (size_t)l * 1536; int64_t n4 = 96;
void* ac[] = {&dst, &src, &n4};
launch(cpy, 1, 1, 256, 1, ac);
for (int row = 0; row < T; ++row) {
CUdeviceptr rw = q.raw.p + ((size_t)l * T + row) * 512, pd = q.commit.p + (2 + row) * 4, gm = q.gam[l].p, tl = q.tail[0][l].p, dd = q.dead[0][l].p, po = q.pooled[0][l].p, bpp = q.bpos[0][l].p, mt = 0, rc = 0, rs = 0;
int pb = 0, mc = kMaxCells, rm = 0; float e = 1e-6f, th = r.theta_scale, fs = r.freq_scale, cl = r.corr_low, ch = r.corr_high, ex = r.ext_factor, ms = r.mscale;
void* aa[] = {&rw, &pd, &pb, &gm, &e, &tl, &dd, &po, &bpp, &mc, &th, &fs, &cl, &ch, &ex, &ms, &mt, &rc, &rs, &rm};
launch(app, 1, 1, 256, 1, aa);
}
}
}
static void qsa_fused(QsaOperands& q, CUfunction fused, const Rope& r) {
QsaArgs qa;
memset(&qa, 0, sizeof qa);
for (int l = 0; l < 12; ++l) { qa.tail[l] = q.tail[1][l].p; qa.dead[l] = q.dead[1][l].p; qa.pooled[l] = q.pooled[1][l].p; qa.block_pos[l] = q.bpos[1][l].p; qa.gamma[l] = q.gam[l].p; }
qa.raw_base = q.raw.p; qa.snap_base = q.snap.p; qa.commit = q.commit.p; qa.mtab = 0; qa.rows = T; qa.max_cells = kMaxCells; qa.pos_base = 0;
qa.epsilon = 1e-6f; qa.theta_scale = r.theta_scale; qa.freq_scale = r.freq_scale; qa.corr_low = r.corr_low; qa.corr_high = r.corr_high; qa.ext_factor = r.ext_factor; qa.mscale = r.mscale;
void* af[] = {&qa};
launch(fused, 12, 1, 128, 4, af);
}
static void test_qsa(CUmodule mq, CUmodule mo, CUmodule md) {
CUfunction app = get_fn(mq, "verify_qsa_append_analytic"), cpy = get_fn(mo, "verify_copy_from_mapped_kernel"), fused = get_fn(md, "draft_commit_qsa_layers");
const int blocks = 40; // positions below 160: block index <= 40
const size_t pooled_bytes = (size_t)(blocks + 2) * D * 4;
const Rope ropes[] = {{0.7f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}, {0.8409f, 0.25f, 1.0f, 20.0f, 1.0f, 1.1f}, {0.9f, 0.5f, 2.0f, 30.0f, 0.5f, 1.3f}};
QsaOperands q;
for (int v = 0; v < 2; ++v) for (int l = 0; l < 12; ++l) { q.tail[v][l].alloc(1536); q.dead[v][l].alloc(512); q.pooled[v][l].alloc(pooled_bytes); q.bpos[v][l].alloc(16); }
for (int l = 0; l < 12; ++l) q.gam[l].alloc(512);
q.raw.alloc((size_t)12 * T * 512); q.snap.alloc(12 * 1536); q.commit.alloc((T + 2) * 4);
int cases = 0;
for (int seed = 0; seed < 60; ++seed) {
const Rope r = ropes[seed % 3];
std::vector<float> hraw((size_t)12 * T * D), hsnap(12 * 384), hg(128);
for (auto& x : hraw) x = rndf() * 2.0f;
for (auto& x : hsnap) x = rndf();
q.raw.put(hraw.data(), hraw.size() * 4); q.snap.put(hsnap.data(), hsnap.size() * 4);
for (int l = 0; l < 12; ++l) { for (auto& x : hg) x = rndf() + 1.2f; q.gam[l].put(hg.data(), 512); }
// accepted positions: a run of consecutive cells from p0 (sometimes 0 or around a block edge), the rest -1
int32_t cm[T + 2] = {0, 0};
const int p0choices[] = {0, 1, 2, 3, 4, 5, 7, 8, 37, 100, 101, 102, 103};
const int p0 = p0choices[seed % 13];
const int keep = 1 + (int)(rnd() % T);
cm[0] = keep;
for (int row = 0; row < T; ++row) cm[2 + row] = row < keep ? p0 + row : -1;
if (seed % 7 == 6) cm[2 + 2] = -1; // a hole
q.commit.put(cm, sizeof cm);
std::vector<float> ts(12 * 384), ds(12 * 128), ps(12 * (pooled_bytes / 4));
for (auto& x : ts) x = rndf();
for (auto& x : ds) x = rndf();
for (auto& x : ps) x = rndf();
int32_t bp0[4] = {-1, 0, 0, 0};
for (int v = 0; v < 2; ++v) for (int l = 0; l < 12; ++l) {
q.tail[v][l].put(&ts[l * 384], 1536); q.dead[v][l].put(&ds[l * 128], 512); q.pooled[v][l].put(&ps[(size_t)l * (pooled_bytes / 4)], pooled_bytes); q.bpos[v][l].put(bp0, 16);
}
qsa_original(q, app, cpy, r);
qsa_fused(q, fused, r);
CU(cuStreamSynchronize(g_s));
for (int l = 0; l < 12; ++l) {
char what[96];
snprintf(what, sizeof what, "qsa seed %d layer %d tail", seed, l); same(what, q.tail[0][l].get(), q.tail[1][l].get());
snprintf(what, sizeof what, "qsa seed %d layer %d dead", seed, l); same(what, q.dead[0][l].get(), q.dead[1][l].get());
snprintf(what, sizeof what, "qsa seed %d layer %d pooled", seed, l); same(what, q.pooled[0][l].get(), q.pooled[1][l].get());
snprintf(what, sizeof what, "qsa seed %d layer %d block_pos", seed, l); same(what, q.bpos[0][l].get(), q.bpos[1][l].get());
}
++cases;
}
printf("RESULT qsa cases=%d\n", cases);
const Rope r = ropes[0];
double t_orig = time_graph([&] { qsa_original(q, app, cpy, r); });
double t_fused = time_graph([&] { qsa_fused(q, fused, r); });
printf("RESULT qsa_time original_us=%.1f fused_us=%.1f\n", t_orig, t_fused);
}
int main(int argc, char** argv) {
if (argc < 2) { fprintf(stderr, "usage: commit_probe PTXDIR\n"); return 2; }
std::string dir = argv[1];
CU(cuInit(0));
CUdevice dev; CU(cuDeviceGet(&dev, 0));
CUcontext ctx; CU(cuCtxCreate(&ctx, nullptr, 0, dev));
CU(cuStreamCreate(&g_s, CU_STREAM_NON_BLOCKING));
CUmodule mo = load_module(dir + "/original_verify.ptx"), mq = load_module(dir + "/original_verify_qsa.ptx"), md = load_module(dir + "/draft_commit.ptx"), mdq = load_module(dir + "/draft_commit_qsa.ptx");
test_gdn(mo, md);
test_qsa(mq, mo, mdq);
printf(g_fail ? "PROBE FAILED: %d mismatches\n" : "PROBE OK: every output byte identical\n", g_fail);
return g_fail ? 1 : 0;
}