Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/VISION.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Status: Qwen3-VL image input works on the CPU path and on the GPU path. The projector, image
preprocessing, prompt expansion and M-RoPE text integration run against `qwen3.5-2b` +
`qwen3.5-2b-mmproj.gguf`; `generate`/`chat --image` and `serve` image requests (OpenAI `image_url` and
Anthropic image blocks) work with or without `--gpu`.
Anthropic image blocks) work with or without `--gpu`. Without `--mmproj` the first `mmproj*.gguf` next to
the model is used; `--mmproj none` loads no projector.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Correctness · 4 of 6

Doc promises the 'first' mmproj*.gguf, but directory order is unspecified. If a model directory holds several mmproj*.gguf files, find_sibling picks whichever read_dir returns first, which varies by filesystem. The same command can load different projectors across machines or runs. Sorting the candidates or rejecting ambiguity would make the documented behaviour true.

Prompt for your AI agent
File: docs/VISION.md
Line: 8
Issue: Doc promises the 'first' mmproj*.gguf, but directory order is unspecified. If a model directory holds several mmproj*.gguf files, find_sibling picks whichever read_dir returns first, which varies by filesystem. The same command can load different projectors across machines or runs. Sorting the candidates or rejecting ambiguity would make the documented behaviour true.
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.

Gemma 4 (`gemma4v`) image input works through `generate --image`, `chat --image` and `serve` (OpenAI
`image_url`/Anthropic `image` blocks); `chat`/`serve` use the model's `tokenizer.chat_template` (system
Expand Down
14 changes: 13 additions & 1 deletion src/gemma4/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,19 @@ impl<'a> Gemma4Model<'a> {
if k == 0 { return vec![]; }
for (g, d, _) in &cpu_sel { g.prefetch_expert(0, 1); d.prefetch_expert(0, 1); }
use crate::cpu_moe::{int_dot, quantize_q8k};
let (dgu, ddn) = (int_dot(cpu_sel[0].0.ty).unwrap(), int_dot(cpu_sel[0].1.ty).unwrap());
let (Some(dgu), Some(ddn)) = (int_dot(cpu_sel[0].0.ty), int_dot(cpu_sel[0].1.ty)) else {
// An expert type with no integer kernel (BF16 experts of a UD-Q8_K_XL file): each expert through

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Correctness · 5 of 6

Only cpu_sel[0] is checked for an integer kernel; other experts with different types are not covered. If a layer mixes expert tensor types, the first expert has a kernel while a later one does not. The later expert then hits the unwrap path in the loop and panics, because the else branch was only entered based on the first expert.

Prompt for your AI agent
File: src/gemma4/forward.rs
Line: 283
Symbol: ffn
Issue: Only cpu_sel[0] is checked for an integer kernel; other experts with different types are not covered. If a layer mixes expert tensor types, the first expert has a kernel while a later one does not. The later expert then hits the unwrap path in the loop and panics, because the else branch was only entered based on the first expert.
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.

// Mat::matvec, which takes every type.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Performance · 2 of 6

Fallback runs experts serially with fresh allocations per expert per token. For every expert and every token the closure allocates three Vecs and runs matvec one expert at a time. On CPU decode this is much slower than the integer path and adds allocator pressure on every layer, and nothing reuses scratch buffers across experts.

Prompt for your AI agent
File: src/gemma4/forward.rs
Line: 284
Symbol: ffn
Issue: Fallback runs experts serially with fresh allocations per expert per token. For every expert and every token the closure allocates three Vecs and runs matvec one expert at a time. On CPU decode this is much slower than the integer path and adds allocator pressure on every layer, and nothing reuses scratch buffers across experts.
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.

return cpu_sel.iter().flat_map(|(g, d, _)| {
let mut gu = vec![0f32; 2 * nf];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Correctness · 1 of 6

Fallback sums nothing by routing weight

per-expert outputs are concatenated without applying the third tuple element. cpu_sel entries carry a third element (ignored via _), presumably the router weight. The fast integer path likely scales or accumulates each expert's output by it. The BF16 fallback returns each expert's raw output concatenated, so for a UD-Q8_K_XL model with BF16 experts the MoE output is wrong (wrong length or unweighted combination) and generation quality silently degrades.

Prompt for your AI agent
File: src/gemma4/forward.rs
Line: 286
Symbol: ffn
Issue: Fallback sums nothing by routing weight: per-expert outputs are concatenated without applying the third tuple element. cpu_sel entries carry a third element (ignored via `_`), presumably the router weight. The fast integer path likely scales or accumulates each expert's output by it. The BF16 fallback returns each expert's raw output concatenated, so for a UD-Q8_K_XL model with BF16 experts the MoE output is wrong (wrong length or unweighted combination) and generation quality silently degrades.
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.

g.matvec(&moe_in, &mut gu);
let mut a = vec![0f32; nf];
act(&gu, nf, &mut a);
let mut o = vec![0f32; n];
d.matvec(&a, &mut o);
o
}).collect();
};
let xq = quantize_q8k(&moe_in);
let mut gu = vec![0f32; k * 2 * nf];
gu.par_chunks_mut(16).enumerate().for_each(|(c, ys)| for (j, v) in ys.iter_mut().enumerate() {
Expand Down
32 changes: 21 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ struct Engine {
/// Sequences in GPU memory at once (`serve --parallel`).
#[arg(skip)]
parallel: usize,
/// Multimodal projector (mmproj) GGUF used by `--image`.
/// Multimodal projector (mmproj) GGUF used by `--image` (default: an `mmproj*.gguf` next to the model; `none`
/// loads no projector).
#[arg(long)]
mmproj: Option<PathBuf>,
}
Expand Down Expand Up @@ -372,6 +373,15 @@ fn restore_calib_env() {
}

impl Engine {
/// The projector for `model`: `--mmproj`, else an `mmproj*.gguf` next to it; `--mmproj none` gives none.
fn mmproj_for(&self, model: &Path) -> Option<PathBuf> {
match &self.mmproj {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Maintainability · 3 of 6

The string sentinel "none" is matched on a PathBuf, so a real file named none is silently ignored. A user with a projector file literally named none in the working directory passes --mmproj none, expecting the file, and gets no projector. Option-of-path state also cannot distinguish 'unset' from 'explicitly disabled' without this string check. An enum or a separate flag would be unambiguous.

Prompt for your AI agent
File: src/main.rs
Line: 378
Symbol: ds_env
Issue: The string sentinel "none" is matched on a PathBuf, so a real file named none is silently ignored. A user with a projector file literally named `none` in the working directory passes `--mmproj none`, expecting the file, and gets no projector. Option-of-path state also cannot distinguish 'unset' from 'explicitly disabled' without this string check. An enum or a separate flag would be unambiguous.
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.

Some(p) if p.as_os_str() == "none" => None,
Some(p) => Some(p.clone()),
None => vision::find_sibling(model),
}
}

/// Hand `--backend`, `--device`, and `--kv` to the DeepSeek, GLM, Gemma 4 and Inkling 2 paths.
fn ds_env(&self) {
// SAFETY: called at the start of a command, before any thread reads these variables.
Expand Down Expand Up @@ -1319,7 +1329,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
if g.get("general.architecture").ok().and_then(|v| v.as_str()) == Some("gemma4") {
let prompt = if prompt == "-" { let mut s = String::new(); std::io::stdin().read_to_string(&mut s)?; s } else { prompt };
if image.is_empty() { return rusting_strata::gemma4::generate(&g, &prompt, n); }
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let mmproj = mmproj.as_deref().context("--image needs --mmproj")?;
let input = load_images(mmproj, &image)?;
return rusting_strata::gemma4::generate_image(&g, &prompt, &input.grids, &input.embeddings, n);
Expand Down Expand Up @@ -1347,7 +1357,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
if g.get("general.architecture").ok().and_then(|v| v.as_str()) .is_some_and(rusting_strata::inkling2::is_arch) {
let prompt = if prompt == "-" { let mut s = String::new(); std::io::stdin().read_to_string(&mut s)?; s } else { prompt };
let max_ctx = 0;
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let opts = rusting_strata::inkling2::chat::Inkling2Opts {
max_ctx,
sampler: engine.sampler(),
Expand Down Expand Up @@ -1375,7 +1385,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
let vision_prompt = if image.is_empty() {
None
} else {
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let mmproj = mmproj.as_deref().context("--image needs --mmproj")?;
let input = load_images(mmproj, &image)?;
Some((build_image_prompt(&tok, &input, &image, &prompt)?, input))
Expand Down Expand Up @@ -1657,7 +1667,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
engine.own_warn(&g);
if g.get("general.architecture").ok().and_then(|v| v.as_str()) == Some("gemma4") {
let input = if image.is_empty() { None } else {
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let mmproj = mmproj.as_deref().context("--image needs --mmproj")?;
Some(load_images(mmproj, &image)?)
};
Expand All @@ -1684,7 +1694,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
}
if g.get("general.architecture").ok().and_then(|v| v.as_str()) .is_some_and(rusting_strata::inkling2::is_arch) {
let max_ctx = 0;
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let opts = rusting_strata::inkling2::chat::Inkling2Opts {
max_ctx,
sampler: engine.sampler(),
Expand All @@ -1709,7 +1719,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> {
let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?;
let tok = Tokenizer::from_gguf(&g)?;
let image_input = if image.is_empty() { None } else {
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model));
let mmproj = engine.mmproj_for(&model);
let mmproj = mmproj.as_deref().context("--image needs --mmproj")?;
Some(load_images(mmproj, &image)?)
};
Expand Down Expand Up @@ -3622,7 +3632,7 @@ fn serve_model(model: &Path, mut engine: Engine, s: &Serving, w: &mut Option<Tcp
let _own = rusting_strata::config::OWN_ARCHS.contains(&arch).then(OwnState::new);
let (aux_g, m);
let mut r = if _own.is_some() {
let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(model));
let mmproj = engine.mmproj_for(model);
Runner::Own(match arch {
"gemma4" => rusting_strata::gemma4::serve(&g, &tok, &name, mmproj.as_deref())?,
"glm5-next" => rusting_strata::glm::serve(&g, &tok, &name)?,
Expand Down Expand Up @@ -3792,7 +3802,7 @@ fn handle(mut w: TcpStream, q: anyhow::Result<serve::Request>, l: Option<&mut Lo
("GET", "/api/models") => {
// The model `--auto-unload` freed counts as loaded: a chat loads it again.
let asleep = s.asleep.lock().unwrap().clone();
let models: Vec<_> = s.models().into_iter().map(|(id, p, n)| json!({"id": id, "bytes": n, "image": e.mmproj.is_some() || vision::find_sibling(&p).is_some(), "loaded": l.as_ref().is_some_and(|l| l.path == p) || asleep.as_ref() == Some(&p)})).collect();
let models: Vec<_> = s.models().into_iter().map(|(id, p, n)| json!({"id": id, "bytes": n, "image": e.mmproj_for(&p).is_some(), "loaded": l.as_ref().is_some_and(|l| l.path == p) || asleep.as_ref() == Some(&p)})).collect();
// The values in effect: with a model loaded, `chunk` and `mtp` include its calibration.
let options = json!({"ctx": e.ctx, "kv": e.kv, "cache_ram": e.cache_ram, "vram_margin": e.vram_margin.join(","), "mtp": e.mtp, "chunk": e.chunk,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Maintainability · 6 of 6

No test covers the new mmproj resolution order or the new BF16 fallback branch. A future change to find_sibling or int_dot could break the none disable path or the no-kernel fallback without any test failing. Behaviour across run, chat, serve and the models listing now depends on mmproj_for with nothing pinning it down.

Prompt for your AI agent
File: src/main.rs
Line: 3807
Symbol: handle
Issue: No test covers the new mmproj resolution order or the new BF16 fallback branch. A future change to find_sibling or int_dot could break the `none` disable path or the no-kernel fallback without any test failing. Behaviour across run, chat, serve and the models listing now depends on mmproj_for with nothing pinning it down.
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.

"gpu": e.gpu, "ctx_train": s.stats.lock().unwrap().ctx_train,
Expand Down Expand Up @@ -3861,7 +3871,7 @@ fn handle(mut w: TcpStream, q: anyhow::Result<serve::Request>, l: Option<&mut Lo
let prompt = l.tpl.render(&req.messages, &req.tools, req.think.unwrap_or(e.think_default), req.preserve_thinking.unwrap_or(e.preserve_thinking), true);
let images: Vec<String> = req.messages.iter().flat_map(|m| m.images.iter().cloned()).collect();
let n = if images.is_empty() { l.tok.encode(&prompt)?.len() } else {
let mmproj = e.mmproj.clone().or_else(|| vision::find_sibling(&l.path));
let mmproj = e.mmproj_for(&l.path);
let mmproj = mmproj.as_deref().context("image input needs --mmproj")?;
let input = load_images(mmproj, &images)?;
build_image_prompt(l.tok, &input, &images, &prompt)?.ids.len()
Expand Down Expand Up @@ -3940,7 +3950,7 @@ fn handle(mut w: TcpStream, q: anyhow::Result<serve::Request>, l: Option<&mut Lo
let system = tpl.render(&req.messages[..n_sys], &req.tools, req.think.unwrap_or(e.think_default), preserve, false);
let images: Vec<String> = req.messages.iter().flat_map(|m| m.images.iter().cloned()).collect();
let image_input = if images.is_empty() { None } else {
let mmproj = e.mmproj.clone().or_else(|| vision::find_sibling(&l.path));
let mmproj = e.mmproj_for(&l.path);
let mmproj = mmproj.as_deref().context("image input needs --mmproj")?;
Some(load_images(mmproj, &images)?)
};
Expand Down
Loading