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.

Fix Gemma 4 E2B / E4B DPO CheckpointError after the ref adapter switch (#14705)

Daniel Han88041a6bdfff
diff --git a/tests/test_peft_shared_active_adapter.py b/tests/test_peft_shared_active_adapter.py
new file mode 100644
index 00000000000..49a9389a78a
--- /dev/null
+++ b/tests/test_peft_shared_active_adapter.py
@@ -0,0 +1,129 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""TRL DPO's ref / default adapter round trip must not invalidate compiled LoRA graphs (Gemma 4 E2B DPO)."""
+
+import ast
+import functools
+import sys
+from pathlib import Path
+
+import pytest
+
+torch = pytest.importorskip("torch")
+peft = pytest.importorskip("peft")
+from peft import LoraConfig, get_peft_model # noqa: E402
+import peft.tuners.tuners_utils as tu # noqa: E402
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+
+
+def _load_patch():
+ src = (REPO_ROOT / "unsloth" / "import_fixes.py").read_text(encoding = "utf-8")
+ ns = {"functools": functools, "sys": sys}
+ for node in ast.parse(src).body:
+ if isinstance(node, ast.FunctionDef) and node.name == "patch_peft_shared_active_adapter":
+ exec(ast.get_source_segment(src, node), ns)
+ return ns["patch_peft_shared_active_adapter"]
+
+
+def _pristine_set_adapter():
+ # conftest imports unsloth, which may already have wrapped it.
+ fn = tu.BaseTunerLayer.set_adapter
+ while getattr(fn, "_unsloth_shared_active_adapter", False):
+ fn = fn.__wrapped__
+ return fn
+
+
+def unpatched(monkeypatch):
+ monkeypatch.setattr(tu.BaseTunerLayer, "set_adapter", _pristine_set_adapter())
+ yield
+
+
+def patched(unpatched):
+ _load_patch()()
+ yield
+
+
+class _Block(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.q_proj = torch.nn.Linear(16, 16, bias = False)
+ self.up_proj = torch.nn.Linear(16, 16, bias = False)
+
+ def forward(self, x):
+ return self.up_proj(torch.relu(self.q_proj(x)))
+
+
+def _lora_model():
+ torch.manual_seed(0)
+ model = get_peft_model(_Block(), LoraConfig(r = 4, target_modules = ["q_proj", "up_proj"]))
+ model.add_adapter("ref", LoraConfig(r = 4, target_modules = ["q_proj", "up_proj"]))
+ return model
+
+
+def _lora_layers(model):
+ return [m for m in model.modules() if isinstance(m, tu.BaseTunerLayer)]
+
+
+def _round_trip(model):
+ # trl.trainer.utils.use_adapter(model, "ref").
+ model.set_adapter("ref")
+ model.set_adapter("default")
+
+
+def _recompiles_across_round_trip(model):
+ from torch._dynamo.testing import CompileCounter
+
+ torch._dynamo.reset()
+ counter = CompileCounter()
+ block = model.base_model.model
+ compiled = torch.compile(block.forward, backend = counter)
+ x = torch.randn(2, 16)
+ compiled(x)
+ first = counter.frame_count
+ _round_trip(model)
+ compiled(x)
+ return counter.frame_count - first
+
+
+def test_round_trip_keeps_one_shared_active_adapter(patched):
+ model = _lora_model()
+ _round_trip(model)
+ layers = _lora_layers(model)
+ assert len({id(layer._active_adapter) for layer in layers}) == 1
+ for layer in layers:
+ assert layer.active_adapters == ["default"]
+ assert layer.active_adapter == "default"
+
+
+def test_round_trip_keeps_peft_trainability(patched):
+ model = _lora_model()
+ _round_trip(model)
+ for layer in _lora_layers(model):
+ assert all(p.requires_grad for p in layer.lora_A["default"].parameters())
+ assert not any(p.requires_grad for p in layer.lora_A["ref"].parameters())
+
+
+def test_multi_adapter_selection_is_left_alone(patched):
+ model = _lora_model()
+ layer = _lora_layers(model)[0]
+ layer.set_adapter(["default", "ref"])
+ assert layer.active_adapters == ["default", "ref"]
+
+
+def test_patch_is_idempotent(patched):
+ first = tu.BaseTunerLayer.set_adapter
+ _load_patch()()
+ assert tu.BaseTunerLayer.set_adapter is first
+
+
+def test_compiled_lora_forward_does_not_recompile_after_round_trip(patched):
+ assert _recompiles_across_round_trip(_lora_model()) == 0
+
+
+def test_unpatched_peft_recompiles_after_round_trip(unpatched):
+ if _recompiles_across_round_trip(_lora_model()) == 0:
+ pytest.skip("this peft / torch no longer recompiles on the adapter round trip")
diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py
index 3879fc6316c..818610ed5ec 100644
--- a/unsloth/_gpu_init.py
+++ b/unsloth/_gpu_init.py
@@ -399,6 +399,7 @@ from .import_fixes import (
fix_peft_transformers_weight_conversion_import,
patch_peft_weight_converter_compatibility,
patch_peft_float8_adapter_upcast,
+ patch_peft_shared_active_adapter,
fix_peft_stale_torchao_import_error,
fix_peft_torchao_missing_tensor_subclass,
patch_accelerate_recursively_apply,
@@ -500,6 +501,7 @@ fix_peft_transformers_tensor_parallel_import_compat()
fix_peft_transformers_weight_conversion_import()
patch_peft_weight_converter_compatibility()
patch_peft_float8_adapter_upcast()
+patch_peft_shared_active_adapter()
# After peft is importable, so the already-bound is_torchao_available in peft.tuners.lora.torchao is
# replaced too, not just import_utils'.
fix_peft_stale_torchao_import_error()
@@ -557,6 +559,7 @@ del fix_peft_transformers_tensor_parallel_import_compat
del fix_peft_transformers_weight_conversion_import
del patch_peft_weight_converter_compatibility
del patch_peft_float8_adapter_upcast
+del patch_peft_shared_active_adapter
del fix_peft_stale_torchao_import_error
del fix_peft_torchao_missing_tensor_subclass
del patch_accelerate_recursively_apply
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index a8fe7751b16..f4d667f655f 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -9543,6 +9543,29 @@ def patch_peft_float8_adapter_upcast():
tu.cast_adapter_dtype = cast_adapter_dtype
+def patch_peft_shared_active_adapter():
+ """Compiled layers guard on peft's shared active-adapter str; TRL DPO's ref switch broke Gemma 4 E2B recompute."""
+ try:
+ import peft.tuners.tuners_utils as tu
+ except Exception:
+ return
+ layer_cls = getattr(tu, "BaseTunerLayer", None)
+ original = getattr(layer_cls, "set_adapter", None)
+ if original is None or getattr(original, "_unsloth_shared_active_adapter", False):
+ return
+
+ @functools.wraps(original)
+ def set_adapter(self, adapter_names, *args, **kwargs):
+ out = original(self, adapter_names, *args, **kwargs)
+ active = self.__dict__.get("_active_adapter")
+ if type(active) is list and len(active) == 1 and type(active[0]) is str:
+ self._active_adapter = sys.intern(active[0])
+ return out
+
+ set_adapter._unsloth_shared_active_adapter = True
+ layer_cls.set_adapter = set_adapter
+
+
def patch_peft_weight_converter_compatibility():
"""Allow PEFT converter rebuilds on legacy converter constructors."""
try: