Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep DeepseekV4 per-label rope bases when rope_scaling is replaced on transformers 5.21 (#15425)

Daniel Han388e0adc6723
diff --git a/tests/test_import_fixes_drift.py b/tests/test_import_fixes_drift.py
index ca5525b5a92..8854192e82a 100644
--- a/tests/test_import_fixes_drift.py
+++ b/tests/test_import_fixes_drift.py
@@ -1279,6 +1279,55 @@ def test_rope_theta_carry_follows_rope_type_labels_not_only_layer_types():
assert flat.rope_parameters["rope_theta"] == 10000.0
+def test_rope_carry_overrides_a_global_base_the_setter_filled_into_a_nested_entry():
+ """transformers 5.21.0's ``rope_scaling`` setter runs ``standardize_rope_params``, which
+ ``setdefault``s the global ``rope_theta`` into every nested entry before the carry sees it.
+ A label the caller gave no base still gets its own carried base, not the global one."""
+ from types import SimpleNamespace
+
+ from unsloth.import_fixes import (
+ _carry_rope_theta_across_assignment as carry,
+ _rope_labels_naming_no_base,
+ _rope_theta_snapshot,
+ )
+
+ config = SimpleNamespace(
+ _rope_type_labels = DEEPSEEK_V4_LABELS,
+ rope_theta = 10000.0,
+ rope_parameters = {k: dict(v) for k, v in DEEPSEEK_V4_ROPE.items()},
+ )
+ carried = _rope_theta_snapshot(config)
+ value = {
+ "main": {"rope_type": "linear", "factor": 4.0},
+ "compress": {"rope_type": "default"},
+ "unknown": {"rope_type": "default"},
+ }
+ unnamed = _rope_labels_naming_no_base(config, value)
+ assert unnamed == {"main", "compress", "unknown"}
+ # What the 5.21.0 setter leaves behind: the global base defaulted into each entry.
+ config.rope_parameters = {k: dict(v, rope_theta = 10000.0) for k, v in value.items()}
+ carry(config, carried, unnamed)
+ restored = config.rope_parameters
+ assert restored["compress"]["rope_theta"] == 160000.0, f"global base kept: {restored!r}"
+ assert restored["main"]["rope_theta"] == 10000.0
+ assert restored["main"]["factor"] == 4.0
+ assert "rope_theta" not in restored
+
+ # A base the caller stated is never overridden.
+ stated = {
+ "main": {"rope_type": "default"},
+ "compress": {"rope_type": "default", "rope_theta": 5.0},
+ }
+ unnamed = _rope_labels_naming_no_base(config, stated)
+ assert unnamed == {"main"}
+ config.rope_parameters = {k: dict(v) for k, v in stated.items()}
+ config.rope_parameters["main"]["rope_theta"] = 10000.0
+ carry(config, carried, unnamed)
+ assert config.rope_parameters["compress"]["rope_theta"] == 5.0
+ # A flat incoming dict is not per-label, so nothing is reported.
+ assert _rope_labels_naming_no_base(config, {"rope_type": "default"}) is None
+
+
def test_rope_carry_handles_rope_labels_not_all_present_in_layer_types():
"""transformers asks ``isdisjoint``, not ``issubset``: laguna, mellum and zaya ship a rope
dict naming a label their default ``layer_types`` omits. Under the subset test all their
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index c8cc6fed172..cf79fb6df48 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -2915,23 +2915,48 @@ def _rope_theta_snapshot(config):
return snapshot or None
-def _carry_per_layer_rope_theta(config, parameters, carried):
+def _rope_labels_naming_no_base(config, value):
+ """Labels of a NESTED incoming ``rope_scaling`` whose entry states no ``rope_theta``.
+
+ Read before the upstream setter runs: from transformers 5.21.0 that setter calls
+ ``standardize_rope_params``, which ``setdefault``s the ONE global ``rope_theta`` into
+ every nested entry in place, so afterwards a filled-in default looks caller-stated.
+ """
+ if not isinstance(value, dict) or not _rope_parameters_are_per_layer(config, value):
+ return None
+ return frozenset(
+ label
+ for label, entry in value.items()
+ if isinstance(entry, dict) and entry.get("rope_theta", None) is None
+ )
+
+
+def _carry_per_layer_rope_theta(
+ config,
+ parameters,
+ carried,
+ unnamed = None,
+):
"""Put each layer type's base back into a per-layer replacement that lost it.
A top-level ``rope_theta`` would make the dict read as flat, so the base goes back
into each nested entry. Copies throughout, never the caller's dicts in place. An
- entry naming its own base is left alone. ``None`` means nothing was written -- no nested
- snapshot, OR every entry already named its base, OR refused; never "fall through as scalar".
+ entry naming its own base is left alone; ``unnamed`` lists the labels the caller gave
+ no base, so a global default transformers filled in there is not mistaken for one.
+ ``None`` means nothing was written -- no nested snapshot, OR every entry already named
+ its base, OR refused; never "fall through as scalar".
"""
if not isinstance(carried, dict) or not carried:
return None
+ unnamed = unnamed or ()
replacement = {}
changed = False
for layer_type, entry in parameters.items():
if (
isinstance(entry, dict)
- and entry.get("rope_theta", None) is None
+ and (entry.get("rope_theta", None) is None or layer_type in unnamed)
and carried.get(layer_type, None) is not None
+ and entry.get("rope_theta", None) != carried[layer_type]
):
entry = dict(entry)
entry["rope_theta"] = carried[layer_type]
@@ -2946,7 +2971,11 @@ def _carry_per_layer_rope_theta(config, parameters, carried):
return carried
-def _carry_rope_theta_across_assignment(config, carried):
+def _carry_rope_theta_across_assignment(
+ config,
+ carried,
+ unnamed = None,
+):
"""Keep the RoPE base frequency across a ``rope_scaling`` replacement.
Separated from the wrapper so every parameter shape is testable without a live
@@ -2972,7 +3001,7 @@ def _carry_rope_theta_across_assignment(config, carried):
per_layer = _rope_parameters_are_per_layer(config, parameters)
if per_layer:
# Per-layer rope bases are restored from the nested snapshot; the global attribute is used only when none exists.
- restored = _carry_per_layer_rope_theta(config, parameters, carried)
+ restored = _carry_per_layer_rope_theta(config, parameters, carried, unnamed)
if restored is not None:
return restored
if isinstance(carried, dict):
@@ -3815,9 +3844,13 @@ def fix_transformers_rope_scaling_drops_theta():
carried = _rope_theta_snapshot(self)
except Exception:
carried = None
+ try:
+ unnamed = _rope_labels_naming_no_base(self, value)
+ except Exception:
+ unnamed = None
result = original(self, value)
try:
- _carry_rope_theta_across_assignment(self, carried)
+ _carry_rope_theta_across_assignment(self, carried, unnamed)
except Exception:
pass
return result