Repository navigation
Studio: budget the Metal context fit from free memory, not total - #8706
Merged
Merged
Conversation
_apple_metal_memory_budget_bytes fed the Apple context fit from MLX's max_recommended_working_set_size, falling back to psutil.virtual_memory().total. Both describe the machine, not the moment: they are static properties, so the budget came out the same on an idle Mac and on one already holding several gigabytes. On a 16 GB Mac that is roughly 10 GB of working set times the 0.85 fraction, so about 9 GB budgeted, while Studio's own idle footprint is already over a gigabyte once the warm thread has imported torch and, on Apple Silicon, MLX, before anything the user is running. The fit then sized a context against headroom that was not there and llama-server died in KV or compute allocation, which is the failure #5118 and #6529 describe and a plausible reading of #8566 on 16 GB. Take whichever is smaller, the device ceiling or what is actually free. available rather than free, since on macOS that counts the inactive and purgeable pages the kernel hands back under pressure, so it is the number a new allocation can expect. It can only lower the budget: on a machine with plenty free the working set still binds, which is the case that number was chosen for. No behaviour change off Apple Silicon, where the function returns 0 before any of this. One existing test had to change, and it is worth being explicit about why: test_uses_metal_working_set stubbed only `total`, so `available` was read from whichever host ran the suite. It was passing by accident of this machine having enough free. Both stubs are now pinned, via a _install_fake_psutil helper, so the test measures the code rather than the runner. Tests: 65 in the context-fit suite, 1608 across the llama.cpp, Apple and hardware suites. Five new cases: a busy 16 GB machine budgets from free, a free machine is still bound by the device ceiling, a psutil without `available` keeps the old answer rather than collapsing to zero, and the working set still applies with no psutil at all.
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ 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". |
…till applies Comment only, no behaviour change. available is inactive plus free in psutil's macOS implementation, so it adds back the reclaimable cache that free leaves out; it is still an estimate, since it counts dirty inactive pages that cost a compression to reclaim and ignores the compressor. Also records the second job _APPLE_UNIFIED_MEMORY_FRACTION picks up here: over a static ceiling it was pure headroom, but available is a snapshot and llama-server spends seconds loading weights, so another process can take memory inside that window.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happens today
_apple_metal_memory_budget_bytesfeeds the Apple context fit from MLX'smax_recommended_working_set_size, falling back topsutil.virtual_memory().total, then multiplies by_APPLE_UNIFIED_MEMORY_FRACTION(0.85).Both of those inputs describe the machine, not the moment. They are static properties, so the budget is identical on an idle Mac and on one already holding several gigabytes.
On a 16 GB Mac that is roughly 10 GB of working set times 0.85, so about 9 GB budgeted. Studio's own idle footprint is already over a gigabyte once the warm thread has imported torch and, on Apple Silicon, MLX, before counting the browser process or anything else the user is running. The fit sizes a context against headroom that is not there, and llama-server dies during KV or compute allocation.
That is the failure mode #5118 and #6529 describe, and it is a plausible reading of #8566 on 16 GB, where the reported symptom is that no local GGUF loads at all.
What this changes
Take whichever is smaller, the device ceiling or what is actually free:
availablerather thanfree, because on macOS that counts the inactive and purgeable pages the kernel will hand back under pressure, so it is the number a new allocation can actually expect.It can only lower the budget. On a machine with plenty free, the working set still binds, which is the case that number was chosen for.
Scope
Apple Silicon only. The function returns 0 before any of this off Apple Silicon, so Linux and Windows are untouched.
A note on one existing test
test_uses_metal_working_setstubbed onlytotal, soavailablewas being read from whichever host ran the suite. It was passing by accident of this machine having enough memory free. Both are now pinned through a small_install_fake_psutilhelper, so the test measures the code rather than the runner. Worth flagging rather than burying, since it means the previous test was weaker than it looked.Tests
65 in the context-fit suite, 1608 across the llama.cpp, Apple and hardware suites. Five new cases:
psutilwithoutavailablekeeps the old answer rather than collapsing to zeropsutilat allThree failures in
test_video_backend.pyandtest_video_prequant.pyare present onmainunchanged and are unrelated to this.