New sealed sm_120 asset sm120_gr_down_fused.ptx (nvcc 13.2.86): leleka_gr_down_fused_t2w8 replaces chain_gr_split + chain_gr_down_t2w8_nb4 (96 launches fewer per verify graph). The block stages R' * w_norm of its two tokens into shared memory, adds the sum of squares in the split kernel's order, scales with rs and runs the down kernel's dots; the operations and their order are those of the two kernels (bitwise by construction; upstream's gr_down_v3 sums in another order). A no-op, said once, without the chain gr pair. Planner, switch resolution (Sm120Switches, GrConfig.down_fused), unit tests, model-free probe tools/gr-down-fused (not run: no GPU). Not measured. Microbench harness: the NaN outputs were a stream race between the poison memsets and the kernels, fixed; inputs are checked finite. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
200 lines
13 KiB
C++
200 lines
13 KiB
C++
// claude/u5a-gr-down-fused: model-free probe of crates/cuda-driver/src/sm120_gr_down_fused.ptx. NOT RUN by its author (no GPU access in that task): the orchestrator runs it.
|
|
//
|
|
// The fused kernel `leleka_gr_down_fused_t2w8` is run next to the two kernels it replaces (`chain_gr_split` + `chain_gr_down_t2w8_nb4` of chain_window.ptx, both
|
|
// launched exactly as the product launches them) on real-size random FINITE inputs, for every window width T = 1..8, with and without the folded previous
|
|
// half (apply), on three input scales (the third has a large dynamic range, so a different order of summation or of rounding would show), each followed by the
|
|
// unchanged `chain_gr_up8`. EVERY byte of lo, rs, inject_out, mixed and R (updated in place by the up kernel) is compared, and the fused launch must leave `xn`
|
|
// untouched (it no longer writes it). Prints "RESULT name key=value" lines and exits 1 if any comparison differs or any compared output is not finite.
|
|
// gr_down_fused_probe PTXDIR PTXDIR holds chain_window.ptx and sm120_gr_down_fused.ptx (copies of crates/cuda-driver/src/)
|
|
// With the exact check passed, it times (CUDA events over graphs of 64 launches, the weights rotating over 12 sets of 13 MB so that none is in L2 at its next
|
|
// use) the pair against the fused launch, per T: `RESULT time T=.. pair_us=.. fused_us=..` (per split + down pair and per fused launch, launch gaps inside the graph included; the layer-half time
|
|
// of the whole read is the thing to look at in CUPTI, not this).
|
|
#include <cuda.h>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <cmath>
|
|
#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 uint16_t bf16_of(float f) { uint32_t u; memcpy(&u, &f, 4); return (uint16_t)((u + 0x7fff + ((u >> 16) & 1)) >> 16); }
|
|
|
|
static std::string read_file(const std::string& p) {
|
|
FILE* f = fopen(p.c_str(), "rb");
|
|
if (!f) return std::string();
|
|
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);
|
|
if (s.empty()) { fprintf(stderr, "cannot read %s\n", p.c_str()); exit(2); }
|
|
while (!s.empty() && s.back() == '\0') s.pop_back();
|
|
s.push_back('\0'); // the product appends one NUL to the PTX text
|
|
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, 0xff, bytes)); } // 0xff: a word nobody wrote is a NaN
|
|
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(cuMemcpyDtoH(v.data(), p, n)); return v; }
|
|
};
|
|
static CUstream g_s;
|
|
static void launch(CUfunction f, unsigned gx, unsigned gy, unsigned bx, unsigned shared, void** params) {
|
|
CU(cuLaunchKernel(f, gx, gy, 1, bx, 1, 1, shared, g_s, params, nullptr));
|
|
}
|
|
static bool all_finite(const std::vector<uint8_t>& b) {
|
|
for (size_t i = 0; i + 4 <= b.size(); i += 4) { float f; memcpy(&f, &b[i], 4); if (!std::isfinite(f)) return false; }
|
|
return true;
|
|
}
|
|
static bool same(const char* what, int t, int apply, int scale, const std::vector<uint8_t>& a, const std::vector<uint8_t>& b) {
|
|
bool eq = a.size() == b.size() && memcmp(a.data(), b.data(), a.size()) == 0;
|
|
bool fin = all_finite(a) && all_finite(b);
|
|
size_t diff = 0;
|
|
for (size_t i = 0; i < std::min(a.size(), b.size()); ++i) diff += a[i] != b[i];
|
|
if (!eq || !fin) { printf("DIFF %s T=%d apply=%d scale=%d bytes=%zu differing_bytes=%zu finite=%d\n", what, t, apply, scale, a.size(), diff, (int)fin); ++g_fail; }
|
|
return eq && fin;
|
|
}
|
|
|
|
struct State { Dev R, bo, inj, injo, lo, rs, mixed, xn; int T; };
|
|
struct Weights { Dev norm, down, up, inject; };
|
|
static void alloc_state(State& s, int T) {
|
|
s.T = T; s.R.alloc((size_t)T * 10240 * 4); s.bo.alloc((size_t)T * 2560 * 4); s.inj.alloc((size_t)T * 16); s.injo.alloc((size_t)T * 16); s.lo.alloc((size_t)T * 320 * 4);
|
|
s.rs.alloc((size_t)T * 16); s.mixed.alloc((size_t)T * 2560 * 4); s.xn.alloc((size_t)T * 10240 * 4);
|
|
}
|
|
// the 114 words of GrMulti (original_verify_window_gr/assets.rs `multi_words`)
|
|
static std::vector<uint64_t> multi_words(const State& s, const Weights& w, bool apply) {
|
|
std::vector<uint64_t> words(114, 0);
|
|
for (int k = 0; k < s.T; ++k) {
|
|
uint64_t* a = &words[k * 14];
|
|
a[0] = s.R.p + (uint64_t)k * 10240 * 4; a[1] = a[0]; a[2] = apply ? 1 : 0;
|
|
a[3] = s.bo.p + (uint64_t)k * 2560 * 4; a[4] = s.inj.p + (uint64_t)k * 16; a[5] = w.norm.p; a[6] = w.down.p; a[7] = w.up.p; a[8] = w.inject.p;
|
|
float eps = 1e-6f; uint32_t eb; memcpy(&eb, &eps, 4); a[9] = eb;
|
|
a[10] = s.lo.p + (uint64_t)k * 320 * 4; a[11] = s.rs.p + (uint64_t)k * 16; a[12] = s.injo.p + (uint64_t)k * 16; a[13] = s.mixed.p + (uint64_t)k * 2560 * 4;
|
|
}
|
|
words[112] = s.xn.p; words[113] = s.T;
|
|
return words;
|
|
}
|
|
static void reset(State& dst, const State& base) {
|
|
CU(cuMemcpyDtoD(dst.R.p, base.R.p, base.R.n)); CU(cuMemcpyDtoD(dst.bo.p, base.bo.p, base.bo.n)); CU(cuMemcpyDtoD(dst.inj.p, base.inj.p, base.inj.n));
|
|
for (Dev* d : {&dst.injo, &dst.lo, &dst.rs, &dst.mixed, &dst.xn}) CU(cuMemsetD8(d->p, 0xff, d->n));
|
|
CU(cuCtxSynchronize());
|
|
}
|
|
|
|
struct Kernels { CUfunction split, down, up8, fused; };
|
|
static unsigned down_shared(int T) { return 4u * (unsigned)std::min(T, 2) * 5120u; }
|
|
static unsigned fused_shared(int T) { return (unsigned)std::min(T, 2) * 40960u; }
|
|
static void run_pair(const Kernels& k, int T, void** p) {
|
|
launch(k.split, T, 4, 256, 0, p);
|
|
launch(k.down, 41, (T + 1) / 2, 256, down_shared(T), p);
|
|
}
|
|
static void run_fused(const Kernels& k, int T, void** p) { launch(k.fused, 41, (T + 1) / 2, 256, fused_shared(T), p); }
|
|
|
|
static double time_graph(const std::function<void(int)>& body, int nodes) {
|
|
CUgraph g; CUgraphExec ge; CU(cuStreamBeginCapture(g_s, CU_STREAM_CAPTURE_MODE_THREAD_LOCAL));
|
|
for (int i = 0; i < nodes; ++i) body(i);
|
|
CU(cuStreamEndCapture(g_s, &g)); CU(cuGraphInstantiateWithFlags(&ge, g, 0));
|
|
CUevent e0, e1; CU(cuEventCreate(&e0, 0)); CU(cuEventCreate(&e1, 0));
|
|
std::vector<double> t;
|
|
for (int rep = 0; rep < 12; ++rep) {
|
|
CU(cuGraphLaunch(ge, g_s)); CU(cuEventRecord(e0, g_s)); CU(cuGraphLaunch(ge, g_s)); CU(cuGraphLaunch(ge, g_s)); CU(cuEventRecord(e1, g_s)); CU(cuStreamSynchronize(g_s));
|
|
float ms; CU(cuEventElapsedTime(&ms, e0, e1)); if (rep >= 2) t.push_back(1000.0 * ms / (2.0 * nodes));
|
|
}
|
|
std::sort(t.begin(), t.end());
|
|
CU(cuGraphExecDestroy(ge)); CU(cuGraphDestroy(g));
|
|
return t[t.size() / 2];
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) { fprintf(stderr, "usage: gr_down_fused_probe PTXDIR\n"); 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));
|
|
char name[256]; cuDeviceGetName(name, sizeof name, dev);
|
|
int maj, min; cuDeviceGetAttribute(&maj, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, dev); cuDeviceGetAttribute(&min, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, dev);
|
|
printf("# %s sm_%d%d\n", name, maj, min);
|
|
CU(cuStreamCreate(&g_s, CU_STREAM_NON_BLOCKING));
|
|
CUmodule mChain = load_module(dir + "/chain_window.ptx"), mFused = load_module(dir + "/sm120_gr_down_fused.ptx");
|
|
Kernels k{get_fn(mChain, "chain_gr_split"), get_fn(mChain, "chain_gr_down_t2w8_nb4"), get_fn(mChain, "chain_gr_up8"), get_fn(mFused, "leleka_gr_down_fused_t2w8")};
|
|
CU(cuFuncSetAttribute(k.fused, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, 81920));
|
|
CU(cuFuncSetAttribute(k.down, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, 81920));
|
|
|
|
const int NS = 12;
|
|
std::vector<Weights> W(NS);
|
|
{
|
|
std::vector<float> nh(10240); for (auto& v : nh) v = 0.5f + fabsf(rndf());
|
|
std::vector<uint16_t> dh((size_t)320 * 10240), uh((size_t)10240 * 320), ih((size_t)4 * 10240);
|
|
for (auto& v : dh) v = bf16_of(rndf() * 0.02f);
|
|
for (auto& v : uh) v = bf16_of(rndf() * 0.2f);
|
|
for (auto& v : ih) v = bf16_of(rndf() * 0.02f);
|
|
for (int s = 0; s < NS; ++s) {
|
|
W[s].norm.alloc(10240 * 4); W[s].down.alloc(dh.size() * 2); W[s].up.alloc(uh.size() * 2); W[s].inject.alloc(ih.size() * 2);
|
|
W[s].norm.put(nh.data(), 10240 * 4); W[s].down.put(dh.data(), dh.size() * 2); W[s].up.put(uh.data(), uh.size() * 2); W[s].inject.put(ih.data(), ih.size() * 2);
|
|
}
|
|
}
|
|
// scale 0: unit; 1: small activations; 2: a large dynamic range (every 7th element x 1e3, every 11th x 1e-3)
|
|
const float scales[3] = {3.0f, 1e-2f, 3.0f};
|
|
for (int T = 1; T <= 8; ++T) {
|
|
for (int apply = 0; apply < 2; ++apply) {
|
|
for (int sc = 0; sc < 3; ++sc) {
|
|
State base, a, b; alloc_state(base, T); alloc_state(a, T); alloc_state(b, T);
|
|
std::vector<float> rh((size_t)T * 10240), bh((size_t)T * 2560), ih((size_t)T * 4);
|
|
for (size_t i = 0; i < rh.size(); ++i) { rh[i] = rndf() * scales[sc]; if (sc == 2) { if (i % 7 == 0) rh[i] *= 1e3f; if (i % 11 == 0) rh[i] *= 1e-3f; } }
|
|
for (auto& v : bh) v = rndf() * (sc == 1 ? 1e-2f : 1.0f);
|
|
for (auto& v : ih) v = rndf() * 2;
|
|
base.R.put(rh.data(), rh.size() * 4); base.bo.put(bh.data(), bh.size() * 4); base.inj.put(ih.data(), ih.size() * 4);
|
|
reset(a, base); reset(b, base);
|
|
auto wa = multi_words(a, W[0], apply), wb = multi_words(b, W[0], apply);
|
|
void* pa[] = {wa.data()}; void* pb[] = {wb.data()};
|
|
run_pair(k, T, pa); launch(k.up8, 320, 1, 256, 0, pa);
|
|
run_fused(k, T, pb); launch(k.up8, 320, 1, 256, 0, pb);
|
|
CU(cuStreamSynchronize(g_s));
|
|
bool ok = same("lo", T, apply, sc, a.lo.get(), b.lo.get());
|
|
ok &= same("rs", T, apply, sc, a.rs.get(), b.rs.get());
|
|
ok &= same("inject_out", T, apply, sc, a.injo.get(), b.injo.get());
|
|
ok &= same("mixed", T, apply, sc, a.mixed.get(), b.mixed.get());
|
|
ok &= same("R", T, apply, sc, a.R.get(), b.R.get());
|
|
// the fused launch writes no xn: it must still hold the poison, every byte
|
|
const std::vector<uint8_t> xn = b.xn.get();
|
|
bool xn_untouched = std::all_of(xn.begin(), xn.end(), [](uint8_t c) { return c == 0xff; });
|
|
if (!xn_untouched) { printf("DIFF xn written by the fused launch T=%d apply=%d scale=%d\n", T, apply, sc); ++g_fail; }
|
|
printf("RESULT exact T=%d apply=%d scale=%d ok=%d\n", T, apply, sc, (int)(ok && xn_untouched));
|
|
for (Dev* d : {&base.R, &base.bo, &base.inj, &base.injo, &base.lo, &base.rs, &base.mixed, &base.xn, &a.R, &a.bo, &a.inj, &a.injo, &a.lo, &a.rs, &a.mixed, &a.xn,
|
|
&b.R, &b.bo, &b.inj, &b.injo, &b.lo, &b.rs, &b.mixed, &b.xn}) cuMemFree(d->p);
|
|
}
|
|
}
|
|
}
|
|
if (g_fail == 0) {
|
|
for (int T = 1; T <= 8; ++T) {
|
|
State s; alloc_state(s, T);
|
|
std::vector<float> rh((size_t)T * 10240); for (auto& v : rh) v = rndf() * 3; s.R.put(rh.data(), rh.size() * 4);
|
|
std::vector<std::vector<uint64_t>> ws(NS);
|
|
for (int i = 0; i < NS; ++i) ws[i] = multi_words(s, W[i], false);
|
|
double pair = time_graph([&](int i) { void* p[] = {ws[i % NS].data()}; run_pair(k, T, p); }, 64); // a node is the two launches
|
|
double fused = time_graph([&](int i) { void* p[] = {ws[i % NS].data()}; run_fused(k, T, p); }, 64);
|
|
printf("RESULT time T=%d pair_us=%.3f fused_us=%.3f\n", T, pair, fused);
|
|
}
|
|
}
|
|
printf("RESULT summary failures=%d\n", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|