Fix Qwen35 VLM crash on text-only inference#149
Open
dirvine wants to merge 1 commit intoml-explore:mainfrom
Open
Fix Qwen35 VLM crash on text-only inference#149dirvine wants to merge 1 commit intoml-explore:mainfrom
dirvine wants to merge 1 commit intoml-explore:mainfrom
Conversation
Qwen35Language.LanguageModel.callAsFunction assumes inputs is always 2D [batch, seq], but text-only callers like WiredMemoryUtils.tune and TokenIterator can pass 1D [seq] token arrays. This causes getRopeIndex() and subsequent dim(1) calls to crash with "SmallVector out of range" when accessing a non-existent dimension. Add an ndim check at the top of callAsFunction to expand 1D inputs to 2D before any dimension-dependent logic runs. Fixes ml-explore#148
davidkoski
reviewed
Mar 19, 2026
| videoGridTHW: [THW]? = nil | ||
| ) -> LMOutput { | ||
| // Ensure inputs is 2D [batch, seq]. Text-only callers (e.g. | ||
| // WiredMemoryUtils, TokenIterator) may pass 1D token arrays. |
Collaborator
There was a problem hiding this comment.
I wonder if that is incorrect (as in this is the bug). I would expect the shape to always be [B, S].
So:
- are these paths buggy in that they allow 1d arrays?
- if not, perhaps the callers should ensure the correct shape -- otherwise this is a one-off fix for a single model
davidkoski
requested changes
Mar 27, 2026
Collaborator
davidkoski
left a comment
There was a problem hiding this comment.
Please look at my comment on the shape -- let me know what you think. I suspect it is a caller issue, but not 100% sure.
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.
Problem
Loading a Qwen3.5 MoE model (e.g.
mlx-community/Qwen3.5-35B-A3B-4bit) viaVLMModelFactoryworks fine — the containr loads successfully. But the first text-only inference call crashes with:The same model loads and runs without issue through
LLMModelFactory.Root Cause
Qwen35Language.LanguageModel.callAsFunctionpassesinputsdirectly toQwen3VLLanguage.getRopeIndex(), which does:When the VLM container is used for text-only generation (no images), callers like
WiredMemoryUtils.tune()andTokenIteratorpass 1D[seq]token tensors.dim(1)on a 1D array is out of range → fatal crash.The same issue affects
inputs.dim(1)at lines 946 and 966 in the same function.Fix
Add an
ndimcheck at the top ofcallAsFunctionto expand 1D inputs to 2D[1, seq]before any dimension-dependent logic runs. This matches what the LLM path does viatokens[text: .newAxis]inprefillOnly.Testing
Verified locally with
mlx-community/Qwen3.5-35B-A3B-4biton M4 Max 96GB:VLMModelFactory✅WiredMemoryUtils.tune()completes (was crashing) ✅Fixes #148