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: fix two first-use regressions from deferred startup hardware detection (#16629)
Daniel Han23907add511f
diff --git a/studio/backend/core/inference/diffusion_engine_router.py b/studio/backend/core/inference/diffusion_engine_router.pyindex 2f7d0dcedf8..fc63f451b0b 100644--- a/studio/backend/core/inference/diffusion_engine_router.py+++ b/studio/backend/core/inference/diffusion_engine_router.py@@ -1163,9 +1163,12 @@ def family_buildable_here(*,model_kind: Optional[str],gguf_filename: Optional[str] = None,+ provisional: bool = False,) -> bool:"""True when THIS host can actually build ``fam`` for a ``model_kind`` load, by either engine.+ ``provisional`` (listing before detection) answers engine predictions True so detection never starts.+The two engines need different things. diffusers instantiates ``fam.pipeline_class``, which thenewer families ship only in a newer diffusers -- and packaging still allows an older one onPython 3.9, whose ceiling predates them. The native sd.cpp engine assembles the same GGUF from@@ -1182,26 +1185,30 @@ def family_buildable_here(``predict_engine`` activates nothing and installs nothing."""if fam is None:return False- if native_gguf_only(fam, model_kind):++ def predicts(engine_name: str, **kwargs) -> bool:+ if provisional:+ return Truetry:- return predict_engine(fam, model_kind = "gguf") == ENGINE_SD_CPP- except Exception: # noqa: BLE001 -- refused or unprobeable: not buildable here+ return predict_engine(fam, model_kind = "gguf", **kwargs) == engine_name+ except Exception: # noqa: BLE001 -- refused or unprobeable: not buildable by that enginereturn False++ if native_gguf_only(fam, model_kind):+ return predicts(ENGINE_SD_CPP)if family_pipeline_available(fam):return True- if model_kind == "gguf" and gguf_filename and family_mlx_supported(fam):- try:- if predict_engine(fam, model_kind = "gguf", gguf_filename = gguf_filename) == ENGINE_MLX:- return True- except Exception: # noqa: BLE001 -- a probe failure must not hide/refuse a usable model- pass+ if (+ model_kind == "gguf"+ and gguf_filename+ and family_mlx_supported(fam)+ and predicts(ENGINE_MLX, gguf_filename = gguf_filename)+ ):+ return True# only a GGUF can go native, and only for a family with the single-file assets sd.cpp needsif model_kind != "gguf" or not family_sd_cpp_supported(fam):return False- try:- return predict_engine(fam, model_kind = "gguf") == ENGINE_SD_CPP- except Exception: # noqa: BLE001 -- a probe failure must not hide/refuse a usable model- return False+ return predicts(ENGINE_SD_CPP)@functools.cachediff --git a/studio/backend/hub/services/models/catalog_classification.py b/studio/backend/hub/services/models/catalog_classification.pyindex 4327ba4c637..5198dde680f 100644--- a/studio/backend/hub/services/models/catalog_classification.py+++ b/studio/backend/hub/services/models/catalog_classification.py@@ -102,10 +102,10 @@ def _family_buildable_for_listing(family, *, model_kind: str) -> bool:from core.inference.diffusion_engine_router import family_buildable_herefrom utils.hardware import hardware as hw- # Listing must not run hardware detection; the load re-checks buildability.- if not hw.DETECTION_COMPLETE.is_set():- return family is not None- return family_buildable_here(family, model_kind = model_kind)+ if hw.DETECTION_COMPLETE.is_set():+ return family_buildable_here(family, model_kind = model_kind)+ # Listing must not run hardware detection; the load re-checks the provisional answer.+ return family_buildable_here(family, model_kind = model_kind, provisional = True)def _gguf_family_buildable(name_hints: tuple[Optional[str], ...]) -> bool:diff --git a/studio/backend/tests/test_cached_gguf_routes.py b/studio/backend/tests/test_cached_gguf_routes.pyindex a91823a69a3..bc242417fb7 100644--- a/studio/backend/tests/test_cached_gguf_routes.py+++ b/studio/backend/tests/test_cached_gguf_routes.py@@ -2620,6 +2620,42 @@ def test_legacy_delete_delegates_to_shared_service(monkeypatch):assert calls == [("org/repo", None, "token", "/data/hf/hub")]+def _hardware_detected(monkeypatch) -> None:+ """These pin the post-detection contract: listing and loader share one buildability answer."""+ from utils.hardware import hardware as hw++ event = threading.Event()+ event.set()+ monkeypatch.setattr(hw, "DETECTION_COMPLETE", event)+++def test_listing_before_detection_keeps_device_free_refusals(monkeypatch):+ """Before detection only the device-dependent engine prediction is provisional."""+ from core.inference import diffusion_engine_router as router+ from core.inference.diffusion_families import detect_family+ from utils.hardware import hardware as hw++ monkeypatch.setattr(hw, "DETECTION_COMPLETE", threading.Event())+ monkeypatch.setattr(+ router, "predict_engine", lambda *a, **k: pytest.fail("listing predicted an engine")+ )+ krea = detect_family("", override = "krea-2")+ moss = detect_family("", override = "moss-image")+ buildable = _classification._family_buildable_for_listing++ assert buildable(None, model_kind = "gguf") is False+ assert buildable(moss, model_kind = "gguf") is True+ monkeypatch.setattr(router, "family_pipeline_available", lambda fam: True)+ assert buildable(krea, model_kind = "pipeline") is True+ monkeypatch.setattr(router, "family_pipeline_available", lambda fam: False)+ monkeypatch.setattr(router, "family_sd_cpp_supported", lambda fam: False)+ assert buildable(krea, model_kind = "pipeline") is False+ assert buildable(krea, model_kind = "gguf") is False+ monkeypatch.setattr(router, "family_sd_cpp_supported", lambda fam: True)+ assert buildable(krea, model_kind = "pipeline") is False+ assert buildable(krea, model_kind = "gguf") is True++def test_krea2_gguf_is_an_image_model_not_chat(monkeypatch):"""krea2-arch GGUFs (molbal/krea2-gguf) are images, not chat RUN (#14474)."""for hints in (@@ -2633,6 +2669,7 @@ def test_krea2_gguf_is_an_image_model_not_chat(monkeypatch):# The arch alone names the family, so an unbuildable host hides even an uninformative name.import core.inference.diffusion_engine_router as router+ _hardware_detected(monkeypatch)seen = []def buildable(fam, *, model_kind):@@ -2657,6 +2694,7 @@ def test_krea2_gguf_is_an_image_model_not_chat(monkeypatch):def test_moss_image_gguf_inventory_uses_native_family_support(monkeypatch, arch, hints, buildable):from core.inference import diffusion_engine_router as router+ _hardware_detected(monkeypatch)seen = []def can_build(fam, *, model_kind):@@ -3644,6 +3682,8 @@ def _pretend_old_diffusers(monkeypatch, *, engine):monkeypatch.setitem(sys.modules, "diffusers", types.SimpleNamespace(__version__ = "0.36.0"))monkeypatch.setattr(router, "predict_engine", lambda fam, **kwargs: engine)+ # A pinned engine is a post-detection answer.+ _hardware_detected(monkeypatch)def test_gguf_picker_hides_a_family_no_engine_here_can_build(monkeypatch):diff --git a/studio/backend/tests/test_cold_model_catalog.py b/studio/backend/tests/test_cold_model_catalog.pyindex 8d6bbfb41bb..e12d80b24da 100644--- a/studio/backend/tests/test_cold_model_catalog.py+++ b/studio/backend/tests/test_cold_model_catalog.py@@ -171,7 +171,7 @@ def test_local_gguf_listing_defers_the_device_probe_until_hardware_is_known(monkeypatch.setattr(diffusion_engine_router,"family_buildable_here",- lambda family, **kwargs: probed.append(family) or False,+ lambda family, provisional = False, **kwargs: provisional or probed.append(family) or False,)classify = lambda: catalog_classification._arch_to_task(arch, ("model",))assert classify() == "text-to-image"diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex b17f089ba9b..c18c09e8c7b 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -50,7 +50,7 @@ import { isMlxId } from "@/features/model-picker/components/model-selector/recomimport { loadManagedLlamaFlags } from "@/features/model-picker/api/llama-flags";import { fetchLoadExtraArgs } from "@/features/model-picker/api/model-overrides";import { sanitizeStoredExtraArgs } from "@/features/model-picker/model-config/llama-extra-args";-import { usePlatformStore } from "@/config/env";+import { fetchDeviceType, usePlatformStore } from "@/config/env";import { resolveSpeculativeType } from "@/lib/speculative-modes";import { getSkillsSnapshot, settleSkillsForText } from "./skills-api";@@ -3373,6 +3373,46 @@ async function adoptInFlightServerLoad(: "server-idle";}+// Busy-host detection outlasts readHealth's one 5 s wait; past this bound autoload stays fail-open.+const AUTO_LOAD_VERDICT_WAIT_MS = 60_000;+const AUTO_LOAD_VERDICT_POLL_MS = 500;++async function awaitMeasuredVerdict(signal?: AbortSignal): Promise<void> {+ const deadline = Date.now() + AUTO_LOAD_VERDICT_WAIT_MS;+ // readHealth has no timeout or signal: a stalled read must not outlive the deadline or a stopped send.+ const bounded = (read: () => Promise<unknown>) =>+ new Promise<"ok" | "failed" | "timeout">((resolve, reject) => {+ const timer = setTimeout(() => settle("timeout"), Math.max(0, deadline - Date.now()));+ const onAbort = () => {+ cleanup();+ reject(signal?.reason);+ };+ function cleanup() {+ clearTimeout(timer);+ signal?.removeEventListener("abort", onAbort);+ }+ function settle(outcome: "ok" | "failed" | "timeout") {+ cleanup();+ resolve(outcome);+ }+ signal?.addEventListener("abort", onAbort, { once: true });+ read().then(+ () => settle("ok"),+ () => settle("failed"),+ );+ });+ signal?.throwIfAborted();+ if ((await bounded(() => fetchDeviceType({ detectHardware: true }))) !== "ok") return;+ while (Date.now() < deadline) {+ signal?.throwIfAborted();+ const platform = usePlatformStore.getState();+ if (!platform.capabilitiesUnknown() || platform.detectionDeferred) return;+ await new Promise((resolve) => setTimeout(resolve, AUTO_LOAD_VERDICT_POLL_MS));+ signal?.throwIfAborted();+ if ((await bounded(() => fetchDeviceType({ force: true }))) !== "ok") return;+ }+}+async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{loaded: boolean;blockedByTrustRemoteCode: boolean;@@ -4007,6 +4047,20 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{ggufSettled.status === "rejected" || modelsSettled.status === "rejected";if (inventoryIncomplete) hadNonTrustFailure = true;+ // A deferred verdict admits non-GGUF caches a chat-only host refuses; GGUF-only caches skip the probe.+ if (+ usePlatformStore.getState().detectionDeferred &&+ (allModelRepos.some(isChattableCachedRepo) ||+ localRows.some(+ (row) =>+ !isGgufLocalRow(row) &&+ isAutoLoadableLocalRow(row, cachedInventoryFailed),+ ))+ ) {+ await awaitMeasuredVerdict(options?.abortSignal);+ options?.abortSignal?.throwIfAborted();+ }+// Include on-disk models the picker indexes, not just the managed cache.const sources = orderAutoLoadSources(buildAutoLoadSources(diff --git a/tests/studio/test_chat_autoload_failure_gate.py b/tests/studio/test_chat_autoload_failure_gate.pyindex 46e77347928..cf870856e46 100644--- a/tests/studio/test_chat_autoload_failure_gate.py+++ b/tests/studio/test_chat_autoload_failure_gate.py@@ -129,6 +129,12 @@ export type Scenario = {platformFetched: boolean;/** true = the backend answered but deferred hardware detection. */detectionDeferred: boolean;+ /** What a detectHardware read measures; unset keeps chatOnly. */+ detectedChatOnly?: boolean;+ /** Health reads after the detectHardware read before the verdict lands (a busy host). */+ detectionReads?: number;+ /** The detectHardware read never answers; the user stops the send shortly after. */+ stallHealth?: boolean;variants: Record<string, any>;lastLoaded: any;validate: (payload: any) => any;@@ -600,10 +606,37 @@ const usePlatformStore = {// Server-reported unless a scenario says the probe has not landed.fetched: SCENARIO.platformFetched !== false,isChatOnly: () => SCENARIO.chatOnly === true,+ detectionDeferred: SCENARIO.detectionDeferred === true,capabilitiesUnknown: () =>SCENARIO.platformFetched === false || SCENARIO.detectionDeferred === true,}),};+// A detectHardware read starts detection; it lands after detectionReads more health reads (0 = within+// that read's own wait). detectedChatOnly is what the host turns out to be.+let DETECTION_READS_LEFT = -1;+async function fetchDeviceType(options?: any) {+ if (options?.detectHardware) {+ EVENTS.push({ kind: "detectHardware" });+ if (SCENARIO.stallHealth) {+ setTimeout(() => (globalThis as any).__stopSend?.(), 50);+ return new Promise(() => {});+ }+ SCENARIO.detectionDeferred = false;+ DETECTION_READS_LEFT = SCENARIO.detectionReads ?? 0;+ } else if (DETECTION_READS_LEFT > 0) {+ EVENTS.push({ kind: "healthRead" });+ DETECTION_READS_LEFT -= 1;+ }+ if (DETECTION_READS_LEFT === 0) {+ SCENARIO.platformFetched = true;+ if (SCENARIO.detectedChatOnly !== undefined) SCENARIO.chatOnly = SCENARIO.detectedChatOnly;+ DETECTION_READS_LEFT = -1;+ } else if (DETECTION_READS_LEFT > 0) {+ // Still detecting: the reply is provisional, so the verdict stays unmeasured.+ SCENARIO.platformFetched = false;+ }+ return SCENARIO.platform?.deviceType ?? SCENARIO.deviceType ?? "linux";+}function isMlxId(value: string) {return typeof value === "string" && value.toLowerCase().includes("mlx");}@@ -962,8 +995,15 @@ def _run(+ textwrap.dedent(f"""setScenario({scenario_expr});- const signal = new AbortController().signal;- const result = await {entry};+ const run = new AbortController();+ const signal = run.signal;+ globalThis.__stopSend = () => run.abort(new DOMException("Stopped", "AbortError"));+ let result;+ try {{+ result = await {entry};+ }} catch (error) {{+ result = {{ thrown: error?.name ?? String(error) }};+ }}const s = storeState();console.log(JSON.stringify({{result,@@ -1699,14 +1739,74 @@ def test_a_provisional_platform_does_not_hide_cached_non_gguf_rows():def test_a_deferred_hardware_verdict_does_not_hide_cached_non_gguf_rows():- """Deferred detection reports a provisional chat_only, not a measurement."""+ """Deferred detection reports a provisional chat_only, not a measurement: autoload measures it+ first, and a host that turns out to have a GPU backend still loads the cached non-GGUF repo."""out = _run(- "scenario({ chatOnly: true, platformFetched: true, detectionDeferred: true,"+ "scenario({ chatOnly: true, detectedChatOnly: false, platformFetched: true,"+ " detectionDeferred: true,"" modelRepos: [{ repo_id: 'org/st', load_id: 'org/st', size_bytes: 1 }] })")+ assert [e for e in out["events"] if e["kind"] == "detectHardware"] == [+ {"kind": "detectHardware"}+ ]assert _loaded_paths(out) == ["org/st"]+def test_a_deferred_verdict_on_a_chat_only_host_falls_back_to_the_default_gguf():+ """Measured chat-only: the cached non-GGUF repo is skipped and the default GGUF loads, as when+ detection ran at startup, instead of a refused load ending the autoload with an error."""+ out = _run(+ "scenario({ chatOnly: true, detectedChatOnly: true, platformFetched: true,"+ " detectionDeferred: true,"+ " modelRepos: [{ repo_id: 'org/st', load_id: 'org/st', size_bytes: 1 }],"+ " load: (p) => p.model_path === 'org/st'"+ " ? new Error('Chat-only install: this model needs a GPU backend; load a GGUF model.')"+ " : LOADED(p) })"+ )+ assert "org/st" not in _loaded_paths(out)+ assert _loaded_paths(out) == [DEFAULT_MODEL]+ assert out["result"]["loaded"] is True+++def test_a_deferred_verdict_that_outlasts_the_health_wait_is_still_awaited():+ """On a busy host detection outlasts the health read's own wait: autoload keeps reading until the+ verdict lands instead of trying the cached non-GGUF repo on the still-unknown one."""+ out = _run(+ "scenario({ chatOnly: true, detectedChatOnly: true, detectionReads: 3,"+ " platformFetched: true, detectionDeferred: true,"+ " modelRepos: [{ repo_id: 'org/st', load_id: 'org/st', size_bytes: 1 }],"+ " load: (p) => p.model_path === 'org/st'"+ " ? new Error('Chat-only install: this model needs a GPU backend; load a GGUF model.')"+ " : LOADED(p) })"+ )+ assert [e["kind"] for e in out["events"] if e["kind"] in ("detectHardware", "healthRead")] == [+ "detectHardware",+ "healthRead",+ "healthRead",+ "healthRead",+ ]+ assert "org/st" not in _loaded_paths(out)+ assert _loaded_paths(out) == [DEFAULT_MODEL]+++def test_a_stalled_health_read_does_not_outlive_a_stopped_send():+ out = _run(+ "scenario({ chatOnly: true, platformFetched: true, detectionDeferred: true, stallHealth: true,"+ " modelRepos: [{ repo_id: 'org/st', load_id: 'org/st', size_bytes: 1 }] })"+ )+ assert out["result"] == {"thrown": "AbortError"}+ assert _loaded_paths(out) == []+++def test_a_deferred_verdict_with_only_gguf_cached_does_not_detect_hardware():+ """A GGUF-only cache runs anywhere, so autoload keeps the idle path free of the hardware probe."""+ out = _run(+ "scenario({ chatOnly: true, platformFetched: true, detectionDeferred: true,"+ " ggufRepos: [{ repo_id: 'org/g-GGUF', size_bytes: 1 }] })"+ )+ assert [e for e in out["events"] if e["kind"] == "detectHardware"] == []++def test_a_cached_adapter_repo_is_never_auto_loaded():"""A cached LoRA has no weights of its own: /load fetches the base from theHub when it is not cached, which is why the local twin is dropped too.