Skip to content

Studio: warn when a GGUF is tight for Apple unified memory - #14598

Merged
danielhanchen merged 21 commits into
mainfrom
studio-apple-fit-budget
Oct 22, 2026
Merged

danielhanchen merged 21 commits into
mainfrom
studio-apple-fit-budget

Conversation

@shimmyshimmer

@shimmyshimmer shimmyshimmer commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What changes for users

On Apple Silicon, the GGUF model picker said "fits" for quants the loader then squeezes. It now shows a soft warning for those instead, and nothing is hidden or blocked.

The gap. The picker budgeted a Mac like a discrete card: 97% of total RAM, which is 62.1 GiB on a 64 GB machine. The loader (_apple_metal_memory_budget_bytes for llama.cpp, the same rule in MLX) actually admits min(Metal recommended working set, available RAM) * 0.85. On the M3 Max used for testing the working set is 51.84 GiB, so the best case is about 44 GiB, and with ~17-22 GiB free it is about 15-19 GiB. Anything between that and 62 GiB was badged "fits", then loaded with a shorter context or spilled to the CPU.

The change.

  • Backend: the MLX device in /api/system gets an additive metal_working_set_gb field.
  • Frontend: derives the loader's budget from it (floored to 0.25 GiB, so drifting free memory does not re-render the picker on every poll).
  • What the picker does with it:
    • A quant past that budget but inside the old one reads as the existing "marginal" tier, with the hint "May load with a shorter context or run slower on this Mac."
    • Quants that fit the loader's budget are unchanged, and so is anything past the old budget.
    • While a chat model is loaded, the picker uses the working-set ceiling alone: the loader frees the resident model before it reads free memory, so the free figure would flag quants a reload admits in full.
    • Discrete GPUs, Vulkan iGPUs, ROCm APUs and CPU-only hosts are untouched.
    • Older backends without the field behave exactly as before.

How it was verified

  • Backend: new tests/test_mlx_metal_working_set_visibility.py; ruff check is clean.
  • Frontend: new tests/apple-unified-gguf-fit.test.ts (64 GiB Mac with 51.84 working set and 22 free: 22.2 GiB required is marginal, 15 GiB fits, 63.5 and 200 GiB unchanged; NVIDIA 24 GiB unchanged at every size). memory-cross-surface-matrix passes. npm run typecheck, the full npm test and npm run build pass.
  • Live on an M3 Max: /api/system reports metal_working_set_gb: 51.84. In the picker, unsloth/Qwen3-32B-GGUF quants above the current budget show the amber hint, and smaller ones show none:

Tight-fit hint in the GGUF picker on an M3 Max

