From c9b9bdf79f641e18efdf14771ff161773fbe7227 Mon Sep 17 00:00:00 2001 From: Heimdular <97338903+Heimdular@users.noreply.github.com> Date: Sun, 6 Dec 2026 12:35:49 +0100 Subject: [PATCH] gpu: enable batched image prefill for Qwen4 (QSA + PLE) --- src/gpu_model/prefill.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/gpu_model/prefill.rs b/src/gpu_model/prefill.rs index 89ca4587..859e3dc8 100644 --- a/src/gpu_model/prefill.rs +++ b/src/gpu_model/prefill.rs @@ -238,7 +238,7 @@ impl GpuModel<'_> { pub(super) fn prefill_image_rows(&mut self, embs: &[Vec], pos: &[[i32; 4]], ids: &[u32]) -> anyhow::Result { let c = &self.m.cfg; let Some(chunk) = self.pf.as_ref().map(|p| p.chunk) else { return Ok(0) }; - if c.mrope_sections.is_none() || self.qsa.is_some() || self.ple_ring.is_some() { return Ok(0); } + if c.mrope_sections.is_none() { return Ok(0); } let n = embs.len().min(chunk).min(pos.len().saturating_sub(1)); if n < 2 { return Ok(0); } let segs = crate::backend::mrope_segments(&pos[..n]); @@ -246,8 +246,13 @@ impl GpuModel<'_> { anyhow::ensure!(self.pos + n <= self.ctx, "context full ({} tokens; raise --ctx)", self.ctx); self.kv_need(self.cur, self.pos + n)?; let emb: Vec = embs[..n].iter().flat_map(|x| x.repeat(c.hc_n.max(1))).collect(); + let pe: Vec = if self.ple_ring.is_some() { + ids[..n].iter().flat_map(|&tk| self.m.ple_embed(&mut self.ple_prev, tk)).collect() + } else { + vec![] + }; self.pf_mrope = Some((segs, first)); - let r = self.prefill_chunk(&ids[..n], Some((&emb, &[])), None); + let r = self.prefill_chunk(&ids[..n], Some((&emb, &pe)), None); self.pf_mrope = None; r?; crate::term::prefilled(n); @@ -501,7 +506,8 @@ impl GpuModel<'_> { let (hd, nh, nkv) = (c.head_dim, c.n_head, c.n_head_kv); let kv = nkv * hd; // The draft head runs as one more layer on the chunk while it is in step with the main model. - let fill = self.mtp_synced(); + // Skip during M-RoPE image chunks (matching `step_embed`, which does not feed placeholder tokens to MTP). + let fill = self.mtp_synced() && self.pf_mrope.is_none(); let is_eagle = self.mtp.as_ref().is_some_and(|mh| mh.eagle.is_some()); // STRATA_MTP_FILL_STEP=1: fill a hyper-connection head after the chunk, TMAX rows a time (the old way; to compare). let hc_step = c.hc_n > 0 && std::env::var("STRATA_MTP_FILL_STEP").is_ok_and(|v| v == "1"); @@ -764,7 +770,7 @@ impl GpuModel<'_> { // Rows with at most `top` complete blocks attend densely: no mask while all of them do. if self.pos + t >= super::QSA_POOL * (b.top + 1) { g.rms_norm(iq, dq, a.qn.ptr, iqn, dq, dq, t * super::QSA_H, eps)?; - g.rope_neox(iqn, dq, t * super::QSA_H, super::QSA_H, c.n_rot, rope, c.rope_base)?; + self.rope_chunk(iqn, dq, t, super::QSA_H, rope)?; for r0 in (0..t).step_by(super::QSA_ROWS) { g.qsa_select(iqn + (4 * r0 * nq) as u64, a.pool[self.cur].addr(), b.score.addr(), 0, b.mask.addr() + (4 * r0 * b.mw) as u64, step, r0, super::QSA_ROWS.min(t - r0), b.nbs, b.top, 0, b.mw)?;