Repository navigation
Studio: cap GGUF context to unified memory on Apple Silicon - #6622
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
…e unified-memory cap The Apple Silicon GGUF context cap mirrored the discrete-GPU auto-fit branch but missed two protections the discrete path already applies: - It passed the full unified-memory budget with budget_frac=1.0 without first reserving the flat MTP fraction the discrete path takes off via _pin_fraction. With an MTP draft whose KV cannot be byte-sized (e.g. Qwen3.6-MTP, unslothai#6529), the cap filled the whole budget and left nothing for the draft, so unified memory could still over-commit. Reserve _flat_mtp_reserve up front; this is a no-op when MTP is not engaged. - It required _can_estimate_kv(), so a GGUF with sparse KV metadata skipped the cap entirely and launched at full native context. Mirror the discrete file-size-only fallback and floor the auto context to 4096 when the cache cannot be sized. Adds regression tests for both paths.
|
Thanks for this, it is fixing a real issue. I confirmed the mechanism: with no discrete GPU enumerated on Apple Silicon, I pushed two small follow-up commits on top of your branch to cover a couple of edge cases where the Apple branch diverged from the discrete-GPU path:
Both come with regression tests. I also ran a cross-OS pass to check backward compatibility and the real Metal path:
On real Apple Silicon the helper reads As with your earlier run, a full model decode was not exercised on the runners (limited RAM), so this covers the budget computation, the context-fit math, and the full test suite on real hardware rather than an end-to-end Metal decode. |
Condense the verbose comment blocks in the Apple budget helper, the no-GPU Metal branch, and the context-fit tests. Comments only, no code change (verified with ast-based comment_tools check); suite still green.
|
@codex review |
|
/gemini review |
|
@codex review |
There was a problem hiding this comment.
Code Review
This pull request introduces unified-memory budget estimation and context capping for Apple Silicon (Metal) in the Llama.cpp backend. This prevents over-committing unified memory when no discrete GPU is present by capping the auto-context length based on the MLX working set size or total system RAM. Extensive unit tests have been added to verify this new behavior. There are no review comments, so I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
Closes #5118
Closes #6529
Problem
On Apple Silicon, Studio does no context-length capping. It always launches with the model's full native context (e.g.
-c 262144), regardless of available memory. On CUDA/ROCm it shrinks the context to fit VRAM; on Metal it skips that, because_get_gpu_memory()returns[](it only knows nvidia-smi /torch.cuda).So a GGUF load over-commits unified memory and dies on the first decode with
Error: Compute error.(llama-server failing to build the Metal compute graph), even though the same model runs fine in a standalone llama.cpp Mac app. Reported on an M3 Pro / 36 GB: ~15.7 GB weights + ~16 GB KV at full context.--fit ondoesn't save it (llama.cpp only shrinks context at-c 0, and layer offload can't free unified RAM).Fix
Give the fit path a memory budget on Apple Silicon: Metal's recommended working-set (via mlx, falling back to total RAM) at 0.85, run through the same
_fit_context_to_vramthat CUDA and ROCm already use.gpu_indices=None/use_fit=Truestay untouched (nothing to pin on Metal), so it just ships a reduced-cwith--fit onas a backstop. Explicit user contexts are honored verbatim; only the auto context is shrunk. Off Apple Silicon the budget is 0 and behavior is unchanged.Verification
Tests in
test_llama_cpp_context_fit.pycover the budget helper and the cap/floor/explicit paths; full file and the broad VRAM-fit suites pass.Also run on real Mac runners (CI): on
macos-26-arm64the helper reads a 5.01 GB Metal working-set, budgets 4.26 GB, and caps262144to39680; onmacos-15-intelit returns 0 (no-op). A full model decode wasn't exercised (runner RAM too small).