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
fix(studio): stop offering a transformers upgrade where it cannot load anything (#9878)
Long Yixing909d797fa4e9
diff --git a/studio/backend/tests/test_transformers_latest.py b/studio/backend/tests/test_transformers_latest.pyindex 9a015b244a2..c536928226c 100644--- a/studio/backend/tests/test_transformers_latest.py+++ b/studio/backend/tests/test_transformers_latest.py@@ -1383,3 +1383,143 @@ def test_a_truncated_source_fails_the_lookup_instead_of_shrinking_the_map(monkeywith _BodyServer(truncated) as server:monkeypatch.setattr(tl, "_RAW_URL", server.url + "?{ref}{name}")assert tl._fetch_remote_model_types("v5.15.0") is None+++def _hardware_module(device):+ """Stand in for utils.hardware, whose import needs real hardware."""+ import enum++ module = _types.ModuleType("utils.hardware")++ class DeviceType(str, enum.Enum):+ CUDA = "cuda"+ XPU = "xpu"+ MLX = "mlx"+ CPU = "cpu"++ module.DeviceType = DeviceType+ module.get_device = lambda: DeviceType(device)+ return module++[email protected](autouse = True)+def _transformers_backend_host(monkeypatch):+ """Pin a transformers-loading device so these tests do not depend on the+ host: on MLX the upgrade check short-circuits by design."""+ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module("cuda"))++[email protected]("device", ["cuda", "cpu", "xpu"])+def test_transformers_backends_still_get_the_upgrade_offer(device, monkeypatch):+ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module(device))+ monkeypatch.setattr(tl, "_disabled", lambda: False)+ monkeypatch.setattr(tl, "_env_offline", lambda: False)+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: {"llama"})+ monkeypatch.setattr(tl, "_hardcoded_model_types", lambda: frozenset())+ monkeypatch.setattr(tl, "_load_config_json", lambda *a, **k: {"model_type": "brandnew_arch"})+ monkeypatch.setattr(+ tl,+ "latest_transformers_supports",+ lambda _t: {"pypi_version": "5.15.0", "supported_in_pypi": True, "supported_in_main": True},+ )++ offered = tl.check_upgrade_for_model("org/brandnew")++ assert offered is not None and offered["model_type"] == "brandnew_arch"+++def test_mlx_host_is_never_offered_a_transformers_upgrade(monkeypatch):+ """MLX picks its backend from hardware and never falls back to transformers,+ so no install can make an architecture loadable there. Same inputs as the+ transformers-backend test above, which does get an offer."""+ monkeypatch.setattr(tl, "_disabled", lambda: False)+ monkeypatch.setattr(tl, "_env_offline", lambda: False)+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: {"llama"})+ monkeypatch.setattr(tl, "_hardcoded_model_types", lambda: frozenset())+ monkeypatch.setattr(tl, "_load_config_json", lambda *a, **k: {"model_type": "muse_glimmer"})+ monkeypatch.setattr(+ tl,+ "latest_transformers_supports",+ lambda _t: {"pypi_version": "5.15.0", "supported_in_pypi": True, "supported_in_main": True},+ )++ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module("cuda"))+ assert tl.check_upgrade_for_model("mlx-community/Muse-Glimmer-30B-4bit") is not None++ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module("mlx"))+ assert tl.check_upgrade_for_model("mlx-community/Muse-Glimmer-30B-4bit") is None+++def _bnb(model_type):+ return {+ "model_type": model_type,+ "quantization_config": {"quant_method": "bitsandbytes", "load_in_4bit": True},+ }+++def test_mlx_still_offers_the_upgrade_for_a_bitsandbytes_repo(monkeypatch):+ """mlx-lm cannot read bnb weights, so the MLX loader dequantizes them through+ ``AutoModelForCausalLM.from_pretrained``. That call is transformers building the+ architecture, and on a brand-new type it raises the very unrecognized-architecture+ error this offer fixes -- so a bnb repo keeps the offer even on MLX."""+ monkeypatch.setattr(tl, "_disabled", lambda: False)+ monkeypatch.setattr(tl, "_env_offline", lambda: False)+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: {"llama"})+ monkeypatch.setattr(tl, "_hardcoded_model_types", lambda: frozenset())+ monkeypatch.setattr(+ tl,+ "latest_transformers_supports",+ lambda _t: {"pypi_version": "5.15.0", "supported_in_pypi": True, "supported_in_main": True},+ )+ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module("mlx"))++ # Control: the same architecture unquantized is MLX's own to load, and is skipped.+ monkeypatch.setattr(tl, "_load_config_json", lambda *a, **k: {"model_type": "muse_glimmer"})+ assert tl.check_upgrade_for_model("mlx-community/Muse-Glimmer-30B-4bit") is None++ # A third-party bnb build of it goes through transformers, so the offer is real.+ monkeypatch.setattr(tl, "_load_config_json", lambda *a, **k: _bnb("muse_glimmer"))+ offered = tl.check_upgrade_for_model("someorg/Muse-Glimmer-30B-bnb-4bit")+ assert offered is not None and offered["model_type"] == "muse_glimmer"+++def test_mlx_skips_the_unsloth_bnb_repo_it_swaps_for_a_base(monkeypatch):+ """An ``unsloth/*-bnb-4bit`` id is remapped to its full-precision base before+ the loader looks at the weights, so MLX quantizes it and transformers is never+ asked to build it. Those keep the skip -- they are most of the bnb rows Studio+ suggests on a Mac, and offering an install for them is the annoyance this+ short-circuit exists to remove."""+ monkeypatch.setattr(tl, "_disabled", lambda: False)+ monkeypatch.setattr(tl, "_env_offline", lambda: False)+ monkeypatch.setattr(tl, "_config_model_types", lambda tier: {"llama"})+ monkeypatch.setattr(tl, "_hardcoded_model_types", lambda: frozenset())+ monkeypatch.setattr(tl, "_load_config_json", lambda *a, **k: _bnb("muse_glimmer"))+ monkeypatch.setattr(+ tl,+ "latest_transformers_supports",+ lambda _t: {"pypi_version": "5.15.0", "supported_in_pypi": True, "supported_in_main": True},+ )+ monkeypatch.setitem(sys.modules, "utils.hardware", _hardware_module("mlx"))++ for remapped in (+ "unsloth/Muse-Glimmer-30B-unsloth-bnb-4bit",+ "unsloth/Muse-Glimmer-30B-bnb-4bit",+ ):+ assert tl.check_upgrade_for_model(remapped) is None++ # Same suffix, different owner: not remapped, so it still goes to transformers.+ assert tl.check_upgrade_for_model("someorg/Muse-Glimmer-30B-bnb-4bit") is not None+++def test_upgrade_offer_survives_a_broken_hardware_import(monkeypatch):+ """Detection is best effort: failing it must not silence a real offer."""+ broken = _types.ModuleType("utils.hardware")++ def _raise():+ raise RuntimeError("no hardware")++ broken.get_device = _raise+ broken.DeviceType = object()+ monkeypatch.setitem(sys.modules, "utils.hardware", broken)++ assert tl._architecture_cannot_come_from_transformers() is Falsediff --git a/studio/backend/utils/transformers_latest.py b/studio/backend/utils/transformers_latest.pyindex e8fa67ff157..47c360c7515 100644--- a/studio/backend/utils/transformers_latest.py+++ b/studio/backend/utils/transformers_latest.py@@ -358,6 +358,60 @@ def clear_caches() -> None:end_sidecar_swap()+# What unsloth_zoo strips to reach a bnb repo's full-precision base.+_MLX_BNB_SUFFIXES = ("-unsloth-bnb-4bit", "-bnb-4bit")+++def _is_bitsandbytes_config(cfg: dict | None) -> bool:+ """Whether *cfg* describes a bitsandbytes-quantized checkpoint."""+ if not isinstance(cfg, dict):+ return False+ quant = cfg.get("quantization_config")+ return isinstance(quant, dict) and quant.get("quant_method") == "bitsandbytes"+++def _mlx_swaps_bnb_repo_for_its_base(model_name: str) -> bool:+ """Whether the MLX loader replaces this bnb Hub id with its base repo.++ Mirrors unsloth_zoo's ``_remap_unsloth_bnb_hub_id_for_mlx``: only an+ ``unsloth/`` Hub id is remapped, never a local directory. What it remaps, MLX+ quantizes itself, so transformers never sees that architecture.+ """+ if not isinstance(model_name, str) or not model_name.startswith("unsloth/"):+ return False+ if os.path.exists(model_name):+ return False+ return model_name.endswith(_MLX_BNB_SUFFIXES)+++def _architecture_cannot_come_from_transformers(+ model_name: str = "", cfg: dict | None = None+) -> bool:+ """Whether this host builds model architectures somewhere other than transformers.++ The inference backend is chosen by hardware, and the MLX branch has no+ transformers path to fall back to, so on MLX mlx-lm and mlx-vlm decide what+ loads. Upgrading transformers cannot make an architecture loadable there, so+ offering the install costs minutes and changes nothing.++ One bitsandbytes repo is the exception. mlx-lm cannot read bnb weights, so the+ MLX loader dequantizes them through ``AutoModelForCausalLM.from_pretrained``+ -- transformers building the architecture after all -- and only an+ ``unsloth/*-bnb-4bit`` Hub id is swapped for its base repo before that. A+ third-party or local bnb repo therefore still fails inside transformers with+ the unrecognized-architecture error this offer exists to fix, so it keeps it.+ """+ try:+ from utils.hardware import DeviceType, get_device+ if get_device() != DeviceType.MLX:+ return False+ except Exception:+ return False+ if _is_bitsandbytes_config(cfg) and not _mlx_swaps_bnb_repo_for_its_base(model_name):+ return False+ return True++def latest_transformers_supports(model_type: str) -> dict | None:"""Whether the newest transformers (PyPI release and/or GitHub main) ships *model_type*.@@ -398,6 +452,9 @@ def check_upgrade_for_model(model_name: str, hf_token: str | None = None) -> dicerror. Returns ``{"model_type", "pypi_version", "supported_in_pypi","supported_in_main"}`` when the newest transformers knows the type, else None.+ Also None on a host that does not build architectures through transformers at+ all -- see ``_architecture_cannot_come_from_transformers``.+Never raises; every network touch is bounded and cached. Offline or with the``UNSLOTH_STUDIO_NO_LATEST_TRANSFORMERS`` kill switch it returns None immediately."""@@ -407,6 +464,8 @@ def check_upgrade_for_model(model_name: str, hf_token: str | None = None) -> diccfg = _load_config_json(model_name, hf_token)if not isinstance(cfg, dict):return None+ if _architecture_cannot_come_from_transformers(model_name, cfg):+ return Nonecandidates = _model_types_from_config(cfg)if not candidates:return None