Skip to content

gpu: enable batched image prefill for Qwen4 (QSA + PLE) - #246

Merged
Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
Heimdular:patch-1
Dec 6, 2026
Merged

Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
Heimdular:patch-1

Conversation

@Heimdular

Copy link
Copy Markdown
Contributor

Disclosure: This PR was created with the assistance of an AI coding assistant (Antigravity); all changes were built and verified locally. This is my first ever PR and I hope that it is useful for others.

What changed and why

In src/gpu_model/prefill.rs, prefill_image_rows() currently returns Ok(0) whenever self.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-token
prefill (~65–73 tok/s) instead of using the batched image prefill added in #149.

This change enables batched image prefill for qwen4exp in src/gpu_model/prefill.rs:

  1. prefill_image_rows(): Remove || self.qsa.is_some() || self.ple_ring.is_some() from the early return, compute pe via self.m.ple_embed(&mut self.ple_prev, tk) for ids[..n], and pass Some((&emb, &pe)) into self.prefill_chunk().
  2. QSA prefill in prefill_chunk(): Replace g.rope_neox(iqn, ...) with self.rope_chunk(iqn, dq, t, super::QSA_H, rope)? so QSA indexer queries rotate with M-RoPE positions, matching step_embed in src/gpu_model.rs. (qsa_pool keys already use sequential block positions 4 * b in both paths.)
  3. MTP guard in prefill_chunk(): Set let 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 (matching step_embed).

Related: #149

Verification

  • Build: RUSTFLAGS="-C target-cpu=native" cargo build --release compiles cleanly with no warnings.
  • Benchmarks & Parity: RX 9070 XT (16 GB, Vulkan / Mesa RADV 26.2.8), Ryzen 7 5700X, Fedora 44, unsloth/Qwen4-35B-A3B-UD-Q4_K_M + mmproj-BF16.gguf, greedy (--temp 0, non-thinking):
    rustingstrata serve --model Qwen4-35B-A3B-UD-Q4_K_M-00001-of-00002.gguf \
    --gpu --backend vulkan --ctx 65536 --parallel 1 \
    --vram-margin 2560 --kv q8 --cache-ram 0 --threads 8 \
    --mtp 0 --mmproj mmproj-BF16.gguf --temp 0
Test Case Before (v1.5.2, token-by-token) After (main 2936596 + patch) Speedup
2048×1536 (#149 image, 3,104 tok) 65.0 tok/s PP / 55.26 s wall 798.5 tok/s PP / 11.58 s wall 12.3× PP / 4.77× wall
1280×720 (967 tok) 73.2 tok/s PP / 15.90 s wall 464.3 tok/s PP / 4.62 s wall 6.3× PP / 3.44× wall
Deep QSA + 1280×720 (6,983 tok, exercises qsa_select) 396.2 tok/s PP / 18.80 s wall 1,020.0 tok/s PP / 8.11 s wall 2.6× PP / 2.32× wall
Follow-up on cached #149 image (3,099 cached) 1.15 s 1.08 s Prefix cache intact
  • Verified accurate image descriptions and spatial layout across all cases (including the [Bug]: Vision is very poor performance #149 sketch and the 6,983-token QSA sparse-attention test).
  • Note on --mtp 1: Prefill speedup also holds with --mtp 1 (415.7 tok/s on 720p, 952.6 tok/s on 7K deep QSA), though on a cached follow-up turn with --mtp 1 a 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

  • I followed this repository's contribution guide and required checks.
  • I updated documentation or examples where needed. (No doc updates needed; docs/VISION.md already covers qwen4exp.)
  • I added regression coverage where it helps verify the change. (Verified with live qwen4exp Vulkan parity/benchmark runs.)
  • I have the right to contribute the included code and assets under this repository's contribution terms.

@aletheore

aletheore Bot commented Dec 6, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

Merge confidence Review effort Merge risk
5 / 5 1 / 5 Low
Area Files Lines
src/gpu_model 1 +10 −4

No issues held up under independent verification (9 grounded, 0 confirmed by a second model).

Review details

Reviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 9 of 9 proposed finding(s).

@aletheore

aletheore Bot commented Dec 6, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph 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
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/gpu_model/prefill.rs · modified · +10/-4 · 1 other dependent

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 1

@Vasyl-Trefilov
Vasyl-Trefilov merged commit 3746502 into RustingStudio:main Dec 6, 2026
3 checks passed
@Vasyl-Trefilov

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants