Skip to content

gemma4: CPU experts without an integer kernel (#115); --mmproj none (#111) - #117

Merged
Vasyl-Trefilov merged 2 commits into
mainfrom
fix-gemma4-int-dot
Nov 26, 2026
Merged

Vasyl-Trefilov merged 2 commits into
mainfrom
fix-gemma4-int-dot

Conversation

@Vasyl-Trefilov

Copy link
Copy Markdown
Member

Fixes #115 and #111.

  • gemma4: CPU experts of a type with no integer dot kernel (UD-Q8_K_XL) run through Mat::matvec instead of panicking on unwrap.
  • --mmproj none loads no projector, even with an mmproj*.gguf next to the model.

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.

…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
@Vasyl-Trefilov
Vasyl-Trefilov merged commit 9358adf into main Nov 26, 2026
2 checks passed
Comment thread src/gemma4/forward.rs
// 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];

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.

Comment thread src/gemma4/forward.rs
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.

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.

Comment thread src/main.rs
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.

Comment thread docs/VISION.md
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.

Comment thread src/gemma4/forward.rs
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.

Comment thread src/main.rs
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.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

What 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 --mmproj none disables the projector, while an omitted flag still auto-detects an mmproj*.gguf beside the model; this affects users running Gemma 4 on CPU with mixed-precision GGUF files and anyone using --image, serve, or the /v1/models image flag.

Merge confidence Review effort Merge risk
3 / 5 1 / 5 Medium
Area Files Lines
docs 1 +2 −1
src 1 +21 −11
src/gemma4 1 +13 −1

6 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness forward.rs:286 Fallback sums nothing by routing weight
2 🔵 Low Performance forward.rs:284 Fallback runs experts serially with fresh allocations per expert per token. For every expert and every token…
3 🔵 Low Maintainability main.rs:378 The string sentinel "none" is matched on a PathBuf, so a real file named none is silently ignored. A user…
4 🟡 Medium Correctness VISION.md:8 Doc promises the 'first' mmproj*.gguf, but directory order is unspecified. If a model directory holds several…
5 🟡 Medium Correctness forward.rs:283 Only cpu_sel[0] is checked for an integer kernel; other experts with different types are not covered. If a…
6 🔵 Low Maintainability main.rs:3807 No test covers the new mmproj resolution order or the new BF16 fallback branch. A future change to…
Review details

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

Fix all with your 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.

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.

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.

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.

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.

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.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

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

  • docs/VISION.md · modified · +2/-1
  • src/gemma4/forward.rs · modified · +13/-1 · 2 other dependents
  • src/main.rs · modified · +21/-11 · +1 function

Aggregate deltas

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

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.

Thread Panicked

1 participant