Files
leleka/scripts/e2e/codex/speed/verify-hc-down-kernel-microbench-vs-upstream/upstream_down_launch.inc
T
Pavlo RatushnyiandClaude Sonnet 5.5 aa2f237590 speed: kernel micro-benchmark of upstream gr_down_v3 (masks 0/5/133) against chain_gr_down_t2w8_nb4 (ledger U5)
Harness (one TU including upstream fused_gr.cu whole, ours from the sealed PTX through the driver API plus an nvcc cubin),
GPU queue stub, and the method note with input sha256. Not run on the GPU yet.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 23:26:26 +01:00

116 lines
8.1 KiB
C++

// Included by microbench.cu INSIDE namespace strata::kernels::microbench, after upstream's fused_gr.cu was included whole
// (so N, HC, D, LR, PR, THREADS, WARPS, GrMulti, gr_down_v3_kernel, gr_down_pf_dispatch, ... are upstream's own, unmodified).
//
// 1. mb_launch_upstream_down: the down-kernel selection of upstream's fused_gr_read_multi_slice (fused_gr.cu, the lines from
// `const int pfm = ...` to the `else gr_down_v3_kernel<1, kFusedGrMaxT, false>` ladder), copied in the same order, for S == 1
// (split == 1, the case on an sm_120 card), calling upstream's own static launch helpers and kernels.
// 2. mb_set_upstream_attributes: the cudaFuncSetAttribute calls of the same function (its split3 block), for the instantiations the
// plain ladder launches (the PF and RB helpers set their own attributes on first use, as upstream does).
// 3. mb_finalize_down: a TRANSCRIPTION (not upstream's compiled code) of the lines of gr_up_v3_kernel (S == 1, PF == 0 path) that turn
// the down kernel's `part` / `ssg` into rs, lo and inject_out, so the down stage's result can be compared with ours.
// Upstream's up kernel keeps lo in shared memory only, hence the transcription. The same lines run inside the real up kernel in the
// pipeline measurement (whose rs / inject_out / mixed outputs are compared with the real kernel's, not the transcription's).
static void mb_set_upstream_attributes() {
const int need1 = (int) (kFusedGrMaxT * N * sizeof(float)), need5 = (int) (5 * N * sizeof(float)), need6 = (int) (6 * N * sizeof(float));
const int need7 = (int) (7 * N * sizeof(float)), need8 = (int) (8 * N * sizeof(float));
const cudaFuncAttribute dyn = cudaFuncAttributeMaxDynamicSharedMemorySize, carve = cudaFuncAttributePreferredSharedMemoryCarveout;
cudaFuncSetAttribute(gr_down_v3_kernel<1, kFusedGrMaxT, false>, dyn, need1);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, dyn, need6);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true>, dyn, need5);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true, 2>, dyn, need5);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true, 2>, dyn, need6);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 1>, dyn, need7);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 2>, dyn, need7);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 7, true, 4>, dyn, need7);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 1>, dyn, need8);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 2>, dyn, need8);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 8, true, 4>, dyn, need8);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 1, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 2, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 3, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 4, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, 4, false>, carve, 100);
cudaFuncSetAttribute(gr_down_v3_kernel<1, kFusedGrMaxT, false>, carve, 100);
}
// grid / dynamic shared bytes the selection above gives for `mask` and `n_tok` rows (reported next to the timings)
struct MbLaunchShape { int blocks_x, blocks_y; size_t shared; const char* kernel; };
static MbLaunchShape mb_upstream_down_shape(int mask, int n_tok) {
const size_t sm = (size_t) n_tok * N * sizeof(float);
if ((mask & 128) != 0 && n_tok >= 5) return {LR / (WARPS * 2) + 1, HC, sm, "gr_down_v3_kernel<1,T,exact,R2V=2,PF=0,RB=true>"};
if ((mask & 3) != 0) return {LR / WARPS + 1, HC, sm, "gr_down_v3_kernel<1,T,exact,R2V=1,PF=mask&3>"};
if (n_tok >= 5) return {LR / (WARPS * 2) + 1, HC, sm, "gr_down_v3_kernel<1,T,exact,R2V=2,PF=0> (STRATA_GR_R2 default 2)"};
return {LR / WARPS + 1, HC, sm, "gr_down_v3_kernel<1,T,exact,R2V=1,PF=0>"};
}
static void mb_launch_upstream_down(int pfm, int n_tok, const GrMulti& m, float* part, float* ssg, cudaStream_t st) {
const size_t sm = (size_t) n_tok * N * sizeof(float);
if ((pfm & 128) != 0 && n_tok >= 5 && gr_down_rb_dispatch_k<2>(n_tok, m, part, ssg, st)) {}
else if ((pfm & 256) != 0 && gr_down_rb_dispatch_k<4>(n_tok, m, part, ssg, st)) {}
else if (((pfm >> 4) & 3) == 1 && gr_down_r2_dispatch_k<2>(n_tok, m, part, ssg, st)) {}
else if (((pfm >> 4) & 3) == 2 && gr_down_r2_dispatch_k<4>(n_tok, m, part, ssg, st)) {}
else if ((pfm & 3) != 0 && gr_down_pf_dispatch(pfm & 3, n_tok, m, part, ssg, st)) {}
else if (n_tok == 1) gr_down_v3_kernel<1, 1, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 2) gr_down_v3_kernel<1, 2, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 3) gr_down_v3_kernel<1, 3, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 4) gr_down_v3_kernel<1, 4, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 5 && gr_r2() == 2) gr_down_v3_kernel<1, 5, true, 2><<<dim3(LR / (WARPS * 2) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 5) gr_down_v3_kernel<1, 5, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 6 && gr_r2() == 2) gr_down_v3_kernel<1, 6, true, 2><<<dim3(LR / (WARPS * 2) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 6) gr_down_v3_kernel<1, 6, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 7 && gr_r2() == 2) gr_down_v3_kernel<1, 7, true, 2><<<dim3(LR / (WARPS * 2) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 7 && gr_r2() == 4) gr_down_v3_kernel<1, 7, true, 4><<<dim3(LR / (WARPS * 4) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 7 && gr_r2() == 1) gr_down_v3_kernel<1, 7, true, 1><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 8 && gr_r2() == 2) gr_down_v3_kernel<1, 8, true, 2><<<dim3(LR / (WARPS * 2) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 8 && gr_r2() == 4) gr_down_v3_kernel<1, 8, true, 4><<<dim3(LR / (WARPS * 4) + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else if (n_tok == 8 && gr_r2() == 1) gr_down_v3_kernel<1, 8, true, 1><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
else gr_down_v3_kernel<1, kFusedGrMaxT, false><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);
}
// part / ssg live in the read's scratch exactly as fused_gr_read_multi_slice lays them out
static float* mb_part(float* scratch) { return scratch; }
static float* mb_ssg(float* scratch, int n_tok) { return scratch + (size_t) n_tok * HC * 2 * PR; }
__global__ void __launch_bounds__(THREADS) mb_finalize_down(GrMulti m, const float* part, const float* ssg, float* lo_out, float* inject_out_out, float* rs_out) {
__shared__ float rsS[kFusedGrMaxT][HC];
const int t = threadIdx.x, T = m.T;
if (t < T * HC) { // gr_up_v3_kernel: the rms scale of (token, stream)
const int k = t / HC, c = t - k * HC;
float ss = 0.0f;
ss += ssg[t];
const float r = rsqrtf(ss / (float) N + m.a[k].eps);
rsS[k][c] = r;
rs_out[t] = r;
}
__syncthreads();
for (int i = t; i < T * LR; i += THREADS) {
const int k = i / LR, r = i - k * LR;
float sum = 0.0f;
#pragma unroll
for (int c = 0; c < HC; ++c) {
float p = 0.0f;
p += part[(((size_t) k * HC + c) * 1 + 0) * PR + r];
sum = fmaf(rsS[k][c], p, sum);
}
const float x = sum / (float) HC;
lo_out[(size_t) k * LR + r] = x / (1.0f + __expf(-x));
}
if (t < T * HC) {
const int k = t / HC, cc = t - k * HC;
float sum = 0.0f;
#pragma unroll
for (int c = 0; c < HC; ++c) {
float p = 0.0f;
p += part[(((size_t) k * HC + c) * 1 + 0) * PR + LR + cc];
sum = fmaf(rsS[k][c], p, sum);
}
inject_out_out[t] = sum;
}
}
// launch-shape floor: a kernel with the same grid / block / dynamic shared memory / by-value argument and an empty body
__global__ void __launch_bounds__(THREADS) mb_empty_upstream_shape(GrMulti m) { if (m.T < 0) m.a[0].lo[0] = 0.0f; }