Skip to content

fix(mtmd): guard clip d_head/kq_scale against n_head==0 (Gemma 4 12B SIGFPE)#166

Closed
TheTom wants to merge 1 commit into
feature/turboquant-kv-cachefrom
fix/gemma4-clip-fpe
Closed

fix(mtmd): guard clip d_head/kq_scale against n_head==0 (Gemma 4 12B SIGFPE)#166
TheTom wants to merge 1 commit into
feature/turboquant-kv-cachefrom
fix/gemma4-clip-fpe

Conversation

@TheTom
Copy link
Copy Markdown
Owner

@TheTom TheTom commented Jun 5, 2026

Summary

Fixes #163. Loading the Gemma 4 12B mmproj crashes with SIGFPE, Arithmetic exception at clip.cpp:250.

Root cause: Gemma 4 12B's vision tower has no attention encoder, so hparams.n_head == 0. The clip_graph constructor's init list does:

d_head(n_embd / n_head),                  // integer divide-by-zero -> SIGFPE
...
kq_scale(1.0f / sqrtf((float)d_head)),    // 1/sqrt(0) once d_head is fixed

Fix

Guard both, identical to the upstream fix for ggml-org#24085 (closed):

d_head(n_head > 0 ? n_embd / n_head : 0),
kq_scale(d_head > 0 ? 1.0f / sqrtf((float)d_head) : 0.0f),

When the tower has no encoder, d_head/kq_scale are unused, so 0 is safe.

Testing

  • mtmd target builds clean (Metal, clip.cpp.o compiles).
  • Matches the canonical upstream patch already merged in ggml-org/llama.cpp.
  • Did not repro locally (no Gemma 4 12B mmproj on hand); the guard is a strict superset of the crashing path.

Thanks to @guarismo for the clear report and backtrace.

…SIGFPE)

Gemma 4 12B's vision tower has no attention encoder, so hparams.n_head is 0.
The clip_graph constructor computed d_head(n_embd / n_head) and
kq_scale(1/sqrt(d_head)), causing a SIGFPE (integer divide-by-zero) at load
of the mmproj.

Guard both: d_head = n_head > 0 ? n_embd/n_head : 0, and kq_scale falls back
to 0.0f when d_head is 0. Mirrors the upstream fix for
ggml-org#24085.

Fixes #163.
@TheTom
Copy link
Copy Markdown
Owner Author

TheTom commented Jun 5, 2026

Superseded by #168. Testing on the actual gemma-4-12B revealed the fork was missing the whole gemma4uv projector (it errored unknown projector type: gemma4uv before ever reaching the d_head division). #168 ports the full upstream fix (ggml ggml-org#24077), which includes this d_head guard, and is verified end-to-end (loads the mmproj + describes an image correctly). Closing this in favor of that.

@TheTom TheTom closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: gemma-4-12B-it-GGUF crashes with Floating point exception

1 participant