- cuda-driver: optional cuMemcpyBatchAsync_v2 + cuStreamWaitEvent in AsyncApi (api/enabled/batch_copy.rs, stub answers can_batch() == false); pure call plan refill_batch_copy::plan_batch_calls (<= 512 copies per call, same order/bytes/destinations) and issue_copies with per-copy fallback after a refusal. - OriginalExpertCache::fill_slots_queued_batched: same checks, pieces and order as fill_slot_queued_on, one context entry; batch on the fill stream ordered after the legacy default stream by fill_event; registered bank only; a refused call is reported once and the rest goes per copy; driver errors poison as before. - engine: settings memory.refill_batch (LELEKA_REFILL_BATCH on|1) and refill_lent's batched branch; profile counters refill.batch_calls / refill.batch_copies / refill.per_copy. - tests: host-side plan tests; #[ignore] GPU test comparing per-copy vs batched slot bytes. - docs/experiments/refill-batch-copy.md. No GPU run, no speedup claimed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7.4 KiB
Lent-slot refill with batched copies (LELEKA_REFILL_BATCH, EXPERIMENTAL, off by default)
Date 2026-11-10. Branch: a sub-agent branch on top of claude/prefill-latency (cdb0958b6). Status: code and host-side
tests only; nothing has run on a GPU. No speedup is claimed. Step ledger row Q6.
Problem (M, ledger Q6)
After every prompt the expert-cache slots the prompt borrowed as scratch (999 to 2,226 slots) are refilled from the
registered host bank: original_serve_residency::refill_lent calls OriginalExpertCache::fill_slot_queued (or
_async under LELEKA_REFILL_DEFER) once per slot. Each call enters the context and issues one
cuMemcpyHtoDAsync_v2 per piece. A slot has three pieces (gate, up, down), or more when pin_resident ranges cut a
piece. On the 5090 (driver 595.99.02, CUDA 13.2) the host time to issue the copies is refill.queue = 70 ms per
request (about 70 us per slot); refill.sync is 23 ms. Both sit on the request's critical path. LELEKA_REFILL_DEFER
only moves the wait.
Design
LELEKA_REFILL_BATCH=on|1(settings::memory::MemorySettings::refill_batch; the driver reads no environment). When it is on,refill_lentcollects every(slot, spans)pair that needs a refill, in slot order, and makes one call:OriginalExpertCache::fill_slots_queued_batched(bank, fills, RefillBatchOptions { own_stream, max_copies_per_call: 512 }).- The cache checks every slot the way
fill_slot_queued_ondoes: owner, state, registered source and capacity. It also refuses a duplicate slot. It builds the piece list with the per-copy path's own code (cache_source,copy_pieces, thechecked_subspandestinations) and in its order: slot by slot, then gate, up, down. It retains every sourceRcinpendingbefore anything is queued, enters the context once, and issues the list. refill_batch_copy::plan_batch_calls(pure) cuts the list into consecutive calls of at most 512 copies. It leaves out empty copies, which move no byte and which the batch entry refuses.issue_copiesmakes onecuMemcpyBatchAsync_v2per call with one attribute:srcAccessOrder = STREAM, no location hints, flags 0. Each call gets the same destinations, sources and sizes as the per-copy path, so the slot bytes must come out identical. Only the host cost of issuing them changes.- Stream. The batch entry does not take the legacy default stream. The per-copy refill uses that stream unless the
refill is deferred. As far as I recall, the CUDA documentation says the batch entry refuses the legacy NULL stream;
no header or documentation was available here to confirm it, and it has not been checked on the 5090. So the batch always runs on the cache's own non-blocking
fill_stream. For a refill that is not deferred, the cache first recordsfill_eventon the legacy default stream and makesfill_streamwait for it (cuStreamWaitEvent). The copies therefore still start only after everything queued there before them.sync_queuedalready waits for both streams, and it stays the fence (settle_refill,lend_prefill_tail, close). - Binding.
api::AsyncApiresolvescuMemcpyBatchAsync_v2andcuStreamWaitEventas optional symbols (api/enabled/batch_copy.rs). If either is missing, the batch is unavailable and the refill is issued per copy. The prototype is the CUDA 13_v2form withoutfailIdx: dsts, srcs, sizes, count, attrs, attrsIdxs, numAttrs, stream.CUmemcpyAttributesis 24 bytes, and a compile-time assertion checks that size. These are the prototype and layout thatoriginal_gpu_schedule_ffialready uses for the verify window's PCIe DMA. The stub for non-CUDA builds answerscan_batch() == false. - Fallback and errors. The per-copy path is used when:
- the driver has no batch entry;
- the bank is pageable (the low-RAM file mapping);
- this cache saw a batch call refused before.
If a batch call fails, the warning is printed once (
leleka-serve: WARNING: the driver refused a batched expert refill ...) andbatch_refusedis set on the cache. The refused call and every later copy are then issued onecuMemcpyHtoDAsync_v2each on the same stream. A copy the refused call may already have queued is copied again with the same bytes. An error from a per-copy call, or from the event record or wait, poisons the cache and is returned, exactly like the per-copy path.
Counters (LELEKA_SERVE_PROFILE=1)
| name | meaning |
|---|---|
refill.batch_calls |
cuMemcpyBatchAsync_v2 calls the driver accepted (sum per request) |
refill.batch_copies |
copies those calls carried |
refill.per_copy |
copies issued one call each by the batched path: no entry, pageable bank, or after a refusal |
refill.queue, refill.spans, refill.sync |
unchanged; with the switch on, refill.queue is the one batched call |
Tests
- Host (run on the laptop,
cargo test -p leleka-cuda-driver --features cuda-driver --lib refill_batch_copy):- calls are cut into consecutive ranges no longer than the cap;
- order, destinations, sources and sizes are preserved;
- a list within the cap gives one call, and an empty list gives none;
- 2,226 slots x 3 pieces give 14 calls of up to 512 copies;
- empty copies are left out but keep their place;
- a cap of 0 or above 4,096 is refused.
- Settings:
LELEKA_REFILL_BATCHis inrefill_defer_batch_and_locked_only_are_on_or_one. It is an engine-crate test, so it runs in WSL/Linux only and was not run here. - GPU,
#[ignore]:refill_batch_copy_gpu_slots_equal_per_copy(crates/cuda-driver/src/refill_batch_copy_gpu.rs) fills 1,024 slots of 3 role-major pieces from one registered host buffer twice: once per copy, once throughissue_copiesin calls of 512. It requires every byte to be equal between the two and to the host bytes, with the gaps left untouched:cargo test --release -p leleka-cuda-driver --features cuda-driver,sm120 --lib -- --ignored --nocapture refill_batch_copy_gpu.
What the GPU validation must show (not done)
- The ignored GPU test passes on the 5090 (and, separately, on the laptop: on the RTX 3080 under Windows,
cuMemcpyBatchAsync_v2from page-locked memory faulted with error 700 indocs/windows.md, so the switch must stay off there unless this test passes). - Serve identity: the same requests with
LELEKA_REFILL_BATCHoff and on give identical greedy replies and logprob rows.verify_slotof refilled slots, or a slot read-back aftersettle_refill, matches the bank. LELEKA_SERVE_PROFILE=1, 14 or more warm requests per arm on the default set:refill.queueper request, before and after;refill.sync;refill.batch_callsandrefill.batch_copies(expected: copies / 512 calls, andrefill.per_copy= 0 on the registered bank);- wall time per output token. A speedup may be claimed only from that paired A/B with the quality result.
Risks
- The prototype and attribute layout come from the existing verify-window binding, not from the headers on this machine (none are installed here). An ABI mismatch would show as a refused call (fallback) or as wrong bytes; the GPU test checks for both.
- The first non-deferred batched refill creates
fill_streamandfill_event. The fill stream is non-blocking, so default-stream work queued after the refill is not ordered after it. Every reader of the lent slots waits insync_queuedfirst, as with the deferred path. - Driver faults of the batch entry on some Windows drivers (above): the switch is off by default.