Skip to content

Studio: budget the Metal context fit from free memory, not total - #8706

Merged
danielhanchen merged 5 commits into
mainfrom
fix-apple-metal-budget-available
Aug 14, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix-apple-metal-budget-available

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What happens today

_apple_metal_memory_budget_bytes feeds the Apple context fit from MLX's max_recommended_working_set_size, falling back to psutil.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:

if available > 0:
    rec_bytes = min(rec_bytes, available)
return int(rec_bytes * _APPLE_UNIFIED_MEMORY_FRACTION)

available rather than free, 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_set stubbed only total, so available was 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_psutil helper, 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:

  • a busy 16 GB machine budgets from what is free
  • a machine with plenty free is still bound by the device ceiling
  • a psutil without available keeps the old answer rather than collapsing to zero
  • the working set still applies with no psutil at all
  • off Apple Silicon is still 0

Three failures in test_video_backend.py and test_video_prequant.py are present on main unchanged and are unrelated to this.

_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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 8b2c22652d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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.
@danielhanchen
danielhanchen merged commit 9cc6e65 into main Aug 14, 2026
51 of 55 checks passed
@danielhanchen
danielhanchen deleted the fix-apple-metal-budget-available branch August 14, 2026 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant