Files
leleka/docs/chain-kernels.md
T

11 KiB

Chain kernels: the GPU chain of the decode verify window and of the MTP draft step (claude/chain-kernels)

Status: tables of section 2 are measured (ledger CUPTI traces, jobs 9062/9004). Sections 4-6 are filled as the model-free probe and A/B jobs (tools/chain-probe/, jobs 9c*) report; a number without a job name is not measured.

0. Summary

  • In the hit-heavy regime of real agent sessions the verify window is the GPU chain plus the host/PCIe waits. The port's chain is the Original's chain: kernel for kernel the same launch shapes and the same durations (12,561 us per 4-row window for both, section 2). Nothing in the window is slower than the Original's equivalent, and nothing is faster, so "the port must be faster than the Original" needs kernels that the Original does not have.
  • The chain is latency-bound, not bandwidth-bound: 2,418 kernels per 4-row window, 25 to 60 per layer, HBM busy about a quarter of the time. The biggest losses against the bandwidth floor are small-grid kernels with serial dependent loads: the hyper-connection read (3.4 ms, 27 % of the chain, about 4x its floor), the CPU-leg row copy and the plan copy from mapped host memory, the GDN alpha/beta gates, the QSA decode attention chunk, the hyper-connection up projection.
  • chain_window.cu holds bit-identical variants of those kernels (same operations, same order, loads issued ahead), plus a few fusions; all behind LELEKA_CHAIN (off by default).
  • Draft vocabulary: section 7.
  • Second pass on the hyper-connection read (section 8, claude/perf3): the down projection is re-tiled over rows and tokens, 16.8 -> 9.7 us per read at 4 rows (bit-identical, byte-compared against the Original's kernels), 29.3 -> 22.0 us for the whole read; the window-level A/B is queued (job p36).

1. Tools

tool what
tools/chain-probe/window_table.py per-role kernel table of the verify windows of two CUPTI traces (claude/ledger's tools/cupti-trace, scripts/profile/lct.py)
tools/chain-probe/chain_probe.cpp model-free probe: loads the sealed Original PTX and chain_window.ptx, runs both on real-size random inputs, compares every output byte, times both inside a CUDA graph with cold weights
tools/chain-probe/mb_pdl.cu graph kernel-to-kernel latency with and without programmatic dependent launch, and prefetch-ahead
tools/chain-probe/jobs/ holder jobs: 9c5 paired A/B (chain off / on), 9c6 CUPTI traces of both arms, 9c7 draft vocabulary on the CRM replay
scripts/chain/{build_rt,test_driver,seal_chain}.sh WSL builds, driver unit tests, compile+seal of chain_window.ptx with compile_assets.py / seal_pins.py

2. The window, kernel by kernel (port against Original, hit regime)

Traces: ledger job 9062, port-corpus-hit (rt-base, no draft vocabulary bound) and orig-corpus-hit (unmodified Original, --dump-routing profile of the corpus's own routing, static 2048 slots, ledger tracer). Windows of 4 rows (49 port / 115 Original windows) and 6 rows (6 / 22). "chain" = every kernel that is not a host or PCIe wait.

2.1 Four-row window

TABLE4

2.2 Six-row window

TABLE6

2.3 Gaps inside the graph

Every window is one stream and one graph; consecutive kernels start 64 to 128 ns after the previous one ends (98 % of 112,000 gaps in the 64-128 ns bin): 218 us per port window (1.7 % of the chain), 239 us for the Original. There are no event gaps (no cross-stream edges) in the window. The graph kernel gap is therefore not where the chain is lost; the loss is the 0.7 to 1.0 us a tiny kernel takes between its first and last thread, and the serial dependent loads of the small-grid kernels. The only exception in the Original's trace is an 0.9 us hole after the QSA K projection (native_mmvq_multi -> norm, 11 us per window) which the port does not have.

2.4 Ranking against the floor (4 rows, HBM 1.69 TB/s)

rank family port us/window floor us gap us what limits it
1 hyper-connection read: norm + staged down + up (96 calls, 13.1 MB each) 3,391 910 (7.7 us HBM + 1.5 norm per call) 2,480 41-block down projection 13 us (8 serial cp.async tiles), up 7.5 us (8 rows per warp loaded one behind the other)
2 routed experts gate/up + down (hits + staged) 2,434 950 (1.6 GB) ~1,500 grouped MMQ at 12.5 us per call; ramp and tail of many small groups; not touched here
3 dense projections (qkv, z, out, q, k, v, shared) 2,250 ~2,000 ~250 at the HBM floor already
4 CPU-leg rows copy + plan copy + doorbell + their waits' PCIe reads 1,040 ~250 ~790 serial volatile loads from mapped host memory: 5 and 6 PCIe round trips
5 QSA decode attention chunk 470 ~60 ~410 64 cells walked one dependent load at a time
6 GDN alpha/beta 368 ~100 ~270 12 blocks, 10 dependent weight/activation rounds
7 greedy argmax (248,320 logits, one block per token) 79 10 ~70 243 dependent loads per thread
8 ~25 tiny kernels per layer (quantize, swiglu, scale, sigmoid, ...) ~900 ~450 launch + ramp (0.7-1.0 us each)

Which families are slower than the Original's equivalents: none beyond noise; routed experts gate/up and down are +100 us each in the port because the 4-row set contains more groups (the port's hit regime differs by about 2 experts per layer between the two runs); doorbell is 142 us faster in the port. Extra kernels the port has: the split QSA scores/topk/resolve (qsa_decode_scores, qsa_topk_ref, leleka_qsa_resolve, leleka_qsa_miss_copy) replace the Original's block_scores_multi, block_topk_reg (+60 us), and the PLE history snapshot copies; no port-only guard kernels exist in the verify window (they are in the MTP step, section 5).

7. The MTP draft vocabulary (coordinator request)

DRAFTVOCAB

Agreement at long context and decode gain (jobs 9c5, 9d6)

  • Speed (9c5, paired by corpus prompt, 5 prompts x 3 repeats, short prompts, static tier): LELEKA_CHAIN=all 74.13 tok/s against 72.53 without it (+2.2 %); all 15 pairs are positive and the number of verify rounds is the same in both arms.
  • Agreement (9d6): the 40 requests of the live CRM session 9e3 teacher-forced along their recorded tokens (about 64k rows at 30-70k tokens of context), top-16 log-probabilities of every row, against the same run without the chain kernels (arm G of 9d4, same chunk cut, static tier). The kernels are NOT bit-identical at this length (the fusions change rounding): 5,993 of 64,355 rows are identical, d_top1 p99 0.327, max 4.31, dmax16 p99 3.48, argmax flips 697 (1.08 %). The control for rounding noise is a chunk-cut change of the exact path (job 9d5): p99 0.394, max 4.64, flips 1.45 %. The chain kernels deviate less than that, so they are within the rounding noise of any change in the order of the arithmetic (the rule of scripts/quality/q_tail_gate.py: each of p99, max and flip rate at most 1.25 x the control's). They are enabled by the rtx5090-32g profile (engine_env).

8. Hyper-connection read, second pass (claude/perf3, probe jobs run model-free on the 5090, A/B job p36 queued)

The 3.4 ms (96 reads of 33.8 us, old_chain) were split 2.5 + staged down 18.6 + up 8.9 us; the first chain pass (section 0) took the split to 2.2 and the up to 5.7 us and the down to 16.8 us (chain_gr_down_w8_nb3): 29.3 us for the whole read in the chain. The down projection is the long pole and it was a placement problem, not a bandwidth one.

Where the 16.9 us went (tools/chain-probe grdiag, T = 4, cold weights, one kernel at a time, graph timing): full 16.9; without the weight loads (constant weights) 13.0; without the activation staging and shared reads (constant activations) 11.5; neither 5.5 us. Both halves cost and even the bare instruction stream is 5.5 us, because the kernel ran 41 blocks of 8 warps (one weight row per warp, every warp walking all T tokens): 41 of 170 SMs, 2 warps per scheduler, each SM ingesting 320 KB (160 KB of weights, 160 KB of activations through cp.async).

Fix: tile the work over rows AND tokens (chain_gr_down_t2w8_nb4): a block is 8 rows x 2 tokens, grid (41, ceil(T/2)), the same whole-row weight preload, four tile buffers of two tokens (40 KB of shared memory at any T). Per (row, token) the arithmetic is the one of gr_down_staged_kernel (lane l accumulates chunks h*160 + l + 32q ascending with dot8, then the xor warp reduction), so every output is bit-identical: chain_probe gr compares every output byte (lo, rs, R, inject, mixed, xn) against the Original's split / staged / up kernels at every T from 1 to 8 with and without the injection (gr_down_exact: 16 comparisons of the tiled kernel plus 48 of the other variants, gr_exact: 12 of split / up8; 0 mismatches, model-free run p33-final3).

per call, cold weights (us) T = 4 T = 6 T = 8
Original staged down (old_staged) 18.4 23.3 30.6
first chain pass w8_nb3 16.8 20.0 - (w8_nb2 28.9)
tiled t2w8_nb4 9.2-9.7 11.3-12.0 15.0-15.3
whole read, Original (split + staged + up) 33.8 40.3 -
whole read, first chain pass (split + w8_nb3 + up8) 29.3 34.3 -
whole read, now (split + t2w8_nb4 + up8) 22.0 25.2 -

Per window (96 reads): 3.25 -> 2.11 ms of GR at 4 rows against the Original's kernels (-1.14 ms; 6 rows -1.45 ms), -0.70 ms against the first chain pass at 4 rows (-0.87 ms at 6 rows); of a ~12.6 ms window that is -5.6 % (synthetic, kernel level: the end-to-end number is job p36). LELEKA_CHAIN_DOWN=w8 selects the previous shape (the window graph records per request, so the A/B arms can live in one process: replay arm flag !w8); default on inside LELEKA_CHAIN's gr group. Sealed with the other chain kernels (chain_window.ptx re-sealed, 17 entries); the launch geometry is the pure function tiled_down_geometry (unit tested), and chain_probe launches with the same formulas.

Sweep (T = 4 / 6 / 8, us): rows x tokens per block: 8 x 2 with 3 / 4 / 5 / 6 tile buffers 10.1 / 9.74 / 9.68 / 9.85 (T = 4), 12.3 / 11.3 / 11.7 / 12.4 (T = 6); 8 x 1: 11.4 / 16.8 / 20.5; 8 x 3: 11.4-11.6 / 11.7-11.9 / 15.1; 8 x 4: 13.3 / 14.2 / 16.9; 4 rows x 2 tokens (164 blocks of 4 warps): 15.9 / 20.0 / 24.9 (the SM spread alone is not enough: the per-block pipeline needs the 8 warps' weight preload).

Rejected, measured: (1) R rows per warp (the activation tile read once for 2 or 4 rows, weights streamed tile by tile through registers): exact, but 23.7-28.8 us at T = 4 (slower than the 16.8 it was meant to beat: the just-in-time weight tiles expose the L2/HBM latency 8 times); (2) bulk L2 prefetch (cp.async.bulk.prefetch.L2) of the down and up weights from extra blocks of the split kernel: the split alone grew from 2.2 to 20.7 us (chain 35.6 us); (3) deeper tile buffers than 4 (no gain). What remains of the read at T = 4 (22 us in the chain against 17.6 us summed in isolation, floor ~9 us for the 13.1 MB at 1.69 TB/s) is the dependent launches (split -> down -> up, ~1 us each) and the cold first touch of the activations; fusing split into the previous kernel is the next step.