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 the conversion backfill's donor stub off the transformers package (#12034)

Daniel Han96b63165fe38
diff --git a/tests/test_peft_symbol_backfill.py b/tests/test_peft_symbol_backfill.py
index ef48bd0f969..2530cfe4186 100644
--- a/tests/test_peft_symbol_backfill.py
+++ b/tests/test_peft_symbol_backfill.py
@@ -40,12 +40,22 @@ backfill = FIXES._backfill_missing_peft_symbols
@pytest.fixture(autouse = True)
def _restore_modules():
saved = {k: sys.modules.get(k) for k in (CONV, CORE)}
+ # An import also binds the submodule on its package, and `import a.b as m` reads it
+ # from there, so a stale binding outlives the sys.modules restore below.
+ package = sys.modules.get("transformers")
+ bound = dict(vars(package)) if package is not None else {}
yield
for k, v in saved.items():
if v is None:
sys.modules.pop(k, None)
else:
sys.modules[k] = v
+ if package is not None:
+ for child in (CONV.rpartition(".")[2], CORE.rpartition(".")[2]):
+ if child in bound:
+ setattr(package, child, bound[child])
+ else:
+ vars(package).pop(child, None)
def _fake_real_module(name, **attrs):
@@ -114,10 +124,11 @@ def test_core_model_loading_classes_are_subclassable():
assert issubclass(_Child, mod.ConversionOps)
-def test_missing_module_returns_empty():
- sys.modules.pop(CONV, None)
- # No real transformers submodule of this name under the test alias.
- assert backfill(CONV) == () or isinstance(backfill(CONV), tuple)
+def test_missing_module_returns_empty(monkeypatch):
+ # A None entry makes the import raise, which is what an absent submodule does, without
+ # executing the real module a second time as a pop-and-reimport would.
+ monkeypatch.setitem(sys.modules, CONV, None)
+ assert backfill(CONV) == ()
def test_required_symbols_match_peft_import_list():
diff --git a/tests/version_compat/test_peft_conversion_symbol_backfill.py b/tests/version_compat/test_peft_conversion_symbol_backfill.py
index 62a30fcffe5..94b4cf7ed9e 100644
--- a/tests/version_compat/test_peft_conversion_symbol_backfill.py
+++ b/tests/version_compat/test_peft_conversion_symbol_backfill.py
@@ -69,6 +69,28 @@ def test_the_missing_names_are_added(fake_modules):
assert hasattr(fake_modules[name], symbol), f"{name}.{symbol}"
+def test_the_transformers_package_keeps_no_stub(fake_modules):
+ """The donor stub must not reach the package either.
+
+ `import transformers.conversion_mapping as m` resolves through the package attribute,
+ not sys.modules, so a stub attached there outlives the real module going back into
+ sys.modules and answers every later conversion lookup with None.
+ """
+ parent = types.ModuleType("transformers")
+ saved = sys.modules.get("transformers")
+ sys.modules["transformers"] = parent
+ try:
+ F._backfill_missing_conversion_symbols()
+ finally:
+ if saved is None:
+ sys.modules.pop("transformers", None)
+ else:
+ sys.modules["transformers"] = saved
+ for name in F._PEFT_CONVERSION_SYMBOLS:
+ attached = vars(parent).get(name.rpartition(".")[2])
+ assert attached is None, f"{name}: {getattr(attached, '__file__', attached)}"
+
+
def test_the_real_module_is_not_replaced(fake_modules):
before = {n: m for n, m in fake_modules.items()}
F._backfill_missing_conversion_symbols()
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index ddb0162deac..974245b3903 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -7657,14 +7657,7 @@ def _backfill_conversion_symbols_once(builders, added):
continue
# Build the stub off to the side rather than installing it, so the real module keeps its
# identity and everything else it exports.
- saved = sys.modules.pop(name, None)
- try:
- donor = builders[name]()
- finally:
- if saved is not None:
- sys.modules[name] = saved
- else:
- sys.modules.pop(name, None)
+ donor = builders[name]()
for symbol in missing:
qualified = f"{name}.{symbol}"
if symbol == "_MODEL_TO_CONVERSION_PATTERN":
@@ -7711,9 +7704,12 @@ def _backfill_missing_conversion_symbols():
Never replaces a module and never overwrites a name transformers defines,
so this is a no-op on every release that still exports them.
"""
+ # The _build_ variants, not _install_: installing also attaches the stub to the transformers
+ # package, where `import transformers.conversion_mapping as m` would keep finding it after the
+ # real module is back in sys.modules.
builders = {
- "transformers.conversion_mapping": _install_transformers_conversion_mapping_stub,
- "transformers.core_model_loading": _install_transformers_core_model_loading_stub,
+ "transformers.conversion_mapping": _build_transformers_conversion_mapping_stub,
+ "transformers.core_model_loading": _build_transformers_core_model_loading_stub,
}
added = []
# One pass is not enough when the drifts coincide: conversion_mapping imports names from