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.

Load the compiled class when auto_model is a concrete class (#12757)

Daniel Han56a520c2319d
diff --git a/tests/test_compiled_auto_model.py b/tests/test_compiled_auto_model.py
new file mode 100644
index 00000000000..95bcbf655af
--- /dev/null
+++ b/tests/test_compiled_auto_model.py
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+"""A concrete `auto_model` class resolves to the compiler's replacement in its modeling module."""
+
+import sys
+import types
+
+import pytest
+
+from unsloth.models.loader import _compiled_auto_model
+
+
+class _Stock:
+ @classmethod
+ def from_pretrained(cls, *args, **kwargs):
+ return cls()
+
+
+def _compiled(monkeypatch, cls):
+ module = types.ModuleType("unsloth_compiled_module_fake")
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ setattr(module, cls.__name__, cls)
+ return cls
+
+
+def _fake_module(monkeypatch, cls):
+ module = types.ModuleType("_unsloth_test_modeling_fake")
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ monkeypatch.setattr(cls, "__module__", module.__name__)
+ setattr(module, cls.__name__, cls)
+ return module
+
+
+def test_swapped_concrete_class_resolves_to_replacement(monkeypatch):
+ stock = type("FakeForConditionalGeneration", (_Stock,), {})
+ module = _fake_module(monkeypatch, stock)
+ compiled = _compiled(monkeypatch, type("FakeForConditionalGeneration", (_Stock,), {}))
+ setattr(module, "FakeForConditionalGeneration", compiled)
+ assert _compiled_auto_model(stock) is compiled
+
+
+def test_unswapped_class_and_none_are_unchanged(monkeypatch):
+ stock = type("FakeForConditionalGeneration", (_Stock,), {})
+ _fake_module(monkeypatch, stock)
+ assert _compiled_auto_model(stock) is stock
+ assert _compiled_auto_model(None) is None
+
+
+def test_auto_class_is_unchanged(monkeypatch):
+ auto = type("FakeAutoModel", (_Stock,), {"_model_mapping": {}})
+ module = _fake_module(monkeypatch, auto)
+ setattr(module, "FakeAutoModel", type("FakeAutoModel", (_Stock,), {}))
+ assert _compiled_auto_model(auto) is auto
+
+
+ "replacement", ["renamed", "no_from_pretrained", "not_a_class", "not_compiled"]
+)
+def test_unsuitable_replacement_is_ignored(monkeypatch, replacement):
+ stock = type("FakeForConditionalGeneration", (_Stock,), {})
+ module = _fake_module(monkeypatch, stock)
+ other = {
+ "renamed": type("Other", (_Stock,), {}),
+ "no_from_pretrained": type("FakeForConditionalGeneration", (), {}),
+ "not_a_class": object(),
+ "not_compiled": type("FakeForConditionalGeneration", (_Stock,), {}),
+ }[replacement]
+ setattr(module, "FakeForConditionalGeneration", other)
+ assert _compiled_auto_model(stock) is stock
+
+
+def test_real_whisper_class_follows_module_swap(monkeypatch):
+ modeling_whisper = pytest.importorskip("transformers.models.whisper.modeling_whisper")
+ stock = modeling_whisper.WhisperForConditionalGeneration
+ compiled = _compiled(monkeypatch, type("WhisperForConditionalGeneration", (stock,), {}))
+ monkeypatch.setattr(modeling_whisper, "WhisperForConditionalGeneration", compiled)
+ assert _compiled_auto_model(stock) is compiled
+ from transformers import AutoModelForSpeechSeq2Seq
+
+ assert _compiled_auto_model(AutoModelForSpeechSeq2Seq) is AutoModelForSpeechSeq2Seq
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 5bc947eb8c5..2cd6f8df97e 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -581,6 +581,43 @@ def _config_has_native_class(auto_class, config):
return False
+def _compiled_auto_model(auto_model):
+ """The compiled replacement of a concrete `auto_model` class, else `auto_model` unchanged.
+
+ unsloth_compile_transformers swaps the class in its modeling module, which an Auto class
+ resolves at load time; a class the caller imported earlier (the Whisper notebook passes
+ WhisperForConditionalGeneration) still points at the stock one and skips every compiled forward.
+ """
+ if not isinstance(auto_model, type) or getattr(auto_model, "_model_mapping", None) is not None:
+ return auto_model
+ module = sys.modules.get(getattr(auto_model, "__module__", None) or "")
+ replacement = getattr(module, auto_model.__name__, None) if module is not None else None
+ if (
+ isinstance(replacement, type)
+ and replacement is not auto_model
+ and replacement.__name__ == auto_model.__name__
+ and callable(getattr(replacement, "from_pretrained", None))
+ and _built_by_unsloth_compiler(replacement)
+ ):
+ return replacement
+ return auto_model
+
+
+def _built_by_unsloth_compiler(cls):
+ """True when `cls` is exported by a module the compiler generated (unsloth_compiled_module_*).
+
+ The compiled class keeps the original `__module__`, so check where it actually lives; a same-named
+ class another library rebound in the modeling module is not taken.
+ """
+ for name, module in list(sys.modules.items()):
+ if (
+ name.rsplit(".", 1)[-1].startswith("unsloth_compiled_module_")
+ and getattr(module, cls.__name__, None) is cls
+ ):
+ return True
+ return False
+
+
def _resolve_omni_auto_model(
model_config,
trust_remote_code = None,
@@ -2380,6 +2417,7 @@ class FastModel(FastBaseModel):
for model_type in DISABLE_SDPA_MODEL_NAMES:
if model_type in model_types_all:
supports_sdpa = False
+ auto_model = _compiled_auto_model(auto_model)
# Keep the local checkpoint dir as tokenizer when self-sufficient; a VLM also needs local processor files, else fall back to the base repo so its cached processor loads.
_ckpt_arch = getattr(model_config, "architectures", None) or []