From 23f0ecb97dcd73a3d28b7e65c69139934b969ac6 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Mon, 9 Nov 2026 22:51:15 +0100 Subject: [PATCH] Speed program O7: trace of verify end to first draft kernel (ours and vanilla) Vanilla's greedy accept is host-side (generate.cpp:15255); the difference to ours is the post-drain finite-verdict scan (three syncs against one) and the host chain before the draft launch. A device-side accept needs draft-chain edits; options and recommendation in the note. Co-Authored-By: Claude Sonnet 5.5 --- docs/experiments/verify-accept-on-device.md | 146 ++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 docs/experiments/verify-accept-on-device.md diff --git a/docs/experiments/verify-accept-on-device.md b/docs/experiments/verify-accept-on-device.md new file mode 100644 index 00000000..98fc2cf5 --- /dev/null +++ b/docs/experiments/verify-accept-on-device.md @@ -0,0 +1,146 @@ +# Verify end to the first draft kernel (ledger row O7): where the 0.32 ms go, and what "accept on the device" is + +Status: reading of the code and of existing measurements; no GPU run, no code change. Base: `5b8c0eba0` (branch +`claude/speed-vs-strata-08143a`), Strata pin `41cf05a06a25` (`docs/upstream.md`). Ledger row: `step-ledger.md` section C3, +O7: LEL 0.32 ms, VANL 0.08 ms ("accept decided on the device"), +0.24 ms, source ORCH (a figure given by the +orchestrator session; this note does not re-derive it). + +## 1. Result in five lines + +1. **The premise of the row is not what the vanilla code does.** In vanilla's greedy serve loop the accept count `a` is + computed on the host (`engine/strata/src/program/generate.cpp:15255`: `while (a < T-1 && window[a+1] == outv[a]) ++a`), + the commit gets `a + 1` from the host (`:15271`), and `MtpDrafter::draft` receives `a` as an `int`, writes it to a + host-mapped word (`src/core/mtp.cpp:1605`, `h_row_[0] = a`) and picks the round graph by `T` (`:1580-1582`). The + device-side accept in the vanilla tree is the probabilistic rejection sampler (`STRATA_SPEC_PROB`, + `src/kernels/cuda/sampler.cu:883-1045`), which is another mode and not the default greedy path. +2. **What vanilla does keep on the device is the finite (NaN) verdict of the head logits.** `Verifier::record` captures + `logits_nonfinite_rows` into the window's own stream after the greedy pick (`src/core/verify.cpp:2380`); after the + window's one `cudaStreamSynchronize` (`:3178`) the host only reads the mapped ids (`:3260`) and the mapped flags + (`note_nonfinite`, `:4180`). No extra launch, no second sync. +3. **Ours does that check after the drain, with two more synchronisations** (section 2, hops 4-5), in files that are ours + (`verify_gdn_journal_ladder_logits_scan.rs`, `original_verify_schedule/window_phases.rs`, + `original_gpu_verify_bridge.rs`). +4. **An exact device-side accept that removes a host round trip cannot be built inside our files.** Every consumer of `a` + (the controller, the draft round, the chain steps, token emission, the NaN guard) is host code outside them + (section 4). I stop at the decision point and give the options (section 5); nothing is implemented, the switch + `LELEKA_SM120_ACCEPT_ON_DEVICE` does not exist yet. +5. **Recommendation:** option A (the finite verdict into the verify graph, mapped result, no new PTX; it is vanilla's + structure and lives in our files) as the step for this row, with the honest size: the only measured span of the scan + is 0.05 ms per round (older tree), i.e. about a fifth of the +0.24 ms. The rest of the gap is the host chain between the + sync and the draft launch (hex-finish). + +## 2. Our path, verify graph end to the first draft kernel + +Per round, in call order. File:line refer to this tree (`crates/cuda-driver/src` and +`crates/engine-qwen35-cuda/src/qwen_forward`). + +| hop | what happens | where | host or GPU | figure (source) | +| --- | --- | --- | --- | --- | +| 1 | last kernel of the window graph (greedy argmax into the head rows' mapped words) ends | `native_head/original_native_head_rows/original_native_head_rows_greedy_submit.rs:84-104` (`greedy`), captured by `capture_head_greedy` from `original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs:244` | GPU | n/a | +| 2 | `compute.synchronize_with_receipt()` returns, then `copy.synchronize()` (already idle), `release_pcie_sources`, `record_window_profile` (off) | `original_verify_schedule/window_phases.rs:277-285` (`drv.sync_compute`, `drv.sync_copy`) | host wake from a blocking sync | not isolated; `drv.sync_compute` + `drv.head` together 0.88 ms incl. the last layer and head on the GPU (`docs/serve-step-costs.md`, job 901) | +| 3 | `verifier_head_after_drain`: stream and receipt checks, `resources_after_stream_drain` | `original_gpu_verify_bridge.rs:7-20` | host | below the noise of the spans | +| 4 | **post-drain logits scan, launch and stream sync**: `verify_clear_status` + `verify_scan_logits` over all `T x vocab` words are launched on the compute stream, then `stream.synchronize()` | `original_verify/original_verify_gdn_journal/verify_gdn_journal_ladder_logits_scan.rs:116-136` (`head.validate_stream`) | host launch (2 kernels) + a second blocking sync + the scan itself on the GPU | `head.validate_logits` (the whole of hops 4 and 5): 0.05 ms per round, job 901 on `claude/ms-trim` (`docs/serve-step-costs.md`: "head validation scan 0.05") | +| 5 | **status read back**: `cuMemcpyDtoH` of the 4-byte status from the arena's `hit_count` scratch, then a context-wide `synchronize` | same file `:138-157` (`head.validate_ctx_copy`) | host, third sync of the window | in the 0.05 ms above | +| 6 | `sample_on_stream` is skipped for greedy; `read_selection` reads the mapped ids and range-checks them; `complete_window` copies them, sets `window_ready` | `original_native_head_rows_greedy_submit.rs:156-168, 235-242`, `window_phases.rs:303-321` (`head.read_ids`) | host | not isolated | +| 7 | back in the engine: `admit_receipt`, `quality_force::apply`, optional traces, `NativeWindow` built | `original_serve_native/native_serve_port_original_serve_port.rs:60-89` | host | in the "verify_commit outside any span" row below | +| 8 | controller: `accepted_prefix` (the `while` loop over `inputs`/`outputs`), `note_round_profile`, `begin_adaptation` (choose + queue swaps), `commit_verified(keep = a + 1)` | `original_spec_controller/original_spec_controller_methods/commit_parts.rs:12-24, 78-121`; `native_serve_port_original_serve_port.rs:92-109` | host | `verify_commit` outside any span 0.094 ms at 247k context (0.038 at 45k) (`draft-tail-5090.md` table); `round.adapt` (choose + queue) 0.079-0.084 ms (`decode-tail-5090.md`, section 5) | +| 9 | commit enqueue: `host.commit_enqueued`, `Verifier::commit` = `stage_commit(keep)`, graph launch, event record (async, no wait) | `original_verify_schedule/original_verifier.rs:34-67`, `original_verify/share_window_modules/original_verify_bank.rs:158` | host | `drv.commit_enqueue` 0.004 ms, `round.commit` 0.005-0.006 ms (`draft-round-5090.md` section 2; `draft-tail-5090.md`) | +| 10 | `draft_overlapped` -> `launch_overlapped`: `take_window_parts`, `host.launch_draft` (staging of the round's mapped words including `a`, snapshot publication, round graph launch). The draft stream does not wait for the commit event unless `LELEKA_SERVE_DRAFT_AFTER_COMMIT=on` | `original_serve_native/native_window.rs:155-201`, `draft_waits_for_commit` `:272`; draft side in `mtp_original_*` (hex-finish) | host | `draft.launch` 0.027-0.041 ms untraced (`draft-round-5090.md` section 2; `decode-tail-5090.md` section 5); window graph launch 0.008 ms | +| 11 | first kernel of the MTP round graph starts | | GPU | tracer timeline: verify end -> first kernel of the round graph 315 us (before), 230 us (fused commit + attention switch) in a CUPTI window; the tracer inflates the launch calls (`draft-round-5090.md` section 2) | + +Sum of the independently measured host spans of hops 4-5, 8, 9, 10: 0.05 + (0.09 + 0.08) + 0.005 + 0.035 = about 0.26 +ms, plus the wake-up latencies of three blocking syncs that nobody has isolated. That is the same size as the ORCH 0.32 +ms. It is an addition of figures from different trees and runs (older `ms-trim` for the scan, 247k context for +`verify_commit`), so it is an estimate and not a trace. The CUPTI window `CL.1` has the real gap; the script that prints +round and verify-graph spans is `scripts/e2e/codex/speed/decode_rounds.py` (the "verify end to first draft kernel" +column is the orchestrator's). + +Where the draft sits relative to the commit: the commit graph is not on the draft's critical path (the draft stream no +longer waits for it, `draft-round-5090.md` section 2: draft end - commit end 1,066 us), so hop 9 costs the critical path +only its host time (about 0.01 ms), not the commit graph's GPU time. + +## 3. Vanilla's path (the 0.08 ms) + +| hop | what happens | where | +| --- | --- | --- | +| 1 | window graph ends; its tail contains the greedy pick **and** `logits_nonfinite_rows` writing to mapped flags | `src/core/verify.cpp:2372-2380` | +| 2 | one `cudaStreamSynchronize(cs_)`; `copy_` is synchronised only if it was used | `:3178`, `:3213-3216` | +| 3 | the host reads the mapped ids (`out[t]`), then `note_nonfinite` reads the mapped flags: no launch, no sync | `:3260`, `:4180-4200` | +| 4 | `a` by the `while` loop; adaptive tier on a separate `std::thread` (not on the path) | `generate.cpp:15255-15270` | +| 5 | `ver.commit(a + 1)`: enqueue, async | `:15271`, `core/verify.cpp` `set_commit_async` | +| 6 | `nonfinite_hit` reads the flags already read; tokens are printed and flushed; `mtp.draft(T, outv, p, a, ...)` stages the mapped words (`h_row_[0] = a`, cells `p + a + j`) and launches the round graph on its own stream `cs_` with no dependency on the commit | `:15276-15316`, `src/core/mtp.cpp:1597-1629` | + +So the difference is not "device against host accept". It is: one blocking sync against three (hops 2, 4, 5 of ours), +no scan launch against two kernels, and a short host chain (no `begin_adaptation`, no controller bookkeeping, no +snapshot publication) before the draft launch. + +## 4. Can an exact device-side accept be built inside our files? + +The computation itself is trivial and exact: one single-thread kernel after the greedy pick that compares +`inputs[i + 1]` with `ids[i]` (the staged window tokens are on the device) and stores `a` or `keep = a + 1`. It would be +bitwise-equal to the host loop (integer compare). The question is who can consume it without a host read, and there the +answer is no, inside our files: + +1. **The draft round and chain** need `a` in host-staged mapped words before the launch: `launch_inner` reads + `window.accepted_row` (`mtp_original_full_drafter/original_mtp_drafter/original_mtp_drafter_launch_rounds.rs:16, 131`), + the chain steps' cells are `p + a + j` staged by the host (vanilla: `mtp.cpp:1601-1603`). Making the round graph read + `a` from the device means editing `mtp_original_*` and the draft kernels' operand addresses: another team's files, and + the chain steps `j >= 1` would still have to be launched from the host after reading draft `j - 1` (the stop rule + `p_j >= min_p` is host-side, both in vanilla `mtp.cpp:1694` and ours). +2. **The controller** (emission of tokens, EOS, the budget `max_new`, the adaptive window policy `observe`, the + cancel check) needs `a` on the host in the same round; it cannot be deferred. +3. **The commit** is the only consumer that could take a device value: its `keep` is a mapped word staged by + `stage_commit` (`original_verify_bank.rs:158`, ours). Enqueuing the commit graph right behind the window in the same + stream, with `keep` computed on the device, would remove hop 9 from the host path (about 0.01 ms) and nothing else, + because the commit is not on the draft's critical path. It is also **unsafe**: the NaN guard may refuse the window + (`discard_window` in `verifier_commit_window_lifecycle.rs` requires that no commit was enqueued, and + `native_serve_port_original_serve_port.rs` `discard_window` fails the port if one is pending: "an enqueued commit + cannot be undone"), and the finite verdict is exactly what is decided after the drain. A pre-launched commit would + have to run behind the finite verdict on the device (a conditional graph node), which is a larger design for a 0.01 + ms host saving. +4. **CUDA conditional nodes** (graph `switch` on `a`) could select the right draft round graph on the device and drop + the host launch altogether, but the draft graphs are keyed per row count and rebuilt per request + (`OriginalGraphKey::MtpRound { rows, coupled }`), the chain steps are host-driven, and it is a new draft design + (`hex-finish`, `mtp_original_*`). Not a step of the device part. + +Verdict: **not implementable as an exact, same-policy device accept inside `original_verify_window_*`, +`original_verify_gpu_plan*` and the cuda-driver verify files without edits to the draft chain and the controller**. +Per the brief I stop here. + +## 5. Options and recommendation + +| option | what | files | saves (documented / estimated) | risk | +| --- | --- | --- | --- | --- | +| **A. Finite verdict in the verify graph** (vanilla's structure; the "verdict decided on the device") | capture `verify_clear_status` + `verify_scan_logits` (already in the sealed `original_verify.ptx`) after `capture_head_greedy` in `OriginalVerifyWindowOps::head`, the result in a host-mapped word; post-drain code only reads it (no launch, no stream sync, no `cuMemcpyDtoH`, no context sync) | ours: `original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs`, `verify_gdn_journal_ladder_logits_scan.rs`, `original_gpu_verify_bridge.rs`, the mapped-slot list in `original_verify.rs`; one new mapped word; no new PTX | hops 4-5: 0.05 ms per round measured on `ms-trim` (job 901); the GPU part (the scan over `T x 248k` words, 2 extra kernels) moves into the graph's own tail | the scan kernel `atomicOr`s into the status: on a device-arena word that is fine, on host-mapped memory atomics are platform dependent. The safe form is: scan into a device word (as now), then a plain 4-byte store to the mapped word at the end of the graph (the greedy kernel already stores its ids to mapped memory this way). The NaN guard behaviour must stay (`Error::NonFiniteMmqOutput("Original Verifier full logits")` before any token is used); a test with an injected non-finite logit (the vanilla analogue is `STRATA_NAN_INJECT`) is needed on the GPU. No number of the model changes | +| B. Shorten the host chain between the sync and the draft launch | `begin_adaptation` (0.08), `verify_commit` bookkeeping (0.09 at 247k, O(context)), draft staging (0.03-0.04): move off the path or make O(1) | `original_spec_controller*`, `original_serve_native*`, `mtp_original_*`: hex-finish | up to 0.15-0.20 ms of the 0.24 (sum of documented spans) | owned by `hex-finish`; the two earlier trims (`decode-tail`, `draft-round`) already took 0.16 ms and 0.03 ms of it | +| C. Commit pre-launched with a device `keep` | section 4, item 3 | ours | 0.01 ms host, 0 on the critical path | breaks `discard_window` unless conditioned on the finite verdict; not recommended | +| D. Device-selected draft (conditional graph nodes) | section 4, item 4 | draft chain | host launch of the round graph, potentially most of hop 10 | new design in their files; not a device-part step | + +Recommendation: **A for this row** (smallest, exact, in our files, it is what vanilla has), with the expectation set +from the data: about 0.05 ms, not 0.24. Close the remaining part of O7 by B with `hex-finish`. If A is approved I would +implement it under `LELEKA_SM120_ACCEPT_ON_DEVICE` (default off, resolved in `original_verify_window_sm120.rs` in its own +block), keep the post-drain scan as the off path, and add unit tests for the switch resolution, for the graph recording +(two more nodes in the verify graph, `DBG graph window t=.. nodes=`), and for the refusal on a non-zero status. Suggested +ledger text for O7 meanwhile: "vanilla's greedy accept is host-side (generate.cpp:15255); the difference is the +finite-verdict scan after the drain (3 syncs against 1) and the host chain before the draft launch". + +## 6. How the orchestrator would see option A in a CUPTI window (if built) + +* Before: after the verify graph, standalone launches of `verify_clear_status` and `verify_scan_logits` (1 block, then up + to 65535 blocks of 256 threads) on the compute stream, followed by a gap while the host does `cuMemcpyDtoH` and a + context synchronize, then the commit and draft launches. +* After: the same two kernels as the last nodes of the verify graph (kernels per verify graph +2, outside-the-graph + kernels per round -2), and no `cuMemcpyDtoH` / `cuCtxSynchronize` in the API-call list between the window and the + commit (the ledger's API calls per round, row O4, and syncs per round, row O6, fall by one and one respectively). +* Host-side check without CUPTI: `LELEKA_SERVE_PROFILE=1`, spans `head.validate_logits`, `head.validate_stream` and + `head.validate_ctx_copy` (all three should vanish or become sub-microsecond), `drv.head`. + +## 7. Sources + +* `engine/strata/src/program/generate.cpp:15236-15316`, `src/core/mtp.cpp:1559-1744`, `src/core/verify.cpp:2372-2380, + 3170-3270, 4180-4200` (vanilla, read-only). +* `docs/experiments/draft-round-5090.md` section 2 (CUPTI window of the commit, round and step graphs; untraced host + spans), `docs/experiments/decode-tail-5090.md` sections 5-6 (host spans per round), `docs/experiments/draft-tail-5090.md` + (per-round table at 247k), `docs/serve-step-costs.md` (job 901, "head validation scan 0.05"), + `docs/experiments/codex-compare-speed-gap.md` "Kernel level: one CUPTI window of request 29" (round 17.86 against 15.89 + ms; after the verify graph to the next start 4.30 against 4.14), `docs/experiments/step-ledger.md` C3/C5.