Merge branch 'claude/verify-gdn-fused-kernels' into claude/speed-vs-strata-08143a

# Conflicts:
#	crates/cuda-driver/ptx-sm86/MANIFEST.json
#	crates/cuda-driver/src/original_verify.rs
#	crates/cuda-driver/src/original_verify_window_sm120.rs
#	scripts/gpu-bringup/extra-seal-plan.json
This commit is contained in:
Pavlo Ratushnyi
2026-11-09 23:39:52 +01:00
19 changed files with 12342 additions and 42 deletions
@@ -0,0 +1,182 @@
# Verify window, GDN layers: one launch instead of three, and the unrolled a/b projection
Branch `claude/verify-gdn-fused-kernels` (base `91a130da6`, branch `claude/speed-vs-strata-08143a`). Ledger rows
`docs/experiments/step-ledger.md`: K7 (GDN role in total, +0.155 ms per round against vanilla), K7a (GDN step/norm in
the verify graph, +0.029 ms, 36 against 30.9 launches), U3 (upstream `STRATA_SMALL_UNR` bit 1, the unrolled GDN a/b
projection). Nothing here was measured on a GPU: this note states what the code does, how it was checked without one,
and what the orchestrator should look at.
## What a GDN layer records today
`record_window` calls `gdn_conv`, `gdn_alpha_beta`, `gdn_recurrence` for each of the 36 GDN layers
(`original_verify_window_record.rs`, `pre_gdn_front` / `pre_gdn_back`). Each one is one kernel of `original_verify.ptx`:
| call | kernel | grid x block |
| --- | --- | --- |
| `gdn_conv` | `verify_gdn_conv_l2_multi_kernel` | 80 x T, 128 |
| `gdn_alpha_beta` | `verify_gdn_ab_multi_kernel`, or `chain_gdn_ab_multi_kernel` with `LELEKA_CHAIN_AB` (`LELEKA_CHAIN=all`, the 5090 profile) | 12 x 256, or 96 x 32 |
| `gdn_recurrence` | `verify_gdn_step_norm_multi_kernel` | 48, 128 x 4 |
That is 108 launches per verify graph. Vanilla also runs conv, ab and step as three kernels, so the gap K7 is launch count
and the conv (grid 80 x T of 128 threads for a 10240 x 4 tap convolution, one launch of its own).
## `LELEKA_SM120_FUSED_GDN` (default off)
One new kernel, `leleka_verify_gdn_fused_step` (`crates/cuda-driver/src/sm120_gdn_fused.cu`), 48 blocks of 128 x 4,
one block per value head: the Original's step loop with its loads redirected to shared memory, preceded by:
* **conv prologue.** The block convolves the q chunk (`head % 16`), the k chunk (`16 + head % 16`) and its own v chunk
(`32 + head`), 128 channels each, for the new tokens `[t_begin, t_end)`. The 3 x m tasks are dealt to the four 128-thread
groups (`threadIdx.y`); every group runs the same number of rounds and one barrier per round, an idle task computes zeros.
q, k and v go to shared memory (`FusedShared`, 18.6 KB). The blocks of heads < 16 write q and k to `h`, every block writes
its v chunk to `h`: the commit graph (`draft_commit.cu`, `verify_gdn_step_norm_multi_kernel` with `n_keep`) and a later
group of a split window read `h`, so each of the 80 chunks of a row still has exactly one writer. The q/k conv of a head
pair is computed by the three heads that share it (the redundancy of this design; only the heads < 16 store it).
* **gates.** With `ab_x != 0` warp 0 and warp 1 of the block compute alpha row `head` and beta row `head` for the new
tokens (the ten weight chunks are loaded first, so they are in flight during the conv; the dot products then run per
lane in the Original's order) and write `gate` / `beta` to global (the commit reads them) and to shared memory. With
`ab_x == 0` they are read (see SIDE_MIXER below).
* **replayed tokens.** Tokens before `t_begin` (the earlier group of a split window) are read from `h` / `gate` / `beta`
into shared memory, as the Original reads them. Their state update is done; their o dot product and output path never
feed the state and are not computed (as `draft_commit.cu`).
* **step loop.** `s = fma(g, s, k * delta)` etc. over shared `q`, `k`, `v`, `gate`, `beta`. Barriers per token: one for
`kv` (double buffered by token parity, so a replayed token has only this one), and for an output token one for `o` and
one for the squared-sum partials. The Original had six per token.
The state is not written (the window passes `n_keep = 0`), as before.
### Dispatch (`original_verify_window_ops/gdn_stages.rs`, `GdnFusedPolicy::stages`)
| switches | `gdn_conv` | `gdn_alpha_beta` | `gdn_recurrence` | GDN launches per layer |
| --- | --- | --- | --- | --- |
| off | conv | ab | step | 3 |
| `FUSED_GDN` | nothing | nothing | fused (gates in kernel) | 1 |
| `SIDE_MIXER` | conv | nothing (side stream 0) | step | 3 (one on the side stream) |
| `FUSED_GDN` + `SIDE_MIXER` | nothing | nothing (side stream 0) | fused (gates read, after the side joins) | 2 (one on the side stream) |
So 108 launches per verify graph become 36 (72 on the main stream with SIDE_MIXER). Only the conv is fused with
SIDE_MIXER, as designed: the gates stay on side stream 0 and the fused launch comes after both joins. The conv no longer
overlaps the side-stream gates in that combination; that is a cost to read off the CUPTI window, not a measured one.
Split windows (`groups == 2`) fuse with the gates in the kernel.
## `LELEKA_SM120_GDN_AB_UNROLL` (default off, independent)
Upstream commit `08b20b104` (`gdn_ab_body`, `STRATA_SMALL_UNR` bit 1): the same a/b projection with the loop over the ten
32-lane passes (`j = lane + 32 jj`, all below 320) and over the tokens at constant trip counts, so the loads of several
passes can be issued before the first FMA chain. Its own entries in the same asset: `leleka_gdn_ab_unrolled_t1..t6`
(exact token count) and `_t8` (T = 7 and 8, run time count, upstream's `kVerifyNarrowT` split). It replaces the a/b kernel
wherever that runs as a kernel of its own (no `FUSED_GDN`, and the side-stream gates).
Geometry: one warp per row, 96 blocks of 32 threads, the shape of `chain_gdn_ab_multi_kernel` that `LELEKA_CHAIN=all`
records, so the unrolled loop is the only difference to the shipped kernel. Upstream's own shape (48 blocks of 64
threads, two rows a block) runs the same entries (`row = blockIdx.x * (blockDim.x / 32) + warp`); the probe covers both.
Expectation to keep honest: the shipped chain kernel already hoists the ten weight loads (K7b: 8.5 us, ahead of
vanilla's 10.5 us), so the unroll may add little over it; with `FUSED_GDN` the gates are inside the fused launch and this
switch only affects the side-stream branch.
## Bitwise argument
The target is "identical to the three kernels", after the driver's JIT, which loads `cuModuleLoadData` with default
options (`original_verify_ffi.rs`).
1. **Same expressions, same PTX shape, not intrinsics.** The brief suggested `__fmul_rn` / `__fsub_rn` to forbid
contraction. That is wrong for these kernels. ptxas contracts a plain `mul.f32` that feeds an `add.f32` / `sub.f32`
into one FFMA (checked on `rnr5090` with ptxas 13.2.86 `-arch=sm_120`: a two-instruction kernel with `mul.f32` +
`sub.f32` assembles to a single FFMA, the `.rn` forms to FMUL + FADD; `--fmad=false` makes the plain form behave like
`.rn`). The sealed `original_verify.ptx` has a plain `mul.f32`, `sub.f32`, `mul.f32` for `delta = beta * (v - g * kv)`,
so the Original *executes* `beta * fma(-g, kv, v)`. An intrinsic version would not. The kernel therefore uses the
same plain C++ expressions as `original_verify.cu` (and explicit `fmaf` where the Original has `fmaf`), and a unit test
compares the PTX.
2. **PTX comparison (CPU, unit tests `original_verify_window_gdn_fused::tests`):** the numeric instruction sequence of the
Original's conv kernel (26 instructions: `mul`, 3 `fma`, silu by `div.rn`, `y * y`, five butterfly adds, barrier, the
four-partial sum, `rsqrt.approx`, `mul`) is a contiguous run of the fused kernel; the delta (`add add add mul sub mul`)
and the whole output tail (`rsqrt.approx` of 128 at run time, `ss / 128 + eps` as one `fma.rn`, `rcp.rn` for the
sigmoid) are runs of both; the set of numeric opcodes of the fused kernel equals the union of the three Originals', with
no `.rn`-suffixed `add` / `mul` / `sub`, no `.ftz`, no approximate division; each unrolled entry has the Original a/b
kernel's opcode set. The ten `log1pf` polynomial constants are the same in 13.0.88 (`original_verify.ptx`) and 13.2.86
(`chain_window.ptx`, this asset).
3. **Arithmetic order.** Conv: `((v1*w1 -> fma v0 -> fma v2 -> fma x)`, silu `div.rn`, L2 partials by xor butterfly over
a warp then `((p0+p1)+p2)+p3`. Gates: per-lane chunk-ascending fma chains, xor butterfly, the Original's gate/beta
formulas. Step: `kv` fma chain from 0, `((r0+r1)+r2)+r3`, `s = fma(g, s, k*delta)`, `o = fma(s, q, o)` in `r` order,
`oc = sum * rsqrt(128)`, `scale = rsqrt(ss/128 + eps)`, `y = oc * scale * gamma * rcp(1 + exp(-z))`.
4. **Model-free probe, not run by this task (no GPU):** `tools/gdn-fused/gdn_fused_probe.cpp` runs the fused launch next
to the three Original kernels for T = 1..8, every split of the window into two groups, gates in the kernel and joined,
three seeds, and compares every byte of `h`, `gate`, `beta`, `y`, state and history; it also compares every unrolled entry
(both geometries) with the Original and the chain a/b kernel. Build: `tools/gdn-fused/build.sh` (nvcc 13.2.2), run
`gdn_fused_probe OUTDIR/ptx`. It compiles (`nvcc -c`, checked on the server) and has not been executed.
## Asset and sealing
* Source `crates/cuda-driver/src/sm120_gdn_fused.cu`, PTX `sm120_gdn_fused.ptx` beside it, sm_120 only
(`ptx-sm86/MANIFEST.json`: "not built for sm_86 by design"; the switches take effect on a 12.x card only).
* Built on `rnr5090` (compile only, no GPU) with `~/cuda-toolkits/13.2.2/bin/nvcc --ptx -O3 -arch=sm_120 -std=c++17
--fmad=true --ftz=false` (13.2.86, the recipe of `original_verify.cu` / `draft_commit.cu`). sha256 `.cu`
`f46a3c59dd2e203d1ba1b997f5e2f5aeaa40185d72468bce2ceb12d1ecbdb95f`, `.ptx`
`9da33e8217c1e4daa2d26f9739874978e9c206eb5f30f9417f5df8b526e37f17`.
* Pinned in `GDN_FUSED.sha` (`original_verify/original_verify_window_gdn_fused.rs`); plan entry in
`scripts/gpu-bringup/extra-seal-plan.json` (group `sm120-decode`). The pin was filled by hand from the digest (the
`seal_pins.py` flow needs a compile manifest); `assert_sealed_and_fail_closed` checks pin, structure and the refusals.
* Fails closed: a requested switch whose asset is not sealed refuses the recording with `Original verifier asset
unsealed: sm120_gdn_fused.ptx (...)` (`gdn_fused::prepare`), instead of silently running the default path under the name
of the switch (the `ATTN_DAILY` precedent falls back silently, which would mislabel a paired A/B arm).
* `ptxas -v` (sm_120): fused step 64 registers, 18640 bytes shared, 20 bytes of spill; unrolled `t1..t6, t8`: 39, 48, 48,
52, 50, 56, 44 registers, no spill.
* Server path created (orchestrator may move it to garbage):
`/data/leleka-e2e/spd-gap/ptx-build/verify-gdn-fused-kernels/` (the `.cu`, `.ptx`, test PTX of the contraction check,
cubins, `ptxas-v.txt`, probe object).
## Where the code is
| file | change |
| --- | --- |
| `crates/cuda-driver/src/sm120_gdn_fused.cu`, `.ptx` | new asset |
| `crates/cuda-driver/src/original_verify/original_verify_window_gdn_fused.rs` (+ `/tests.rs`) | switches resolution, asset, launch DTOs, stage plan (`GdnFusedPolicy::stages`), 18 tests |
| `crates/cuda-driver/src/original_verify/original_verify_gdn_journal/verify_gdn_fused_step.rs` | `capture_fused_recurrence`, `capture_ab_unrolled` (operand spans as `capture_layer_stage`) |
| `crates/cuda-driver/src/original_verify_window_ops/gdn_stages.rs` | the three `WindowOps` GDN calls (moved out of `window_ops_kernels_and_pcie_plan.rs`) |
| `original_verify_window_sm120.rs` | `fused_gdn`, `gdn_ab_unroll` in `Sm120Switches`, `DEFAULT_ON_*` (separate block) |
| `original_verify.rs`, `original_verify_gdn_journal.rs`, `original_verify_window_ops.rs`, `original_verifier.rs` | one `mod` line each, the `gdn` field of the ops struct and its resolution |
| `verify_gdn_journal_stage_and_commit.rs` | `StageOperands` / `stage_operands` `pub(super)` |
| `ptx-sm86/MANIFEST.json`, `scripts/gpu-bringup/extra-seal-plan.json` | not-built-for-sm_86 note, plan entry |
| `tools/gdn-fused/` | probe and build script |
## Tests and gates (no GPU)
* `test -p leleka-cuda-driver --features cuda-driver --lib original_verify`: 179 passed (161 on the base commit + 18 new),
11 ignored (GPU). Whole crate (`--features cuda-driver`): lib 397 passed, 78 ignored, the integration tests as before.
* New tests: default off; each variable turns on its own switch and nothing else, 12.0 and 12.1 take it, below 12 never;
asset sealed and exports every entry; launches per layer with the switch (3 -> 1; with the side branch 3 -> 2; 108 ->
36 per graph); the unrolled gates replace only the a/b kernel; the argument words follow `FusedGdnArgs` of the kernel
source (parsed from the `.cu`); the fused launch writes `h`, `gate`, `beta`, `y` and carries no activation row when
joined; row range refusals; unrolled entry by token count; the unrolled launch takes the Original's arguments; the four
PTX comparisons above.
* `python3 scripts/check-architecture.py`: 0 errors (the new files use `dir/name.rs` paths, so the `path-include`
ratchet does not rise). `scripts/check-limits.py --base claude/speed-vs-strata-08143a`: 0 errors.
## For the orchestrator
Identity (teacher-forced replay, top-16 rows + draft trace): arm A default, arm B `LELEKA_SM120_FUSED_GDN=1`, arm C
`LELEKA_SM120_GDN_AB_UNROLL=1`, arm D `FUSED_GDN=1` + `LELEKA_SM120_SIDE_MIXER=1` (the joined path), each with the
shipped `LELEKA_CHAIN=all`. Only the env switch is needed. Look for a split window in the replay (`groups == 2`) and for
T = 1..8; the probe covers them without a model.
CUPTI window, kernel names:
* before: `verify_gdn_conv_l2_multi_kernel`, `chain_gdn_ab_multi_kernel` (or `verify_gdn_ab_multi_kernel`),
`verify_gdn_step_norm_multi_kernel` (36 each per verify graph; the last one also appears in the unfused commit graph);
* after `FUSED_GDN`: `leleka_verify_gdn_fused_step`, 36 per verify graph, the three above absent from the window graph;
launches per verify graph (V8) 2061 -> 1989 by count (-72);
* after `GDN_AB_UNROLL`: `leleka_gdn_ab_unrolled_t<T>` in place of the a/b kernel.
The section delta is K7 minus the commit part: sum of the three kernels per graph (conv, ab, step) against the fused
kernel. Risks to read off the window: the fused kernel runs 48 blocks only, so its prologue is serial per block (conv of
three chunks, then the gates, then the loop), the q/k conv is done three times, and with SIDE_MIXER the conv no longer
overlaps the gates. No gain is claimed.
## Identity risks left
* ptxas contraction decisions follow the PTX shape; the shapes were compared, but SASS was not (no `nvdisasm` in the server
toolkit). The probe is the proof.
* The barrier hazards of the double buffered `red_kv`, `l2` and the single `red_o` / `wsum` were argued on paper (each
buffer is re-written only after a barrier all readers have passed) and cannot be seen by a unit test.
* `log1pf` and the other libdevice expansions come from nvcc 13.2.86 here and 13.0.88 in `original_verify.ptx`; the
constants of `log1pf` are equal, the rest of the sequences are covered by the opcode-set test.