Repository navigation
fix(mlx): map per-layer-type RoPE onto mlx-lm's local/global pair (#4678) - #4684
Merged
Merged
Conversation
) #4617 translated transformers-5 `rope_parameters` into the flat `rope_theta`/`rope_scaling` pair, but refused any per-layer-type split because that pair cannot express one. Every Gemma 3 checkpoint is exactly that layout (`sliding_attention` theta 10000, `full_attention` theta 1000000), so `GPTQModel.export(..., "mlx")` and `backend=MLX` loading raised before writing a single weight. mlx-lm does represent the split, just not through `rope_parameters`: `gemma3_text.ModelArgs` builds the global layers from `rope_theta` plus `rope_scaling` and the sliding layers from `rope_local_base_freq`. Add an explicit per-architecture table and map onto those keys, so the 4B+ models also keep their `full_attention` linear factor 8 instead of silently dropping it. Two neighbouring gaps in the same translation: - `gemma4_text` also emits per-layer-type `rope_parameters` in transformers 5 and was refused for the same reason. Its mlx-lm `ModelArgs` selects `rope_parameters[layer_key]` itself, so it joins the native-passthrough set. - The refusal stays where a mapping cannot represent the config: a sliding-attention scaling entry (mlx-lm builds those layers from a bare base) and layer-type keys a mapping does not cover. Tests (tests/test_mlx_rope_parameters_4607.py): - the `gemma3_text` fixture that pinned the rejection now pins the mapping, with Gemma 3 1B (no scaling) and 4B+ (global linear factor 8) cases; - `gemma3n` text_config and `gemma4_text` passthrough cases; - both refusal paths; - consumer-level assertions, including against the real `mlx_lm.models.gemma3_text.ModelArgs` when mlx-lm is installed. Red evidence with the pre-fix module restored: 8 failed, 11 passed. Green after the fix: 19 passed; with the export-contract and method-selection suites 46 passed, 1 skipped.
This was referenced Nov 17, 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.
Fixes #4678
TL;DR
#4617 refused any per-layer-type
rope_parameterssplit because the flatrope_theta/rope_scalingpair cannot express one. Every Gemma 3 checkpoint is exactly that layout, soGPTQModel.export(..., "mlx")andbackend=MLXloading of the whole family raised before writing a single weight. mlx-lm does represent the split — throughrope_theta(global) plusrope_local_base_freq(sliding) — so the translation now maps onto those keys per architecture. Net: +90/−10 ingptqmodel/utils/mlx_rope.py, +8 tests.1. Root cause
normalize_mlx_rope_configruns before every MLX export, requant andbackend=MLXload (utils/mlx.py:341,:624, via_mlx_model_config). For a per-layer-type mapping it called_flatten_rope_parameters, which collapses the dict only when every layer type agrees and otherwise raises. transformers 5 emits the split for exactly three text architectures:model_typerope_parametersgemma3_textsliding_attention1e4,full_attention1e6 (4B+ adds linear ×8)rope_theta+rope_local_base_freq+rope_scalinggemma3n_textgemma4_textpartial_rotary_factorrope_parameters[layer_key]nativelySo the first two need a mapping, and
gemma4_textneeded to join the native-passthrough set — it was being refused for the same reason.2. Mapping
gemma3_text/gemma3n_textsplit →ValueErrorrope_theta= global theta,rope_scaling= global minus theta,rope_local_base_freq= sliding thetagemma4_textsplit →ValueErrorrope_parameters[layer_key]itselfValueError(unchanged)ValueError— mlx-lm builds those layers from a barerope_local_base_freq, so the scaling has no destinationValueErrornaming the keyrope_parametersis always kept, so readers that select by layer type still see the original nested form. VLM wrappers (gemma3,gemma3n,gemma4) are handled by the existingtext_configrecursion.3. Before / after (behavioral)
mlx_lm.models.gemma3_text.ModelArgs.from_dictis the consumer; the test asserts against the real class when mlx-lm is installed:Before this PR both of the translated lines raise
ValueError.4. Tests
tests/test_mlx_rope_parameters_4607.py:test_gemma3_per_layer_rope_maps_to_the_local_global_pairGemma3TextConfig(1B): both bases land, no invented scaling, nested form kept, source not mutatedtest_gemma3_global_linear_scaling_lands_on_rope_scaling_onlyrope_scalingand not the local basetest_gemma3n_text_config_maps_the_local_basegemma3nVLM wrapper throughtext_configtest_gemma4_text_keeps_the_native_per_layer_formtest_sliding_attention_scaling_is_rejectedtest_unknown_per_layer_rope_types_are_rejectedtest_mlx_lm_gemma3_consumes_the_translated_splitModelArgscontracttest_installed_mlx_lm_gemma3_args_read_the_translationmlx_lm.models.gemma3_text.ModelArgsThe two tests that used
gemma3_textto pin the rejection now use a model type with no local/global pair, which is what the refusal is actually for.5. Validation
Red evidence: with the pre-fix
mlx_rope.pyrestored (only that file reverted, tests unchanged),pytest tests/test_mlx_rope_parameters_4607.py -q→ 8 failed, 11 passed, each failure the per-layer-typeValueErrorfrom_flatten_rope_parameters. That is the same exception the issue's repro raises, at the intended assertion.What was NOT executed: no real Gemma 3 checkpoint was exported or loaded, and no logits were compared. This PR pins the config translation and the consumer contract (
mlx_lm.models.gemma3_text.ModelArgs), not MLX numerics. A full-suite run is in progress; the config translation has no kernel impact.6. Reviewer notes
gemma4_textis a second instance of the same regression found while fixing this one; it is included because leaving it refused would be the same bug for the next family._drop_default_rope_typefactors out the existing "droprope_type: default" rule so the split path and the flat path agree.