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: list a Qwen-Image-2.1-Turbo base pipeline cached in its FP8 mirror On Device (#15772)
Daniel Han56744bfbb9a8
diff --git a/studio/backend/hub/services/models/cache_inventory.py b/studio/backend/hub/services/models/cache_inventory.pyindex d12875e5292..e1d7b758a3e 100644--- a/studio/backend/hub/services/models/cache_inventory.py+++ b/studio/backend/hub/services/models/cache_inventory.py@@ -1246,10 +1246,8 @@ def _scan_cached_models(cross_root_folders = any_root_gguf and _hosts_artifacts(repo_id)# The FP8 artifact mirror (unsloth/Qwen-Image-2.1-Turbo-FP8): its folders are what a pick of the BASE# fetches, and a pipeline pick of the repo itself is refused, so the row stays the root checkpoint row- # released builds listed, whatever part of the folders is cached.+ # released builds listed.artifact_checkpoint = not any_root_gguf and _hosts_artifacts(repo_id)- key = repo_id.lower()- existing = seen_lower.get(key)identity = _resolve_load_identity(repo_id,repo_path = repo_path,@@ -1258,6 +1256,22 @@ def _scan_cached_models(payload_snapshots = payload.payload_snapshots,)load_snapshot = identity.load_snapshot+ # Unless the whole base pipeline is cached: then the row is that base pick, listed and loaded by the base id.+ base_pipeline = (+ artifact_checkpoint+ and hf_cache_scan.snapshot_has_pipeline_index(load_snapshot)+ and not hf_cache_scan.snapshot_pipeline_missing_denoiser(load_snapshot)+ )+ cached_id = repo_id+ if base_pipeline:+ from core.inference.diffusion_families import canonical_base++ artifact_checkpoint = False+ repo_id = canonical_base(cached_id)+ if identity.load_id == cached_id:+ identity = identity._replace(load_id = repo_id)+ key = repo_id.lower()+ existing = seen_lower.get(key)local_metadata = (snapshot_metadataif load_snapshot == snapshot_path@@ -1269,13 +1283,13 @@ def _scan_cached_models(# Scoped to the row's snapshot, so an incomplete newer revision cannot flip can_chat.download_partial = hf_cache_scan.is_snapshot_partial("model",- repo_id,+ cached_id,repo_path,snapshot_dir = load_snapshot,variant_state = (variant_states.for_repo("model",- repo_id,+ cached_id,hub_cache = repo_path.parent,)if variant_states is not None@@ -1342,7 +1356,7 @@ def _scan_cached_models("partial_transport": (hf_cache_scan.partial_transport_for("model",- repo_id,+ cached_id,repo_cache_dir = repo_path,)# Only a genuine download partial has a transport; a companion-only snapshot arrived intact and has no Resume story.@@ -1352,7 +1366,7 @@ def _scan_cached_models("partial_resumable": (hf_cache_scan.partial_resume_available("model",- repo_id,+ cached_id,repo_cache_dir = repo_path,)if download_partial@@ -1368,7 +1382,7 @@ def _scan_cached_models(),# Listed so tens of GB of companion weights stay visible and deletable, but flagged so no picker offers a denoiser-less repo as a load."companion": cross_root_folders- or _cached_row_companion(repo_id, load_snapshot),+ or _cached_row_companion(cached_id, load_snapshot),"diffusers": _cached_row_is_diffusers(repo_info, load_snapshot),**local_metadata,}@@ -1460,6 +1474,39 @@ def artifact_folder_bytes(*repo_paths: Path) -> int:return sum(seen.values())+def artifact_mirror_delete_id(repo_id: str, cache_path: Optional[str]) -> Optional[str]:+ """Quant-free artifact mirror copy listed under base *repo_id*, else None."""+ try:+ from core.inference.diffusion_families import mirror_repo++ mirror = mirror_repo(repo_id)+ if not mirror or not _hosts_artifacts(mirror):+ return None+ target = None+ if cache_path:+ name = f"models--{mirror.replace('/', '--')}".lower()+ path = Path(cache_path).expanduser().resolve(strict = False)+ target = next((p for p in (path, *path.parents) if p.name.lower() == name), None)+ if target is None:+ return None+ copies = []+ for hf_cache in all_hf_cache_scans():+ for repo in getattr(hf_cache, "repos", ()) or ():+ if str(repo.repo_type) != "model":+ continue+ if target is None and repo.repo_id.lower() == repo_id.lower():+ return None+ if repo.repo_id.lower() == mirror.lower() and (+ target is None or Path(repo.repo_path).resolve(strict = False) == target+ ):+ copies.append(repo)+ if not copies or any(_repo_has_gguf_files(repo) for repo in copies):+ return None+ return copies[0].repo_id+ except Exception: # noqa: BLE001+ return None++def _artifact_gguf_repo_ids(cache_scans) -> set[str]:"""Lowercased ids of artifact mirrors with a main GGUF in ANY cache root."""found: set[str] = set()diff --git a/studio/backend/hub/services/models/deletion.py b/studio/backend/hub/services/models/deletion.pyindex 7ff522f93a5..53489f8e0e9 100644--- a/studio/backend/hub/services/models/deletion.py+++ b/studio/backend/hub/services/models/deletion.py@@ -1077,16 +1077,25 @@ async def delete_cached_model_response(detail = f"Invalid gguf_variant: {variant!r}",)guard_id = (await asyncio.to_thread(folder_row_for_download, repo_id, variant) or (repo_id,))[0]+ # A base listed from its artifact mirror's copy deletes that copy, so guard both identities.+ mirror_id = (+ await asyncio.to_thread(cache_inventory.artifact_mirror_delete_id, repo_id, cache_path)+ if variant is None+ else None+ )+ guard_ids = (guard_id, mirror_id) if mirror_id else (guard_id,)# Fail closed with 503 rather than unlink weights under a running process.def _load_state_blocks_delete() -> Optional[str]:- if _llama_cpp_blocks_delete(guard_id, variant) or (- _inference_backend_blocks_delete(guard_id)- ):- return "Unload the model before deleting"- if _audio_cpp_blocks_delete(guard_id):- return "Unload the audio model before deleting"- return _diffusion_blocks_delete(guard_id) or _video_blocks_delete(guard_id)+ for gid in guard_ids:+ if _llama_cpp_blocks_delete(gid, variant) or _inference_backend_blocks_delete(gid):+ return "Unload the model before deleting"+ if _audio_cpp_blocks_delete(gid):+ return "Unload the audio model before deleting"+ blocked = _diffusion_blocks_delete(gid) or _video_blocks_delete(gid)+ if blocked:+ return blocked+ return Nonetry:blocks_detail = await asyncio.to_thread(_load_state_blocks_delete)@@ -1103,13 +1112,23 @@ async def delete_cached_model_response()repo_key = await asyncio.to_thread(resolve_cached_repo_id_case, repo_id, repo_type = "model")- if not downloads.registry.begin_delete(repo_key, variant):- detail = (- f"Cancel the {variant} download before deleting it."- if variant is not None- else "Cancel the active downloads before deleting."+ reserved_keys = [repo_key]+ if mirror_id:+ reserved_keys.append(+ await asyncio.to_thread(resolve_cached_repo_id_case, mirror_id, repo_type = "model"))- raise HTTPException(status_code = 400, detail = detail)+ reserved: list[str] = []+ for key in reserved_keys:+ if not downloads.registry.begin_delete(key, variant):+ for held in reserved:+ downloads.registry.end_delete(held, variant)+ detail = (+ f"Cancel the {variant} download before deleting it."+ if variant is not None+ else "Cancel the active downloads before deleting."+ )+ raise HTTPException(status_code = 400, detail = detail)+ reserved.append(key)try:# recheck after reserving the scope so a concurrent load claim is visible.try:@@ -1140,7 +1159,7 @@ async def delete_cached_model_response()result = await asyncio.to_thread(_delete_cached_model_blocking,- repo_id,+ mirror_id or repo_id,variant,hf_token,cache_path,@@ -1160,9 +1179,10 @@ async def delete_cached_model_response(from core.inference.audio_cpp_models import forgetforget()- return result+ return {**result, "repo_id": repo_id} if mirror_id else resultfinally:- downloads.registry.end_delete(repo_key, variant)+ for key in reserved:+ downloads.registry.end_delete(key, variant)cache_inventory.invalidate_hf_cache_scans()diff --git a/studio/backend/tests/test_artifact_mirrors.py b/studio/backend/tests/test_artifact_mirrors.pyindex 4d663175352..96968dc1c2f 100644--- a/studio/backend/tests/test_artifact_mirrors.py+++ b/studio/backend/tests/test_artifact_mirrors.py@@ -208,6 +208,10 @@ def test_the_fp8_artifact_mirror_lists_as_its_checkpoint_not_as_a_pipeline(Whatever part of them is cached, the row is the single-file checkpoint row (the shape the compatibility listinggave the FP8 repo before the folders arrived): visible and deletable, never offered as a pipeline load the backendrejects, never a companion-prefetch partial."""+ if cached == "full" and scan is _inventory_row:+ pytest.skip(+ "a whole base pipeline lists as the base: test_a_base_pipeline_cached_in_an_artifact_mirror_..."+ )files = {**_FP8_ROOT, **_FOLDERS, **(_DENOISER if cached == "full" else {})}row = scan(monkeypatch, tmp_path, _FP8_REPO, files)assert row["single_file"] is True@@ -927,3 +931,197 @@ def test_artifact_folder_bytes_without_inodes_keys_by_blob_path(monkeypatch, tmpmonkeypatch.setattr(Path, "stat", no_inode)assert cache_inventory.artifact_folder_bytes(repo_dir) == 800+++def _scan_rows(monkeypatch, cached_repos, active_hub_cache: Path) -> list[dict]:+ from types import SimpleNamespace++ from hub.services.models import cache_inventory++ scans = [SimpleNamespace(repos = list(cached_repos))]+ monkeypatch.setattr(cache_inventory, "all_hf_cache_scans", lambda: scans)+ monkeypatch.setattr(+ cache_inventory.hf_cache_scan, "is_snapshot_partial", lambda *_a, **_k: False+ )+ monkeypatch.setattr(cache_inventory, "_cached_row_task", lambda *_a, **_k: "text-to-image")+ return cache_inventory._scan_cached_models(active_hub_cache = active_hub_cache)+++_CASED_ARTIFACT_MIRRORS = sorted(m for _u, m in df._MIRROR_PAIRS if df.mirror_hosts_artifacts(m))++[email protected]("root_checkpoint", [False, True])[email protected]("repo", _CASED_ARTIFACT_MIRRORS)+def test_a_base_pipeline_cached_in_an_artifact_mirror_lists_as_the_base(+ monkeypatch, tmp_path, repo, root_checkpoint+):+ """A cached base pipeline in the FP8 mirror lists, loads, sizes and deletes as the base."""+ files = {**_FOLDERS, **_DENOISER, **(_FP8_ROOT if root_checkpoint else {})}+ cached = _cache_repo(tmp_path, repo, files)+ rows = _scan_rows(monkeypatch, [cached], tmp_path)+ base = df.canonical_base(repo)+ assert base.lower() != repo.lower()+ assert [r["repo_id"] for r in rows] == [base]+ row = rows[0]+ assert row["single_file"] is False and not row["partial"] and not row["companion"]+ assert row["load_id"] == base+ assert row["cache_path"] == str(cached.repo_path)+ assert row["size_bytes"] == sum(v for k, v in files.items() if k.endswith(".safetensors"))+++def test_a_base_pipeline_in_a_mirror_outside_the_active_cache_pins_its_snapshot(+ monkeypatch, tmp_path+):+ cached = _cache_repo(tmp_path / "old", _FP8_REPO, {**_FOLDERS, **_DENOISER})+ rows = _scan_rows(monkeypatch, [cached], tmp_path / "live")+ assert [r["repo_id"] for r in rows] == ["Qwen/Qwen-Image-2.1-Turbo"]+ assert rows[0]["load_id"] == str(Path(cached.revisions[0].snapshot_path).resolve())+ assert rows[0]["single_file"] is False and rows[0]["active_cache"] is False+++def test_an_artifact_mirror_without_the_denoiser_keeps_its_checkpoint_row(monkeypatch, tmp_path):+ """Folders without the denoiser do not load as the base, so the row stays the mirror's checkpoint row."""+ cached = _cache_repo(tmp_path, _FP8_REPO, {**_FP8_ROOT, **_FOLDERS})+ rows = _scan_rows(monkeypatch, [cached], tmp_path)+ assert [(r["repo_id"], r["single_file"]) for r in rows] == [(_FP8_REPO, True)]+++def test_deleting_the_base_row_targets_the_artifact_mirror_copy(monkeypatch, tmp_path):+ from types import SimpleNamespace++ from hub.services.models import cache_inventory++ fp8 = _cache_repo(tmp_path, _FP8_REPO, {**_FOLDERS, **_DENOISER})+ gguf = _cache_repo(tmp_path, "unsloth/Z-Image-GGUF", {"z-image-Q4_K_M.gguf": 5, **_FOLDERS})+ scans = [SimpleNamespace(repos = [fp8, gguf])]+ monkeypatch.setattr(cache_inventory, "all_hf_cache_scans", lambda: scans)+ resolve = cache_inventory.artifact_mirror_delete_id+ assert resolve("Qwen/Qwen-Image-2.1-Turbo", str(fp8.repo_path)) == _FP8_REPO+ assert resolve("Qwen/Qwen-Image-2.1-Turbo", None) == _FP8_REPO+ assert (+ resolve("Qwen/Qwen-Image-2.1-Turbo", str(tmp_path / "models--Qwen--Qwen-Image-2.1-Turbo"))+ is None+ )+ assert resolve(_FP8_REPO, str(fp8.repo_path)) is None+ assert resolve("Qwen/Qwen-Image-2512", None) is None+ # A -GGUF mirror holding a quant: its folders belong to the GGUF row, never deleted through the base.+ assert resolve("Tongyi-MAI/Z-Image", str(gguf.repo_path)) is None+ assert resolve("Tongyi-MAI/Z-Image", None) is None+++def test_the_delete_route_passes_the_mirror_id_to_the_destructive_step(monkeypatch, tmp_path):+ import asyncio++ from hub.services.models import cache_inventory, deletion++ fp8 = _cache_repo(tmp_path, _FP8_REPO, {**_FOLDERS, **_DENOISER})+ from types import SimpleNamespace++ monkeypatch.setattr(+ cache_inventory, "all_hf_cache_scans", lambda: [SimpleNamespace(repos = [fp8])]+ )+ monkeypatch.setattr(deletion.account_access, "require_installation_owner", lambda: None)+ seen = {}++ def fake_delete(+ repo_id,+ variant,+ hf_token,+ cache_path = None,+ **_kw,+ ):+ seen["repo_id"] = repo_id+ return {"status": "deleted", "repo_id": repo_id}++ monkeypatch.setattr(deletion, "_delete_cached_model_blocking", fake_delete)+ result = asyncio.run(+ deletion.delete_cached_model_response(+ "Qwen/Qwen-Image-2.1-Turbo", None, None, str(fp8.repo_path)+ )+ )+ assert seen["repo_id"] == _FP8_REPO+ assert result["repo_id"] == "Qwen/Qwen-Image-2.1-Turbo"+++def _mirror_delete_route(monkeypatch, tmp_path):+ from types import SimpleNamespace++ from hub.services.models import cache_inventory, deletion++ fp8 = _cache_repo(tmp_path, _FP8_REPO, {**_FOLDERS, **_DENOISER})+ monkeypatch.setattr(+ cache_inventory, "all_hf_cache_scans", lambda: [SimpleNamespace(repos = [fp8])]+ )+ monkeypatch.setattr(deletion.account_access, "require_installation_owner", lambda: None)+ for name in (+ "_llama_cpp_blocks_delete",+ "_inference_backend_blocks_delete",+ "_audio_cpp_blocks_delete",+ ):+ monkeypatch.setattr(deletion, name, lambda *_a, **_k: False)+ monkeypatch.setattr(deletion, "_video_blocks_delete", lambda *_a, **_k: None)+ deleted = []+ monkeypatch.setattr(+ deletion,+ "_delete_cached_model_blocking",+ lambda repo_id, *_a, **_k: deleted.append(repo_id)+ or {"status": "deleted", "repo_id": repo_id},+ )+ return deletion, fp8, deleted+++def test_a_load_from_the_mirror_snapshot_blocks_deleting_the_base_row(monkeypatch, tmp_path):+ import asyncio++ from fastapi import HTTPException++ deletion, fp8, deleted = _mirror_delete_route(monkeypatch, tmp_path)+ snapshot = str(fp8.revisions[0].snapshot_path)+ monkeypatch.setattr(+ deletion,+ "iter_repo_cache_dirs",+ lambda _t, rid: [Path(fp8.repo_path)] if rid.lower() == _FP8_REPO.lower() else [],+ )+ monkeypatch.setattr(+ deletion,+ "_diffusion_blocks_delete",+ lambda gid: "Unload the model before deleting"+ if deletion._loaded_id_matches_repo(snapshot, gid)+ else None,+ )+ with pytest.raises(HTTPException) as exc:+ asyncio.run(+ deletion.delete_cached_model_response(+ "Qwen/Qwen-Image-2.1-Turbo", None, None, str(fp8.repo_path)+ )+ )+ assert exc.value.status_code == 400 and deleted == []+++def test_a_download_into_the_mirror_blocks_deleting_the_base_row(monkeypatch, tmp_path):+ import asyncio++ from fastapi import HTTPException++ deletion, fp8, deleted = _mirror_delete_route(monkeypatch, tmp_path)+ monkeypatch.setattr(deletion, "_diffusion_blocks_delete", lambda *_a, **_k: None)+ held, released = [], []++ def begin_delete(key, variant):+ if key.lower() == _FP8_REPO.lower():+ return False+ held.append(key)+ return True++ monkeypatch.setattr(deletion.downloads.registry, "begin_delete", begin_delete)+ monkeypatch.setattr(+ deletion.downloads.registry, "end_delete", lambda key, _v: released.append(key)+ )+ with pytest.raises(HTTPException) as exc:+ asyncio.run(+ deletion.delete_cached_model_response(+ "Qwen/Qwen-Image-2.1-Turbo", None, None, str(fp8.repo_path)+ )+ )+ assert exc.value.status_code == 400 and deleted == []+ assert held == released == ["Qwen/Qwen-Image-2.1-Turbo"]