Repository navigation
fix: reduce VRAM headroom fractions to prevent auto-context size spill (#6682) - #6688
anmolxlight wants to merge 11 commits into
Conversation
unslothai#6682) The auto context sizing in _fit_context_to_vram and _select_gpus used aggressive VRAM headroom fractions (0.95 pin fraction, 0.90/0.85 fit budget) that left too little margin for CUDA runtime overhead, compute buffers, and memory fragmentation. On tight VRAM tiers this caused llama-server with -ngl -1 to spawn the model at a context length that couldn't actually fit, spilling most layers to CPU (29% GPU util, 300% CPU). Changes: - _GPU_PIN_VRAM_FRACTION: 0.95 -> 0.90 (10% headroom instead of 5%) - _fit_context_to_vram budget: 0.90 -> 0.85 (15% headroom) - MTP fit budget: 0.85 -> 0.80 (20% headroom with MTP engaged) The trade-off: slightly fewer models get -ngl -1 (more fall through to --fit on which puts some layers on CPU), but those that do land on -ngl -1 will actually run at full GPU speed instead of spilling.
There was a problem hiding this comment.
Code Review
This pull request reduces the VRAM budget fractions in llama_cpp.py to prevent runtime OOM errors on tight GPUs. Specifically, _GPU_PIN_VRAM_FRACTION is reverted from 0.95 to 0.90, and the context-fit budget fractions are lowered to 0.85 (and 0.80 with MTP). The reviewer suggested defining these budget fractions as class-level constants rather than magic numbers to improve code maintainability and consistency.
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.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c5bd36de60
ℹ️ 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".
…room # Conflicts: # studio/backend/core/inference/llama_cpp.py
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: da0c412fd4
ℹ️ 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".
Agreed. Let’s not merge #6688 standalone while #6704 covers the same behavior with measurements and no-regression coverage. Please combine the approaches here or close this in favor of #6704, then we can merge once CI is green. |
|
Pushed a commit here that swaps the lowered fractions for a measured floored reserve: I did consider keeping the fit/pin fraction split too, but once this is on current main (both fractions are back at 0.95) it turns out to be a no-op, so I left it out to keep the diff minimal. This was measured overnight on a 24 GB 3090. Models that already fit keep their context (a 1.7B, a 12B, and the same 27B on a 49 GB card are all unchanged); only the tight tiers that were spilling get a smaller context that stays on the GPU. Measurements and the no-regression matrix are in #6704 (which I'll close). @Imagineer99 happy for you to merge whenever, and more testing is very welcome. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 124d36a9d1
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 87f67bd982
ℹ️ 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".
| known_total = sum(total_by_idx.get(i, 0) for i, _ in subset if total_by_idx.get(i, 0) > 0) | ||
| budget = sum(f * frac for i, f in subset if total_by_idx.get(i, 0) <= 0) | ||
| if known_total > 0: | ||
| budget += max(0.0, known_free - _vram_reserve_mib(known_total, frac)) |
There was a problem hiding this comment.
Don’t let low-free GPUs subsidize the pool
When the auto path adds another known-total GPU to a prefix, this pooled calculation can treat that GPU’s raw free memory as extra budget even if the GPU has no usable headroom on its own. For example, two 24 GiB cards with free [(0, 24000), (1, 500)] make the one-GPU budget about 20.9 GiB, but the two-GPU budget becomes (24000 + 500) - 3072, so a ~21.1 GiB footprint is accepted only after selecting GPU 1; load_model then masks both GPUs and emits -ngl -1 with --fit off, forcing a nearly-full card into the full-offload launch instead of falling back to --fit.
Useful? React with 👍 / 👎.
|
Thanks @anmolxlight for reporting and diagnosing #6682. Auto-context was sizing past the real VRAM headroom and spilling to CPU, which halved decode, and the original fix here (dropping the headroom fractions) pointed at the right thing. This PR then moved to the floored reserve I pushed on top (the 3 GiB floor). It works, but we are consolidating on #6718, which reserves the same headroom itemized (CUDA context, mmproj, MTP draft) rather than as a flat floor, so the model stays on GPU with more usable context. Head-to-head on a real 3090 (24 GB), same model, same session:
Both keep the model fully on GPU at the same speed, so both resolve the spill. #6718 reclaims more context because the itemized reserve is less conservative than the flat floor, and it carries the Closing in favor of #6718. Thanks again for the report and the original fix. |
Problem
The auto context sizing in
_fit_context_to_vramand_select_gpususes aggressive VRAM headroom fractions (0.95 pin fraction, 0.90/0.85 fit budget) that leave too little margin for CUDA runtime overhead, compute buffers, and memory fragmentation.On tight VRAM tiers (e.g. 24-32 GB) this causes
llama-serverwith-ngl -1to spawn at a context length the estimation says fits but which can't actually fit at runtime, spilling most layers to CPU. Users see 29% GPU utilization with 300% CPU instead of full GPU speed.Reported in #6682.
Changes
_GPU_PIN_VRAM_FRACTION_fit_context_to_vrambudget (normal)_fit_context_to_vrambudget (MTP)Trade-off
Slightly fewer models will get
-ngl -1(full GPU offload); more will fall through to--fit onwhich puts some layers on CPU. But models that do land on-ngl -1will actually run at full GPU speed instead of silently spilling, which is a better outcome than the current broken state.Reverts the
_GPU_PIN_VRAM_FRACTIONchange from 0.90 → 0.95 made in #5106, which turned out to be too aggressive.Closes #6682