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: show the hosted text encoder download in image load progress (#12894)

Etherla5bf34bf34fe
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index be678e65c4f..ca5aef26585 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -3386,6 +3386,15 @@ class DiffusionBackend:
kwargs["_te_prequant_resolved"] = bool(te_prequant_files)
if dit_prequant is not None:
expected += int(dit_prequant[2])
+ # load_pipeline downloads the pre-cast encoder. A mirrored file is read in place, never cached.
+ from .diffusion_te_prequant import te_prequant_unmirrored
+
+ te_hub_files = [
+ (repo, name, int(size or 0))
+ for repo, files in te_prequant_files.values()
+ for name, size in te_prequant_unmirrored(repo, files)
+ ]
+ expected += sum(size for _repo, _name, size in te_hub_files)
# Only shards this prefetch staged may be materialised by the dense fallback, so read it off the staged
# list: a failed size estimate drops every base file too. A LOCAL base directory has no listing to fail at
# (model_info raises on a path) and its shards are already there, so it counts as staged on the filesystem
@@ -3449,10 +3458,7 @@ class DiffusionBackend:
self._loading.fetch_repo = fetch_base
self._loading.expected_bytes = expected
if skip_transformer_weights:
- # Claimed before a byte moves: a mid-fetch delete would leave this load with nothing.
- self._loading.asset_repos = tuple(
- dict.fromkeys(self._loading.asset_repos + (dit_prequant[0],))
- )
+ # Claimed before a byte moves. File entry first: load_progress reads lock-free.
self._loading.asset_files += (
(
dit_prequant[0],
@@ -3461,12 +3467,32 @@ class DiffusionBackend:
asset_baseline,
),
)
+ self._loading.asset_repos = tuple(
+ dict.fromkeys(self._loading.asset_repos + (dit_prequant[0],))
+ )
if skip_transformer_weights:
self._fetch_denoiser_prequant(
dit_prequant,
kwargs.get("hf_token"),
cancel_event = cancel_event,
)
+ if te_hub_files:
+ # Baselined AFTER the denoiser fetch: one repo can hold both, and its bytes are not the encoder's.
+ te_baselines = {
+ repo: self._cache_bytes(repo) for repo in {r for r, _n, _s in te_hub_files}
+ }
+ with self._load_cancel_lock:
+ if self._load_token == token and self._loading is not None:
+ self._loading.asset_files += tuple(
+ (repo, name, size, te_baselines[repo])
+ for repo, name, size in te_hub_files
+ )
+ self._loading.asset_repos = tuple(
+ dict.fromkeys(
+ self._loading.asset_repos
+ + tuple(repo for repo, _n, _s in te_hub_files)
+ )
+ )
# Download outside the lock so unload/an eviction can preempt the pull. The carried snapshot is the
# fallback, never the override: it fires only when the estimate came back empty, since the metadata that
# fills it is the same call whose failure earned the escape. Without it the load 401s with every byte
@@ -3557,11 +3583,14 @@ class DiffusionBackend:
downloaded = self._cache_bytes(loading.repo_id)
if companion and companion != loading.repo_id:
downloaded += self._cache_bytes(companion)
- scoped = {entry[0] for entry in loading.asset_files}
- for asset in loading.asset_repos:
+ # Repos read before files (_run_load stores files first), so every repo seen has its file entry.
+ asset_repos = loading.asset_repos
+ asset_files = loading.asset_files
+ scoped = {entry[0] for entry in asset_files}
+ for asset in asset_repos:
if asset and asset not in (loading.repo_id, companion) and asset not in scoped:
downloaded += self._cache_bytes(asset)
- for repo, filename, size, baseline in loading.asset_files:
+ for repo, filename, size, baseline in asset_files:
if not repo or repo in (loading.repo_id, companion):
continue
# The finished file plus what this load added since it claimed the repo (in-flight
@@ -10646,9 +10675,16 @@ class DiffusionBackend:
loading = self._loading
if loading is not None and loading.error is None:
# _run_load's finally drops this, so it spans the prefetch too, where nothing
- # is registered in _load_accounts.
+ # is registered in _load_accounts. Asset repos too: the encoder download ignores the cancel event.
self._draining_repos.setdefault(cancelled_token, set()).update(
- r for r in (loading.repo_id, loading.base_repo, loading.fetch_repo) if r
+ r
+ for r in (
+ loading.repo_id,
+ loading.base_repo,
+ loading.fetch_repo,
+ *loading.asset_repos,
+ )
+ if r
)
self._cancel_event.set()
self._load_token += 1
diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.py
index 54f713b8287..ebafc8edb4c 100644
--- a/studio/backend/tests/test_diffusion_backend.py
+++ b/studio/backend/tests/test_diffusion_backend.py
@@ -4878,6 +4878,223 @@ def test_run_load_counts_a_complete_local_base_as_staged(monkeypatch, tmp_path):
assert dmod._local_base_transformer_present(None) is False
+_TE_REPO = "unsloth/Qwen-Image-2.1-FP8"
+_TE_FILE = "Qwen-Image-2.1-text_encoder-INT8-ConvRot.safetensors"
+
+
+def _stub_te_run_load(
+ monkeypatch,
+ *,
+ base,
+ base_bytes,
+ cache,
+ file_bytes,
+ dit_prequant = None,
+):
+ from core.inference import diffusion as dmod
+
+ monkeypatch.setattr(dmod, "_resolve_base_repo", lambda *a, **k: base)
+ monkeypatch.setattr(
+ DiffusionBackend,
+ "_te_prequant_plan_files",
+ staticmethod(lambda *a, **k: {"text_encoder": (_TE_REPO, [(_TE_FILE, 900)])}),
+ )
+ monkeypatch.setattr(
+ DiffusionBackend, "_pipeline_planned_denoiser_scheme", lambda self, *a, **k: None
+ )
+ monkeypatch.setattr(
+ DiffusionBackend, "_dit_prequant_plan_source", lambda self, *a, **k: dit_prequant
+ )
+ monkeypatch.setattr(
+ DiffusionBackend, "declared_footprint_shortfall", lambda self, *a, **k: None
+ )
+ monkeypatch.setattr(dmod, "_assert_base_repo_accessible", lambda *a, **k: None)
+ monkeypatch.setattr(dmod, "prefer_ungated_mirror", lambda base, *a, **k: base)
+ monkeypatch.setattr(dmod, "assert_flux2_pick_compatible", lambda *a, **k: None)
+ monkeypatch.setattr(dmod, "assert_pick_is_not_speech", lambda *a, **k: None)
+ monkeypatch.setattr(
+ DiffusionBackend,
+ "_estimate_download_bytes",
+ staticmethod(lambda *a, **k: (base_bytes, ["model_index.json", "vae/x.safetensors"])),
+ )
+ monkeypatch.setattr(DiffusionBackend, "_prefetch_files", lambda self, *a, **k: None)
+ monkeypatch.setattr(
+ DiffusionBackend, "_cache_bytes", staticmethod(lambda repo: cache.get(repo, 0))
+ )
+ monkeypatch.setattr(
+ DiffusionBackend,
+ "_cache_file_bytes",
+ staticmethod(lambda repo, f: file_bytes.get((repo, f), 0)),
+ )
+
+
+def _run_te_load(monkeypatch, on_load, **run_kwargs):
+ backend = DiffusionBackend()
+ monkeypatch.setattr(DiffusionBackend, "load_pipeline", lambda self, **kw: on_load(self))
+ backend._loading = _LoadingState(repo_id = run_kwargs["repo_id"], base_repo = None)
+ backend._load_token = 7
+ backend._run_load(_load_token = 7, **run_kwargs)
+ return backend
+
+
+def test_run_load_reports_the_pre_cast_encoder_download_as_downloading(monkeypatch):
+ # GGUF + base cached; a cached fp8 sibling in the encoder repo must not stand in for the int8 file.
+ cache = {"unsloth/Qwen-Image-2.1-GGUF": 700, "Qwen/Qwen-Image-2.1": 300, _TE_REPO: 950}
+ file_bytes: dict = {}
+ _stub_te_run_load(
+ monkeypatch,
+ base = "Qwen/Qwen-Image-2.1",
+ base_bytes = 1000,
+ cache = cache,
+ file_bytes = file_bytes,
+ )
+ seen = []
+ repo_ids = []
+
+ def on_load(backend):
+ seen.append(backend.load_progress())
+ repo_ids.extend(backend.loading_repo_ids())
+ cache[_TE_REPO] = 950 + 400 # 400 bytes in flight as an .incomplete blob
+ seen.append(backend.load_progress())
+ file_bytes[(_TE_REPO, _TE_FILE)] = 900
+ cache[_TE_REPO] = 950 + 900
+ seen.append(backend.load_progress())
+
+ _run_te_load(
+ monkeypatch,
+ on_load,
+ repo_id = "unsloth/Qwen-Image-2.1-GGUF",
+ gguf_filename = "qwen-image-2.1-Q8_0.gguf",
+ model_kind = "gguf",
+ )
+ assert [(p["phase"], p["bytes_downloaded"], p["bytes_total"]) for p in seen] == [
+ ("downloading", 1000, 1900),
+ ("downloading", 1400, 1900),
+ ("finalizing", 1900, 1900),
+ ]
+ assert _TE_REPO in repo_ids # the delete-cached guard covers the encoder repo too
+
+
+def test_run_load_credits_a_shared_prequant_repo_once(monkeypatch):
+ # One repo hosts denoiser and encoder; a pre-fetch encoder baseline would count the denoiser twice.
+ dit_file = "Qwen-Image-2.1-INT8.safetensors"
+ cache = {"unsloth/Qwen-Image-2.1": 300}
+ file_bytes: dict = {}
+ _stub_te_run_load(
+ monkeypatch,
+ base = "unsloth/Qwen-Image-2.1",
+ base_bytes = 300,
+ cache = cache,
+ file_bytes = file_bytes,
+ dit_prequant = (_TE_REPO, dit_file, 700),
+ )
+
+ def fetch(self, *a, **k):
+ cache[_TE_REPO] = 700
+ file_bytes[(_TE_REPO, dit_file)] = 700
+
+ monkeypatch.setattr(DiffusionBackend, "_fetch_denoiser_prequant", fetch)
+ seen = []
+ _run_te_load(
+ monkeypatch,
+ lambda backend: seen.append(backend.load_progress()),
+ repo_id = "unsloth/Qwen-Image-2.1",
+ model_kind = "pipeline",
+ )
+ assert (seen[0]["phase"], seen[0]["bytes_downloaded"], seen[0]["bytes_total"]) == (
+ "downloading",
+ 1000,
+ 1900,
+ )
+
+
+def test_run_load_leaves_a_mirrored_pre_cast_encoder_out_of_the_download(monkeypatch):
+ # A mirrored encoder never reaches the HF cache, so counting it would stall the bar short of 100%.
+ from core.inference import diffusion_te_prequant
+
+ monkeypatch.setattr(
+ diffusion_te_prequant, "te_prequant_mirror_path", lambda repo, name: "/mirror/" + name
+ )
+ cache = {"unsloth/Qwen-Image-2.1-GGUF": 700, "Qwen/Qwen-Image-2.1": 300}
+ _stub_te_run_load(
+ monkeypatch,
+ base = "Qwen/Qwen-Image-2.1",
+ base_bytes = 1000,
+ cache = cache,
+ file_bytes = {},
+ )
+ seen = []
+
+ def on_load(backend):
+ seen.append(backend.load_progress())
+ seen.append(backend._loading.asset_files)
+
+ _run_te_load(
+ monkeypatch,
+ on_load,
+ repo_id = "unsloth/Qwen-Image-2.1-GGUF",
+ gguf_filename = "qwen-image-2.1-Q8_0.gguf",
+ model_kind = "gguf",
+ )
+ assert (seen[0]["phase"], seen[0]["bytes_total"]) == ("finalizing", 1000)
+ assert seen[1] == ()
+
+
+class _PollOnClaimState(_LoadingState):
+ def __setattr__(self, name, value):
+ super().__setattr__(name, value)
+ if name in ("asset_repos", "asset_files") and getattr(self, "backend", None) is not None:
+ self.polls.append(self.backend.load_progress()["phase"])
+
+
[email protected]("claim", ["encoder", "denoiser"])
+def test_run_load_never_shows_a_claimed_repo_without_its_file(monkeypatch, claim):
+ # A repo claimed with no file entry is counted whole (cached siblings included).
+ dit_repo = "unsloth/Qwen-Image-2.1-DiT"
+ cache = {"unsloth/Qwen-Image-2.1": 300}
+ cache[_TE_REPO if claim == "encoder" else dit_repo] = 1700
+ _stub_te_run_load(
+ monkeypatch,
+ base = "unsloth/Qwen-Image-2.1",
+ base_bytes = 300,
+ cache = cache,
+ file_bytes = {},
+ dit_prequant = (dit_repo, "Qwen-Image-2.1-INT8.safetensors", 700),
+ )
+ monkeypatch.setattr(DiffusionBackend, "_fetch_denoiser_prequant", lambda self, *a, **k: None)
+ seen = []
+ monkeypatch.setattr(
+ DiffusionBackend, "load_pipeline", lambda self, **kw: seen.append(self.load_progress())
+ )
+ backend = DiffusionBackend()
+ state = _PollOnClaimState(repo_id = "unsloth/Qwen-Image-2.1", base_repo = None)
+ state.polls = []
+ state.backend = backend
+ backend._loading = state
+ backend._load_token = 7
+ backend._run_load(_load_token = 7, repo_id = "unsloth/Qwen-Image-2.1", model_kind = "pipeline")
+ assert state.polls and set(state.polls) == {"downloading"}
+ assert seen[0]["phase"] == "downloading"
+
+
+def test_load_progress_reads_a_claimed_repo_before_its_file(monkeypatch):
+ class _ClaimLandsMidPoll(_LoadingState):
+ def __getattribute__(self, name):
+ value = super().__getattribute__(name)
+ if name == "asset_files" and not value:
+ self.asset_files = ((_TE_REPO, _TE_FILE, 900, 950),)
+ self.asset_repos = (_TE_REPO,)
+ return value
+
+ monkeypatch.setattr(
+ DiffusionBackend, "_cache_bytes", staticmethod(lambda repo: {_TE_REPO: 950}.get(repo, 0))
+ )
+ monkeypatch.setattr(DiffusionBackend, "_cache_file_bytes", staticmethod(lambda repo, f: 0))
+ backend = DiffusionBackend()
+ backend._loading = _ClaimLandsMidPoll(repo_id = "org/pick", base_repo = None, expected_bytes = 900)
+ assert backend.load_progress()["phase"] == "downloading"
+
+
def test_the_widening_decision_is_taken_on_the_repo_listing(monkeypatch):
# The widening turns on which repo the fetch resolves to and on whether EVERY transformer shard
# is cached, and only the base repo's listing answers either. So the estimate defers to a
diff --git a/studio/backend/tests/test_diffusion_eject_ownership.py b/studio/backend/tests/test_diffusion_eject_ownership.py
index 22f486b93ec..efd186d9f0b 100644
--- a/studio/backend/tests/test_diffusion_eject_ownership.py
+++ b/studio/backend/tests/test_diffusion_eject_ownership.py
@@ -263,6 +263,18 @@ def test_a_cancelled_load_still_reports_the_repos_it_is_reading(backend):
assert backend.draining_repo_ids() == ()
+def test_a_cancelled_load_still_reports_its_claimed_asset_repos(backend):
+ """The encoder download ignores the cancel event, so its repo stays undeletable after an eject."""
+ backend._loading = _LoadingState(
+ repo_id = "unsloth/Qwen-Image-2.1-GGUF",
+ base_repo = "Qwen/Qwen-Image-2.1",
+ account_id = ALICE,
+ asset_repos = ("unsloth/Qwen-Image-2.1-FP8",),
+ )
+ backend.unload(expected_account = ALICE)
+ assert "unsloth/Qwen-Image-2.1-FP8" in backend.draining_repo_ids()
+
+
def test_the_load_thread_releases_its_own_drain(backend, monkeypatch):
"""Nothing else knows when a prefetch returned, and during one there is no record to key on."""
backend._loading = _LoadingState(