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.

Studio: keep a local folder's own text encoder when the text encoder setting is unset (#14738)

Daniel Hane3bba82715da
diff --git a/studio/backend/core/inference/diffusion_te_prequant.py b/studio/backend/core/inference/diffusion_te_prequant.py
index 5bebbf854ad..0289d478223 100644
--- a/studio/backend/core/inference/diffusion_te_prequant.py
+++ b/studio/backend/core/inference/diffusion_te_prequant.py
@@ -1007,7 +1007,12 @@ def te_prequant_sources_for_base(
A variant base whose encoder is a different model (``te_prequant_variant_repos``) resolves
against that variant's rows and files only, never the family base's.
+
+ An automatic pick (unset request) never swaps a hosted encoder into a load from a local folder: the folder name
+ matching the upstream repo says nothing about the weights inside it, which may be a fine-tune. That load keeps
+ the folder's own encoder, as it did before the family had an automatic default. An explicit scheme is unchanged.
"""
+ auto_local = _auto_pick(te_quant_mode) and _is_local_base(base)
fam = te_variant_family_view(fam, base)
sources = te_prequant_sources(
fam,
@@ -1021,11 +1026,24 @@ def te_prequant_sources_for_base(
for component, source in sources.items():
checkpoint_base = standalone.get(component) or registered_base
selected_base = standalone.get(component) or base
+ if auto_local and component not in standalone:
+ continue
if checkpoint_base and selected_base and te_base_equivalent(checkpoint_base, selected_base):
compatible[component] = source
return compatible
+def _is_local_base(base: Optional[str]) -> bool:
+ """Whether ``base`` is a folder on disk rather than a Hub id (the loaders' own rule)."""
+ if not base:
+ return False
+ try:
+ from .diffusion_families import _is_local_path
+ return bool(_is_local_path(str(base)))
+ except Exception: # noqa: BLE001
+ return False
+
+
def te_variant_family_view(fam: Any, base: Optional[str]) -> Any:
"""``fam`` as seen by a load of ``base``: unchanged unless ``base`` is (equivalent to) a variant listed in the
family's ``te_prequant_variant_repos``, in which case a copy whose ``base_repo`` is that variant and whose
diff --git a/studio/backend/tests/test_diffusion_te_auto_local_folder.py b/studio/backend/tests/test_diffusion_te_auto_local_folder.py
new file mode 100644
index 00000000000..8c475eda62d
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_te_auto_local_folder.py
@@ -0,0 +1,121 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""An unset text-encoder request on a local folder named like the upstream repo keeps the folder's own encoder."""
+
+from __future__ import annotations
+
+import types
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+import core.inference.diffusion_te_prequant as tpq # noqa: E402
+from core.inference.diffusion_families import detect_family # noqa: E402
+from core.inference.diffusion_precision import resolve_te_quant_request # noqa: E402
+
+CUDA_BF16 = types.SimpleNamespace(device = "cuda", dtype = torch.bfloat16)
+
+# family -> (folder named like the upstream repo, component the family hosts a file for)
+FAMILIES = {
+ "flux.1-kontext": ("FLUX.1-Kontext-dev", "text_encoder_2"),
+ "qwen-image-layered": ("Qwen-Image-Layered", "text_encoder"),
+ "qwen-image": ("Qwen-Image", "text_encoder"),
+ "flux.2-dev": ("FLUX.2-dev", "text_encoder"),
+}
+
+
+def _auto_mode(fam):
+ mode, auto = resolve_te_quant_request(None, getattr(fam, "te_quant_auto", None))
+ assert auto and mode is not None
+ return mode
+
+
[email protected]("family", sorted(FAMILIES))
+def test_unset_request_on_local_folder_takes_no_hosted_encoder(family, tmp_path):
+ folder, component = FAMILIES[family]
+ fam = detect_family("", override = family)
+ local = tmp_path / folder
+ local.mkdir()
+ mode = _auto_mode(fam)
+ hub = tpq.te_prequant_sources_for_base(fam, fam.base_repo, te_quant_mode = mode, target = CUDA_BF16)
+ assert component in hub
+ assert (
+ tpq.te_prequant_sources_for_base(fam, str(local), te_quant_mode = mode, target = CUDA_BF16)
+ == {}
+ )
+ # Neither does a request made inside a load whose raw request was unset (plain string mode).
+ with tpq.auto_te_request(True):
+ assert (
+ tpq.te_prequant_sources_for_base(
+ fam, str(local), te_quant_mode = str(mode), target = CUDA_BF16
+ )
+ == {}
+ )
+
+
[email protected]("family", sorted(FAMILIES))
+def test_explicit_request_on_local_folder_is_unchanged(family, tmp_path):
+ folder, component = FAMILIES[family]
+ fam = detect_family("", override = family)
+ local = tmp_path / folder
+ local.mkdir()
+ explicit = str(_auto_mode(fam))
+ hub = tpq.te_prequant_sources_for_base(
+ fam, fam.base_repo, te_quant_mode = explicit, target = CUDA_BF16
+ )
+ got = tpq.te_prequant_sources_for_base(
+ fam, str(local), te_quant_mode = explicit, target = CUDA_BF16
+ )
+ assert got == hub and component in got
+
+
[email protected]("family", sorted(FAMILIES))
+def test_unset_request_on_local_folder_injects_nothing(family, tmp_path, monkeypatch):
+ folder, component = FAMILIES[family]
+ fam = detect_family("", override = family)
+ local = tmp_path / folder
+ local.mkdir()
+ sentinel = object()
+ calls = []
+
+ def fake_load(base, comp, source, **kwargs):
+ calls.append((base, comp, source.location))
+ return sentinel
+
+ monkeypatch.setattr(tpq, "load_prequant_text_encoder", fake_load)
+ monkeypatch.setattr(tpq, "supplied_component_pipe_kwargs", lambda *a, **k: {})
+ mode = _auto_mode(fam)
+ with tpq.auto_te_request(True):
+ local_kwargs = tpq.te_prequant_pipe_kwargs(
+ fam,
+ str(local),
+ te_quant_mode = mode,
+ target = CUDA_BF16,
+ dtype = torch.bfloat16,
+ local_files_only = True,
+ dense_source = str(local),
+ )
+ hub_kwargs = tpq.te_prequant_pipe_kwargs(
+ fam,
+ fam.base_repo,
+ te_quant_mode = mode,
+ target = CUDA_BF16,
+ dtype = torch.bfloat16,
+ local_files_only = True,
+ )
+ assert component not in local_kwargs
+ assert hub_kwargs.get(component) is sentinel
+ assert all(base == fam.base_repo for base, _c, _l in calls)
+
+
+def test_non_local_ids_still_resolve(tmp_path):
+ fam = detect_family("", override = "qwen-image")
+ mode = _auto_mode(fam)
+ assert tpq.te_prequant_sources_for_base(
+ fam, "Qwen/Qwen-Image", te_quant_mode = mode, target = CUDA_BF16
+ )
+ assert not tpq._is_local_base("Qwen/Qwen-Image")
+ assert tpq._is_local_base(str(tmp_path))
+ assert not tpq._is_local_base("")