Repository navigation
fix(mlx): translate transformers-5 rope_parameters into the keys mlx-lm reads (#4607) - #4617
Merged
Merged
Conversation
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
) (#4684) #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 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 #4607
TL;DR
transformers 5 saves RoPE as
rope_parametersand omits the flatrope_theta/rope_scalingpair that most mlx-lm architectures read, so every Llama/Qwen2/Gemma export or in-process MLX load silently ran withrope_theta=10000and no scaling. A new pure-Python translation (gptqmodel/utils/mlx_rope.py) is applied at both MLX config-construction sites, and it refuses or warns instead of silently defaulting. Net: +110 in the new module, +12/−5 inutils/mlx.py, 11 tests, one docs section.1. Root cause
_packed_mlx_weightsbuiltmlx_config = deepcopy(config)and the requant path passedconfigstraight to_get_classes/quantize_model.auto.py:1255then writes that dict as the exportedconfig.json. transformers 5to_dict()produces:{"rope_parameters": {"rope_type": "llama3", "factor": 32.0, ..., "rope_theta": 500000.0}}with no top-level
rope_theta/rope_scaling. mlx-lm'sllama,qwen2,gemma2,gemma3_text,phi3,olmo2andgraniteargs only read the flat pair, so the key is dropped and theirrope_theta=10000.0default applies.mlx_config = deepcopy(config)(utils/mlx.py)mlx_config = _mlx_model_config(config)— deep copy + translationconfigto_get_classes/quantize_modelmlx_configrope_parametersforwarded untouchedrope_thetaand non-default scaling copied to the flat keys; the nested dict is keptValueErrorotherwiserope_parameterswithoutrope_thetaqwen3is left untouched because its mlx-lmModelArgsreads the nested form directly.2. Mapping
rope_parameters)rope_thetarope_thetarope_type!=default, plusfactor,high_freq_factor,low_freq_factor,original_max_position_embeddings,partial_rotary_factorrope_scalingrope_type: defaultrope_scalingfull_attention/sliding_attention)ValueErrorwhen they differrope_parametersNested
text_config(Gemma3 VL wrappers) is normalized too.3. Before / after (behavioral)
The test file models mlx-lm's
ModelArgs.from_dictcontract — unknown keys filtered,rope_thetadefault 10000,rope_scalingdefault None:4. Tests
tests/test_mlx_rope_parameters_4607.py(Linux, no MLX needed — the translation is pure config manipulation):test_llama3_rope_theta_and_scaling_reach_the_flat_mlx_keystest_qwen2_keeps_its_non_default_thetatest_gemma2_theta_is_the_configured_value_not_a_fallbacktest_gemma3_text_config_is_normalized_tootext_confighandledtest_uniform_per_layer_rope_parameters_collapse/test_conflicting_..._are_rejectedtest_missing_theta_warns_instead_of_silently_defaultingtest_qwen3_keeps_the_native_nested_formtest_normalization_is_idempotent_and_serializabletest_mlx_lm_consumes_the_translated_configtest_both_mlx_config_builders_use_the_translation5. Validation
What was NOT executed: no MLX/Metal runtime or Apple hardware here (
mlx/mlx_lmare not installed), somlx_lm.load(...)andm.args.rope_thetawere not run, and no exported model was executed. The tests pin the config translation and the consumer contract; they do not prove MLX numerics.Detection evidence, stated plainly: with the new module absent the test file cannot be collected, which is not valid red evidence. With only
utils/mlx.pyreverted (module present, call sites not wired)test_both_mlx_config_builders_use_the_translationfails — that is wiring evidence, not behavioral. The behavioral defect is demonstrated by the raw-config assertion intest_mlx_lm_consumes_the_translated_config(raw transformers-5 config → 10000/None), which holds before and after by design.6. Apple-device verification
On an Apple Silicon host:
Expected:
config: 500000.0 {'rope_type': 'llama3', 'factor': 32.0, ...}andmlx args: 500000.0 {...}. Before this fix the same script printed10000.0 None. Repeat for a Qwen2 checkpoint (theta 1000000) and a Gemma2/Gemma3 checkpoint (its configured theta, no scaling).7. Reviewer notes
rope_parameters, so architectures that read the nested form keep working.qwen3is skipped entirely to avoid double-applying scaling.gptqmodel/utils/mlx.pystill cannot be imported without MLX (it usesmx.float16at module scope, unrelated to this change), which is why the translation lives in its own module and is tested without MLX.