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 compiler on for native architectures loaded with trust_remote_code = True (#11516)

Daniel Han7c28d7435f82
diff --git a/studio/backend/tests/test_diffusion_xpu_training.py b/studio/backend/tests/test_diffusion_xpu_training.py
index f555c808ae9..062715c6c76 100644
--- a/studio/backend/tests/test_diffusion_xpu_training.py
+++ b/studio/backend/tests/test_diffusion_xpu_training.py
@@ -263,8 +263,8 @@ def test_an_unprobeable_xpu_is_left_to_the_child_not_refused_up_front(host):
host(cuda = False, xpu = _fake_xpu(is_available = lambda: True, is_bf16_supported = _unprobeable))
assert xpu_native_bf16_probe() is None
- assert bf16_unsupported_reason("krea-2") is None # route proceeds
- assert native_bf16_supported_xpu() is False # child still refuses, on the device itself
+ assert bf16_unsupported_reason("krea-2") is None # route proceeds
+ assert native_bf16_supported_xpu() is False # child still refuses, on the device itself
def test_an_xpu_run_captures_and_restores_its_own_noise_generator(host, monkeypatch):
@@ -307,7 +307,9 @@ def test_a_checkpoint_cannot_silently_resume_across_accelerator_backends(host, t
"sampler": {"pos": 0},
"files": {k: f"{k}.pt" for k in ("adapter", "optimizer", "scheduler", "rng")},
}
- written_here = capture_rng_state({"loop": random.Random(1), "variant": random.Random(2)})["json"]
+ written_here = capture_rng_state({"loop": random.Random(1), "variant": random.Random(2)})[
+ "json"
+ ]
assert written_here["accelerator"] in ("cuda", "xpu", "cpu")
# Same host: accepted.
diff --git a/tests/test_trust_remote_code_native_compile.py b/tests/test_trust_remote_code_native_compile.py
new file mode 100644
index 00000000000..27e1a369c47
--- /dev/null
+++ b/tests/test_trust_remote_code_native_compile.py
@@ -0,0 +1,170 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+
+"""`trust_remote_code = True` on a native architecture must not switch the compiler off.
+
+The compiler pass (fast LoRA forward, fused linear cross entropy, compiled norms and
+attention) was skipped whenever the flag was set, on the grounds that remote code
+cannot be traced. That is only true when the checkpoint actually ships its own
+modeling files. Gemma-4 loaded with the flag lost all of it: PEFT's own Linear4bit
+forward ran, casting every activation to the float32 LoRA dtype and running both
+LoRA matmuls as fp32 SIMT GEMMs, and the 262k-vocab logits were materialised in
+full instead of going through the fused loss.
+"""
+
+import os
+from types import SimpleNamespace
+
+import pytest
+
+
+def _helper():
+ from unsloth.models.loader import _config_uses_remote_code
+ return _config_uses_remote_code
+
+
+def test_native_config_is_not_remote_code():
+ f = _helper()
+ assert f(SimpleNamespace(auto_map = None)) is False
+ assert f(SimpleNamespace()) is False
+
+
+def test_auto_map_means_remote_code():
+ f = _helper()
+ assert f(SimpleNamespace(auto_map = {"AutoModelForCausalLM": "modeling_x.XForCausalLM"})) is True
+
+
+def test_sub_config_auto_map_counts():
+ f = _helper()
+ cfg = SimpleNamespace(
+ auto_map = None,
+ text_config = SimpleNamespace(auto_map = {"AutoConfig": "configuration_x.XConfig"}),
+ )
+ assert f(cfg) is True
+
+
+def test_transformers_modules_config_class_counts():
+ f = _helper()
+
+ class RemoteConfig: # what a dynamically loaded config looks like
+ auto_map = None
+
+ RemoteConfig.__module__ = "transformers_modules.some_repo.configuration_x"
+ assert f(RemoteConfig()) is True
+
+
+def test_no_config_keeps_the_conservative_answer():
+ assert _helper()(None) is True
+
+
+def test_tokenizer_only_auto_map_is_still_native():
+ """A custom tokenizer or processor is not model code the compiler must trace."""
+ f = _helper()
+ assert (
+ f(SimpleNamespace(auto_map = {"AutoTokenizer": ["tokenization_x.XTokenizer", None]})) is False
+ )
+ assert (
+ f(
+ SimpleNamespace(
+ auto_map = {
+ "AutoProcessor": "processing_x.XProcessor",
+ "AutoImageProcessor": "image_processing_x.XImageProcessor",
+ }
+ )
+ )
+ is False
+ )
+ assert (
+ f(
+ SimpleNamespace(
+ auto_map = {
+ "AutoTokenizer": "tokenization_x.XTokenizer",
+ "AutoModelForCausalLM": "modeling_x.XForCausalLM",
+ }
+ )
+ )
+ is True
+ )
+
+
+def test_sub_config_from_transformers_modules_counts():
+ """The remote-class check applies to sub-configs the same way as to the root."""
+ f = _helper()
+
+ class RemoteTextConfig:
+ auto_map = None
+
+ RemoteTextConfig.__module__ = "transformers_modules.some_repo.configuration_x"
+ assert f(SimpleNamespace(auto_map = None, text_config = RemoteTextConfig())) is True
+
+
+def test_dict_shaped_configs_are_handled():
+ f = _helper()
+ assert f({"auto_map": None, "model_type": "gemma4"}) is False
+ assert f({"auto_map": {"AutoConfig": "configuration_x.XConfig"}}) is True
+ assert f({"text_config": {"auto_map": {"AutoModel": "modeling_x.XModel"}}}) is True
+
+
+def _cuda_is_available():
+ # Importing torch in the decorator itself turns this skip into a collection error on
+ # a runner that does not ship torch, taking the whole module with it.
+ try:
+ import torch
+ except ImportError:
+ return False
+ return torch.cuda.is_available()
+
+
[email protected](not _cuda_is_available(), reason = "needs a GPU to load a 4-bit model")
+def test_native_model_with_trust_remote_code_keeps_fast_lora(tmp_path, monkeypatch):
+ """The arm that fails without the fix: PEFT's Linear4bit forward is left in place."""
+ monkeypatch.chdir(tmp_path) # fresh unsloth_compiled_cache
+ import torch
+ import unsloth # noqa: F401
+ from unsloth import FastModel
+
+ model, _ = FastModel.from_pretrained(
+ "tiny-random/gemma-4-moe",
+ max_seq_length = 256,
+ dtype = torch.bfloat16,
+ load_in_4bit = True,
+ trust_remote_code = True,
+ )
+ model = FastModel.get_peft_model(model, r = 8, lora_alpha = 16, lora_dropout = 0, bias = "none")
+ from peft.tuners.lora.bnb import Linear4bit
+
+ assert Linear4bit.forward.__name__ == "unsloth_forward", Linear4bit.forward.__module__
+ assert any(
+ f.startswith("unsloth_compiled_module_gemma4") for f in os.listdir("unsloth_compiled_cache")
+ )
+
+
+def test_compiler_call_site_gates_the_flag_on_the_config():
+ """Every other test here calls the predicate directly, so all of them stay green if
+ the one line that uses it is reverted. This is the only test that fails on main."""
+ import ast
+ import inspect
+
+ from unsloth.models import loader
+
+ gated = []
+ for node in ast.walk(ast.parse(inspect.getsource(loader))):
+ if not isinstance(node, ast.Call):
+ continue
+ if getattr(node.func, "id", None) != "unsloth_compile_transformers":
+ continue
+ keywords = {k.arg: k.value for k in node.keywords}
+ assert "trust_remote_code" in keywords, "the compiler call lost its trust_remote_code"
+ gated.append(
+ any(
+ isinstance(n, ast.Call)
+ and getattr(n.func, "id", None) == "_config_uses_remote_code"
+ for n in ast.walk(keywords["trust_remote_code"])
+ )
+ )
+
+ assert gated, "no unsloth_compile_transformers call site found"
+ assert all(gated), (
+ f"{gated.count(False)} of {len(gated)} compiler call sites pass trust_remote_code "
+ "straight through instead of gating it on _config_uses_remote_code(model_config)"
+ )
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 2108cedd6c4..49727b1f069 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -207,6 +207,37 @@ def _loaded_skip_modules(model_config):
)
+def _config_uses_remote_code(config):
+ """Whether the model code lives outside transformers: an `auto_map` naming a model or
+ config class, on this config or a sub-config, or a config class out of
+ `transformers_modules`. The flag alone does not mean remote code, and skipping the
+ compiler for a native architecture costs the fast LoRA forward, the fused loss and the
+ compiled norms. No config keeps the old, conservative answer."""
+ if config is None:
+ return True
+
+ def _remote(cfg):
+ if (getattr(type(cfg), "__module__", "") or "").startswith("transformers_modules"):
+ return True
+ auto_map = getattr(cfg, "auto_map", None)
+ if isinstance(cfg, dict):
+ auto_map = cfg.get("auto_map", auto_map)
+ if not auto_map:
+ return False
+ # A custom tokenizer, processor or feature extractor is not code the compiler traces.
+ return any(str(k).startswith(("AutoModel", "AutoConfig")) for k in auto_map)
+
+ if _remote(config):
+ return True
+ for sub in ("text_config", "vision_config", "audio_config"):
+ cfg = getattr(config, sub, None)
+ if cfg is None and isinstance(config, dict):
+ cfg = config.get(sub)
+ if cfg is not None and _remote(cfg):
+ return True
+ return False
+
+
def _config_diff(config):
if isinstance(config, dict):
return config
@@ -1834,7 +1865,8 @@ class FastModel(FastBaseModel):
import_from_cache = False,
disable = False,
return_logits = return_logits,
- trust_remote_code = trust_remote_code,
+ # Only real remote code is untraceable; a native architecture keeps every optimization.
+ trust_remote_code = trust_remote_code and _config_uses_remote_code(model_config),
unsloth_force_compile = unsloth_force_compile,
)
for model_type in DISABLE_SDPA_MODEL_NAMES: