Repository navigation
gemma4: CPU experts without an integer kernel (#115); --mmproj none (#111) - #117
Conversation
…Mat::matvec (the decode FFN unwrapped int_dot and panicked on gemma-4-26B-A4B-it-UD-Q8_K_XL, #115). Checked on UD-Q4_K_XL with the integer path switched off and the experts spilled to the CPU: the fallback ran 334 times and the greedy reply is the same
…o it always loaded it, #111); every lookup goes through Engine::mmproj_for
| // An expert type with no integer kernel (BF16 experts of a UD-Q8_K_XL file): each expert through | ||
| // Mat::matvec, which takes every type. | ||
| return cpu_sel.iter().flat_map(|(g, d, _)| { | ||
| let mut gu = vec![0f32; 2 * nf]; |
There was a problem hiding this comment.
🟠 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.
| 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 | ||
| // Mat::matvec, which takes every type. |
There was a problem hiding this comment.
🔵 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.
| 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 { |
There was a problem hiding this comment.
🔵 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.
| 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. | ||
|
|
There was a problem hiding this comment.
🟡 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.
| 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 |
There was a problem hiding this comment.
🟡 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.
| 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, |
There was a problem hiding this comment.
🔵 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.
Aletheore Flash Lite reviewWhat this PR does. This pull request adds a fallback in the Gemma 4 CPU MoE path so that experts stored in a type with no integer kernel (such as BF16 experts in a UD-Q8_K_XL file) are computed through the generic matvec instead of panicking. It also changes multimodal projector selection so that
6 finding(s) posted as inline review comment(s).
Review detailsReviewed 3 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 6 of 6 proposed finding(s). Fix all with your agent |
🔍 Aletheore evidence diffgraph LR
n0["forward.rs"]
n1["mod.rs"]
n2["vision.rs"]
n0 --> n1
n0 --> n2
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
class n2 dependent
What changed
Aggregate deltas
|
Fixes #115 and #111.
Checked: Gemma 4 26B with the fallback forced on gives the same greedy output; --mmproj none skips the sibling projector, without it the sibling is still found.