Skip to content

Studio: hold the Auto-layers context exemption only while --fit runs - #8825

Merged
danielhanchen merged 3 commits into
mainfrom
fix-metal-fit-off-native-ctx
Aug 16, 2026
Merged

Studio: hold the Auto-layers context exemption only while --fit runs#8825
danielhanchen merged 3 commits into
mainfrom
fix-metal-fit-off-native-ctx

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #8709, which landed while this last case was still being fixed.

The problem

#8709 stops Metal ever being sent -c 0, because llama.cpp reads it as fit_params_min_ctx = UINT32_MAX, which pins the model's full native context and disables the reduction --fit would do (#5118, #6529).

Auto-layers (gpu_memory_mode manual with a negative layer count) is exempted from that floor, and correctly so: it deliberately omits -c and lets --fit size the context. But that exemption is only as good as the fitter it defers to. User extras are appended after Studio's own --fit on and llama.cpp is last-wins, so a pass-through --fit off leaves a command carrying neither a -c nor a fitter. llama.cpp then falls back to the model's native context, which is the same over-commit the floor exists to prevent.

--fit is not on the pass-through denylist, so this is reachable from the extra-arguments box.

The fix

Gate the exemption on the effective fit state: the floor now receives auto_fit and fit_is_effectively_on(extra_args). That reuses the helper already in llama_server_args.py rather than adding a second definition of what turns the fitter off.

Extras alone are the right input here. Studio's own --fit on already beats an inherited LLAMA_ARG_FIT, since llama.cpp applies the environment before parsing argv, so only the trailing extras can win.

Evidence

Driving the real load_model on Metal in Auto-layers, context values in the emitted argv, before and after:

extras before after
--fit off [] ['4096']
--fit off -c 0 [] ['4096']
--fit on [] []
none [] []
--fit off, non-Apple [] []

A caller-owned fixed manual layer count still emits ['0'] untouched, since there the user owns the memory budget.

Tests

New TestAutoLayersWithTheFitterTurnedOff covers all five rows at argv level. 2254 tests pass across the llama.cpp, Metal, paravirtual, context and launch-flag suites.

danielhanchen and others added 3 commits August 14, 2026 14:01
Follow-up to #8709. Auto-layers earns its exemption from the Metal context
floor by leaving the context to --fit, so the exemption is only sound while
--fit is actually going to run. User extras land after Studio's own --fit on
and llama.cpp is last-wins, so a pass-through --fit off produced a command
carrying neither a -c nor a fitter, which is llama.cpp's native context and
the same over-commit the floor exists to prevent (#5118, #6529).

Gate the exemption on fit_is_effectively_on over the extras, reusing the
helper in llama_server_args.py rather than adding a second definition of what
turns the fitter off. Extras alone are the right input because Studio's own
--fit on already beats an inherited LLAMA_ARG_FIT, argv being parsed after
the environment.

Driving load_model on Metal in Auto-layers, before and after:

  --fit off            []  ->  ['4096']
  --fit off -c 0       []  ->  ['4096']
  --fit on             []  ->  []
  no extras            []  ->  []
  --fit off, non-Apple []  ->  []

A caller-owned fixed layer count is untouched.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: fe70722fc2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 27326c4 into main Aug 16, 2026
43 of 48 checks passed
@danielhanchen
danielhanchen deleted the fix-metal-fit-off-native-ctx branch August 16, 2026 12:24
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