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
Studio: keep an anonymous Hub token anonymous in the hosted bf16 VAE path (#16214)
Daniel Han65f72dbe5c3f
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex 617dd5ea2f0..9e7213949fc 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -5512,7 +5512,7 @@ class DiffusionBackend:kind,base,kwargs.get("gpu_ordinal"),- kwargs.get("hf_token"),+ kwargs.get("_hub_token", kwargs.get("hf_token")),local_files_only,))@@ -9116,8 +9116,8 @@ class DiffusionBackend:"torch_dtype": dtype,"cache_dir": hub_cache_dir(),}- if hf_token:- pipe_kwargs["token"] = hf_token+ if hub_token is not None:+ pipe_kwargs["token"] = hub_token# fp16 load with a twin for every component reads the twins (bit-identical after cast)._te_replaced = te_precast_components(fam, fetch_base, text_encoder_quant, target@@ -9208,7 +9208,7 @@ class DiffusionBackend:fetch_base,kind = kind,dtype = dtype,- hf_token = hf_token,+ hf_token = hub_token,local_files_only = local_files_only,config_source = _base_local_dir,cache_dir = hub_cache_dir(),diff --git a/studio/backend/core/inference/diffusion_hosted_vae.py b/studio/backend/core/inference/diffusion_hosted_vae.pyindex d3e35f8cb66..e1a163588a1 100644--- a/studio/backend/core/inference/diffusion_hosted_vae.py+++ b/studio/backend/core/inference/diffusion_hosted_vae.py@@ -8,6 +8,8 @@ from __future__ import annotationsimport osfrom typing import Any, Optional+from hub.utils.hf_tokens import normalize_token+HOSTED_VAE_ENV = "UNSLOTH_DIFFUSION_HOSTED_VAE"HOSTED_VAE_COMPONENT = "vae"@@ -64,7 +66,7 @@ def hosted_vae_plan_files(repo_id, filename = srctry:from huggingface_hub import HfApi- infos = HfApi(token = hf_token or None).get_paths_info(repo_id, [filename])+ infos = HfApi(token = normalize_token(hf_token)).get_paths_info(repo_id, [filename])size = next((int(getattr(i, "size", 0) or 0) for i in infos if i.path == filename), 0)except Exception as exc: # noqa: BLE001 -- unresolvable: keep the dense VAEif logger is not None:@@ -88,7 +90,7 @@ def _vae_config(source,"vae/config.json",cache_dir = cache_dir,- token = hf_token or None,+ token = normalize_token(hf_token),local_files_only = local_files_only,)with open(local, encoding = "utf-8") as fh:@@ -167,7 +169,7 @@ def hosted_vae_pipe_kwargs(repo_id,filename,cache_dir = cache_dir,- token = hf_token or None,+ token = normalize_token(hf_token),local_files_only = local_files_only,)config = _vae_config(@@ -225,6 +227,8 @@ def _dense_vae_kwargs(}if cache_dir:kwargs["cache_dir"] = cache_dir- if hf_token:- kwargs["token"] = hf_token+ hub_token = normalize_token(hf_token)+ # False is kept: it means anonymous, while an omitted token falls back to the cached login.+ if hub_token is not None:+ kwargs["token"] = hub_tokenreturn {HOSTED_VAE_COMPONENT: vae_cls.from_pretrained(str(base), **kwargs)}diff --git a/studio/backend/tests/test_diffusion_hosted_vae.py b/studio/backend/tests/test_diffusion_hosted_vae.pyindex a33b492afa9..0a98b1c9275 100644--- a/studio/backend/tests/test_diffusion_hosted_vae.py+++ b/studio/backend/tests/test_diffusion_hosted_vae.py@@ -291,3 +291,52 @@ def test_the_backend_plans_through_the_load_target(fam, monkeypatch):)assert backend._hosted_vae_plan_files(fam, "pipeline", BASE, None, None, False) == {}assert json.dumps(got)+++def test_an_anonymous_token_reaches_every_hub_call_as_false(fam, monkeypatch, tmp_path):+ import huggingface_hub++ seen = []++ class _Api:+ def __init__(self, token = None):+ seen.append(("plan", token))++ def get_paths_info(self, repo_id, paths):+ return [types.SimpleNamespace(path = p, size = 1) for p in paths]++ def _download(repo_id, filename, **kw):+ seen.append((filename, kw.get("token", "omitted")))+ if filename == "vae/config.json":+ path = tmp_path / "config.json"+ path.write_text(json.dumps({"_class_name": "AutoencoderKL"}))+ return str(path)+ raise RuntimeError("stop")++ class _Vae:+ @classmethod+ def from_pretrained(cls, base, **kw):+ seen.append(("dense", kw.get("token", "omitted")))+ return object()++ monkeypatch.setattr(huggingface_hub, "HfApi", _Api)+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", _download)+ monkeypatch.setattr(diffusers, "AutoencoderKL", _Vae)+ import core.inference.diffusion_te_prequant as tp++ monkeypatch.setattr(tp, "te_prequant_mirror_path", lambda *a, **k: None)++ assert hv.hosted_vae_plan_files(+ fam, BASE, kind = "pipeline", dtype = torch.bfloat16, hf_token = False+ )+ out = hv.hosted_vae_pipe_kwargs(+ fam,+ BASE,+ kind = "pipeline",+ dtype = torch.bfloat16,+ hf_token = False,+ config_source = str(tmp_path / "missing"),+ dense_staged = False,+ )+ assert "vae" in out+ assert seen == [("plan", False), (FILE, False), ("vae/config.json", False), ("dense", False)]diff --git a/studio/backend/tests/test_diffusion_hosted_vae_pipeline.py b/studio/backend/tests/test_diffusion_hosted_vae_pipeline.pynew file mode 100644index 00000000000..6ad78470281--- /dev/null+++ b/studio/backend/tests/test_diffusion_hosted_vae_pipeline.py@@ -0,0 +1,72 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from __future__ import annotations++import sys++import pytest++from core.inference.diffusion import DiffusionBackend+from .test_diffusion_backend import (+ _FakePipeline,+ _FakeTransformer,+ _force_cuda_target,+ fake_runtime as fake_runtime,+)+++ "token,hub_token,expected",+ [+ (False, None, False),+ (None, None, "omitted"),+ ("test-token", None, "test-token"),+ (None, False, False),+ ],+ ids = ["anonymous-direct", "ambient", "explicit", "anonymous-worker"],+)+def test_hosted_vae_staged_fallback_keeps_pipeline_token(+ fake_runtime, monkeypatch, token, hub_token, expected+):+ import huggingface_hub+ from core.inference import diffusion as dmod+ from core.inference import diffusion_te_prequant++ class ReachedPipeline(BaseException):+ pass++ backend = DiffusionBackend()+ _force_cuda_target(backend, monkeypatch)+ diffusers = sys.modules["diffusers"]+ monkeypatch.setattr(diffusers, "QwenImage21Pipeline", _FakePipeline, raising = False)+ monkeypatch.setattr(diffusers, "QwenImage21Transformer2DModel", _FakeTransformer, raising = False)+ monkeypatch.setattr(dmod, "prefer_ungated_mirror", lambda base, *_a: base)+ monkeypatch.setattr(diffusion_te_prequant, "te_prequant_mirror_path", lambda *_a, **_k: None)+ seen = []++ def download(repo, filename, **kwargs):+ seen.append((filename, kwargs.get("token", "omitted")))+ raise FileNotFoundError(filename)++ def construct(base, **kwargs):+ seen.append(("pipeline", kwargs.get("token", "omitted")))+ raise ReachedPipeline++ monkeypatch.setattr(huggingface_hub, "hf_hub_download", download)+ monkeypatch.setattr(_FakePipeline, "from_pretrained", construct)+ with pytest.raises(ReachedPipeline):+ backend.load_pipeline(+ "Qwen/Qwen-Image-2.1",+ model_kind = "pipeline",+ hf_token = token,+ _hub_token = hub_token,+ speed_mode = "off",+ text_encoder_quant = "off",+ transformer_quant = "off",+ )+ assert (+ "vae/qwen_image_2.1_vae_bf16.safetensors",+ None if expected == "omitted" else expected,+ ) in seen+ assert seen[-1] == ("pipeline", expected)