Repository navigation
Keep image and video models loaded beside chat models on Apple Silicon - #15693
Conversation
… Apple Silicon On Apple Silicon, Keep multiple models loaded (When they fit / Always) denied every image or video model beside chat, so loading one unloaded the chat model or was refused. Unified memory is now priced as one pool: free memory is the smaller of the Metal wired headroom and available RAM, the readings llama.cpp's Metal fit already uses. An MLX image load is priced from the quantization plan it will take (header sizes of the GGUF, text encoder and VAE plus decode headroom), and the diffusers streaming check is skipped on unified memory. A GGUF chat load beside a resident image or video model nets that model out of the Metal budget and wired headroom (its weights off the working set, its unallocated generation headroom off the live readings), so llama.cpp fits its context to what is left or reports the shortfall that makes room. An MLX chat model allocates its KV cache as it generates, which live memory does not show, so a media load beside one still unloads it (When they fit) or is refused (Always), and an unpriced non-GGUF chat load evicts first under Always too, since past the budget unified memory pages instead of failing.
…pple Silicon An MLX chat load on Apple Silicon is now priced before launch instead of evicting resident image and video models first. The price reuses the Run Settings estimate (the /estimate-memory MLX branch, factored into _mlx_load_estimate): weights, KV cache and compute at the context the load will serve, plus the prompt-cache reserve when the load keeps history, plus one KV cache per further parallel slot. Under When they fit and Always the load joins a resident media model when that fits in unified memory; otherwise it frees media models only (never kept chat models) or is refused. The loaded entry records what it has yet to allocate (mlx_growth) and the worker's memory footprint at load. An image or video load beside it charges that growth less what the worker has already grown by, so cache memory that live readings already show is not charged twice. A GGUF launch beside a media model nets the same growth out of its Metal budget. An MLX model with no recorded price is still treated as unpriced, as before.
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". |
|
@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". |
…dia on Metal [skip ci]
…lot width reserved
…t reserved [skip ci]
…e window [skip ci]
…variant's own base
…a kept slot's own reload [skip ci]
…fallback skip the price drop [skip ci]
…red memory [skip ci]
…d vision only past one slot
…riced media model is kept on Metal [skip ci]
|
The The new Apple Silicon block in from core.inference.llama_server_args import clamp_parallel_slots
mlx_chat_need = await asyncio.to_thread(
_mlx_chat_need, request, config, model_identifier, clamp_parallel_slots(_n_parallel),
)The guard keeps the load route on the one width The other failures in that shard ( |
## Summary `(Python 3.13, a-f)` fails on main and on every PR based on it (seen on the #15693 and #15761 staging runs) with two stale tests. Both product changes are intentional; the tests had not caught up. **1. `test_account_chat_settings.py::test_list_export_and_delete_stay_in_the_account`** ``` assert {'threads': [...Guards': True} == {'threads': []} Left contains 1 more item: {'threadPatchGuards': True} ``` #15685 advertises the chat-title repair guards on the thread lists (`threadPatchGuards` on the `/chat/threads` reply). This test checks that another account's thread stays invisible, but compared the whole reply to `{"threads": []}`. It now asserts the thread list itself is empty, which is the privacy claim. **2. `test_checkpoint_compaction.py::test_the_reachability_probe_closes_the_connection_it_opens`** ``` assert _open_archive_handles() <= before + 1 E assert 4 <= (0 + 1) ``` #15719 keeps one WAL keeper connection per `rag.db` for the life of the process (`storage/rag_db._ensure_wal_keeper`), holding `rag.db` plus its `-wal` and `-shm` sidecars. The first probe in a fresh process creates it, so the test counted the keeper's descriptors as a leak. It also failed when run alone, not only under xdist. The test now probes once to warm the keeper up, takes its baseline after that, and requires no growth at all over 50 probes. That is stricter than the old `before + 1`, and it is the leak the test exists for: one leaked probe connection per call. ## Testing - Both tests fail on main (`75555ccb54`) and pass with this change; `test_account_chat_settings.py` 93 passed. - Sabotage: replacing the `conn.close()` in `conversation_archive.reachable()` with a no-op makes the probe test fail with `assert 23 <= 5`.
) ## Summary #15763 broke `tests/python/test_windows_setup_download_progress.py` on main (seen on the #15693 staging run; reproduces locally wherever `pwsh` is installed): ``` test_windows_real_download_reports_progress_while_active[node|llama|whisper] E assert [0.3, 5.2, 10.0, 15.0, 20.0] == [5, 10, 15, 20] ``` To let the new stall window fire on a trickling connection, #15763 reads real HTTP responses with `read1`, which returns what one receive brings. It also moved the progress update onto every receive, so the first percent line now appears at 0.3% and the milestones print at whatever receive crosses them (5.2%) instead of at the whole 1 MiB reads the installers used before. The progress text the Windows setup scripts relay changed shape, which is what this test pins. ## Change `prebuilt_core.copy_response_body`: - the chunk size is back to 1 MiB; - on the `read1` path each read asks only for the bytes up to the next 1 MiB boundary, and progress is reported when the boundary is reached, so the updates land exactly where whole-chunk reads put them; - the stall window is still checked after every receive, so a trickle is still abandoned within the window; - plain `read` bodies (file:// mirrors, test doubles) read whole chunks and report each one, as before #15763. ## Testing - All 23 test files that touch the installer downloaders, main vs this branch: the only difference in failing IDs is the three `test_windows_real_download_reports_progress_while_active` cases, which now pass. The 21 `tests/studio/install/test_dependency_pass_skips.py` failures and the `tests/utils/aime_eval.py` collection error are on both sides on this host. - New `test_progress_on_a_real_response_lands_on_whole_chunks`: a real local server streams 4 MiB in 48 KiB writes and progress must be updated at exactly 1, 2, 3 and 4 MiB. It runs without `pwsh`, so Linux CI covers the shape too. - Sabotage: reporting progress on every receive again fails the new test and all three PowerShell cases. - The trickle tests from #15763 still pass (the stall is detected within the shortened window), and a real nodejs.org download verifies (55.6 MiB in 0.27 s).
[skip ci]
|
Review record for #15693, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#584. 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
|
Problem
On Apple Silicon, Keep multiple models loaded (When they fit / Always) never let an image or video model stay beside a chat model. Loading Z-Image while a chat model was resident unloaded the chat model under When they fit and was refused under Always. In practice the Images pill could not be used with a local chat model on a Mac.
Root cause
The co-residency check had no memory reading for the MLX device:
get_device() == DeviceType.MLXreturnednon_accelerator, so every media load was denied. Two more gaps sat behind it:What changed
Commit 1: image and video models beside a GGUF chat model
--fit-targetfloor. llama.cpp then fits its context to what is left, or reports the shortfall that makes room.Commit 2: MLX chat models beside image and video models
/estimate-memoryMLX branch is factored into_mlx_load_estimatefor this. The price is one row (weights, KV cache and compute at the context the load will serve), plus the prompt-cache reserve when the load keeps history, plus one KV cache and one prefill compute buffer per further parallel slot (rows prefill as one batch). It joins a resident media model when that fits; otherwise it frees media models only, never kept chat models, or is refused.mlx_growth) and the worker'sphys_footprintat load. An image or video load beside it is charged that growth minus what the worker has already grown, so cache that live readings already show is not counted twice.CUDA behaviour is unchanged. Every new path is gated on the Metal pool, and an autouse test fixture keeps the existing verdicts off the Metal paths when the suite runs on a Mac.
Risks and tradeoffs
media_only: it is keyed on the platform rather than on the acquire carrying an MLX price.UNSLOTH_DIFFUSION_MLX_VAE_TILINGforces it off), and the renderer's free-memory reading is capped by the shared pool less chat growth. This costs some decode speed, only under When they fit / Always.Review follow-ups
Validation
Targeted tests, on Apple Silicon:
All pass.
tests/test_memory_estimate.pyhas 16 failures, and they are identical onmain.Mutation checks: each added test was checked against seeded defects in the code it covers, and each fails when its defect is present.
Live on an M3 Max (128 GB), with Unsloth Studio running from this branch:
generate_imageand the image rendered.