Files
leleka/tools/chain-probe/mb_pdl.cu
T
Pavlo Ratushnyi da8c974f68 Drop the stale engine/strata paths and retired generator names from comments, doc strings and generated fixtures (STRA-407)
Rust comments that named scripts/offline/*, gen_goldens.py, oracle/gen_*.{sh,cpp,py}, build_oracle.sh and similar retired generators now say where the data
came from (the Original's code, Strata 41cf05a0) or drop the sentence. The path/generator text of seven generated fixtures (sampler_oracle.json, sampling_keys.json,
memory_plan_golden.json, rope_golden.json, cvec_oracle.json, stra161 fixtures.json, commentary-tool-prompt.json) is reworded; no test reads those fields and no pin
covers their bytes. cmd_time/doc.rs: the two path texts in the format string; its lines are re-broken so they stay within 120 columns (the string is unchanged
otherwise). Comment-only except those data/string spots; no behaviour change.
2026-11-23 22:37:17 +01:00

207 lines
10 KiB
Plaintext

// claude/chain-kernels: model-free micro-benchmark of kernel-to-kernel latency inside a CUDA graph, with and without
// programmatic dependent launch (PDL, griddepcontrol), and of the L2-prefetch-before-wait trick for weight streaming.
// Prints "RESULT <name> key=value" lines. Never touches the model.
#include <cuda_runtime.h>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <algorithm>
#define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { fprintf(stderr, "CUDA error %s at %d: %s\n", #x, __LINE__, cudaGetErrorString(e_)); exit(2); } } while (0)
__device__ __forceinline__ void pdl_wait() { asm volatile("griddepcontrol.wait;" ::: "memory"); }
__device__ __forceinline__ void pdl_trigger() { asm volatile("griddepcontrol.launch_dependents;" ::: "memory"); }
// A tiny dependent kernel: x[i] = x[i] * 1.0001 + 1 on `n` floats. MODE 0 plain, 1 wait only, 2 trigger + wait.
template <int MODE>
__global__ void tiny(float* x, int n) {
if (MODE == 2) pdl_trigger();
if (MODE >= 1) pdl_wait();
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) x[i] = x[i] * 1.0001f + 1.0f;
}
// A weight-streaming matvec stand-in: y[r] = sum_c W[r][c] * x[c] (bf16 weights as uint4), one warp per row.
// MODE 0 plain; MODE 3 PDL with the first 4 weight chunks loaded into registers BEFORE griddepcontrol.wait;
// MODE 4 PDL, L2 prefetch of the warp's weight row before the wait, then the normal body.
template <int MODE>
__global__ void __launch_bounds__(256) mv(const uint4* __restrict__ W, const float* __restrict__ x, float* __restrict__ y, int cols8, int rows) {
if (MODE >= 3) pdl_trigger();
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int row = blockIdx.x * 8 + warp;
const uint4* w4 = W + (size_t)row * cols8;
if (MODE == 4 && row < rows) {
for (int j = lane * 8; j < cols8; j += 32 * 8) asm volatile("prefetch.global.L2 [%0];" ::"l"(w4 + j));
}
uint4 reg[4];
reg[0] = reg[1] = reg[2] = reg[3] = make_uint4(0, 0, 0, 0);
if (MODE == 3 && row < rows) {
#pragma unroll
for (int q = 0; q < 4; ++q) reg[q] = (lane + 32 * q < cols8) ? __ldg(w4 + lane + 32 * q) : make_uint4(0, 0, 0, 0);
}
if (MODE >= 3) pdl_wait();
if (row >= rows) return;
float acc = 0.f;
for (int j = lane, q = 0; j < cols8; j += 32, ++q) {
uint4 w;
if (MODE == 3 && q < 4) w = reg[q]; else w = __ldg(w4 + j);
const float* xx = x + j * 8;
const uint32_t v[4] = {w.x, w.y, w.z, w.w};
#pragma unroll
for (int k = 0; k < 4; ++k) {
acc = fmaf(__uint_as_float(v[k] << 16), xx[2 * k], acc);
acc = fmaf(__uint_as_float(v[k] & 0xffff0000u), xx[2 * k + 1], acc);
}
}
#pragma unroll
for (int o = 16; o > 0; o >>= 1) acc += __shfl_xor_sync(0xffffffffu, acc, o);
if (lane == 0) y[row] = acc;
}
static void launch_cfg(const void* fn, dim3 g, dim3 b, cudaStream_t s, bool pdl, void** args) {
cudaLaunchConfig_t cfg = {};
cfg.gridDim = g; cfg.blockDim = b; cfg.dynamicSmemBytes = 0; cfg.stream = s;
cudaLaunchAttribute at[1];
at[0].id = cudaLaunchAttributeProgrammaticStreamSerialization;
at[0].val.programmaticStreamSerializationAllowed = 1;
cfg.attrs = at; cfg.numAttrs = pdl ? 1 : 0;
CK(cudaLaunchKernelExC(&cfg, fn, args));
}
static double time_graph(cudaGraphExec_t ge, cudaStream_t s, int reps) {
for (int i = 0; i < 3; ++i) CK(cudaGraphLaunch(ge, s));
CK(cudaStreamSynchronize(s));
std::vector<double> v;
cudaEvent_t a, b; CK(cudaEventCreate(&a)); CK(cudaEventCreate(&b));
for (int i = 0; i < reps; ++i) {
CK(cudaEventRecord(a, s)); CK(cudaGraphLaunch(ge, s)); CK(cudaEventRecord(b, s)); CK(cudaEventSynchronize(b));
float ms; CK(cudaEventElapsedTime(&ms, a, b)); v.push_back(ms * 1000.0);
}
std::sort(v.begin(), v.end());
return v[v.size() / 2];
}
template <int MODE>
static double chain_tiny(int nk, int blocks, float* x, int n, cudaStream_t s) {
cudaGraph_t g; cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeGlobal));
for (int i = 0; i < nk; ++i) {
void* args[] = {&x, &n};
launch_cfg((const void*)tiny<MODE>, dim3(blocks), dim3(256), s, MODE != 0, args);
}
CK(cudaStreamEndCapture(s, &g));
CK(cudaGraphInstantiate(&ge, g, 0));
double us = time_graph(ge, s, 30);
CK(cudaGraphExecDestroy(ge)); CK(cudaGraphDestroy(g));
return us;
}
// chain of matvecs over `nm` distinct matrices (each rows x cols bf16) with a small dependent kernel between each pair
template <int MODE>
static double chain_mv(int nm, int rows, int cols, const uint4* W, float* x, float* y, float* z, int n, cudaStream_t s) {
cudaGraph_t g; cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeGlobal));
int cols8 = cols / 8;
for (int i = 0; i < nm; ++i) {
const uint4* w = W + (size_t)i * rows * cols8;
const float* xc = x;
void* a1[] = {&w, &xc, &y, &cols8, &rows};
launch_cfg((const void*)mv<MODE>, dim3((rows + 7) / 8), dim3(256), s, MODE >= 3, a1);
float* zz = z; int nn = n;
void* a2[] = {&zz, &nn};
if (MODE >= 3) launch_cfg((const void*)tiny<2>, dim3(10), dim3(256), s, true, a2);
else launch_cfg((const void*)tiny<0>, dim3(10), dim3(256), s, false, a2);
}
CK(cudaStreamEndCapture(s, &g));
CK(cudaGraphInstantiate(&ge, g, 0));
double us = time_graph(ge, s, 20);
CK(cudaGraphExecDestroy(ge)); CK(cudaGraphDestroy(g));
return us;
}
// ---- test D: the previous (latency-bound) kernel prefetches the next kernel's weights into L2 ("prefetch ahead", no PDL needed).
// lat: every CTA sleeps `us` microseconds (a stand-in for a latency-bound kernel such as the hyper-connection read) and, when PF, issues
// prefetch.global.L2 for its share of `pf_bytes` at `pf` BEFORE sleeping.
template <int PF>
__global__ void lat(float* x, int us, const char* pf, size_t pf_bytes) {
if (PF) {
const size_t total = (size_t)gridDim.x * blockDim.x;
const size_t lines = pf_bytes / 128;
for (size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; i < lines; i += total) asm volatile("prefetch.global.L2 [%0];" ::"l"(pf + i * 128));
}
for (int i = 0; i < us; ++i) __nanosleep(1000);
if (threadIdx.x == 0 && blockIdx.x == 0) x[0] += 1.0f;
}
template <int PF>
static double chain_lat_mv(int nm, int rows, int cols, const uint4* W, float* x, float* y, int us, int ctas, cudaStream_t s) {
cudaGraph_t g; cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeGlobal));
const int cols8 = cols / 8;
const size_t per = (size_t)rows * cols * 2;
for (int i = 0; i < nm; ++i) {
const uint4* w = W + (size_t)i * rows * cols8;
const char* pf = (const char*)w;
size_t pfb = per;
float* xx = x; int uu = us;
void* a0[] = {&xx, &uu, &pf, &pfb};
launch_cfg((const void*)lat<PF>, dim3(ctas), dim3(256), s, false, a0);
const float* xc = x; int cc = cols8, rr = rows;
void* a1[] = {&w, &xc, &y, &cc, &rr};
launch_cfg((const void*)mv<0>, dim3((rows + 7) / 8), dim3(256), s, false, a1);
}
CK(cudaStreamEndCapture(s, &g));
CK(cudaGraphInstantiate(&ge, g, 0));
double t = time_graph(ge, s, 20);
CK(cudaGraphExecDestroy(ge)); CK(cudaGraphDestroy(g));
return t;
}
int main() {
cudaDeviceProp p; CK(cudaGetDeviceProperties(&p, 0));
printf("# %s sm_%d%d L2 %d MiB\n", p.name, p.major, p.minor, p.l2CacheSize >> 20);
cudaStream_t s; CK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
float* x; CK(cudaMalloc(&x, 1 << 20)); CK(cudaMemset(x, 0, 1 << 20));
const int nk = 2000;
for (int blocks : {1, 10, 40, 160}) {
double a = chain_tiny<0>(nk, blocks, x, blocks * 256, s);
double b = chain_tiny<1>(nk, blocks, x, blocks * 256, s);
double c = chain_tiny<2>(nk, blocks, x, blocks * 256, s);
printf("RESULT tiny_chain blocks=%d plain_us_per_kernel=%.3f pdl_wait_us_per_kernel=%.3f pdl_trigger_wait_us_per_kernel=%.3f\n",
blocks, a / nk, b / nk, c / nk);
}
// weight streaming: matrices cycle through 1.5 GiB so every matrix is cold in L2
struct Shape { const char* name; int rows, cols; } shapes[] = {{"qkv_10240x2560", 10240, 2560}, {"gate_6144x2560", 6144, 2560}, {"out_2560x4096", 2560, 4096}, {"gr_320x10240", 320, 10240}};
for (auto& sh : shapes) {
size_t per = (size_t)sh.rows * sh.cols * 2;
int nm = (int)std::min<size_t>(40, (size_t)(1536ull << 20) / per);
uint4* W; CK(cudaMalloc(&W, per * nm)); CK(cudaMemset(W, 0x3c, per * nm));
float *xa, *ya, *za; CK(cudaMalloc(&xa, 4 * 16384)); CK(cudaMalloc(&ya, 4 * 16384)); CK(cudaMalloc(&za, 4 * 16384));
CK(cudaMemset(xa, 0, 4 * 16384)); CK(cudaMemset(ya, 0, 4 * 16384)); CK(cudaMemset(za, 0, 4 * 16384));
double a = chain_mv<0>(nm, sh.rows, sh.cols, W, xa, ya, za, 2560, s);
double b = chain_mv<3>(nm, sh.rows, sh.cols, W, xa, ya, za, 2560, s);
double c = chain_mv<4>(nm, sh.rows, sh.cols, W, xa, ya, za, 2560, s);
printf("RESULT mv_chain shape=%s mats=%d bytes_per_mat=%zu plain_us_per_pair=%.3f pdl_regpref_us_per_pair=%.3f pdl_l2pref_us_per_pair=%.3f floor_us=%.3f\n",
sh.name, nm, per, a / nm, b / nm, c / nm, per / 1.69e6);
CK(cudaFree(W)); CK(cudaFree(xa)); CK(cudaFree(ya)); CK(cudaFree(za));
}
// test D
for (auto& sh : shapes) {
size_t per = (size_t)sh.rows * sh.cols * 2;
int nm = (int)std::min<size_t>(40, (size_t)(1536ull << 20) / per);
uint4* W; CK(cudaMalloc(&W, per * nm)); CK(cudaMemset(W, 0x3c, per * nm));
float *xa, *ya; CK(cudaMalloc(&xa, 4 * 16384)); CK(cudaMalloc(&ya, 4 * 16384)); CK(cudaMemset(xa, 0, 4 * 16384));
for (int us : {0, 4, 8, 16, 32}) {
for (int ctas : {16, 160}) {
double a = chain_lat_mv<0>(nm, sh.rows, sh.cols, W, xa, ya, us, ctas, s);
double b = chain_lat_mv<1>(nm, sh.rows, sh.cols, W, xa, ya, us, ctas, s);
printf("RESULT prefetch_ahead shape=%s sleep_us=%d ctas=%d plain_us_per_pair=%.3f prefetched_us_per_pair=%.3f saved_us=%.3f\n", sh.name, us, ctas, a / nm, b / nm, (a - b) / nm);
}
}
CK(cudaFree(W)); CK(cudaFree(xa)); CK(cudaFree(ya));
}
return 0;
}