Repository navigation
fix(gemma3-vlm): cast the embedding scale to the embedding dtype - #226
Merged
Merged
Conversation
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>
This was referenced Oct 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Fixes #204
Defect.
Libraries/MLXVLM/Models/Gemma3.swift:333-334(main 0871943). The text model casts the scalesqrt(hidden_size)toinputs?.dtype ?? h.dtype. With token IDs,inputsis int32, so the scale isint32(5.66) = 5for hidden size 32. With embeddings (the prompt with the image),inputsis 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"VisionCaseof PR #186 without itswithKnownIssue. 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 areprivateto 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
knownIssuesof the "Gemma3" case inTests/MLXLMTests/Kernel/Vision/VisionModelForwardPassTests.swift@924cb8c7:295-302. The pull request that merges second (this one or #186) must remove the namedknownIssuesin the same change. A known issue that is not intermittent and no longer fails is reported as a failure.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (I ranswift formaton the changed Swift files only.)fork.yaml)🤖 Generated with Claude Code