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: use a ComfyUI text_encoders file for a local ComfyUI DiT instead of downloading the base repo's encoder (#13314)

Daniel Hana5403a2bb33b
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index e7199498c24..b227937e150 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -3222,6 +3222,102 @@ class DiffusionBackend:
)
return overrides
+ def _comfy_text_encoders(
+ self,
+ fam: Optional[DiffusionFamily],
+ kind: str,
+ repo_id: str,
+ gguf_filename: Optional[str],
+ base: str,
+ hf_token: Optional[str],
+ *,
+ local_files_only: bool = False,
+ text_encoder_quant: Optional[str] = None,
+ ) -> list[str]:
+ """Text-encoder files from the ComfyUI layout beside a local DiT, used when the request names none, so a
+ ComfyUI user's ``text_encoders/`` copy replaces the base repo's multi-GB download. Empty whenever any part
+ cannot be settled: the load then takes the base repo's encoder, as before."""
+ if (
+ fam is None
+ or kind not in ("gguf", "single_file")
+ or not gguf_filename
+ or not _is_local_path(repo_id)
+ or getattr(fam, "single_file_is_pipeline", False)
+ or getattr(fam, "pipeline_only", False)
+ or os.environ.get("UNSLOTH_DIFFUSION_COMFY_TEXT_ENCODERS", "").strip().lower()
+ in ("0", "false", "off", "no")
+ ):
+ return []
+ started = time.monotonic()
+ try:
+ from hub.utils.comfy_models import comfy_text_encoder_files
+
+ dit = resolve_local_gguf_child(Path(repo_id).expanduser(), gguf_filename)
+ try:
+ from hub.storage.scan_folders import list_scan_folders
+ scan_folders = [row["path"] for row in list_scan_folders()]
+ except Exception: # noqa: BLE001 - the layout above the file still answers
+ scan_folders = []
+ candidates = [str(p) for p in comfy_text_encoder_files(dit, scan_folders)]
+ if candidates:
+ from hub.services.models import account_access
+ if account_access.managed_account():
+ candidates = [
+ p for p in candidates if account_access.model_visible(os.path.realpath(p))
+ ]
+ if not candidates:
+ return []
+ from .diffusion_comfy_components import (
+ pick_comfy_text_encoders,
+ read_model_index,
+ verify_override_fit,
+ )
+
+ mirror = prefer_ungated_mirror(base, hf_token)
+ index = read_model_index(
+ mirror,
+ base_local_dir = base if _is_local_path(base) else None,
+ hf_token = hf_token,
+ local_files_only = local_files_only,
+ cache_dir = hub_cache_dir(),
+ )
+ planned_quant, auto = resolve_te_quant_request(
+ text_encoder_quant, getattr(fam, "te_quant_auto", None)
+ )
+
+ def _fits(overrides: Any) -> None:
+ verify_override_fit(
+ overrides,
+ base = base if _is_local_path(base) else mirror,
+ hf_token = hf_token,
+ local_files_only = local_files_only,
+ family = fam.name,
+ cache_dir = hub_cache_dir(),
+ text_encoder_quant = text_encoder_quant,
+ )
+
+ picked = pick_comfy_text_encoders(
+ candidates,
+ model_index = index,
+ family = fam.name,
+ fits = _fits,
+ auto_scheme = planned_quant if auto else None,
+ )
+ except Exception as exc: # noqa: BLE001 - the base repo's encoder is always the fallback
+ logger.info(
+ "diffusion.comfy_text_encoders: none taken from the ComfyUI folder (%s)",
+ type(exc).__name__,
+ )
+ return []
+ if picked:
+ logger.info(
+ "diffusion.comfy_text_encoders: %s from the ComfyUI folder beside %s (%.1fs)",
+ ", ".join(os.path.basename(p) for p in picked),
+ gguf_filename,
+ time.monotonic() - started,
+ )
+ return picked
+
def validate_load_request(
self,
repo_id: str,
@@ -3566,6 +3662,22 @@ class DiffusionBackend:
kwargs.get("gguf_filename"),
)
kwargs["base_repo"] = base
+ discovered = False
+ if not kwargs.get("text_encoder_files") and not kwargs.get("vae_file"):
+ kwargs["text_encoder_files"] = (
+ self._comfy_text_encoders(
+ fam,
+ kind,
+ kwargs["repo_id"],
+ kwargs.get("gguf_filename"),
+ base,
+ kwargs.get("hf_token"),
+ local_files_only = local_files_only,
+ text_encoder_quant = kwargs.get("text_encoder_quant"),
+ )
+ or None
+ )
+ discovered = bool(kwargs["text_encoder_files"])
# Claimed before a byte moves, so the cache-delete guard sees a Hub component repo.
from .diffusion_comfy_components import hub_repo_ids
@@ -3592,6 +3704,9 @@ class DiffusionBackend:
cancel_event = cancel_event,
text_encoder_quant = kwargs.get("text_encoder_quant"),
)
+ if discovered and component_overrides is not None:
+ component_overrides.discovered = True
+ component_overrides.te_quant_request = kwargs.get("text_encoder_quant")
kwargs["_component_overrides"] = component_overrides
replaced = tuple(component_overrides.components) if component_overrides else ()
if component_overrides is not None and component_overrides.text_encoder_components:
@@ -4713,6 +4828,20 @@ class DiffusionBackend:
fam, repo_id, gguf_filename, base, hf_token
) or speech_pick_refusal(repo_id, gguf_filename, hf_token)
component_overrides = None
+ if not load_kwargs.get("text_encoder_files") and not load_kwargs.get("vae_file"):
+ # The same pick the load makes, so the plan stages no encoder the load will not open.
+ load_kwargs["text_encoder_files"] = (
+ self._comfy_text_encoders(
+ fam,
+ kind,
+ repo_id,
+ gguf_filename,
+ base,
+ hf_token,
+ text_encoder_quant = text_encoder_quant,
+ )
+ or None
+ )
if (
load_kwargs.get("text_encoder_files") or load_kwargs.get("vae_file")
) and fam is not None:
@@ -5882,6 +6011,11 @@ class DiffusionBackend:
# "none"/"off" -> the released bf16 encoder; an explicit scheme pins itself. Also validates the raw request,
# which is why it runs before this load evicts the previous pipeline.
text_encoder_quant_requested = text_encoder_quant
+ from .diffusion_comfy_components import active_component_overrides
+
+ discovered_components = active_component_overrides()
+ if discovered_components is not None and discovered_components.discovered:
+ text_encoder_quant_requested = discovered_components.te_quant_request
text_encoder_quant, text_encoder_quant_auto = resolve_te_quant_request(
text_encoder_quant, getattr(fam, "te_quant_auto", None)
)
diff --git a/studio/backend/core/inference/diffusion_comfy_components.py b/studio/backend/core/inference/diffusion_comfy_components.py
index a1fd9fa18d9..59e6015c403 100644
--- a/studio/backend/core/inference/diffusion_comfy_components.py
+++ b/studio/backend/core/inference/diffusion_comfy_components.py
@@ -837,6 +837,9 @@ class ComponentOverrides:
classes: dict[str, str] = field(default_factory = dict) # component -> class name
kinds: dict[str, str] = field(default_factory = dict) # component -> detected file kind
modules: dict[str, Any] = field(default_factory = dict)
+ # Found beside a ComfyUI DiT rather than named by the request: status keeps the caller's own precision ask.
+ discovered: bool = False
+ te_quant_request: Optional[str] = None
@property
def components(self) -> tuple[str, ...]:
@@ -1271,3 +1274,81 @@ def validate_component_specs(
if len(set(names)) != len(names):
raise ComponentFileError("the same file was supplied twice")
return refs
+
+
+def _file_precision(path: str) -> str:
+ """``dense`` / ``fp8`` / ``int8`` (ConvRot, stays int8 in memory) / ``int8_dequant`` (loads dense) / ``mixed``
+ from the header; raises for a format Studio cannot load."""
+ from .diffusion_comfy_quant import FP8_FORMATS, INT8_TENSORWISE
+
+ layers = quant_layers(path).values()
+ formats = {layer.format for layer in layers}
+ if not formats:
+ return "dense"
+ if formats <= set(FP8_FORMATS):
+ return "fp8"
+ if formats == {INT8_TENSORWISE}:
+ return "int8" if all(layer.convrot for layer in layers) else "int8_dequant"
+ return "mixed"
+
+
+_PRECISION_ORDER = ("dense", "fp8", "int8", "int8_dequant")
+
+
+def pick_comfy_text_encoders(
+ candidates: Sequence[str],
+ *,
+ model_index: dict,
+ family: str = "",
+ fits: Callable[[ComponentOverrides], None],
+ auto_scheme: Optional[str] = None,
+) -> list[str]:
+ """The ComfyUI text-encoder files to load in place of the base repo's, one per slot, from ``candidates``.
+
+ A slot takes a file only when it fits that slot's real class strictly (``fits`` raises otherwise) and is
+ the single best by precision: dense, then fp8, then int8, the base repo's own precision first. Under a
+ family's automatic scheme (``auto_scheme``) only files that stay in that scheme once loaded count (int8
+ ConvRot; fp8 and plain int8 load dense), so a larger resident encoder never displaces the one the family
+ loads by default. Two equally good files for one slot (a
+ fine-tune beside the original) leave it to the base repo. Unreadable or unfitting files are skipped."""
+ classes = component_classes_from_index(model_index)
+ standalone = _STANDALONE_TE_SLOTS.get(family, ())
+ wanted = {classes[c] for c in TEXT_ENCODER_COMPONENTS if c in classes and c not in standalone}
+ if not wanted:
+ return []
+ order = (("int8",) if auto_scheme == "int8" else ()) if auto_scheme else _PRECISION_ORDER
+ by_slot: dict[str, list[tuple[int, str]]] = {}
+ for path in candidates:
+ try:
+ header, _ = read_safetensors_header(path)
+ keys = weight_keys(header)
+ kind = classify_text_encoder(
+ keys + [k for k in header if k in _NON_WEIGHT_KEYS],
+ {k: header[k].get("shape") for k in keys},
+ )
+ if kind is None or not (TE_KIND_CLASSES.get(kind, frozenset()) & wanted):
+ continue
+ precision = _file_precision(path)
+ if precision not in order:
+ continue
+ planned = plan_component_overrides(
+ text_encoder_files = [path],
+ vae_file = None,
+ model_path = None,
+ model_index = model_index,
+ family = family,
+ )
+ if planned is None or len(planned.files) != 1:
+ continue
+ fits(planned)
+ except Exception: # noqa: BLE001 - a file that does not fit is simply not offered
+ continue
+ slot = planned.components[0]
+ by_slot.setdefault(slot, []).append((order.index(precision), path))
+ picked: list[str] = []
+ for slot in TEXT_ENCODER_COMPONENTS:
+ ranked = sorted(by_slot.get(slot, ()))
+ if not ranked or (len(ranked) > 1 and ranked[0][0] == ranked[1][0]):
+ continue
+ picked.append(ranked[0][1])
+ return picked
diff --git a/studio/backend/hub/utils/comfy_models.py b/studio/backend/hub/utils/comfy_models.py
index ae17ad1b311..9b8c8ffc734 100644
--- a/studio/backend/hub/utils/comfy_models.py
+++ b/studio/backend/hub/utils/comfy_models.py
@@ -4,8 +4,9 @@
"""ComfyUI models-folder discovery and loose single-file diffusion checkpoints.
Role folders hold many loose ``.safetensors`` with no ``config.json``; each denoiser is listed as its
-own row when its header says DiT. Text-encoder / VAE folders are resolved but nothing loads from
-them yet. No torch: called on every listing.
+own row when its header says DiT. A local DiT's text encoder is taken from the layout's text-encoder
+folders when one fits (``diffusion_comfy_components.pick_comfy_text_encoders``); VAE folders are
+resolved but nothing loads from them. No torch: called on every listing.
"""
from __future__ import annotations
@@ -216,6 +217,58 @@ def comfy_dit_scan_roots(folder: Path) -> tuple[Path, ...]:
return tuple(roots)
+# models/<dit folder>/ plus the scanner's subfolder depth, then the ComfyUI root above models/.
+_MAX_LAYOUT_ANCESTORS = _MAX_SUBDIR_DEPTH + 3
+_MAX_COMPONENT_FILES = 64
+
+
+def _within(path: Path, roots: Iterable[Path]) -> bool:
+ try:
+ real = Path(os.path.realpath(path))
+ except OSError:
+ return False
+ for root in roots:
+ try:
+ base = Path(os.path.realpath(root))
+ except OSError:
+ continue
+ if real == base or base in real.parents:
+ return True
+ return False
+
+
+def comfy_text_encoder_files(dit_path: Path, scan_folders: Iterable[Path] = ()) -> tuple[Path, ...]:
+ """The ``.safetensors`` in the text-encoder folders of the ComfyUI layout whose DiT folders hold
+ ``dit_path``: one found above the file, else a registered ``scan_folders`` entry whose layout lists it
+ (``extra_model_paths.yaml`` pointing outside the tree). Empty when the file is in none. Never raises."""
+ try:
+ dit_path = Path(dit_path)
+ folders = [*list(dit_path.parents)[:_MAX_LAYOUT_ANCESTORS], *map(Path, scan_folders)]
+ except (OSError, ValueError, TypeError):
+ return ()
+ for folder in folders:
+ layout = comfy_layout(folder)
+ if layout is None or not _within(dit_path.parent, layout.dit_dirs):
+ continue
+ out: list[Path] = []
+ for root in layout.text_encoder_dirs:
+ for sub in (root, *_plain_subdirs(root)):
+ try:
+ with os.scandir(sub) as entries:
+ names = sorted(e.name for e in entries if e.is_file())
+ except OSError:
+ continue
+ for name in names:
+ path = sub / name
+ if name.startswith(".") or not is_loose_checkpoint_candidate(path):
+ continue
+ out.append(path)
+ if len(out) >= _MAX_COMPONENT_FILES:
+ return tuple(out)
+ return tuple(out)
+ return ()
+
+
def comfy_role_dirs(folder: Path) -> frozenset[str]:
layout = comfy_layout(folder)
if layout is None:
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 7c3ec4c945c..ae7576ce47b 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -4383,7 +4383,9 @@ class DiffusionLoadRequest(BaseModel):
"model_path, so ../text_encoders/x.safetensors works from a ComfyUI diffusion_models folder) or "
"owner/repo/path.safetensors under the same repo rule as model_path. Unquantized, scaled fp8 and "
"int8 (ConvRot) files load; other ComfyUI formats are refused. Only with a gguf / single_file load; "
- "the base repo then supplies only configs and tokenizers for these encoders.",
+ "the base repo then supplies only configs and tokenizers for these encoders. Unset on a local DiT "
+ "inside a ComfyUI models tree, the tree's text_encoders file that fits is used when exactly one is "
+ "best (UNSLOTH_DIFFUSION_COMFY_TEXT_ENCODERS=0 turns that off).",
)
vae_file: Optional[str] = Field(
None,
diff --git a/studio/backend/tests/test_comfy_auto_text_encoders.py b/studio/backend/tests/test_comfy_auto_text_encoders.py
new file mode 100644
index 00000000000..3b5093bdc41
--- /dev/null
+++ b/studio/backend/tests/test_comfy_auto_text_encoders.py
@@ -0,0 +1,242 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A local DiT inside a ComfyUI tree takes its text encoder from the tree's ``text_encoders/`` folder instead of
+downloading the base repo's (unslothai/unsloth#8950). CPU only, tiny Qwen3 encoders in the ComfyUI layout."""
+
+from __future__ import annotations
+
+import json
+import types
+from pathlib import Path
+
+import pytest
+
+torch = pytest.importorskip("torch")
+safetensors_torch = pytest.importorskip("safetensors.torch")
+transformers = pytest.importorskip("transformers")
+pytest.importorskip("accelerate") # the strict fit builds the encoder under init_empty_weights
+
+import core.inference.diffusion_comfy_components as C # noqa: E402
+
+INDEX = {"text_encoder": ["transformers", "Qwen3Model"], "vae": ["diffusers", "AutoencoderKL"]}
+
+
+def _cfg(hidden = 64):
+ return transformers.Qwen3Config(
+ vocab_size = 64,
+ hidden_size = hidden,
+ intermediate_size = 128,
+ num_hidden_layers = 2,
+ num_attention_heads = 4,
+ num_key_value_heads = 2,
+ head_dim = 16,
+ max_position_embeddings = 32,
+ tie_word_embeddings = False,
+ )
+
+
+def _encoder_file(
+ path: Path,
+ hidden = 64,
+ int8 = False,
+ convrot = False,
+) -> str:
+ torch.manual_seed(0)
+ state = transformers.Qwen3Model(_cfg(hidden)).state_dict()
+ out = {}
+ for key, value in state.items():
+ name = "model." + key
+ if int8 and ".mlp." in key and key.endswith(".weight"):
+ stem = name[: -len(".weight")]
+ out[name] = torch.zeros(value.shape, dtype = torch.int8)
+ out[stem + ".weight_scale"] = torch.ones(value.shape[0])
+ conf = {"format": "int8_tensorwise"}
+ if convrot:
+ conf.update(convrot = True, convrot_groupsize = 16)
+ blob = json.dumps(conf).encode()
+ out[stem + ".comfy_quant"] = torch.tensor(list(blob), dtype = torch.uint8)
+ else:
+ out[name] = value.to(torch.bfloat16)
+ path.parent.mkdir(parents = True, exist_ok = True)
+ safetensors_torch.save_file({k: v.contiguous() for k, v in out.items()}, str(path))
+ return str(path)
+
+
+def _vae_file(path: Path) -> str:
+ path.parent.mkdir(parents = True, exist_ok = True)
+ safetensors_torch.save_file({"decoder.up.0.block.0.conv1.weight": torch.zeros(4, 4)}, str(path))
+ return str(path)
+
+
+def _base(tmp_path: Path, hidden = 64) -> str:
+ base = tmp_path / "base"
+ _cfg(hidden).save_pretrained(base / "text_encoder")
+ (base / "model_index.json").write_text(json.dumps(INDEX), encoding = "utf-8")
+ return str(base)
+
+
+def _fits(base: str, quant = None):
+ return lambda o: C.verify_override_fit(
+ o, base = base, local_files_only = True, text_encoder_quant = quant
+ )
+
+
+def _comfy_tree(tmp_path: Path) -> Path:
+ models = tmp_path / "ComfyUI" / "models"
+ for name in ("diffusion_models", "text_encoders", "vae", "loras", "checkpoints"):
+ (models / name).mkdir(parents = True, exist_ok = True)
+ (models / "diffusion_models" / "dit.safetensors").write_bytes(b"x")
+ return models
+
+
+def test_text_encoder_folders_of_the_dits_own_layout(tmp_path):
+ from hub.utils.comfy_models import comfy_text_encoder_files
+
+ models = _comfy_tree(tmp_path)
+ te = _encoder_file(models / "text_encoders" / "qwen_3_4b.safetensors")
+ legacy = _encoder_file(models / "clip" / "sub" / "t5.safetensors")
+ (models / "text_encoders" / "notes.txt").write_text("x")
+ found = comfy_text_encoder_files(models / "diffusion_models" / "dit.safetensors")
+ assert [str(p) for p in found] == [te, legacy]
+ # A file beside the text encoders, or in a plain folder, is no ComfyUI DiT pick.
+ assert comfy_text_encoder_files(models / "text_encoders" / "qwen_3_4b.safetensors") == ()
+ (tmp_path / "plain").mkdir()
+ assert comfy_text_encoder_files(tmp_path / "plain" / "dit.safetensors") == ()
+ deep = models / "diffusion_models" / "a" / "b" / "c" / "dit.safetensors"
+ assert [str(p) for p in comfy_text_encoder_files(deep)] == [te, legacy]
+
+
+def test_a_dit_folder_listed_by_extra_model_paths_finds_its_tree(tmp_path, monkeypatch):
+ import hub.storage.scan_folders as scan_folders
+ from hub.utils.comfy_models import comfy_text_encoder_files
+
+ # macOS tmp lives under the denied /private/var; the denylist is not what this test checks.
+ monkeypatch.setattr(scan_folders, "is_denied_system_path", lambda path: False)
+
+ models = _comfy_tree(tmp_path)
+ te = _encoder_file(models / "text_encoders" / "qwen_3_4b.safetensors")
+ shared = tmp_path / "shared" / "dits"
+ shared.mkdir(parents = True)
+ (models.parent / "extra_model_paths.yaml").write_text(
+ f"shared:\n diffusion_models: {shared}\n", encoding = "utf-8"
+ )
+ dit = shared / "dit.safetensors"
+ assert comfy_text_encoder_files(dit) == ()
+ assert [str(p) for p in comfy_text_encoder_files(dit, [models.parent])] == [te]
+
+
+def test_the_one_fitting_encoder_is_picked(tmp_path):
+ base = _base(tmp_path)
+ good = _encoder_file(tmp_path / "te" / "qwen_3_4b.safetensors")
+ wrong_size = _encoder_file(tmp_path / "te" / "qwen_3_8b.safetensors", hidden = 128)
+ vae = _vae_file(tmp_path / "te" / "ae.safetensors")
+ junk = tmp_path / "te" / "broken.safetensors"
+ junk.write_bytes(b"not a safetensors file")
+ picked = C.pick_comfy_text_encoders(
+ [wrong_size, vae, str(junk), good], model_index = INDEX, fits = _fits(base)
+ )
+ assert picked == [good]
+
+
+def test_dense_beats_int8_and_a_tie_falls_back_to_the_base_repo(tmp_path):
+ base = _base(tmp_path)
+ dense = _encoder_file(tmp_path / "te" / "qwen_3_4b.safetensors")
+ int8 = _encoder_file(tmp_path / "te" / "qwen_3_4b_int8.safetensors", int8 = True)
+ assert C.pick_comfy_text_encoders([int8, dense], model_index = INDEX, fits = _fits(base)) == [dense]
+ twin = _encoder_file(tmp_path / "te" / "qwen_3_4b_finetune.safetensors")
+ assert C.pick_comfy_text_encoders([dense, twin], model_index = INDEX, fits = _fits(base)) == []
+
+
+def test_auto_scheme_family_never_takes_a_dense_file(tmp_path):
+ base = _base(tmp_path)
+ dense = _encoder_file(tmp_path / "te" / "qwen3vl_8b_bf16.safetensors")
+ plain = _encoder_file(tmp_path / "te" / "qwen3vl_8b_int8.safetensors", int8 = True)
+ convrot = _encoder_file(
+ tmp_path / "te" / "qwen3vl_8b_int8_convrot.safetensors", int8 = True, convrot = True
+ )
+ kwargs = dict(model_index = INDEX, fits = _fits(base), auto_scheme = "int8")
+ # Plain int8 dequantizes on load, so it is as large in memory as the dense file.
+ assert C.pick_comfy_text_encoders([dense, plain], **kwargs) == []
+ assert C.pick_comfy_text_encoders([dense, plain, convrot], **kwargs) == [convrot]
+ assert C.pick_comfy_text_encoders([convrot], **{**kwargs, "auto_scheme": "fp8"}) == []
+
+
+def test_an_explicit_precision_the_file_cannot_take_skips_it(tmp_path):
+ base = _base(tmp_path)
+ int8 = _encoder_file(tmp_path / "te" / "qwen3_int8.safetensors", int8 = True, convrot = True)
+ assert (
+ C.pick_comfy_text_encoders([int8], model_index = INDEX, fits = _fits(base, quant = "fp8")) == []
+ )
+
+
+def _backend_pick(
+ tmp_path,
+ monkeypatch,
+ *,
+ env = None,
+ kind = "single_file",
+):
+ from core.inference import diffusion
+
+ models = _comfy_tree(tmp_path)
+ te = _encoder_file(models / "text_encoders" / "qwen_3_4b.safetensors")
+ base = _base(tmp_path)
+ if env is not None:
+ monkeypatch.setenv("UNSLOTH_DIFFUSION_COMFY_TEXT_ENCODERS", env)
+ fam = types.SimpleNamespace(
+ name = "z-image", single_file_is_pipeline = False, pipeline_only = False, te_quant_auto = None
+ )
+ picked = diffusion.DiffusionBackend._comfy_text_encoders(
+ diffusion.DiffusionBackend.__new__(diffusion.DiffusionBackend),
+ fam,
+ kind,
+ str(models / "diffusion_models"),
+ "dit.safetensors",
+ base,
+ None,
+ local_files_only = True,
+ )
+ return picked, te
+
+
+def test_backend_takes_the_comfy_encoder_for_a_local_dit(tmp_path, monkeypatch):
+ picked, te = _backend_pick(tmp_path, monkeypatch)
+ assert picked == [str(Path(te))]
+
+
[email protected]("env, kind", [("0", "single_file"), (None, "pipeline")])
+def test_backend_opt_out_and_pipeline_loads_keep_the_base_repo(tmp_path, monkeypatch, env, kind):
+ picked, _ = _backend_pick(tmp_path, monkeypatch, env = env, kind = kind)
+ assert picked == []
+
+
+def test_download_plan_drops_the_base_encoder_a_comfy_file_replaces(tmp_path, monkeypatch):
+ """Through the plan the Images page stages: the base repo's text_encoder/ is no longer fetched."""
+ from core.inference import diffusion
+
+ models = tmp_path / "ComfyUI" / "models"
+ for name in ("diffusion_models", "text_encoders", "vae", "loras"):
+ (models / name).mkdir(parents = True, exist_ok = True)
+ (models / "diffusion_models" / "z_image_turbo_bf16.safetensors").write_bytes(b"x")
+ _encoder_file(models / "text_encoders" / "qwen_3_4b.safetensors")
+ base = _base(tmp_path)
+ seen = {}
+
+ def _estimate(*args, **kwargs):
+ seen.update(kwargs)
+ return 0, []
+
+ monkeypatch.setattr(
+ diffusion.DiffusionBackend, "_estimate_download_bytes", staticmethod(_estimate)
+ )
+ monkeypatch.setattr(diffusion, "_resolve_base_repo", lambda *a, **k: base)
+ backend = diffusion.DiffusionBackend.__new__(diffusion.DiffusionBackend)
+ backend.download_plan(
+ str(models / "diffusion_models"),
+ gguf_filename = "z_image_turbo_bf16.safetensors",
+ model_kind = "single_file",
+ allow_device_probe = False,
+ memory_verdict = False,
+ )
+ assert "text_encoder" in seen["skip_te_components"]