Skip to content

fix(mlx): map per-layer-type RoPE onto mlx-lm's local/global pair (#4678) - #4684

Merged
Qubitium merged 1 commit into
mainfrom
fix/mlx-4678-gemma3-rope
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/mlx-4678-gemma3-rope

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4678

TL;DR

#4617 refused any per-layer-type rope_parameters split because the flat rope_theta / rope_scaling pair cannot express one. Every Gemma 3 checkpoint is exactly that layout, so GPTQModel.export(..., "mlx") and backend=MLX loading of the whole family raised before writing a single weight. mlx-lm does represent the split — through rope_theta (global) plus rope_local_base_freq (sliding) — so the translation now maps onto those keys per architecture. Net: +90/−10 in gptqmodel/utils/mlx_rope.py, +8 tests.

1. Root cause

normalize_mlx_rope_config runs before every MLX export, requant and backend=MLX load (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_type rope_parameters mlx-lm reads
gemma3_text sliding_attention 1e4, full_attention 1e6 (4B+ adds linear ×8) rope_theta + rope_local_base_freq + rope_scaling
gemma3n_text same split same three keys
gemma4_text per-layer split with partial_rotary_factor rope_parameters[layer_key] natively

So the first two need a mapping, and gemma4_text needed to join the native-passthrough set — it was being refused for the same reason.

2. Mapping

Old behaviour New behaviour
gemma3_text / gemma3n_text split → ValueError rope_theta = global theta, rope_scaling = global minus theta, rope_local_base_freq = sliding theta
gemma4_text split → ValueError untouched; mlx-lm selects rope_parameters[layer_key] itself
uniform split on any architecture still collapses to the flat pair (unchanged)
conflicting split on an architecture with no local/global pair still ValueError (unchanged)
sliding-attention scaling ValueError — mlx-lm builds those layers from a bare rope_local_base_freq, so the scaling has no destination
unknown layer-type key for a mapped architecture ValueError naming the key

rope_parameters is always kept, so readers that select by layer type still see the original nested form. VLM wrappers (gemma3, gemma3n, gemma4) are handled by the existing text_config recursion.

3. Before / after (behavioral)

mlx_lm.models.gemma3_text.ModelArgs.from_dict is the consumer; the test asserts against the real class when mlx-lm is installed:

gemma3-4b raw transformers-5 config -> rope_scaling None                       # global factor 8 lost
gemma3-4b translated               -> rope_theta 1e6, rope_local_base_freq 1e4,
                                      rope_scaling {'rope_type': 'linear', 'factor': 8.0}
gemma3-1b translated               -> rope_theta 1e6, rope_local_base_freq 1e4, no scaling

Before this PR both of the translated lines raise ValueError.

4. Tests

tests/test_mlx_rope_parameters_4607.py:

Test Invariant
test_gemma3_per_layer_rope_maps_to_the_local_global_pair real Gemma3TextConfig (1B): both bases land, no invented scaling, nested form kept, source not mutated
test_gemma3_global_linear_scaling_lands_on_rope_scaling_only 4B+ global linear ×8 reaches rope_scaling and not the local base
test_gemma3n_text_config_maps_the_local_base gemma3n VLM wrapper through text_config
test_gemma4_text_keeps_the_native_per_layer_form native passthrough: no lossy flat pair invented
test_sliding_attention_scaling_is_rejected local scaling refuses instead of being dropped
test_unknown_per_layer_rope_types_are_rejected unmapped layer-type key refuses
test_mlx_lm_gemma3_consumes_the_translated_split consumer-level ModelArgs contract
test_installed_mlx_lm_gemma3_args_read_the_translation same assertion against the real mlx_lm.models.gemma3_text.ModelArgs

The two tests that used gemma3_text to pin the rejection now use a model type with no local/global pair, which is what the refusal is actually for.

5. Validation

pytest tests/test_mlx_rope_parameters_4607.py -q
-> 19 passed

pytest tests/test_export_contract_4386.py tests/test_mlx_method_selection.py \
       tests/test_mlx_rope_parameters_4607.py -q
-> 46 passed, 1 skipped

ruff check gptqmodel/utils/mlx_rope.py tests/test_mlx_rope_parameters_4607.py
-> All checks passed!

Red evidence: with the pre-fix mlx_rope.py restored (only that file reverted, tests unchanged), pytest tests/test_mlx_rope_parameters_4607.py -q → 8 failed, 11 passed, each failure the per-layer-type ValueError from _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

  • The refusal is deliberately narrowed, not removed: it still covers a local scaling config and any layer-type key a mapping does not cover, which are the cases mlx-lm genuinely cannot represent.
  • gemma4_text is 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_type factors out the existing "drop rope_type: default" rule so the split path and the flat path agree.

)

#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant