pkg-s16 (51b36adf7), 9/30 requests: RING-REFUSE layer=46|47 check=stage.slot_not_gathered reserved=0
slots=384 sources=410..460. With prefetch hits the copies skip the hit sources, so once a layer's sources exceed
the ring the wrap reaches a slot holding an earlier source's copy that is not gathered yet; the stage_room
shortcut (reserved == 0) then let stage_one refuse.
- stage_target (leleka-cuda-model ring_prefetch) searches forward, wrapping, for the first slot that is not
reserved and free (never filled or gathered); without a prefetch it is stage_next itself (the literal ring:
copies and gathers in one order, lookahead one short of the ring).
- Ring::stage_room: no reserved shortcut; "no copy needed or stage_target finds a slot", so the walk waits for
a gather instead of refusing.
- Host tests: 200 seeds of 460 sources / 384 slots with random prefetch hits among the first 300, other sizes;
the model asserts no stage onto a reserved or unread slot and that stage_target refuses only when no
unreserved slot is free (both fail with the old stage_target). 18 tests.
Switch stays OFF by default. Not run on a GPU.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
149 lines
12 KiB
Markdown
149 lines
12 KiB
Markdown
# 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
|
|
|
|
1. **History** (`leleka_cuda_model::ring_prefetch::MissHistory`). Every `Ring::new` records 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
|
|
last `window` chunks (default 8). Missed sets are per layer because each layer has its own resident set, and the
|
|
lent slots change between requests.
|
|
2. **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
|
|
top `K` are kept, with `K = min(top_k, ring_slots / 2)`, and `K = 0` for a ring under 32 slots: the literal 8-slot
|
|
ring of the 512-row rung is never prefetched into.
|
|
3. **Issue** (`OriginalMmqCopySchedule::prefetch_ring_layer`, called from `run_prompt_layer` through
|
|
`OriginalMoePromptLayer::prefetch_ring_layer`). This runs right after layer `l`'s ring has closed, which drains both
|
|
streams, and before layer `l`'s combine. The predicted experts of layer `l+1` are copied into stage slots
|
|
`0..K-1` of the same row lease. Each copy is the same as `stage_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's `copied` event 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. By default
|
|
the drains stay context-wide: the combine's and the next mixer's drains wait for these copies (the PCIe time then
|
|
overlaps only the GPU work queued before such a drain). With `LELEKA_PREFILL_RING_PREFETCH_DETACH=1` the copy stream
|
|
becomes the `compute_drain` background stream, as under the chunk expert stream: the drains stop waiting for it,
|
|
all-zero uploads become memsets and frees are deferred.
|
|
4. **Adopt** (`Ring::adopt_prefetch`, in `Ring::init` after `admit_sources`). The next ring takes the set (in the detached
|
|
mode this 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 marked `reserved`
|
|
until that source's gather, and its `copied` event 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.
|
|
5. **Ring walk.** The slot bookkeeping is the pure `SlotState` / `stage_needed` / `stage_target` of
|
|
`leleka_cuda_model::ring_prefetch`, which the driver's `Ring` and the host tests both call. `stage_one` skips prefetched
|
|
sources and picks the next slot that is not reserved. Without reservations, that slot is the literal `stage_next`.
|
|
`ring_stage_ahead` stops 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. Since `K <= 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 |
|
|
| `LELEKA_PREFILL_RING_PREFETCH_DETACH` | `1` / `on`: detach the drains from the pending copies (EXPERIMENTAL) | off |
|
|
|
|
The driver reads no environment variable for this feature. The policy is passed with
|
|
`OriginalMmqCopySchedule::set_ring_prefetch` when the arena owners are attached.
|
|
|
|
## Refusal and decision trace (`LELEKA_TRACE_DRIVER_ERRORS` or `LELEKA_BRINGUP_TRACE`)
|
|
|
|
The engine passes its driver-error trace switch with `OriginalMmqCopySchedule::set_ring_error_trace`; the driver reads no
|
|
environment variable. Every copy-ring refusal still returns `InvalidProjectionShape` but also prints
|
|
`RING-REFUSE layer=.. check=.. source=.. stage_next=.. reserved=.. slots=.. sources=.. error=..`. The checks:
|
|
`new.validate`, `new.schedule_slots`, `layer.preflight`, `sources.spans`, `stream.layer` / `stream.no_entry` /
|
|
`stream.entry_bytes` / `stream.pieces`, `stage.no_source`, `stage.source_already_staged`, `stage.all_slots_reserved`,
|
|
`stage.slot_not_gathered`, `gather.no_source`, `gather.no_slot`, `gather.slot_not_live`,
|
|
`gather.slot_already_gathered` (followed by `gather.slot` from the caller), `enqueue.closed_full_or_context`. The
|
|
prefetch prints `RING-PREFETCH layer=.. issued=..`, `layer=.. hits=.. wasted=..`, `layer=.. dropped=.. issued_for=..`,
|
|
`layer=.. cancelled=..` and `layer=.. issue failed: ..`.
|
|
|
|
## 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
|
|
|
|
1. **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.
|
|
2. **Counters.** `ring.prefetch_hit / ring.prefetch_issued` should be near the offline 55-57 % at K = 150-200.
|
|
`ring.copied_mb` should fall by the hit MB.
|
|
3. **Latency.** Run a paired A/B of `prefill.read` and `ring.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. Watch `ring.init_sync`: it now waits for prefetch copies that are still in
|
|
flight. Run the identity first with `_DETACH` off; only then the detached mode, which also changes the mixer's
|
|
drains, memsets and frees while the copies are pending.
|
|
4. **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 tests in `crates/cuda-model/src/ring_prefetch/tests.rs`.
|
|
|
|
## First GPU run (PET.1, 2026-11-10): failures with the first commit
|
|
|
|
With the first commit (`3ba2a0e37`, the background stream always on) 5 of 12 requests failed with
|
|
`InvalidProjectionShape`, and 9 of 30 in the earlier run. From reading the code, the host slot bookkeeping holds: the host
|
|
tests drive the same functions through the scenarios that were suspected (a prefetched source staged again, a reserved
|
|
slot reused, a gather of a slot that was never filled or was already read), and none of them can be reached. The
|
|
unreviewed change was the background stream. It is a process-wide switch that changes every drain, memset and free
|
|
of the small-chunk mixers and routers while the copies are pending. A drain in that mode skips streams that are not
|
|
registered, for example the QSA resident-transfer side stream. This is a suspect, not a proven cause: the error code
|
|
alone does not say which check refused. That mode is now opt-in
|
|
(`_DETACH`). If the failure persists with it off, the `RING-REFUSE` line names the check.
|
|
|
|
## Second GPU run (pkg-s16, 51b36adf7): `stage.slot_not_gathered` on layers 46/47
|
|
|
|
9 of 30 requests failed with the same refusal in both arms (with and without `_DETACH`):
|
|
`RING-REFUSE layer=46|47 check=stage.slot_not_gathered source=391..435 stage_next=7..191 reserved=0 slots=384
|
|
sources=410..460`. So the detached drains were not the cause. The cause was the slot search. Without a prefetch,
|
|
copies and gathers run in the same order and the lookahead is one slot short of the ring, so the next slot in ring order
|
|
always holds the oldest copy, already gathered. With prefetch hits, the copies skip the hit sources, and once the
|
|
layer's sources exceed the ring, the wrap reaches a slot that holds an earlier source's copy that is not gathered yet.
|
|
The old `stage_room` shortcut (`reserved == 0`, true once every hit was gathered) then let `stage_one` refuse.
|
|
|
|
Fix: `stage_target` searches forward, wrapping, for the first slot that is not reserved and is free (never filled, or
|
|
already gathered). Without a prefetch it finds `stage_next` itself, as in the literal ring. `stage_room` is now always
|
|
"needs no copy, or `stage_target` finds a slot", so the walk waits for a gather instead of refusing. Host tests: 200
|
|
seeds of 460 sources on 384 slots with random prefetch hits among the first 300 sources, plus other sizes. The model
|
|
asserts that no stage targets a reserved or unread slot, and that `stage_target` refuses only when no unreserved slot is
|
|
free. With the old `stage_target`, both assertions fail. |