Files
leleka/docs/experiments/decode-graph-reuse.md
T

9.0 KiB

Decode graph reuse across requests (ledger row Q7)

Status: design only, no code committed. The reuse needs an ownership refactor (stack-independent assembly, QSA owner loan, a re-arm API in the verify bank / gpu plan, which is original_verify_gpu_plan*, owned by the speed-vs-strata session); see section 4 for why and the order of changes. No speedup claim.

1. Why the graphs are recorded per request

decode_phase (OriginalServeStack) calls assemble_decode -> AssembleInputs::assemble (original_serve_verifier.rs) for every request, and teardown_decode -> disassemble at its end. assemble is: prepare (geometry, canonical GDN image uploaded from the session's priors, QSA gamma), OriginalVerifyQsaJournal::new over the twelve QSA owners, then capture_verifier: a new OriginalVerifier (new Physical arena, mapped allocations, two streams), one aggregate (OriginalVerifyWindowResources) per T = 1..=max_t plus one for the commit graph, and record_all (nine graphs at T <= 8 with the mode17 policy, 54-62 ms capture + 27-30 ms instantiate). disassemble closes the verifier (drains, drops every graph and aggregate), which releases the aggregates' references to the QSA journal so into_owners can take the owners back, and release_mtp drops the MTP drafter's programs bound to this verifier's bank.

The recording is per request because of OWNERSHIP, not because the kernel arguments change:

  1. The QSA owners (twelve index images + mapped K/V journals) belong to the prompt executor. The next request's prefill writes them (chunks, prefix-cache import, rollback). They can only get there by moving out of the journal (Rc::try_unwrap in into_owners), which requires every graph that holds the journal to be gone.
  2. The resources borrow the stack: DecodeResources<'s, 'ctx> holds &'s DenseWeightCache, &'s MoeWeightCache, &'s OriginalNativeOutput, &'s DeviceBuffer (PLE history and scratch), &SharedKeys (a value of the request's own stage). A value that borrows fields of OriginalServeStack cannot be stored in it (self-reference); this is why DecodeView borrows the fields one by one.
  3. The shared experts and routers are LRU entries of the MoE cache (ensure_original_verify_shared); the prompt path loads layer by layer and may evict and reload them, so their device addresses are not guaranteed to survive a prefill.
  4. The MTP drafter's round programs read the verifier's bank (final R span) and are released with it (mtp_release_verifier); they are re-captured against the next bank (spans draft.*, a second cost of the same cause).

docs/decode-parity.md already records this as "Verifier persistence: not done ... needs the executor to use the owners through the journal".

2. What the captured graphs point at

Per-request buffers (created by assemble, addresses change every request today; data must be re-seeded if the buffer is kept):

Resource Created in Per request Can be stable
Canonical GDN image (36 layers x 12848128 + 10240*3 f32, ~118 MB) prepare_assembly (original_verify_canonical_gdn(states, histories)) allocated and uploaded from the session priors yes: keep the buffer, re-upload (118 MB H2D, stays in the per-request cost)
Verifier arena, prof, 15 mapped allocations, window modules OriginalVerifier::new new yes: sized by verify_geometry(max_t, max_cells, blobs); only the window state is data (last_t, last_position, generation, staged tokens)
Head rows per T, embedding rows per T, GDN journal, window aggregates aggregate() new yes, part of the kept assembly
QSA journal Rc<OriginalVerifyQsaJournal> OriginalVerifyQsaJournal::new new Rc around the executor's buffers the buffers are the executor's (fixed addresses); the Rc must outlive requests
Graphs, streams, event record_all new the point of the feature

Stable resources the graphs also point at (owned across requests today, so reuse needs only a proof that they were not reallocated): dense weights and gamma, router and shared experts (MoE cache entries, see 1.3), OriginalNativeOutput, PLE history and PLE scratch (stack buffers), RoPE tables, the registered expert banks of the pcie path, the VRAM expert cache slots.

Context length, window width, expert tier:

  • Context length and positions are DATA (staged into mapped host memory / device flags before each run); the arena and the QSA journals are sized for max_cells, fixed by the configuration. A longer context does not change the graphs.
  • The window width T is the graph identity (OriginalGraphKey::VerifyWindow(t), t in 1..=8) and max_t is the arena geometry. verifier_max_t can only fall during a process (narrow_window_policy after a graph OOM); a held assembly recorded for another max_t must be retired.
  • The expert tier: pcie_mode (0 DMA, 2 copy kernel) and the per-layer role bytes are baked into the graphs (set_pcie_mode, pcie_role_bytes); the VRAM cache slot contents are data (published plan), the cache object and the registered banks are addresses. decode_pcie_mode is a function of the configuration and of the cache being present.
  • Process-level switches resolved at recording time: qsa_cluster_requested (cluster top-k), the sm120 switches (from_env), WindowPolicy::mode17(). They are fixed for a process; they are in the key so a change is caught.

3. Key and invalidation (the policy the reuse would implement)

DecodeGraphKey { max_t, max_cells, pcie_mode, tier, cluster_topk, weights_epoch, owners_epoch }. weights_epoch and owners_epoch are counters bumped by the owners of the physical buffers whenever one that a window reads or writes in place is freed, moved or reallocated (MoE cache eviction or reload of a shared expert or router, dense cache reload, QSA owner rebuilt, PLE or canonical buffer replaced, expert bank re-registered). GraphReuse::acquire(key, record) returns the held assembly only for an equal key and only if rearm() accepts; otherwise it retires it and calls record(). release keeps an assembly only after a clean request; invalidate retires it (broken stack, reconfigure, failed request, narrowed window). A reuse that cannot show an equal key is a re-record.

4. Why it is not implemented in this change

The reuse needs, in order (none of it needs original_verify_window_*, original_verify_gpu_plan* or graph-stream code, except where marked; without a GPU none of it can be validated here):

  1. Lifetimes: the held assembly must not borrow the stack. Either the caches the windows read (dense, MoE, output, formats, PLE buffers) move behind Rc / a long-lived owner that the stack and the assembly both reference, or the assembly is kept outside the stack in a holder that outlives it. Every &'s in DecodeResources, VerifyWeights, AssembleInputs and the aggregates changes. (unsafe lifetime erasure is not an option in this crate.)
  2. QSA owner loan: the journal must be able to lend the owners to the executor without the graphs being dropped (indices/mapped as RefCell<Option<..>> in qsa_verification_commit.rs; a graph run while lent is refused), and the executor must hand them back at the same addresses (take_qsa_owners / adopt_qsa_owners keep the physical buffers; they rebuild the lineage objects, which is fine). Epoch owners_epoch bumps if any owner is replaced.
  3. Re-arm API in the driver: reseed the canonical GDN image in place, reset the bank's window state (last_*, generation, the gpu-plan state), drain both streams, instead of OriginalVerifier::close. The gpu-plan state lives in original_verify_gpu_plan* (owned by another session): that part needs their change, exact need: a reset_for_new_request on the bank/plan that leaves the registered buffers and clears per-request flags.
  4. MTP drafter: do not release the verifier bindings when the bank is kept (mtp_release_verifier), release them on retire.
  5. Epoch counters in the MoE and dense caches and the executor (the key above), and pinning of the shared experts / routers for as long as an assembly is held, so that a prefill cannot evict a buffer a held graph reads.

Validation (GPU, orchestrator): identity of greedy output reuse-on vs off over N requests with different prompts, lengths and a forced eviction in between (must re-record, and match); paired A/B of decode.assemble, decode.disassemble, record.capture, record.instantiate, record.modules, draft.* first-draft capture, and end-to-end request time; VRAM at the decode low-water mark (a kept set holds the graph pool during prefill: the prefill arena and loan ladder budget shrink by the pool size).

5. Expected size of the win and what stays

Removes record_all (83-85 ms) and the aggregate construction; the canonical image upload and per-request state seeding stay (about 15-25 ms of the 98-109 ms), decode.disassemble (B4, 11.7 ms) becomes a re-arm. The kept pool costs VRAM during prefill.