Repository navigation
MLXVLM Qwen35: apply YaRN from rope_parameters on the VLM rotary - #761
Merged
Merged
Conversation
The VLM rotary built plain M-RoPE and ignored rope_parameters, so a Qwen3.5 checkpoint with rope_type "yarn" ran with default frequencies and no attention factor. MLXLLM Qwen35 already applies YaRN through initializeRope. RotaryEmbedding now computes inv_freq and the attention factor like HF `_compute_yarn_parameters` when the type is "yarn", and scales cos and sin by that factor. This scales only the rotary slice of q and k, as HF does. Other rope types take the old path unchanged. A missing original_max_position_embeddings falls back to max_position_embeddings, as HF does. On sensenova/SenseNova-6.8-Code2Video (factor 2, original 262144), YaRN changes 17 of 32 rotary frequencies and the factor is 1.0693. 8-bit text-position KL against HF bf16 over a 988-token prompt drops from 0.051 to 0.016. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xocialize
force-pushed
the
qwen35-vlm-yarn-main
branch
from
November 3, 2026 20:04
5bcc155 to
78a314a
Compare
davidkoski
approved these changes
Nov 6, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good, thank you!
wballard
added a commit
to swissarmyhammer/mlx-swift-lm-old
that referenced
this pull request
Nov 14, 2026
Apply the upstream commits 22bdf2c..6352907 (the 15 after c8dabc6) as cherry-picks. CLAUDE.md is untouched. Where a conflict was only a fork refactor, upstream's code is taken; the fork's MTP speed and prompt-cache behavior is kept. - 22bdf2c Decode surrogate pairs and the remaining Python escapes (ml-explore#769) - 0877a36 Fix model weight loading progress (ml-explore#551) - 0dabaf7 Add production-ready LFM2.5 model support (ml-explore#528) - 173c163 Add mid-turn steering to ChatSession (ml-explore#608) - 37424a5 Stop image prompts from running VLM language models in float32 (ml-explore#766) - c48e55e Add LiquidAI LFM3 model support (ml-explore#750) - 4ab2679 MLXVLM Qwen35: apply YaRN from rope_parameters on the VLM rotary (ml-explore#761) - 6e3fe5d Report token log probabilities during speculative decoding (ml-explore#774) - ec4c966 Batch greedy MTP verification into one GPU readback per round (ml-explore#724) - c9a9a3d Skip the checkpoint components a model does not have (ml-explore#711) - a8429d0 Measure the memory of an EmbeddingGemma 2 embedding (ml-explore#787) - 7b0e790 Parse triple-quoted strings in PythonLiteralParser (ml-explore#786) - 4c91662 Fix the flaky usage test on the tool-calling path (ml-explore#784) - d7b94ee Add ModelTypeRegistry.contains(_:configuration:) for shared model types (ml-explore#778) - 6352907 Use MLXLogger for the model component warning (ml-explore#790) Conflict resolutions: - ml-explore#551 load progress: `LoadProgressHandlers` and `progress:` are threaded through the fork's async `loadWeights`/`installWeights`. The progress-aware `MLXLanguageModel.init(...progress:load:)` and the `#huggingFaceLanguageModel` macro also take the fork's `speculativeDecoding:`. The fork's tests and benchmark helpers move off the now-deprecated download-only loader. - ml-explore#528 LFM2.5: `RewindableConvolutionCache` lives in ArraysCache.swift without upstream's extra `offset +=`, because the fork's `advance` moves `offset`. Speculative rewind and verify checks trim it like attention, and do not select a Mamba snapshot slot for it. It is also `PromptCacheRestorable`, so a saved LFM2 prompt cache restores into the model's template. `mtpTargetLayerIdsKey` is added to the fork's MTP exchange. `shouldSortExpertRoutes` drives the fork's shared switch projection. The fork's in-place verify (`verifiesInPlace`) and its greedy-only passthrough string are kept. The upstream tests that expect a wrapped sliding window to refuse rewind now expect the fork's behavior: the window rewinds the verify write and keeps speculating. - ml-explore#608 steering: the steering boundary goes into the fork's `GenerationLoop.runTokenLoop`, and `Generation.steering` and `GenerateStopReason.steered` go into GenerationResults.swift. The fork's generation worker keeps its own stream synchronization. - ml-explore#750 LFM3 is added to HybridRecurrentCacheOffsetTests. - ml-explore#711: the new test awaits the fork's async `loadWeights`. Dropped: - ml-explore#774 (speculative log probabilities): the fork's MTP iterator already reports log probabilities by decoding plainly when they are requested. Upstream's batched recording in the draft-model iterator does not fit the fork's `sampleToken` path, so `ChatSession.ObservationError` stays for draft-model speculation. - ml-explore#724 (batched greedy MTP verification): the fork already batches its greedy verify (`verifyDrafts`, MTPGreedyVerificationTests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Problem
Qwen35Language.RotaryEmbeddingbuilds plain M-RoPE and never reads the type inrope_parameters. A Qwen3.5 checkpoint withrope_type: "yarn"therefore runs on the VLM pathwith default frequencies and no attention factor. The text-only
MLXLLMQwen35 already applies YaRNthrough
initializeRope, so the same checkpoint behaves differently under the two factories.Example:
sensenova/SenseNova-6.8-Code2Video(Qwen3.5-27B,factor: 2,original_max_position_embeddings: 262144,partial_rotary_factor: 0.25). YaRN changes 17 of the32 rotary frequencies, and the attention factor is 1.0693. The error is present at every position,
including short contexts.
Change
"yarn",RotaryEmbeddingcomputesinv_freqand the attention factor like HF_compute_yarn_parameters(attention_factor,mscale/mscale_all_dim,beta_fast/beta_slow,truncate).original_max_position_embeddingsis missing it falls back tomax_position_embeddings,as HF does and as qwen3_5: apply YaRN from rope_parameters on the VLM rotary Neywa-Labs/mlx-vlm#2649 does for the Python model.
M-RoPE stays outside the compiled decode traces, so prefill and decode both take this path.
Tests
Qwen35VLMYarnTests:inv_freqand the attention factor match transformers 5.22.1 within 1e-5 relative."default"config produces bit-identical cos and sin.original_max_position_embeddingsusesmax_position_embeddingsin its place.Real weights, measured on this branch: the 8-bit build, a 988-token prompt with one image, compared
with HF bf16 on CPU. Image-placeholder positions are excluded. Text before the image:
The Python mlx-vlm build with the same fix scores 0.0135 (KL between the two: 0.004). On the 3.32.3
tag, a config without YaRN gave byte-identical logits before and after this change.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.