Files
leleka/scripts/e2e/codex/speed/verify-hc-down-kernel-microbench-vs-upstream/microbench.cu
T
Pavlo RatushnyiandClaude Sonnet 5.5 0518311567 verify window: LELEKA_SM120_GR_DOWN_FUSED (hc split + tiled down projection as one launch, step U5a), off by default
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>
2026-11-10 19:14:46 +01:00

357 lines
22 KiB
Plaintext

// Kernel micro-benchmark: upstream Strata's hyper-connection down kernel (gr_down_v3_kernel, STRATA_GR_V3_PF masks 0 / 5 / 133) against
// our chain_gr_down_t2w8_nb4 (sealed PTX through the driver API, as the product loads it; and the same source built by nvcc), window
// rows T = 1..8, same random inputs. Method, inputs and how to read the result: docs/experiments/verify-hc-down-kernel-microbench-vs-upstream.md
//
// One translation unit: upstream's fused_gr.cu is #included whole (unmodified, from git object strata-upstream/main), so the kernels, the
// geometry constants and the launch helpers are upstream's own.
#include <cuda.h>
#include "fused_gr.cu"
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iterator>
#include <string>
#include <vector>
namespace strata::kernels {
// defined in upstream's verify_kernels.cu (timestamp kernel for its profile windows); fused_gr.cu only calls it when a stamp buffer is passed, which this harness never does
void gpu_stamp(unsigned long long*, int, void*) {}
namespace microbench {
#define CU_RT(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { std::fprintf(stderr, "%s:%d %s: %s\n", __FILE__, __LINE__, #x, cudaGetErrorString(e_)); std::exit(2); } } while (0)
#define CU_DRV(x) do { CUresult r_ = (x); if (r_ != CUDA_SUCCESS) { const char* m_ = "?"; cuGetErrorString(r_, &m_); std::fprintf(stderr, "%s:%d %s: %s\n", __FILE__, __LINE__, #x, m_); std::exit(2); } } while (0)
#include "upstream_down_launch.inc"
constexpr int kTokensMax = kFusedGrMaxT; // 8
constexpr int kOursWords = 114; // GrMulti of the sealed chain kernels: 8 x 14 words of FusedGrArgs (112 B), xn, T = 912 bytes
struct OursByValue { uint64_t w[kOursWords]; };
static_assert(sizeof(OursByValue) == 912, "GrMulti of the sealed chain kernels");
__global__ void __launch_bounds__(THREADS) mb_empty_ours_shape(OursByValue m) { if (m.w[113] == ~0ull) *reinterpret_cast<float*>(m.w[10]) = 0.0f; }
__global__ void mb_spin_ns(unsigned long long ns) {
unsigned long long t0, t;
asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t0));
do { asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t)); } while (t - t0 < ns);
}
static uint16_t bf16_bits(float x) { uint32_t u; std::memcpy(&u, &x, 4); return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16); }
struct WeightSet { uint16_t *down, *up, *inject; float* norm; };
struct Work { float *R, *lo, *rs, *inject_out, *mixed, *scratch; };
struct Inputs { float *bo, *inj; std::vector<float> R0; };
static float* dev_floats(size_t n) { float* p; CU_RT(cudaMalloc((void**) &p, n * 4)); CU_RT(cudaMemset(p, 0xff, n * 4)); return p; }
static Work new_work(const Inputs& in) {
Work w{dev_floats((size_t) kTokensMax * D), dev_floats((size_t) kTokensMax * LR), dev_floats((size_t) kTokensMax * HC), dev_floats((size_t) kTokensMax * HC),
dev_floats((size_t) kTokensMax * N), dev_floats((size_t) kTokensMax * D * 4)};
CU_RT(cudaMemcpy(w.R, in.R0.data(), in.R0.size() * 4, cudaMemcpyHostToDevice));
CU_RT(cudaDeviceSynchronize()); // see reset_work: the memsets of dev_floats are on the default stream
return w;
}
static void reset_work(const Work& w, const Inputs& in) {
CU_RT(cudaMemcpy(w.R, in.R0.data(), in.R0.size() * 4, cudaMemcpyHostToDevice));
CU_RT(cudaMemset(w.lo, 0xff, kTokensMax * LR * 4)); CU_RT(cudaMemset(w.rs, 0xff, kTokensMax * HC * 4));
CU_RT(cudaMemset(w.inject_out, 0xff, kTokensMax * HC * 4)); CU_RT(cudaMemset(w.mixed, 0xff, (size_t) kTokensMax * N * 4));
CU_RT(cudaMemset(w.scratch, 0xff, (size_t) kTokensMax * D * 4 * 4));
// The copies and memsets above run on the legacy default stream and the kernels on `s`, a non-blocking stream: the two are NOT ordered by the
// runtime, so a poison memset could land after (or beside) the kernel that was meant to overwrite it. That race, not the input values, made the
// first run's outputs NaN (0xffffffff). Wait for the poison before any launch.
CU_RT(cudaDeviceSynchronize());
}
static FusedGrArgs upstream_args(int t, const Work& w, const WeightSet& ws, const Inputs& in) {
FusedGrArgs a;
a.R = w.R + (size_t) t * D; a.R_out = w.R + (size_t) t * D; a.apply = true;
a.bo_prev = in.bo + (size_t) t * N; a.inj_prev = in.inj + (size_t) t * HC;
a.w_norm = ws.norm; a.w_down = ws.down; a.w_up = ws.up; a.w_inject = ws.inject;
a.lo = w.lo + (size_t) t * LR; a.rs = w.rs + (size_t) t * HC; a.inject_out = w.inject_out + (size_t) t * HC; a.mixed = w.mixed + (size_t) t * N;
return a;
}
static GrMulti upstream_multi(int T, const Work& w, const WeightSet& ws, const Inputs& in) {
GrMulti m{};
for (int t = 0; t < T; ++t) m.a[t] = upstream_args(t, w, ws, in);
m.xn = w.scratch; m.T = T;
return m;
}
// the 14 words per row of `multi_words` (crates/cuda-driver/src/original_verify_window_gr/assets.rs), then xn and T
static OursByValue ours_multi(int T, const Work& w, const WeightSet& ws, const Inputs& in) {
OursByValue o{};
auto p = [](const void* x) { return (uint64_t) (uintptr_t) x; };
const float eps = 1e-6f; uint32_t eps_bits; std::memcpy(&eps_bits, &eps, 4);
for (int t = 0; t < T; ++t) {
uint64_t* a = &o.w[t * 14];
a[0] = p(w.R + (size_t) t * D); a[1] = a[0]; a[2] = 1; a[3] = p(in.bo + (size_t) t * N); a[4] = p(in.inj + (size_t) t * HC);
a[5] = p(ws.norm); a[6] = p(ws.down); a[7] = p(ws.up); a[8] = p(ws.inject); a[9] = eps_bits;
a[10] = p(w.lo + (size_t) t * LR); a[11] = p(w.rs + (size_t) t * HC); a[12] = p(w.inject_out + (size_t) t * HC); a[13] = p(w.mixed + (size_t) t * N);
}
o.w[112] = p(w.scratch); o.w[113] = (uint64_t) T;
return o;
}
struct OursKernels { CUmodule mod = nullptr; CUfunction split = nullptr, down = nullptr, up8 = nullptr; std::string label; };
static bool load_ours(const std::string& path, bool is_ptx, const char* label, OursKernels& k) {
std::ifstream f(path, std::ios::binary);
if (!f) { std::fprintf(stderr, "cannot read %s\n", path.c_str()); return false; }
std::vector<char> image((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
if (is_ptx) { while (!image.empty() && image.back() == '\0') image.pop_back(); image.push_back('\0'); } // the product appends one NUL to the PTX text
CU_DRV(cuModuleLoadData(&k.mod, image.data()));
CU_DRV(cuModuleGetFunction(&k.split, k.mod, "chain_gr_split"));
CU_DRV(cuModuleGetFunction(&k.down, k.mod, "chain_gr_down_t2w8_nb4"));
CU_DRV(cuModuleGetFunction(&k.up8, k.mod, "chain_gr_up8"));
k.label = label;
return true;
}
static void ours_launch(CUfunction fn, OursByValue m, dim3 grid, unsigned shared, cudaStream_t s) {
void* params[1] = {&m};
CU_DRV(cuLaunchKernel(fn, grid.x, grid.y, grid.z, THREADS, 1, 1, shared, (CUstream) s, params, nullptr));
}
static dim3 ours_split_grid(int T) { return dim3(T, HC); }
static dim3 ours_down_grid(int T) { return dim3(LR / 8 + 1, (T + 1) / 2); } // original_verify_window_gr::tiled_down_geometry: 41 blocks of 8 rows, grid.y = ceil(T / 2)
static unsigned ours_down_shared(int T) { return 4u * (unsigned) std::min(T, 2) * 1280u * 4u; }
static dim3 ours_up8_grid() { return dim3((N / 16) * 2); } // UP_BLOCKS * 2 = 320 blocks of 256 threads
// ------------------------------------------------------------------------------------------------ variants
struct Variant { std::string name; std::function<void(int, int, cudaStream_t)> run; }; // (T, weight set, stream)
struct Context {
std::vector<WeightSet> sets; Inputs in;
Work w_down_u, w_down_o, w_down_c, w_pipe_u, w_pipe_o; // separate output buffers per measured variant family
OursKernels ptx, cubin;
};
static std::vector<Variant> down_variants(Context& c, bool with_cubin) {
std::vector<Variant> v;
const int masks[] = {0, 5, 133};
const char* names[] = {"upstream_mask0_plain", "upstream_mask5_batched_staging_loads", "upstream_mask133_register_blocked_dot"};
for (int i = 0; i < 3; ++i) {
const int mask = masks[i];
v.push_back({names[i], [&c, mask](int T, int set, cudaStream_t s) {
const GrMulti m = upstream_multi(T, c.w_down_u, c.sets[set], c.in);
mb_launch_upstream_down(mask, T, m, mb_part(c.w_down_u.scratch), mb_ssg(c.w_down_u.scratch, T), s);
}});
}
v.push_back({"ours_down_t2w8_nb4_sealed_ptx", [&c](int T, int set, cudaStream_t s) {
ours_launch(c.ptx.down, ours_multi(T, c.w_down_o, c.sets[set], c.in), ours_down_grid(T), ours_down_shared(T), s);
}});
if (with_cubin) v.push_back({"ours_down_t2w8_nb4_nvcc_cubin", [&c](int T, int set, cudaStream_t s) {
ours_launch(c.cubin.down, ours_multi(T, c.w_down_c, c.sets[set], c.in), ours_down_grid(T), ours_down_shared(T), s);
}});
v.push_back({"ours_split_plus_down_sealed_ptx", [&c](int T, int set, cudaStream_t s) {
const OursByValue m = ours_multi(T, c.w_down_o, c.sets[set], c.in);
ours_launch(c.ptx.split, m, ours_split_grid(T), 0, s);
ours_launch(c.ptx.down, m, ours_down_grid(T), ours_down_shared(T), s);
}});
v.push_back({"empty_kernel_with_upstream_mask133_launch_shape", [&c](int T, int, cudaStream_t s) {
const MbLaunchShape sh = mb_upstream_down_shape(133, T);
mb_empty_upstream_shape<<<dim3(sh.blocks_x, sh.blocks_y), THREADS, sh.shared, s>>>(upstream_multi(T, c.w_down_u, c.sets[0], c.in));
}});
v.push_back({"empty_kernel_with_our_launch_shape", [&c](int T, int, cudaStream_t s) {
mb_empty_ours_shape<<<ours_down_grid(T), THREADS, ours_down_shared(T), s>>>(ours_multi(T, c.w_down_o, c.sets[0], c.in));
}});
return v;
}
static std::vector<Variant> pipeline_variants(Context& c) {
std::vector<Variant> v;
for (int mask : {0, 5, 133}) {
const std::string name = "pipeline_upstream_mask" + std::to_string(mask) + "_down_plus_up";
v.push_back({name, [&c, mask](int T, int set, cudaStream_t s) {
FusedGrArgs a[kTokensMax];
for (int t = 0; t < T; ++t) a[t] = upstream_args(t, c.w_pipe_u, c.sets[set], c.in);
fused_gr_v3_pf(mask);
fused_gr_read_multi(a, T, c.w_pipe_u.scratch, (void*) s); // upstream's own entry point: STRATA_GR_V3=1 -> gr_down_v3 + gr_up_v3
}});
}
v.push_back({"pipeline_ours_split_down_up8_sealed_ptx", [&c](int T, int set, cudaStream_t s) {
const OursByValue m = ours_multi(T, c.w_pipe_o, c.sets[set], c.in);
ours_launch(c.ptx.split, m, ours_split_grid(T), 0, s);
ours_launch(c.ptx.down, m, ours_down_grid(T), ours_down_shared(T), s);
ours_launch(c.ptx.up8, m, ours_up8_grid(), 0, s);
}});
return v;
}
// ------------------------------------------------------------------------------------------------ statistics
struct Stat { double median, p10, p90, mn, mean, round_med_lo, round_med_hi; size_t n; };
static double pick(const std::vector<double>& s, double p) { return s[(size_t) std::floor(p * (double) (s.size() - 1) + 0.5)]; }
static Stat summarize(const std::vector<std::vector<double>>& rounds) {
std::vector<double> all, meds;
for (auto r : rounds) { std::sort(r.begin(), r.end()); meds.push_back(pick(r, 0.5)); all.insert(all.end(), r.begin(), r.end()); }
std::sort(all.begin(), all.end()); std::sort(meds.begin(), meds.end());
double sum = 0; for (double x : all) sum += x;
return {pick(all, 0.5), pick(all, 0.1), pick(all, 0.9), all.front(), sum / (double) all.size(), meds.front(), meds.back(), all.size()};
}
static void print_stat(const char* mode, int T, const std::string& name, const Stat& s) {
std::printf("TIME %-26s T=%d %-52s median %8.3f p10 %8.3f p90 %8.3f min %8.3f mean %8.3f round-medians %8.3f..%8.3f n=%zu\n", mode, T, name.c_str(), s.median, s.p10, s.p90, s.mn,
s.mean, s.round_med_lo, s.round_med_hi, s.n);
std::printf("CSV,%s,%d,%s,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%zu\n", mode, T, name.c_str(), s.median, s.p10, s.p90, s.mn, s.mean, s.round_med_lo, s.round_med_hi, s.n);
}
struct Options { int tmin = 1, tmax = 8, rounds = 7, replays = 40, nodes = 64, sets = 64, single = 120; std::string ptx, cubin; };
// graph-batched: `nodes` launches (cycling over the weight sets when cold) captured into one CUDA graph; per-launch time = graph time / nodes
static void time_graphs(const Options& o, Context& c, const std::vector<Variant>& vars, int T, bool cold, cudaStream_t s) {
const char* mode = cold ? "graph_cold_weights" : "graph_warm_weights";
std::vector<cudaGraphExec_t> exec(vars.size());
for (size_t vi = 0; vi < vars.size(); ++vi) {
for (int i = 0; i < 2; ++i) vars[vi].run(T, 0, s); // first-use attribute setup and module state outside the capture
CU_RT(cudaStreamSynchronize(s));
cudaGraph_t g;
CU_RT(cudaStreamBeginCapture(s, cudaStreamCaptureModeRelaxed));
for (int i = 0; i < o.nodes; ++i) vars[vi].run(T, cold ? i % (int) c.sets.size() : 0, s);
CU_RT(cudaStreamEndCapture(s, &g));
CU_RT(cudaGraphInstantiate(&exec[vi], g, 0));
CU_RT(cudaGraphDestroy(g));
}
std::vector<cudaEvent_t> e0(o.replays), e1(o.replays);
for (int i = 0; i < o.replays; ++i) { CU_RT(cudaEventCreate(&e0[i])); CU_RT(cudaEventCreate(&e1[i])); }
std::vector<std::vector<std::vector<double>>> data(vars.size());
for (int r = 0; r < o.rounds; ++r)
for (size_t k = 0; k < vars.size(); ++k) {
const size_t vi = (k + (size_t) r) % vars.size(); // rotate the start so no variant always follows the same neighbour
for (int i = 0; i < 3; ++i) CU_RT(cudaGraphLaunch(exec[vi], s));
for (int i = 0; i < o.replays; ++i) { CU_RT(cudaEventRecord(e0[i], s)); CU_RT(cudaGraphLaunch(exec[vi], s)); CU_RT(cudaEventRecord(e1[i], s)); }
CU_RT(cudaStreamSynchronize(s));
std::vector<double> us;
for (int i = 0; i < o.replays; ++i) { float ms; CU_RT(cudaEventElapsedTime(&ms, e0[i], e1[i])); us.push_back(1000.0 * ms / o.nodes); }
data[vi].push_back(us);
}
for (size_t vi = 0; vi < vars.size(); ++vi) { print_stat(mode, T, vars[vi].name, summarize(data[vi])); CU_RT(cudaGraphExecDestroy(exec[vi])); }
for (int i = 0; i < o.replays; ++i) { cudaEventDestroy(e0[i]); cudaEventDestroy(e1[i]); }
}
// single launch: a 50 us spin kernel keeps the stream ahead of the host, then event, launch(es), event; weights rotate over the sets
static void time_single(const Options& o, Context& c, const std::vector<Variant>& vars, int T, cudaStream_t s) {
std::vector<cudaEvent_t> e0(o.single), e1(o.single);
for (int i = 0; i < o.single; ++i) { CU_RT(cudaEventCreate(&e0[i])); CU_RT(cudaEventCreate(&e1[i])); }
std::vector<std::vector<std::vector<double>>> data(vars.size());
int counter = 0;
for (int r = 0; r < o.rounds; ++r)
for (size_t k = 0; k < vars.size(); ++k) {
const size_t vi = (k + (size_t) r) % vars.size();
for (int i = 0; i < o.single; ++i) {
mb_spin_ns<<<1, 1, 0, s>>>(50000ull);
CU_RT(cudaEventRecord(e0[i], s));
vars[vi].run(T, counter++ % (int) c.sets.size(), s);
CU_RT(cudaEventRecord(e1[i], s));
}
CU_RT(cudaStreamSynchronize(s));
std::vector<double> us;
for (int i = 0; i < o.single; ++i) { float ms; CU_RT(cudaEventElapsedTime(&ms, e0[i], e1[i])); us.push_back(1000.0 * ms); }
data[vi].push_back(us);
}
for (size_t vi = 0; vi < vars.size(); ++vi) print_stat("single_launch_cold_weights", T, vars[vi].name, summarize(data[vi]));
for (int i = 0; i < o.single; ++i) { cudaEventDestroy(e0[i]); cudaEventDestroy(e1[i]); }
}
#include "compare_outputs.inc"
static int run(int argc, char** argv) {
Options o;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
auto next = [&]() { return i + 1 < argc ? std::atoi(argv[++i]) : 0; };
if (a == "--ptx" && i + 1 < argc) o.ptx = argv[++i];
else if (a == "--cubin" && i + 1 < argc) o.cubin = argv[++i];
else if (a == "--rounds") o.rounds = next();
else if (a == "--replays") o.replays = next();
else if (a == "--nodes") o.nodes = next();
else if (a == "--sets") o.sets = next();
else if (a == "--single") o.single = next();
else if (a == "--tmin") o.tmin = next();
else if (a == "--tmax") o.tmax = next();
else { std::fprintf(stderr, "unknown argument %s\n", a.c_str()); return 1; }
}
if (o.ptx.empty()) { std::fprintf(stderr, "usage: microbench --ptx chain_window.ptx [--cubin chain_window.cubin] [--rounds R --replays N --nodes B --sets S --single N --tmin 1 --tmax 8]\n"); return 1; }
setenv("STRATA_GR_V3", "1", 1); // upstream's own entry point (fused_gr_read_multi) takes the v3 read, as on an sm_120 card by default
setvbuf(stdout, nullptr, _IOLBF, 0);
CU_RT(cudaFree(0));
int dev = 0, rtv = 0, drv = 0;
CU_RT(cudaGetDevice(&dev)); cudaRuntimeGetVersion(&rtv); cudaDriverGetVersion(&drv);
cudaDeviceProp prop; CU_RT(cudaGetDeviceProperties(&prop, dev));
std::printf("device %d %s cc %d.%d SMs %d L2 %d MiB runtime %d driver %d\n", dev, prop.name, prop.major, prop.minor, prop.multiProcessorCount, prop.l2CacheSize >> 20, rtv, drv);
std::printf("config rounds %d replays %d nodes %d sets %d single %d T %d..%d N %d HC %d LR %d D %d\n", o.rounds, o.replays, o.nodes, o.sets, o.single, o.tmin, o.tmax, N, HC, LR, D);
std::printf("upstream default mask on cc 12.0 is 133 (include/strata/core/bw_dense_defaults.hpp gr_v3_pf_default); gr_r2() = %d (STRATA_GR_R2 default)\n", gr_r2());
Context c;
std::mt19937 rng(7); // the seed and the distributions of upstream's tools gr_v3_screen.cpp
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<float> bo((size_t) kTokensMax * N), inj((size_t) kTokensMax * HC), wn(D);
c.in.R0.resize((size_t) kTokensMax * D);
for (auto& x : c.in.R0) x = nd(rng);
for (auto& x : bo) x = 0.5f * nd(rng);
for (auto& x : inj) x = nd(rng);
for (auto& x : wn) x = 1.0f + 0.3f * nd(rng);
std::vector<uint16_t> wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D);
for (auto& x : wd) x = bf16_bits(0.02f * nd(rng));
for (auto& x : wu) x = bf16_bits(0.05f * nd(rng));
for (auto& x : wi) x = bf16_bits(0.02f * nd(rng));
// the inputs are finite by construction (normal distributions, weights rounded to bf16); say so in the log, so a NaN in an output can only be a bug
{
size_t bad = 0;
for (const auto* v : {&bo, &inj, &wn, &c.in.R0}) for (float x : *v) bad += !std::isfinite(x);
for (const auto* v : {&wd, &wu, &wi}) for (uint16_t x : *v) bad += ((x & 0x7f80u) == 0x7f80u);
std::printf("inputs: %zu non-finite values (must be 0)\n", bad);
if (bad != 0) return 2;
}
CU_RT(cudaMalloc((void**) &c.in.bo, bo.size() * 4)); CU_RT(cudaMemcpy(c.in.bo, bo.data(), bo.size() * 4, cudaMemcpyHostToDevice));
CU_RT(cudaMalloc((void**) &c.in.inj, inj.size() * 4)); CU_RT(cudaMemcpy(c.in.inj, inj.data(), inj.size() * 4, cudaMemcpyHostToDevice));
for (int i = 0; i < o.sets; ++i) {
WeightSet w;
CU_RT(cudaMalloc((void**) &w.down, wd.size() * 2)); CU_RT(cudaMemcpy(w.down, wd.data(), wd.size() * 2, cudaMemcpyHostToDevice));
CU_RT(cudaMalloc((void**) &w.up, wu.size() * 2)); CU_RT(cudaMemcpy(w.up, wu.data(), wu.size() * 2, cudaMemcpyHostToDevice));
CU_RT(cudaMalloc((void**) &w.inject, wi.size() * 2)); CU_RT(cudaMemcpy(w.inject, wi.data(), wi.size() * 2, cudaMemcpyHostToDevice));
CU_RT(cudaMalloc((void**) &w.norm, wn.size() * 4)); CU_RT(cudaMemcpy(w.norm, wn.data(), wn.size() * 4, cudaMemcpyHostToDevice));
c.sets.push_back(w);
}
c.w_down_u = new_work(c.in); c.w_down_o = new_work(c.in); c.w_down_c = new_work(c.in); c.w_pipe_u = new_work(c.in); c.w_pipe_o = new_work(c.in);
CU_RT(cudaFuncSetAttribute(mb_empty_upstream_shape, cudaFuncAttributeMaxDynamicSharedMemorySize, 8 * N * (int) sizeof(float)));
mb_set_upstream_attributes();
CU_DRV(cuInit(0));
if (!load_ours(o.ptx, true, "sealed PTX via cuModuleLoadData", c.ptx)) return 1;
const bool have_cubin = !o.cubin.empty() && load_ours(o.cubin, false, "nvcc cubin", c.cubin);
cudaStream_t s; CU_RT(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
CU_RT(cudaDeviceSynchronize());
const int failures = compare_outputs(o, c, have_cubin, s);
// the activations of the ours-down-only timing: what the split kernel writes for a full window (the down kernel reads xn and nothing else)
for (Work* w : {&c.w_down_o, &c.w_down_c}) {
ours_launch(c.ptx.split, ours_multi(kTokensMax, *w, c.sets[0], c.in), ours_split_grid(kTokensMax), 0, s);
CU_RT(cudaStreamSynchronize(s));
}
const std::vector<Variant> down = down_variants(c, have_cubin), pipe = pipeline_variants(c);
{ // clocks up before the first measurement: about two seconds of the real kernels, all window sizes
const auto t0 = std::chrono::steady_clock::now();
while (std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count() < 2.0) {
for (int i = 0; i < 200; ++i) { down[3].run(1 + i % kTokensMax, i % (int) c.sets.size(), s); down[0].run(1 + i % kTokensMax, i % (int) c.sets.size(), s); }
CU_RT(cudaStreamSynchronize(s));
}
}
for (int T = o.tmin; T <= o.tmax; ++T) {
const MbLaunchShape u133 = mb_upstream_down_shape(133, T), u5 = mb_upstream_down_shape(5, T), u0 = mb_upstream_down_shape(0, T);
std::printf("SHAPE T=%d upstream mask0: grid (%d,%d) block %d dyn %zu B %s | mask5: grid (%d,%d) dyn %zu B %s | mask133: grid (%d,%d) dyn %zu B %s | ours down: grid (%d,%d) block %d dyn %u B\n",
T, u0.blocks_x, u0.blocks_y, THREADS, u0.shared, u0.kernel, u5.blocks_x, u5.blocks_y, u5.shared, u5.kernel, u133.blocks_x, u133.blocks_y, u133.shared, u133.kernel,
ours_down_grid(T).x, ours_down_grid(T).y, THREADS, ours_down_shared(T));
time_graphs(o, c, down, T, true, s);
time_graphs(o, c, down, T, false, s);
time_graphs(o, c, pipe, T, true, s);
time_single(o, c, down, T, s);
}
std::printf("microbench done, correctness failures: %d\n", failures);
return failures ? 3 : 0;
}
} // namespace microbench
} // namespace strata::kernels
int main(int argc, char** argv) { return strata::kernels::microbench::run(argc, argv); }