Repository navigation
prefill: pipeline a prompt's chunks over the parts of a --device split - #243
Conversation
… split A --device split ran prefill chunk by chunk through every part in turn, so with N cards each was busy about 1/N of the time. Now each part runs on its own thread and starts chunk k + 1 as soon as it has handed chunk k on, so the cards work on different chunks at once. Each part reads only its own layers' state from the chunks before, and takes the chunks in order, so the result is the sequential loop's. Taken for one request (--parallel 1), a text prompt of two or more chunks, no STRATA_PF_ADAPT, no DFlash draft. STRATA_PF_PIPE=0 keeps the sequential loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One line per part per chunk: total and in / pre / delta / attn_proj / qsa_idx / qsa_sel (score) / attn / post / moe / tail / out, and the pipeline waits for the part before and after. Each mark syncs the part's streams, so it removes the overlap inside a part (not between parts); profiling runs come out a few percent slower. Off: no syncs, no cost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s (STRATA_PF_PINNED=0: Vecs) Each hop's hidden rows went device -> fresh Vec -> device, both copies through the pageable path. Two portable pinned buffers per hop, owned by the receiving part and passed back once its upload is done, make both copies direct DMA (Backend::alloc_shared_host; host_is_pinned sees them on every device). 4x RTX 3060 (two PLX switches): 980K prefill 2227 -> 2339 tok/s, same answer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gap: the time since the part's previous chunk line, i.e. the waits plus any work outside the timed window; lead: the setup and input embedding before it. Unaccounted gap on a part with no waits is work that paces the pipeline unseen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One thread dequantized (and un-rotated) each 4096-token chunk's embedding rows in ~300 ms before the head part's GPU work: the head's cycle was 1046 ms of GPU work + ~300 ms of this, which paced every part of the pipeline (the others showed it as waits). Now ~68 ms. 4x RTX 3060, Qwen4-35B-A3B UD-Q5_K_M, ctx 1M: 194K prefill 2939 -> 3124 tok/s, 3/3 needles, decode unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🔍 Aletheore evidence diffgraph LR
n0["src/backend.rs"]
n1["src/cuda/mod.rs"]
n2["src/gpu_model.rs"]
n3["cpu_worker.rs"]
n4["src/cuda/host_share.rs"]
n5["chat.rs"]
n6["mat.rs"]
n7["expert_cache.rs"]
n8["src/gpu_model/host_share.rs"]
n9["src/hip/mod.rs"]
n10["dspark.rs"]
n11["forward.rs"]
n12["device.rs"]
n13["pool.rs"]
n14["worker.rs"]
n15["lib.rs"]
n16["main.rs"]
n17["model.rs"]
n18["projector.rs"]
n19["src/vulkan/mod.rs"]
n20["preflight.rs"]
n21["tests/backend.rs"]
n22["tests/common/mod.rs"]
n23["tests/gpu_model.rs"]
n24["inkling2_attn_kv.rs"]
n25["inkling2_q8_attention.rs"]
n26["kernels.rs"]
n27["kv_q8.rs"]
n28["kv_turbo.rs"]
n29["llama_numerics.rs"]
n30["tq_probe.rs"]
n31["vision.rs"]
n32["vision_gpu.rs"]
n33["vulkan_split_kv.rs"]
n0 --> n3
n0 --> n6
n0 --> n7
n0 --> n8
n0 --> n9
n0 --> n10
n0 --> n11
n0 --> n12
n0 --> n13
n0 --> n14
n0 --> n15
n0 --> n16
n0 --> n17
n0 --> n18
n0 --> n19
n0 --> n21
n0 --> n22
n0 --> n23
n0 --> n24
n0 --> n25
n0 --> n26
n0 --> n27
n0 --> n28
n0 --> n29
n0 --> n30
n0 --> n32
n0 --> n33
n1 --> n4
n1 --> n5
n1 --> n9
n1 --> n12
n1 --> n15
n1 --> n16
n1 --> n19
n1 --> n20
n1 --> n22
n1 --> n23
n1 --> n24
n1 --> n27
n1 --> n28
n1 --> n29
n1 --> n33
n2 --> n15
n2 --> n16
n2 --> n17
n2 --> n19
n2 --> n21
n2 --> n23
n2 --> n31
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 changed
class n2 changed
class n3 dependent
class n4 dependent
class n5 dependent
class n6 dependent
class n7 dependent
class n8 dependent
class n9 dependent
class n10 dependent
class n11 dependent
class n12 dependent
class n13 dependent
class n14 dependent
class n15 dependent
class n16 dependent
class n17 dependent
class n18 dependent
class n19 dependent
class n20 dependent
class n21 dependent
class n22 dependent
class n23 dependent
class n24 dependent
class n25 dependent
class n26 dependent
class n27 dependent
class n28 dependent
class n29 dependent
class n30 dependent
class n31 dependent
class n32 dependent
class n33 dependent
What changed
Aggregate deltas
|
| } | ||
| if part.hand.is_empty() { pools.push((None, None)); continue; } | ||
| let (rtx, rrx) = sync_channel::<Pool>(part.hand.len()); | ||
| for b in part.hand.drain(..) { rtx.send(b).expect("hand-off pool"); } |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 4
alloc_shared_host error returns before reattaching detached parts, losing self.next prefill_pipelined takes self.next into rest, then part.gpu.alloc_shared_host(cap)? returns Err (not Ok(None)); function returns immediately, rest is dropped and self.next stays None, so all later prefills run only the head part with wrong/missing layers.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 285
Symbol: adapt_experts
Issue: alloc_shared_host error returns before reattaching detached parts, losing self.next prefill_pipelined takes self.next into rest, then part.gpu.alloc_shared_host(cap)? returns Err (not Ok(None)); function returns immediately, rest is dropped and self.next stays None, so all later prefills run only the head part with wrong/missing layers.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
1 of 4 (High): alloc_shared_host error returns before reattaching — FIXED
Fixed in the latest push. A failed allocation now falls back to Vecs for that hop instead of returning with ?, so the detached parts are always reattached.
| // The upload from the buffer is asynchronous: done before the buffer goes back. | ||
| if hin.buf.is_some() { part.gpu.sync()?; } | ||
| crate::term::prefilled(ch.len()); | ||
| true |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 4
Panic on head thread unwinds past reattachment of rest, leaving model detached A panic in head send_chunk/prefill_chunk_to (e.g. slice out-of-bounds in dtoh to a too-small hand buffer) unwinds through thread::scope before the reattach loop runs; self.next remains None and rest is dropped, corrupting the model for every subsequent call, unlike part-thread panics which are caught via join.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 330
Symbol: cache_begin
Issue: Panic on head thread unwinds past reattachment of rest, leaving model detached A panic in head send_chunk/prefill_chunk_to (e.g. slice out-of-bounds in dtoh to a too-small hand buffer) unwinds through thread::scope before the reattach loop runs; self.next remains None and rest is dropped, corrupting the model for every subsequent call, unlike part-thread panics which are caught via join.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
2 of 4 (High): head panic skips reattachment — FIXED
Fixed. The server catches a request's panic (catch_unwind around handle in main.rs) and keeps the model. The scope now runs under catch_unwind, the parts are reattached, and then the panic resumes.
| let free = frees.get_mut(i + 1).and_then(Option::take); | ||
| handles.push(sc.spawn(move || -> anyhow::Result<Option<(usize, Receiver<Pool>)>> { | ||
| let mut rx = rx.into_iter(); | ||
| loop { |
There was a problem hiding this comment.
🟠 High · Correctness · 3 of 4
Intermediate parts return hand buffer to pool without syncing their GPU upload Only the last part calls gpu.sync() before back.send(b); intermediate parts return hin.buf immediately after prefill_chunk_to. The previous part can then recv that buffer and dtoh-overwrite it while the intermediate GPU's asynchronous htod from it is still in flight, corrupting the next chunk nondeterministically.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 315
Symbol: prefill_cached_with
Issue: Intermediate parts return hand buffer to pool without syncing their GPU upload Only the last part calls gpu.sync() before back.send(b); intermediate parts return hin.buf immediately after prefill_chunk_to. The previous part can then recv that buffer and dtoh-overwrite it while the intermediate GPU's asynchronous htod from it is still in flight, corrupting the next chunk nondeterministically.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
3 of 4 (High): intermediate parts return the buffer without syncing — /dismiss
/dismiss A part with a next part ends prefill_chunk_to with a blocking dtoh of its slab into the outgoing hand-off (wait() followed by a stream sync). That runs on the same stream as the chunk's upload put, after it, so the upload from the incoming buffer has finished before the buffer goes back. Only the last part has no dtoh, which is why only it calls sync(). I added a comment there saying so.
| let on = on(); | ||
| if on { | ||
| g.sync_freed()?; | ||
| SCORE.with(|c| c.set(0.0)); |
There was a problem hiding this comment.
🟡 Medium · Correctness · 4 of 4
Pp error paths skip report, leaving WAIT_IN/WAIT_OUT stale for next chunk Pp::new resets SCORE but not WAIT_IN/WAIT_OUT; report() is the only place that clears them. Any ? error inside prefill_chunk_to drops Pp without report, so the next chunk's line includes the failed chunk's pipeline waits, and LAST is not updated so gap is also wrong.
Prompt for your AI agent
File: src/gpu_model/pf_prof.rs
Line: 70
Issue: Pp error paths skip report, leaving WAIT_IN/WAIT_OUT stale for next chunk Pp::new resets SCORE but not WAIT_IN/WAIT_OUT; report() is the only place that clears them. Any ? error inside prefill_chunk_to drops Pp without report, so the next chunk's line includes the failed chunk's pipeline waits, and LAST is not updated so gap is also wrong.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
There was a problem hiding this comment.
4 of 4 (Medium): Pp error path leaves WAIT_IN/WAIT_OUT stale — FIXED
Fixed. If a chunk drops its Pp without reporting, it clears its score and pipeline waits, so they no longer land on the next chunk's line. This only affects the STRATA_PF_PROF=1 output.
Aletheore Flash Lite review
No issues held up under independent verification (7 grounded, 0 confirmed by a second model). 4 earlier finding(s) from previous pushes still open.
Review detailsReviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 7 of 9 proposed finding(s). Blast radius: 64 other file(s) depend on what this PR changes (2 directly, 62 indirectly)Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.
Indirectly affected: The graph is large; this list is capped and not exhaustive. |
… head panic - A failed alloc_shared_host falls back to Vecs for that hop instead of returning before the detached parts are reattached. - The scope runs under catch_unwind: the server catches a request's panic and keeps the model, so the parts are reattached before the panic resumes. - pf_prof: a chunk that fails before its line clears its score and pipeline waits, so they don't land on the next chunk's line. - Comment: a part with a next part ends prefill_chunk_to with a blocking dtoh, so its upload from the hand-off buffer is done before the buffer goes back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Pushed a follow-up commit: review items 1, 2 and 4 are fixed, and item 3 is answered inline (the |
|
Hi, ty for this, nice work. I have only one card here, so I tested the split with the same 3060 opened twice (--device 0,0) on Qwen4 with a 4K token prompt and chunk 1024. Greedy output is the same on main, on this branch, and on this branch with STRATA_PF_PIPE=0. Single device output is also unchanged, and split_matches_single, split_batch_matches_one_part and qwen4_slots_match_single pass. One small thing, not a blocker: in prefill_pipelined the alloc_shared_host(cap)? runs after self.next was taken. If that alloc fails, the function returns before the parts are put back, so the model loses its later parts. Doing the alloc before the detach (or putting the parts back on that error) would fix it. It is merged now. |
|
Your follow-up commit (keeping the parts on an alloc error or a panic) is merged too, ty. |
What changed and why
With
--device a,b,c,d, prefill ran each prompt chunk through every part in turn, so only one card worked at a time. On a 4-card split that leaves about three quarters of the GPU time idle during long prefills. Five commits:src/gpu_model/prefill.rs): each part runs on its own thread and starts chunk k+1 as soon as it has handed chunk k on. Each part touches only its own layers' state and takes chunks in order, so the result equals the sequential loop's.--parallel 1), text prompts of 2+ chunks, noSTRATA_PF_ADAPT, no DFlash draft. Everything else keeps the sequential loop.STRATA_PF_PIPE=0restores the sequential loop.STRATA_PF_PROF=1(src/gpu_model/pf_prof.rs): per-part, per-chunk component timing (in / pre / delta / attn_proj / qsa_idx / qsa_sel / attn / post / moe / tail / out) plus pipeline waits. Off by default, no cost when off.Backend::alloc_shared_host) instead of a fresh pageableVec, so both copies of a hop are direct DMA.STRATA_PF_PINNED=0switches back.gap/lead: time since the part's previous chunk line, and setup/embedding before the timed window. This is what exposed (5).Independent of #207. If #207 merges first,
STRATA_PF_PROF's qsa marks also need to cover its gather branch (a one-hunk rebase I can do).Verification
Setup: 4× RTX 3060 12 GB on two PLX PEX 8747 switches (no P2P; all hand-offs go through host memory), Qwen4-35B-A3B UD-Q5_K_M, ctx 1M, KV q8,
--devicesplit27,25,27,21, chunk 4096, release build. Needle retrieval 3/3 in every run.First result, pipelining alone on stock v1.5.1 (commit 1 only, same model, flags and prompts; no other patches):
The answer text was identical to the unpatched run at 194K, needles 3/3 at both lengths, no errors in the log.
Pipeline A/B (
STRATA_PF_PIPE=0vs default): same binary and flags, fresh server per mode, the env var is the only difference. Each 194K row is two runs with different needle texts (no prefix-cache hits).Pipelining makes prefill 3.5× faster at 194K and 3.56× at 992K on 4 parts; a 1M-token prompt drops from 22 min to about 6. Decode and needle accuracy are unchanged.
End to end on this box (for context only; it also includes the separate #206 and #207 CUDA follow-ups):
Individual commits:
cargo checkpasses on every commit.STRATA_MLOCK_EXPERTS=1); the pipelining speedup is the ratio between the two modes on that one binary.To reproduce: run a prompt of 100K+ tokens on a multi-GPU
--devicesplit with--parallel 1, once withSTRATA_PF_PIPE=0and once without, and compare theprefill:tok/s line.STRATA_PF_PROF=1prints the per-part, per-chunk breakdown.Notes for review
Checklist
This work was done with Claude Code.
🤖 Generated with Claude Code