Skip to content

fix(gemma3-vlm): cast the embedding scale to the embedding dtype - #226

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/gemma3-vlm-embed-scale
Oct 2, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/gemma3-vlm-embed-scale

Conversation

@davidtai

@davidtai davidtai commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes #204

Defect. Libraries/MLXVLM/Models/Gemma3.swift:333-334 (main 0871943). The text model casts the scale sqrt(hidden_size) to inputs?.dtype ?? h.dtype. With token IDs, inputs is int32, so the scale is int32(5.66) = 5 for hidden size 32. With embeddings (the prompt with the image), inputs is nil and the scale is 5.66. A decode step after an image prompt differs from the same longer prompt.

Reference. mlx_vlm/models/gemma3/language.py:210 (Neywa-Labs/mlx-vlm@06ff63d): h *= mx.array(self.config.hidden_size**0.5, mx.bfloat16).astype(h.dtype).

Fix. The scale is cast to h.dtype.

Test. Tests/MLXLMTests/Gemma3VLMEmbeddingScaleTests.swift, decodeAfterThePromptMatchesALongerPrompt, copied from the "Gemma3" VisionCase of PR #186 without its withKnownIssue. Tiny model with seeded random weights (text hidden size 32, 6 layers; vision tower 1 layer) and a random 8 x 8 image that gives 4 image tokens. After the prompt with the image, one decode step with token 17 must give the logits of the last position of the same prompt with 17 appended, within 1e-4. The defect gave a difference near 0.68. This proves token IDs and embeddings get the same scale.

The test file has private copies of a few helpers from the kernel test support of PR #183 (Tests/MLXLMTests/Kernel/Support/SyntheticModel.swift): configuration decoding, seeded random weights, token rows, the logits call. They are private to the file, so they do not collide with #183 or with the other fix PRs.

Merge note. After this PR and #186 both merge, remove the knownIssues of the "Gemma3" case in Tests/MLXLMTests/Kernel/Vision/VisionModelForwardPassTests.swift@924cb8c7:295-302. The pull request that merges second (this one or #186) must remove the named knownIssues in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (I ran swift format on the changed Swift files only.)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed) (fork.yaml)

🤖 Generated with Claude Code

The text model cast sqrt(hidden_size) to the dtype of the token IDs, so
a decode step with int32 IDs scaled by 5, not 5.66. It now casts the
scale to h.dtype, as mlx-vlm gemma3/language.py does.

Fixes #204

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007
Gajesh2007 merged commit e59d41f into main Oct 2, 2026
2 checks passed
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.

Gemma3 (VLM): embedding scale is cast to the token id dtype, so decode scales by int32(5) instead of 5.66

2 participants