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: stop unsloth start announcing a switch when --model names the file already loaded (#11873)
oobaboogac2ac19dc854e
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.pyindex bdfc819f392..a0c4557f6ae 100644--- a/unsloth_cli/commands/start.py+++ b/unsloth_cli/commands/start.py@@ -1616,14 +1616,19 @@ def _loaded_models(base: str, key: str) -> list:_fail_request(exc, "Couldn't list models")-def _model_still_loaded(base: str, key: str, model_id: object) -> bool:+def _model_loaded_state(base: str, key: str, model_id: object) -> Optional[bool]:+ """Return whether the model is loaded, or None if the listing is unavailable."""try:models = _loaded_models_response(base, key, timeout = 5).get("data", [])except Exception:- return False+ return Nonereturn any(m.get("id") == model_id and m.get("loaded") is not False for m in models)+def _model_still_loaded(base: str, key: str, model_id: object) -> bool:+ return _model_loaded_state(base, key, model_id) is True++_HF_REPO_ID_SEGMENT_RE = re.compile(r"^[A-Za-z0-9._-]+$")@@ -1672,6 +1677,18 @@ def _public_model_id(value: Optional[str]) -> Optional[str]:return name or None+def _public_model_ids(value: Optional[str]) -> set:+ """Return a path's basename ID and HF cache repo ID for old and current servers."""+ ids = {_public_model_id(value)} - {None}+ if ids:+ parts = value.replace("\\", "/").split("/")+ for index, part in enumerate(parts):+ if part.startswith("models--") and parts[index + 1 : index + 2] == ["snapshots"]:+ ids.add(part[len("models--") :].replace("--", "/"))+ break+ return ids++def _model_id_matches(actual: object,requested: object,@@ -1908,8 +1925,8 @@ def _resolve_model(if preload_check is not None:# An explicit knob forces match to None so the server's disk-free dedupe can answer already_loaded; gating it would reject a second session for the model already serving, whose file may have moved. Only the quant is checked below: any other run knob changes the runtime intent, a real reload nothing dedupes.other_overrides = bool(overrides - {"gguf_variant"})- # The loaded listing shows a path-loaded GGUF under its basename, so match that spelling too, or a second session reruns the gate.- wanted_ids = {requested, _public_model_id(requested)} - {None}+ # Match public path IDs before deciding whether to rerun the gate.+ wanted_ids = {requested} | _public_model_ids(requested)resident_serves_request = not other_overrides and any(m.get("loaded") is not Falseand any(@@ -1953,6 +1970,9 @@ def _resolve_model(preload_check(base, key, requested, load.gguf_variant)active_id = active.get("id") if active else Noneannounced_switch = False+ # Public IDs can collide, and status hides paths from API keys.+ # Wait for the load result to distinguish reuse from replacement.+ switch_unknown = Falseif attach_public_id is not None:# An inferred attach never switches model, so the comparison below would misreport a switch and print the server's path.if inferred_differs:@@ -1964,9 +1984,15 @@ def _resolve_model(requested,allow_casefold = allow_casefold,):- typer.echo(f"Switching the Unsloth server from {active_id} to {requested}.")- typer.echo("This unloads the current model for every attached session.")- announced_switch = True+ if any(+ _model_id_matches(active_id, listed, allow_casefold = allow_casefold)+ for listed in _public_model_ids(requested)+ ):+ switch_unknown = True+ else:+ typer.echo(f"Switching the Unsloth server from {active_id} to {requested}.")+ typer.echo("This unloads the current model for every attached session.")+ announced_switch = Trueelif active_id and load.gguf_variant:# Same repo id but an explicit quant still replaces the resident weights; the loaded listing does not carry a variant for every resident model, so ask the status endpoint.try:@@ -2042,13 +2068,20 @@ def _resolve_model(# The warning above promised an unload; if the server refused the load before evicting anything, say so. Not BaseException: Ctrl+C must stay immediate, without a probe or a survivor claim.if announced_switch and _model_still_loaded(base, key, active_id):typer.echo(f"Nothing was unloaded; {active_id} is still serving.", err = True)+ # Report an unannounced eviction only if the listing confirms it.+ if switch_unknown and _model_loaded_state(base, key, active_id) is False:+ typer.echo(f"{active_id} was unloaded for every attached session.", err = True)raiseif loaded.get("status") == "already_loaded":# Show the public id on the inferred path; `requested` may be a server path.shown = attach_public_id or requestedtyper.echo(f"Reusing loaded model: {_display_model_spec(shown, load.gguf_variant)}")- # Unsloth registers the model under a canonical id (resolved identifier, casing) that the loaded listing echoes but which may differ from the path we passed; match on the id the load reports so we do not silently fall through to models[0] and connect to a different loaded model. attach_public_id: our _public_model_id only strips a basename, while the server also maps an HF cache path to its repo id, so the two can disagree.- wanted = {requested, _public_model_id(requested), attach_public_id} - {None}+ elif switch_unknown:+ typer.echo(f"Loaded {requested} in place of {active_id}.")+ typer.echo("This unloaded the previous model for every attached session.")+ # Match public IDs and load-response names, since the listing may omit paths.+ # Keep attach_public_id for inferred requests using opaque identifiers.+ wanted = ({requested, attach_public_id} - {None}) | _public_model_ids(requested)if isinstance(loaded, dict):wanted |= {loaded.get("model"), loaded.get("display_name")} - {None}models = _loaded_models(base, key)diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.pyindex f9ca24f55c1..e805687d854 100644--- a/unsloth_cli/tests/test_start.py+++ b/unsloth_cli/tests/test_start.py@@ -2937,6 +2937,106 @@ def test_connect_model_flag_loads_on_server(fake_studio):_assert_env_set(result.output, "ANTHROPIC_MODEL", "unsloth/Qwen3.5-35B-A3B")+def _fake_path_resident(+ monkeypatch,+ listed_id,+ load_status,+ *,+ load_error = None,+ after_failure = "kept",+):+ # API-key status exposes an opaque ref instead of the resident path.+ inner = start._http_json+ state = {"after": None, "listed": listed_id}++ def http_json(+ method,+ url,+ token,+ payload = None,+ timeout = 30,+ error = None,+ ):+ if url.endswith("/api/inference/loaded-models"):+ if state["after"] == "unreachable":+ raise TimeoutError("timed out")+ return {"data": [{"id": state["listed"], "loaded": state["after"] != "gone"}]}+ if url.endswith("/api/inference/status"):+ return {"is_gguf": True, "active_model": listed_id, "model_identifier": "ref:0123"}+ if url.endswith("/api/inference/load"):+ if load_error is not None:+ state["after"] = after_failure+ raise load_error+ # Load responses use the path and short name; snapshot listings use the repo ID.+ name = os.path.basename(payload["model_path"]).removesuffix(".gguf")+ if load_status == "loaded":+ state["listed"] = name+ return {"status": load_status, "model": payload["model_path"], "display_name": name}+ return inner(method, url, token, payload, timeout, error)++ monkeypatch.setattr(start, "_http_json", http_json)+++ "requested, listed_id",+ [+ ("/models/old/foo-Q4_K_M.gguf", "foo-Q4_K_M"),+ (+ "/cache/hub/models--unsloth--Foo-GGUF/snapshots/abc123/Foo-UD-IQ1_S.gguf",+ "unsloth/Foo-GGUF",+ ),+ ],+)+def test_connect_model_path_reattach_announces_no_switch(+ fake_studio, monkeypatch, requested, listed_id+):+ _fake_path_resident(monkeypatch, listed_id, "already_loaded")+ result = CliRunner().invoke(start.start_app, ["claude", "--no-launch", "--model", requested])+ assert result.exit_code == 0, result.output+ assert "Switching" not in result.output+ assert "unload" not in result.output+ assert f"Reusing loaded model: {requested}" in result.output+++def test_connect_model_path_same_name_switch_announced_after_load(fake_studio, monkeypatch):+ _fake_path_resident(monkeypatch, "foo-Q4_K_M", "loaded")+ requested = "/models/new/foo-Q4_K_M.gguf"+ result = CliRunner().invoke(start.start_app, ["claude", "--no-launch", "--model", requested])+ assert result.exit_code == 0, result.output+ assert "Switching" not in result.output+ assert f"Loaded {requested} in place of foo-Q4_K_M.\n" in result.output+ assert "This unloaded the previous model for every attached session.\n" in result.output+++def test_connect_model_path_other_name_switch_announced_before_load(fake_studio, monkeypatch):+ _fake_path_resident(monkeypatch, "foo-Q4_K_M", "loaded")+ requested = "/models/bar-Q4_K_M.gguf"+ result = CliRunner().invoke(start.start_app, ["claude", "--no-launch", "--model", requested])+ assert result.exit_code == 0, result.output+ assert f"Switching the Unsloth server from foo-Q4_K_M to {requested}.\n" in result.output+ assert "in place of" not in result.output++[email protected]("after_failure", ["kept", "gone", "unreachable"])+def test_connect_model_path_failed_same_name_load_reports_eviction(+ fake_studio, monkeypatch, after_failure+):+ # An unreachable listing must not be treated as evidence of eviction.+ failure = urllib.error.HTTPError(+ f"{BASE}/api/inference/load", 500, "Internal Server Error", None, None+ )+ _fake_path_resident(+ monkeypatch, "foo-Q4_K_M", None, load_error = failure, after_failure = after_failure+ )+ result = CliRunner().invoke(+ start.start_app, ["claude", "--no-launch", "--model", "/models/new/foo-Q4_K_M.gguf"]+ )+ assert result.exit_code != 0+ evicted = "foo-Q4_K_M was unloaded for every attached session." in result.output+ assert evicted is (after_failure == "gone")+ assert "Nothing was unloaded" not in result.output++def test_connect_model_flag_forwards_load_options(fake_studio):# The model-load knobs mirrored from `unsloth run` reach /api/inference/load.result = CliRunner().invoke(