Files
leleka/docs/experiments/draft-tail-5090.md
T

90 lines
11 KiB
Markdown

# 5090 decode at 247k: the per-round host budget of the MTP draft and the tail, and what was removed (claude/draft-tail, STRA-245)
Date 2026-10-29. Branch `claude/draft-tail` from `claude/speed-train` (47d2d389). Evidence: the DBG profile lines of the paired `speedtrain-ab` runs (`/data/leleka-e2e/runs/speedtrain-ab/speedtrain-noseed-r3` and `-r6`: ours,
seed OFF, upstream ranked profile, PLE fix, frozen corpus, 5 turns x ~245k, harness spans on), the sources of ours and of vanilla (`engine/strata`, pin 41cf05a0). Nothing below the "Changes" table was measured
on a GPU after the changes: the GPU was busy with the queue, and the A/B and identity jobs are queued (section 4).
## 1. Per-round budget, ours (measured), ms per round
Mean of the two seed-off runs. The engine spans add up to 99.4 % of the wall time of a round (tokens per round divided by the client's decode tok/s): 15.18 ms of spans against 15.27 ms (turn 5).
The harness runs ours with `LELEKA_SERVE_PROFILE=1` and the bring-up traces (both ours arms of every A/B alike); vanilla runs untraced.
| component | turn 5 (247k) | turns 1-5 | where it runs |
| --- | ---: | ---: | --- |
| window (`round.run_window`) | 13.066 | 13.288 | |
| PLE gather on the host (`window.ple`): archive read 0.260, device scatter/decode 0.119 | 0.390 | 0.369 | host, before the window launches, GPU idle |
| penalty-history setup + `begin_window` + post-check (`sampling_setup`) | 0.103 | 0.061 | host; 0.021 ms at 45k, 0.112 at 247k: O(context) |
| verifier: GPU wait + head 8.582, CPU-expert callbacks 3.905 | 12.543 | 12.836 | the 1.8 ms "verify gap" of STRA-258 lives here (kernels, placement; not this branch) |
| `apply_pending` (wait for the adaptive swaps' PCIe copies) | 0.223 | 0.232 | host waits, GPU idle; the copy tail of every 4th round |
| commit enqueue | 0.005 | 0.006 | |
| `verify_commit` outside any span (checkpoints, `validate_current`, bookkeeping) | 0.094 | 0.065 | host; 0.038 at 45k, 0.098 at 247k: O(context) |
| `draft_committed` | 1.720 | 1.711 | |
| `draft.launch` (round graph record, staging, enqueue) | 0.032 | 0.035 | host |
| `adapt_deferred` (plan and queue the swaps) | 0.081 | 0.085 | host, in the GPU's shadow |
| `settle_commit`: `commit.wait` 0.243 + `publish` 0.115 | 0.358 | 0.378 | host, in the shadow of the draft's first graph |
| `draft.finish`: first-graph wait 0.601, 0.99 chain steps x 0.648 ms (GPU 0.614 each) | 1.248 | 1.211 | GPU-bound |
| `finish_round` outside any span | 0.068 | 0.046 | host; 0.024 at 45k, 0.068 at 247k: O(context) |
| total | 15.18 | 15.35 | tokens per round 2.05 / 2.07 |
Vanilla for scale (STRA-258, timed run on the OLD corpus, route tail skip off, unpaired with anything above): verify 10.65, draft 0.93, adaptive tier 0.21, other 0.03 = 11.81 ms per window at 1.8-1.9
tokens. Its draft is the round graph plus about 0.4 steps, 0.93 ms; ours is `draft_committed` 1.71 ms for the round graph plus 0.99 steps, but 0.36 ms of that is the commit graph (below). The paired
`speedtrain-ab` result is the reference for the end-to-end comparison: ours seed off 134.9 tok/s, vanilla 135.4 (two runs each, vanilla varies 20 % run to run).
### Where the extra 0.4-0.6 ms per round in draft/apply/adapt comes from
1. The draft waits for the commit (about 0.36 ms). The host sees `commit.wait` end 0.36 ms after the draft launch; the commit graph (36 GDN layers x 2 kernels, 12 QSA appends, the PLE history: ~100 nodes, vanilla
fuses it into one 109 us kernel `gdn_step_commit_layers`) runs in front of the draft's first graph because the port made the draft stream wait the commit event
(`mtp_original_full_drafter.rs`, `after`). Vanilla does not: `core/verify.cpp` `set_commit_async` ("the drafter (its own stream) reads only this window's final rows and its own K/V") and `MtpDrafter::draft`
launches on `cs_` with no dependency, so commit and draft run side by side. Timeline in ours: commit 0.36, first graph 0.7, chain 0.99 x 0.65 = 1.71.
2. O(context) host work, 0.19 ms at 247k (the three "outside any span" rows and `sampling_setup` grow linearly with the history): `committed == consumed` compared four times per round (controller
`validate_current`, ledger `begin_window`, `with_sampling` before and after the window) and the whole history range-checked twice (`with_sampling`, `after_verified`). Slope: 0.45 us per 1k tokens in
`sampling_setup`, 0.30 in `verify_commit`, 0.20 in `finish_round`.
3. PLE rows on the critical path (0.37 ms per round, GPU idle): `KarPleBinding::read_rows` starts and joins eight threads for 48 positional reads of 10-20 bytes (0.119 ms per call, 2.1 calls per round),
and `gather` allocates and frees a device buffer per token (`cuMemAlloc` + `cuMemFree`, which waits for the whole device) around two context-synchronizing kernel calls. Vanilla prefetches the
draft's PLE rows during the draft (`prefetch_ple` in `mtp.cpp`); ours reads them after it.
4. Not host: the chain steps. 0.648 ms wall against 0.614 ms of GPU per step: 0.034 ms of host round trip per step (0.034 ms x 0.99 per round); the GPU time of the MTP graph is the kernel agents' lever.
5. `apply_pending` 0.22 ms is the tail of the adaptive swaps' PCIe copies (every 4th round, ~0.9 ms): the copies are longer than the 1.7 ms draft shadow minus their start offset. Moving the wait later does not
help: every `kar_ple_*` call ends with a context-wide synchronize, so the PLE gather waits for the copies anyway. Splitting the swaps over rounds changes when experts become resident (placement class): not done.
## 2. Changes (lossless; one commit each; CPU tests pass, nothing run on a GPU)
| commit | change | expected ms per round (estimate, not measured) | output-identity argument |
| --- | --- | ---: | --- |
| 3cf185a3 | the draft stream no longer waits for the commit event; `LELEKA_SERVE_DRAFT_AFTER_COMMIT=on` restores the wait | 0.2-0.35 | the draft reads the window's final residual rows, its own ring and weights; the commit graph writes only GDN state, QSA index images, PLE history and its "commit" span; no shared span, no device global in the modules; the next window still starts after both |
| 2f267df9 | `token_memo::PrefixMemo`: the five per-round passes over the token history examine only the appended tokens (full pass every 32 rounds, probe tokens, address/length/criterion guards) | 0.11 (mean over the session), 0.18 at 247k | assertions between two append-only host copies; a passing check gives the same answer, a differing new token / length / buffer fails as before; a rewrite of an old token (impossible through the owners) would be found within 32 rounds |
| 8735bbc5 | `read_rows`: the first row is read on the calling thread and timed; at <= 20 us the other 15 rows are read there too, else the eight-thread read (next 256 calls skip the probe) | 0.10-0.15 | the same `read_rows_chunk` reads the same offsets into the same slots; test compares probing, parallel and non-probing paths with the single-thread reference on a two-partition synthetic file, plus the refusals |
| c2198a7b | persistent scatter staging buffers (one per miss count) instead of `cuMemAlloc` + `cuMemFree` per token | 0.05-0.07 | same bytes, same length, same kernel call; every `kar_ple_*` launch is followed by a context synchronize, so a reused buffer is not overwritten under the kernel |
Sum of the estimates 0.45-0.65 ms of 15.35 (3-4 %); the paired A/B has to say what is real. Expect the estimates to overstate when the GPU is the bottleneck (commit overlap shares SMs with the draft).
Examined and not done:
- **Per-request fixed costs (~100 ms in the old runs).** Now `decode.assemble` 67 ms (graph capture 31 + instantiate 20 + prepare 12 + qsa/aggregates), `decode.disassemble` 11 ms, `decode.seed_ple` 0.1 ms (the 21 ms
of the old runs is gone with the PLE fix and the seed off), `prefill.refill` 139 ms, `prefix.export_kv` 78 ms. They sit between the prompt and the first token (TTFT), not in the client's decode tok/s. The one
fix on the table is verifier persistence across requests (`docs/decode-parity.md`), a design; the naive parked-graph update (6fa92c13) broke the stack and was reverted (c45bcaaf). No safe small fix.
- **Pipelined draft chain for min_p <= 0** (vanilla's fast path launches the round graph and every step graph back to back). The adaptive window always uses min_p >= 0.10, and a speculative step past a
failed confidence test writes a ring cell; it would save at most 0.034 ms x steps.
- **PLE rows prefetched during the draft** (what vanilla does). The host read part could overlap the draft only for the first token of the window (the others are known after the draft); the device part
cannot (context synchronize). Worth about 0.05 ms after commit 8735bbc5.
- **A fused commit kernel** (vanilla: `gdn_step_commit_layers` 109 us). Would shorten the commit that is now hidden beside the draft; numerics need their own gate.
- The sampling-free host spans left: `pool.spans` 2.9 us and `pool.publish_plan` 2.7 us per layer (0.27 ms per round inside the window's callbacks), `launch_inner` validations. Needs a GPU trace to know whether
they are on the critical path.
## 3. Not verified
- No GPU run of any of the four commits. The estimates are span arithmetic. The commit/draft overlap is justified by the vanilla source and by a read of the resources; a race would show as a logprob difference in
`drafttail-id.sh` (the control arm `idDac` with the old order tells which change it was).
- The timing of the overlap: both graphs may slow each other; 0.36 ms is an upper bound.
- `read_rows` warm threshold (20 us per row, 3 preads) is a guess for the sealed memfd; a loaded host takes the old eight-thread path, which is never slower than before except for one probe read.
- Two `leleka-runtime` tests fail in this WSL checkout on `/mnt/c` (`model_binding::...::backing::{failed_cleanup_or_replaced_inode_never_deletes_unowned_file, frozen_same_fd_hash_owner_and_source_independence}`:
`Metadata` error from the file-ownership checks); they are in code this branch does not touch.
- The vanilla column is from the old corpus and a timed run; it only gives the shape.
## 4. Jobs (server `rnr5090`, everything under `/data/leleka-e2e/decode/dt/`)
- Binaries `bin-drafttail/` (leleka-inference, leleka-daemon, leleka-cli, strata-next; WSL release build of c2198a7b), test binaries `rt-drafttail` (c2198a7b) and `rt-speedtrain` (speed-train runtime sources,
47d2d389-equivalent: fcb4f02b changes only deploy files), line in `BINS.txt`.
- `jobs/drafttail-ab.sh` (`scripts/e2e/decode/drafttail-ab.sh`): seed OFF, frozen corpus, ranked profile, `speedtrain-noseed` / `drafttail-noseed` alternating x3 (6 arms, ~75 GPU-min), optional
`drafttail-ac-noseed` (old draft order, attribution) and `v-def`. Output `/data/leleka-e2e/runs/draft-tail-ab`; the summary prints per-turn tok/s, adjacent-pair differences and the per-round ms table of section 1.
Acceptance: the pair differences against the spread of the reference runs (the two speed-train repeats differed by 2.7 tok/s in the all-turn mean), turn 5 `window.ple` < 5 ms valid.
- `jobs/drafttail-id.sh` (`scripts/e2e/decode/drafttail-id.sh`): teacher-forced replay, `q_pair` bitwise comparison of the top-16 logprobs, ~36 GPU-min. Output `/data/leleka-e2e/runs/draft-tail-id`.