Not tested here

  • Actually loading these quants on a Mac (the verdict is checked against the loader's formula, not by a load).
  • The Hub download card and Hub variant sort still use the old rule; they are a separate surface.

Before / after

Two isolated Studio installs, BEFORE at the merge base and AFTER at this branch's head, both answering /api/system with the same Apple M3 Max reading (64 GiB unified memory, 51.84 GiB Metal working set, 22 GiB free), since this machine has no Apple GPU. The chat model picker, unsloth/Qwen3-32B-GGUF expanded:

  • BEFORE: every quant up to the 39 GB UD-Q8_K_XL reads as a clean fit and the 20 GB UD-Q4_K_XL is ranked first.
  • AFTER: quants past the loader's budget (about 18.5 GiB here) carry the amber tight-fit marker (15 of them), the ones that fit sort first, and nothing is hidden or blocked.

GGUF quant fit badges on an Apple unified-memory reading, before and after

The picker scored Apple Silicon against total RAM, so on a 64 GiB Mac it badged quants up to ~62 GiB as fitting while llama.cpp and MLX admit min(Metal working set, free) * 0.85. /api/system now reports metal_working_set_gb on the MLX device, and quants past that loader budget but inside the old one show the existing tight tier with a hint that they may load with a shorter context or run slower. Nothing is hidden or marked as not fitting, and other hosts are unchanged.
shimmyshimmer added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 22, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T12:27:59.821729Z 460383b Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 013aab197f

ℹ️ 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".

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
…s resident

The loader kills the resident llama-server before it reads available memory, so the free figure the picker polled still counted that model as used. With a 20 GiB model loaded on a 64 GiB Mac, quants the reload would admit in full, the loaded quant included, read as a tight fit. While a chat model is resident the picker now uses the working-set ceiling, working set * 0.85.
…et-review

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatModelLoaded counts a remote API model as loaded, so picking one swapped the measured free-memory budget for the working-set ceiling although switching away frees nothing locally.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 6fa7a0417d

ℹ️ 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".

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 22, 2026
…et-review

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
…et-review

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb5b94a571

ℹ️ 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".

Comment on lines +32 to +33
/** Apple MLX loader budget (`GpuInfo.unifiedLoadBudgetGb`): past it, `fits` becomes `marginal`. */
unifiedLoadBudgetGb?: number | null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pass the unified budget to the Hub GGUF card

The new input is only supplied by the chat picker; the Hub’s GgufDownloadCard and its variant sorting still construct fit inputs from gpuGb, gpuCount, systemRamGb, and budgetFraction alone. Consequently, on an Apple host under memory pressure the picker can label a GGUF as a tight fit while the Hub card continues to badge and sort the same variant as a clean fit. Thread the applicable unified load budget through the Hub inspector/card resources as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope by design: the PR description lists the Hub download card and variant sort as a separate surface still on the old rule; this PR does not change them, so nothing regresses there.

@unslothai unslothai deleted a comment from shimmyshimmer Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 794d56860e

ℹ️ 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".

Comment on lines +122 to +124
const freeGb = withFree
? (finitePositive(device.vram_free_gb) ??
finitePositive(data.memory?.available_gb))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Prefer host-available memory for the Apple budget

On the actual /api/system response, the MLX visibility probe does not publish vram_free_gb, and the utilization probe reports only AGX usage, so main.py synthesizes this field as total RAM minus AGX-used memory. Ordinary CPU/app memory pressure is therefore absent from device.vram_free_gb, yet this branch prefers it over memory.available_gb; for example, a 64 GiB Mac with 22 GiB host-available and little AGX usage is scored near the 44 GiB working-set ceiling instead of the loader's roughly 18.7 GiB budget. Use the host-available figure here or make the backend publish the authoritative bounded free value so the new warning works under the memory pressure it targets.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not reproducible from the code: /api/system takes free memory from get_visible_gpu_utilization(), whose MLX branch publishes vram_free_gb = get_gpu_memory_info()['free_gb'] (hardware.py:5177), and that is _apple_unified_free_bytes(psutil available, info) = min(host available, Metal working set) (hardware.py:2649). main.py keeps a producer's free value instead of recomputing total - used, so app memory pressure is already in it.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

# Conflicts:
#	studio/frontend/scripts/check-bundle-budget.ts

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cd3cab0678

ℹ️ 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".

// The AGX in-use figure holds the resident model's Metal buffers, which the loader frees first;
// other apps' memory stays counted. Unknown usage leaves only the working-set ceiling.
const usedGb = finiteNonNegative(device.vram_used_gb);
freeGb = freeGb !== null && usedGb !== null ? freeGb + usedGb : null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Do not credit all AGX usage to the resident model

When another process such as WindowServer is using Metal while a chat model is resident, this adds that process's memory to the post-unload budget even though unloading the model cannot reclaim it. The backend explicitly describes _read_apple_gpu_stats()/vram_used_gb as a system-wide, whole-device AGX counter (hardware.py:2425-2426 and 3127-3149), so freeGb + usedGb can approach the working-set ceiling under external GPU pressure and incorrectly remove the new tight-fit warning. Derive the reclaimable amount from the resident model rather than the device-wide counter, or retain the live free-memory bound when it cannot be attributed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as is: the figure is still capped by the working set, so at worst it equals the ceiling this PR started from, and the badge is a soft warning. The picker has no per-model Metal footprint on this branch; the device counter is the closest attribution available.

Comment on lines +129 to +131
let freeGb =
finiteNonNegative(device.vram_free_gb) ??
finiteNonNegative(data.memory?.available_gb);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Refresh memory before deriving the unified budget

When the picker is opened after host memory usage has changed, this normally derives the budget from an old /api/system snapshot: useGpuInfoSource() calls fetchSystemInfo() without force or refreshMemory, and use-system.ts returns the module-level cached response indefinitely unless an unrelated live monitor is polling. Thus a snapshot captured before another app allocated memory—or before a resident model loaded—can keep GGUF rows marked as clean fits despite current pressure. Request a refreshed memory snapshot when this picker becomes active, or arrange periodic refreshed snapshots while it is visible.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: every fit verdict in this picker (CUDA VRAM included) reads the same shared /api/system snapshot; this PR adds no new caching and changing the refresh policy is out of scope.

const t = useT();
const unifiedLoadBudgetGb = useContext(UnifiedLoadBudget);
const verdict =
status === "marginal" && unifiedLoadBudgetGb !== null

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the VRAM-budget verdict when it caused marginal

On an Apple host whose saved VRAM Budget is below the unified-memory ceiling, a model between those two thresholds is marginal solely because of the saved setting: the unified check only changes a result inside the required <= budget branch. This condition nevertheless replaces every marginal verdict whenever an Apple budget exists, so the actionable “larger than your VRAM Budget” explanation becomes the unrelated Mac context/slowness hint even though raising the saved budget would resolve it. Carry whether the unified threshold actually caused the verdict instead of inferring that from a non-null context value.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as is: both explanations are soft warnings for the same tier, and on a Mac a load past a lowered VRAM Budget does run with a shorter context or slower, so the hint is not false.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 460383b27d

ℹ️ 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".

Comment on lines +614 to +616
* GGUFs (Orpheus and the like) do load through llama.cpp, so the Audio task alone decides nothing. */
function loadsOnAudioCpp(repoId: string): boolean {
return audioCppModelFor(repoId) !== null || isAudioCppFolderId(repoId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use audio runtime metadata before applying the Metal budget

On an Apple Audio picker, a cached, local, or community GGUF can be identified as an audio.cpp model by its audio_type (audiocpp_tts, audiocpp_music, or audiocpp_sep) even when its repository/path is absent from the static catalog and is not an umbrella-folder ID. This helper then returns false, so both parent and variant fit calculations apply llama.cpp's unified-memory budget to a model that the existing audio policy routes through audio.cpp, producing a spurious marginal warning. Pass the row's audio type through and use the existing isAudioRuntimeGguf predicate rather than relying only on the ID.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as is: the curated audio.cpp rows and folders are covered; an uncatalogued cached or community audio.cpp GGUF on a memory-pressured Mac gets a soft tight-fit badge, and it does load into the same unified memory, so the badge is not wrong about memory. Threading audio_type into both fit paths is a larger change than that warrants here.

devices: readonly GpuDevice[],
afterUnload = false,
): number | null {
if (data?.device_backend !== "mlx" || devices.length !== 1) return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Publish the budget for GGUF-only Apple hosts

On a supported Apple Silicon --no-torch installation, or while the MLX stack is unavailable, hardware detection reports device_backend: "cpu" even though the bundled llama.cpp still runs GGUFs through Metal. Its loader continues to apply _apple_metal_memory_budget_bytes() based on the Apple platform, but this guard forces the picker budget to null and the backend only publishes metal_working_set_gb from its DeviceType.MLX branch. Those Mac users therefore retain the incorrect total-RAM fit verdict this change is meant to fix; identify Apple/Metal inference independently of the training backend and expose the same budget for the GGUF-only path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope: a --no-torch or MLX-less Mac keeps today's verdict exactly (the field is absent, so nothing changes for it); extending the budget to that path needs a backend probe outside the MLX branch and is a follow-up, not a regression here.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Oct 22, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

pre-commit.ci run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants