After a layer's copy ring drained, the experts the next layer missed most often in its last N chunks (per-layer miss history, top K <= ring/2, none on the 8-slot ring) are copied into the next ring's stage slots on the copy stream, overlapping the combine and the next mixer. The next Ring::new adopts the set: a routed, non-resident source with the same host bytes reads its prefetched slot (reserved until its gather, ordered by the slot's copied event); the rest are dropped. stage_one skips reserved slots; the stage-ahead walk waits for a gather when the next free slot is still live. Same experts, kernels and group order: bitwise neutral by construction. A set no ring took is drained on end_prompt_stream / begin_prompt_stream / close. - leleka-cuda-model ring_prefetch: policy, MissHistory ranking, match_prefetch, next_unreserved_slot (+ host model of the reserved-slot ring walk), 10 tests - driver: policy passed via OriginalMmqCopySchedule::set_ring_prefetch (no env read); counters ring.prefetch_issued / _hit / _wasted / _failed (MB), span ring.prefetch_issue - engine: ServeSettings::prefill_ring_prefetch (+ _WINDOW), settings test; issue from run_prompt_layer - docs/experiments/ring-prefetch.md: design, switch, counters, GPU validation plan (R-hash identity, A/B by tail) Not run on a GPU: no identity probe, no A/B, no speedup claimed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7.5 KiB
Prompt copy-ring prefetch (EXPERIMENTAL, off by default)
Switch: LELEKA_PREFILL_RING_PREFETCH (engine settings, ServeSettings::prefill_ring_prefetch). Status: implemented
and host-tested only. No GPU run, no identity probe, no A/B yet. No speedup is claimed.
Problem
Chunks below the expert stream's row minimum (0.2-1.5k rows on the 5090 profiles; chunks from 1536 rows already use
the expert stream) go through the per-layer copy ring. Our profiles show that ring is bound by PCIe DMA. Per request,
7,000-12,000 experts that are not resident (14-26 GB, 146-266 per layer, about 2.05 MB each) cross the bus at about
45 GB/s. ring.finish_statuses, the wait for the copies and the kernels, takes 230-420 ms of a 530-830 ms
prefill.read. Each layer's GDN/QSA/attention stage takes about 7 ms (about 330 ms per request), and PCIe is idle
during it.
Design
- History (
leleka_cuda_model::ring_prefetch::MissHistory). EveryRing::newrecords the layer's missed set: the experts that were routed and not resident. The history is per layer, as a 512-bit mask per chunk, and keeps the lastwindowchunks (default 8). Missed sets are per layer because each layer has its own resident set, and the lent slots change between requests. - Prediction (
MissHistory::predict). Experts are ranked by how many recorded chunks missed them, then by the most recent chunk that missed them, then by id. Experts resident right now (cache_expert_span) are skipped. The topKare kept, withK = min(top_k, ring_slots / 2), andK = 0for a ring under 32 slots: the literal 8-slot ring of the 512-row rung is never prefetched into. - Issue (
OriginalMmqCopySchedule::prefetch_ring_layer, called fromrun_prompt_layerthroughOriginalMoePromptLayer::prefetch_ring_layer). This runs right after layerl's ring has closed, which drains both streams, and before layerl's combine. The predicted experts of layerl+1are copied into stage slots0..K-1of the same row lease. Each copy is the same asstage_one's: the aligned supersets or the exact pieces, with the same destination offsets. The copies run on the schedule's copy stream, and each slot'scopiedevent is recorded. The engine resolves the spans from the span cache, or from a walk of the binding that changes nothing. Only experts whose bytes already passed the finite admission scan are resolved; others are not prefetched. The copy stream then becomes thecompute_drainbackground stream, so the combine's and the next mixer's drains do not wait for these copies. This is the same mechanism the chunk expert stream uses. - Adopt (
Ring::adopt_prefetch, inRing::initafteradmit_sources). The next ring takes the set, which restores the context-wide drains and flushes the deferred frees. The set is used only if it was issued for this layer, from the same bank (Rc::ptr_eq), into the same slot spans, with the same blob size, and with no chunk stream. Otherwise it is drained and dropped. A routed, non-resident source whose host byte ranges are equal to an entry's ranges is consumed from that slot (source.slot,dest,prefetched). The slot is markedreserveduntil that source's gather, and itscopiedevent orders the gather. Each entry is consumed at most once (match_prefetch). Entries that were not routed, or that became resident, are wasted: the slot stays free, and a later stage copy into it is ordered behind the prefetch by the copy stream's FIFO order. - Ring walk.
stage_oneskips prefetched sources and picks the next slot that is not reserved (next_unreserved_slot). Without reservations, that slot is the literalstage_next.ring_stage_aheadstops early when the next free slot is not gathered yet (Ring::stage_room, always true without reservations). The reserved slots therefore shrink the lookahead and never cause an overwrite. SinceK <= slots / 2, a free slot always exists.
The same experts reach the same gather, the same kernels and the same group order. Only the time and the order of the H2D copies change, so the outputs must be bitwise identical.
Lifetime and errors. end_prompt_stream is called on every forward exit and on a failed layer. Like
begin_prompt_stream and the schedule's close, it cancels a set that no ring took: it synchronizes the copy stream,
drops the set and restores the drains. A failure while issuing is not returned to the layer. It is counted as
ring.prefetch_failed, the copy stream is synchronized, and fewer experts are prefetched; the ring copies the rest as
usual. A set that was adopted is covered by the ring's own drain, because submitted is set.
Switch
| Variable | Values | Default |
|---|---|---|
LELEKA_PREFILL_RING_PREFETCH |
1 / on (top K = 150), or a top K in 2..=512 |
off |
LELEKA_PREFILL_RING_PREFETCH_WINDOW |
recent chunks counted, 1..=64 |
8 |
The driver reads no environment variable for this feature. The policy is passed with
OriginalMmqCopySchedule::set_ring_prefetch when the arena owners are attached.
Counters (LELEKA_SERVE_PROFILE=1)
| Name | Call count | Value |
|---|---|---|
ring.prefetch_issued |
experts copied ahead | MB |
ring.prefetch_hit |
prefetched experts consumed by the ring | MB |
ring.prefetch_wasted |
prefetched experts not consumed (mispredicted, resident by then, or a set dropped) | MB |
ring.prefetch_failed |
issue attempts that stopped on an error | 1 each |
ring.prefetch_issue |
host span of the issue | ms |
ring.copied_mb |
unchanged: copies stage_one still made (hits are not counted here) |
MB |
The hit rate is hit / issued. The PCIe bytes saved from the ring's critical path equal hit MB. The extra bus
traffic equals wasted MB.
What the GPU validation must show
- Identity. Run with
LELEKA_TRACE_PREFILL_R=1, once with the switch off and once with it on, on the same prompts. Use 0.2-1.5k-row chunks (the 2048-row rung, 384 slots) and also 512-row rung chunks (8 slots, where the prefetch is a no-op). Every chunk's R hash must be equal. Also check the first-token logprob rows / greedy output. - Counters.
ring.prefetch_hit / ring.prefetch_issuedshould be near the offline 55-57 % at K = 150-200.ring.copied_mbshould fall by the hit MB. - Latency. Run a paired A/B of
prefill.readandring.finish_statuses, by tail class (chunk rows: <512, 512-1535, >=1536 as the control, where the prefetch must stay inactive). Measure on the same workload and record the run metadata the charter requires. Watchring.init_sync: it now waits for prefetch copies that are still in flight. Also watch the mixer stages: while the background stream is set, their all-zero uploads become memsets, exactly as under the chunk stream. - Sweeps. K in {100, 150, 200} and window in {4, 8, 16}.
Risks and open points
- The prefetch competes with the combine's and the next mixer's own H2D uploads on the copy engines. A large K that outlasts the mixer delays the ring's mandatory copies, which queue on the same stream behind the prefetch.
ring.init_sync(a context synchronize) waits for every prefetch copy, wasted ones included.- Experts that were routed only once so far (and never finite-scanned) are not prefetched.
- Layer 0 of a chunk is never prefetched, because the prefetch is issued from the previous layer of the same chunk and row lease.
- Not tested on a GPU: the event ordering, the background-stream switch and the reserved-slot walk are covered only by
review and by the host model in
crates/cuda-model/src/ring_prefetch/tests.rs.