Repository navigation
gpu: enable batched image prefill for Qwen4 (QSA + PLE) - #246
Conversation
Aletheore Flash Lite review
No issues held up under independent verification (9 grounded, 0 confirmed by a second model). Review detailsReviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 9 of 9 proposed finding(s). |
🔍 Aletheore evidence diffgraph LR
n0["prefill.rs"]
n1["gpu_model.rs"]
n0 --> n1
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 dependent
What changed
Aggregate deltas
|
|
Hi, ty for this. I tested it on Qwen4 here. Text prompts give the same greedy output as main (4K token prompt, chunk 1024), and the kernel and backend tests pass. For images I ran gpu_image_prefill_matches_cpu: with the batched path (chunk 16) the GPU top token matches the CPU one, max logit diff 3.85 out of a 49.8 range, about the same as the token by token path (4.09). One note: the fill guard mtp_synced() && pf_mrope.is_none() does not change anything for image prompts, because prefill_image_with already drops MTP for them (t.mtp = None). So if you still see the stray after an image, it comes from somewhere else, not from MTP state. It is merged now. |
What changed and why
In
src/gpu_model/prefill.rs,prefill_image_rows()currently returnsOk(0)wheneverself.qsa.is_some() || self.ple_ring.is_some(). Because Qwen4-35B-A3B (qwen4exp) uses both Qwen Sparse Attention (QSA) and the per-layer n-gram embedding table (PLE), image prompts fall back to token-by-tokenprefill (
~65–73 tok/s) instead of using the batched image prefill added in #149.This change enables batched image prefill for
qwen4expinsrc/gpu_model/prefill.rs:prefill_image_rows(): Remove|| self.qsa.is_some() || self.ple_ring.is_some()from the early return, computepeviaself.m.ple_embed(&mut self.ple_prev, tk)forids[..n], and passSome((&emb, &pe))intoself.prefill_chunk().prefill_chunk(): Replaceg.rope_neox(iqn, ...)withself.rope_chunk(iqn, dq, t, super::QSA_H, rope)?so QSA indexer queries rotate with M-RoPE positions, matchingstep_embedinsrc/gpu_model.rs. (qsa_poolkeys already use sequential block positions4 * bin both paths.)prefill_chunk(): Setlet fill = self.mtp_synced() && self.pf_mrope.is_none();so an enabled MTP head does not ingest placeholder<|image_pad|>tokens during batched image chunks (matchingstep_embed).Related: #149
Verification
RUSTFLAGS="-C target-cpu=native" cargo build --releasecompiles cleanly with no warnings.26.2.8), Ryzen 7 5700X, Fedora 44,unsloth/Qwen4-35B-A3B-UD-Q4_K_M+mmproj-BF16.gguf, greedy (--temp 0, non-thinking):v1.5.2, token-by-token)main2936596+ patch)2048×1536(#149 image,3,104tok)65.0 tok/sPP /55.26 swall798.5 tok/sPP /11.58 swall1280×720(967tok)73.2 tok/sPP /15.90 swall464.3 tok/sPP /4.62 swall1280×720(6,983tok, exercisesqsa_select)396.2 tok/sPP /18.80 swall1,020.0 tok/sPP /8.11 swall3,099cached)1.15 s1.08 s6,983-token QSA sparse-attention test).--mtp 1: Prefill speedup also holds with--mtp 1(415.7 tok/son 720p,952.6 tok/son 7K deep QSA), though on a cached follow-up turn with--mtp 1a stray</think>token appeared at the start of the reply (clean with--mtp 0), so MTP head state after image chunks may merit a closer look during review.Checklist
docs/VISION.mdalready coversqwen4exp.)qwen4expVulkan parity/benchmark runs.)