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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Walk every sub-config when deciding whether a config carries remote code (#11555)
Daniel Han42a23e871e00
diff --git a/tests/test_trust_remote_code_native_compile.py b/tests/test_trust_remote_code_native_compile.pyindex 27e1a369c47..865eda9170f 100644--- a/tests/test_trust_remote_code_native_compile.py+++ b/tests/test_trust_remote_code_native_compile.py@@ -117,19 +117,41 @@ def _cuda_is_available():@pytest.mark.skipif(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."""+ """The arm that fails without the fix: PEFT's Linear4bit forward is left in place.++ Skips only on errors meaning the checkpoint cannot be built here (old transformers, no torchvision, offline).+ """monkeypatch.chdir(tmp_path) # fresh unsloth_compiled_cacheimport torchimport unsloth # noqa: F401from 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,- )+ try:+ model, _ = FastModel.from_pretrained(+ "tiny-random/gemma-4-moe",+ max_seq_length = 256,+ dtype = torch.bfloat16,+ load_in_4bit = True,+ trust_remote_code = True,+ )+ except Exception as exception:+ text = str(exception)+ if any(+ marker in text+ for marker in (+ "does not recognize this architecture",+ "is not supported yet in",+ "torchvision",+ "Could not load the vision processor",+ "We couldn't connect to",+ "offline mode",+ "Connection error",+ )+ ):+ pytest.skip(+ f"the checkpoint cannot be built on this host ({type(exception).__name__}: {text[:160]})"+ )+ raisemodel = FastModel.get_peft_model(model, r = 8, lora_alpha = 16, lora_dropout = 0, bias = "none")from peft.tuners.lora.bnb import Linear4bit@@ -168,3 +190,96 @@ def test_compiler_call_site_gates_the_flag_on_the_config():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)")+++def test_nested_object_sub_configs_are_walked():+ """A native root whose declared child is itself composite: the remote grandchild counts."""+ from transformers import PretrainedConfig++ f = _helper()++ class LlmConfig(PretrainedConfig):+ model_type = "llm_test"+ sub_configs = {"audio_config": PretrainedConfig}++ class RootConfig(PretrainedConfig):+ model_type = "root_test"+ sub_configs = {"llm_config": LlmConfig}++ class RemoteAudioConfig(PretrainedConfig):+ model_type = "remote_audio_test"++ RemoteAudioConfig.__module__ = "transformers_modules.some_repo.configuration_x"++ root = RootConfig()+ root.llm_config = LlmConfig()+ root.llm_config.audio_config = PretrainedConfig()+ assert f(root) is False+ root.llm_config.audio_config = RemoteAudioConfig()+ assert f(root) is True+ root.llm_config.audio_config = PretrainedConfig(auto_map = {"AutoModel": "modeling_x.XModel"})+ assert f(root) is True+++def test_a_mock_config_does_not_recurse_forever():+ from unittest.mock import MagicMock++ f = _helper()+ config = SimpleNamespace(auto_map = None, text_config = MagicMock(auto_map = None))+ assert f(config) is False+++def test_config_objects_inside_dict_configs_are_walked():+ from transformers import PretrainedConfig++ f = _helper()++ class RemoteAudioConfig(PretrainedConfig):+ model_type = "remote_audio_dict_test"++ RemoteAudioConfig.__module__ = "transformers_modules.some_repo.configuration_x"+ assert f({"model_type": "root", "audio": PretrainedConfig()}) is False+ assert f({"model_type": "root", "audio": RemoteAudioConfig()}) is True+ assert f({"model_type": "root", "nested": {"audio": RemoteAudioConfig()}}) is True+++def test_configs_past_the_depth_bound_count_as_remote():+ """Deeper than the walk goes is answered conservatively, never as native."""+ f = _helper()++ def chain(levels, leaf):+ node = leaf+ for _ in range(levels):+ node = {"model_type": "wrapper", "llm_config": node}+ return node++ remote_leaf = {"auto_map": {"AutoModel": "modeling_x.XModel"}}+ native_leaf = {"model_type": "llama"}+ assert f(chain(3, native_leaf)) is False+ assert f(chain(3, remote_leaf)) is True+ assert f(chain(12, remote_leaf)) is True+++def test_sub_configs_declared_as_a_property_are_read():+ """transformers 4.57 declares backbone configs' `sub_configs` as a property (VitMatte, DPT)."""+ from transformers import PretrainedConfig++ f = _helper()++ class RemoteBackbone(PretrainedConfig):+ model_type = "remote_backbone_test"++ RemoteBackbone.__module__ = "transformers_modules.some_repo.configuration_x"++ class BackboneHolder(PretrainedConfig):+ model_type = "backbone_holder_test"++ @property+ def sub_configs(self):+ return {"backbone_config": PretrainedConfig}++ holder = BackboneHolder()+ holder.backbone_config = PretrainedConfig()+ assert f(holder) is False+ holder.backbone_config = RemoteBackbone()+ assert f(holder) is Truediff --git a/unsloth/models/loader.py b/unsloth/models/loader.pyindex 6dd2dccb91e..4ad564223da 100644--- a/unsloth/models/loader.py+++ b/unsloth/models/loader.py@@ -227,14 +227,53 @@ def _config_uses_remote_code(config):# 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):+ # Sub-configs go beyond text/vision/audio (Qwen-Omni thinker_config, nested llm_config) and nest;+ # a remote child read as native puts the compiler on untraceable code, so walk every level.+ try:+ from transformers import PretrainedConfig as _config_class+ except Exception:+ _config_class = ()++ def _is_config(value):+ return isinstance(value, dict) or (bool(_config_class) and isinstance(value, _config_class))++ def _children(node):+ if isinstance(node, dict):+ return [value for value in node.values() if _is_config(value)]+ names = ["text_config", "vision_config", "audio_config"]+ # Instance read: transformers 4.57 makes backbone configs' `sub_configs` a property.+ sub_configs = getattr(node, "sub_configs", None)+ for sub in sub_configs if isinstance(sub_configs, dict) else ():+ if sub not in names:+ names.append(sub)+ # A callable (e.g. a Mock) is not a config.+ children = [+ child+ for child in (getattr(node, name, None) for name in names)+ if child is not None and (_is_config(child) or not callable(child))+ ]+ try:+ children.extend(value for value in vars(node).values() if _is_config(value))+ except TypeError:+ pass+ return children++ pending = [(config, 0)]+ seen = set()+ while pending:+ current, depth = pending.pop()+ if id(current) in seen:+ continue+ seen.add(id(current))+ if _remote(current):+ return True+ children = _children(current)+ if not children:+ continue+ # Past the bound, answer conservatively.+ if depth >= 8:return True+ pending.extend((child, depth + 1) for child in children)return False