Repository navigation
feat(studio): idle auto-unload an MLX chat model - #15680
Conversation
The keep-warm idle loop only watched llama.cpp, so a primary MLX model held its unified memory indefinitely. It now frees an MLX primary on the same TTL, with the same in-flight and queued-request checks, the same "only unload models loaded by the API" pin, and the same reload stash, so the next request loads it back. Slot KV save stays llama-server only; MLX speech models stay loaded because the stash reload skips their audio preflights. The UI-load pin for an MLX primary now lives on the orchestrator: the /load route sets it, and every orchestrator load publish clears it, so preview and auto-switch loads never inherit an earlier pin.
…ver index The auto-switch reload of an idle-unloaded model classified its id with local_target_is_gguf, which reads a repo id missing from the acting account's resolver snapshot as GGUF. An MLX model stashed under its repo id could then reload with its saved overrides read as GGUF, its alias and provenance written to the llama backend, and GGUF-only request checks applied. The idle loop now records that the model it freed ran on MLX, and the reload trusts that before the index.
With "Keep chat context across unloads" on, the idle loop now saves an MLX chat model's prompt cache through the orchestrator before unloading it, as it already saved llama-server slots for a GGUF model. The next load of the same model restores it through the reload path that already restores a cache kept across a media swap, so the first turn after an idle reload does not re-read the conversation.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…ugh run_pwsh (#15758) ## Summary `Repo tests (CPU, studio)` fails on main, so it is red on every recent staging CI run (#15680, #15696, #15716, #15723 and others). Of its 6 failures, 4 are tests that no longer match intentional changes; this PR fixes those. The other 2 are real product slips on the login screen and are fixed separately. **Media picker contracts (3 failures).** #15726 lined the Images, Audio and Video model pickers up with Chat: - The picker now uses `className="ml-1.5 max-w-full overflow-hidden !pl-2.5 !pr-1.5 !h-[var(--studio-chat-control-height,34px)]"`. - It no longer sets a `triggerLabelClassName` override. `tests/studio/test_desktop_reliability_frontend_contract.py` still pinned the old class string and the old label override, and counted two scaled `!h-[calc(34px*var(--ui-space-scale,1))]` literals on the Images and Video pages (and one on Audio) that are now the shared token: ``` images-page.tsx states 1 scaled !h-34px, not 2 assert 'className="!h-[34px] max-w-full gap-1 ...' in header ``` **PowerShell spawn (1 failure).** `tests/python/test_installer_compile_bytecode.py:271` (from #15646) starts PowerShell with `subprocess.run`. `tests/studio/test_pwsh_calls_use_the_shared_runner.py` forbids that, because direct spawns share one PowerShell startup cache across xdist workers and can die at startup. ## Change - **`test_desktop_reliability_frontend_contract.py`:** - The Images and Audio header contracts assert the new picker class (`_MEDIA_PICKER_CLASS`), and that the label is not overridden, so the trigger reads like Chat's. - The scale table's `!h-34px` counts drop to what the pages still state literally: Images 1, Audio 0, Video 1. The "no bare `!h-[34px]`" half of that check is unchanged. - New test `test_the_media_pickers_take_chats_scaled_control_height`: all three pages use the picker class, and `index.css` still defines `--studio-chat-control-height` as `calc(Npx * var(--ui-space-scale, 1))`. This keeps the original point of the scale check, a pill that grows with the UI scale, attached to the token the height now comes from. - **`test_installer_compile_bytecode.py`:** the PowerShell case runs through `run_pwsh` from `tests/_shared/unsloth_pwsh_runner.py`, with the same argv and keyword arguments. ## Testing - These three files: 80 passed (pwsh is installed here, so the PowerShell cases really ran). On main, the same tests fail 4. - **Sabotage:** with `--studio-chat-control-height` changed to a bare `33px`, the new scaling test fails.
|
Review record for #15680, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#586. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1: reviewed mirror
|
Summary
Unsloth Studio's chat idle auto-unload only freed a primary GGUF model. An MLX chat model loaded on a Mac stayed resident forever, holding unified memory after the configured idle TTL even when it was loaded by an API client. This PR extends the idle loop to an MLX primary with the same rules as GGUF: it is freed after the TTL when no request is in flight or queued, a UI-loaded model stays resident when "Only unload models loaded by the API" is on, and the next request reloads it through the existing idle-reload path. With "Keep chat context across unloads" on, its prompt cache is saved before the unload and restored on reload, like GGUF slots.
Changes
llama_keepwarm.py,orchestrator.py,routes/inference.py)(model id, no quant, advertised id), so saved overrides are looked up under the same key._loaded_by_user_action) is now recorded on the orchestrator for a model it holds. The orchestrator clears it whenever it publishes a new load, so loads that bypass the load route (preview, auto-switch) never inherit a stale pin.routes/inference.py,llama_keepwarm.py)local_target_is_ggufassumes GGUF for a repo id missing from the acting account's resolver snapshot. A managed account whose own scan was never built gets an empty snapshot while the stash is global, so the reload would read GGUF overrides and run GGUF-only request checks. The idle loop now records that the stash it wrote is MLX (stash_is_mlx(identity), valid only for that exact stash), and the reload branch uses that fact before the index heuristic.llama_keepwarm.py)save_prompt_cache_for_resume(should_abort)(added in Studio: keep the MLX chat context when an image or video model takes the GPU #15358) in place of the GGUF slot save. It reuses the loop's existing manifest handling: a save dropped by a late request or a setting change deletes its files, and the reload restores throughnote_model_loaded(backend)→restore_kv_resume.GGUF idle unload is unchanged. The in-worker MLX GPU keep-warm timer (#12721) never stamps the parent's idle clock and is ended by the unload command, so the two don't interact.
Risks / tradeoffs
Validation
Unit tests next to the existing idle-loop tests in
tests/test_openai_auto_switch.py. They cover: the unload being blocked by in-flight work; unload, stash and reload of an API-loaded MLX primary with the prompt cache saved, restored and its files removed; a cold-index precondition; a save dropped after it was written; stash-format ownership; UI-pinned, non-MLX and speech-model orchestrator models staying loaded; and every orchestrator load publish clearing the UI pin.Each new test was mutation-checked against the production line it guards (17 mutations, all caught).
Live, on Apple Silicon with
mlx-community/Qwen2.5-0.5B-Instruct-4bit, a 60s TTL and "API only" on: