Skip to content

fix: reduce VRAM headroom fractions to prevent auto-context size spill (#6682) - #6688

Closed
anmolxlight wants to merge 11 commits into
unslothai:mainfrom
anmolxlight:fix/vram-spill-auto-context-size
Closed

anmolxlight wants to merge 11 commits into
unslothai:mainfrom
anmolxlight:fix/vram-spill-auto-context-size

Conversation

@anmolxlight

Copy link
Copy Markdown
Contributor

Problem

The auto context sizing in _fit_context_to_vram and _select_gpus uses 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-server with -ngl -1 to 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

Constant Before After Headroom
_GPU_PIN_VRAM_FRACTION 0.95 0.90 5% → 10%
_fit_context_to_vram budget (normal) 0.90 0.85 10% → 15%
_fit_context_to_vram budget (MTP) 0.85 0.80 15% → 20%

Trade-off

Slightly fewer models will get -ngl -1 (full GPU offload); more will fall through to --fit on which puts some layers on CPU. But models that do land on -ngl -1 will 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_FRACTION change from 0.90 → 0.95 made in #5106, which turned out to be too aggressive.

Closes #6682

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@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: 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".

@Imagineer99 Imagineer99 self-assigned this Jun 26, 2026
…room

# Conflicts:
#	studio/backend/core/inference/llama_cpp.py

@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: 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".

@oobabooga

Copy link
Copy Markdown
Member

I did some extensive speed measurements on this issue yesterday and came up with #6704 (e.g. on a 24 GB 3090, #6312's auto-pick already runs at ~35 of 60 tok/s on an idle card; the PR has the full curve and a no-regression check). Maybe the two PRs can be combined here.

@Imagineer99

Copy link
Copy Markdown
Member

I did some extensive speed measurements on this issue yesterday and came up with #6704 (e.g. on a 24 GB 3090, #6312's auto-pick already runs at ~35 of 60 tok/s on an idle card; the PR has the full curve and a no-regression check). Maybe the two PRs can be combined here.

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.

@oobabooga

Copy link
Copy Markdown
Member

Pushed a commit here that swaps the lowered fractions for a measured floored reserve: max(5%, 3 GiB), capped at 12.5%. Why I replaced the approach: lowering the fractions fixes the spill but reserves 15% on every card (12 GB on an 80 GB), whereas the floor keeps datacenter cards at 5% and only tightens the consumer tiers that actually spill.

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.

@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: 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".

@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: 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))

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 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 👍 / 👎.

@oobabooga

Copy link
Copy Markdown
Member

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:

slots this PR (floored reserve) #6718
1 ctx 37120, on GPU, ~56 tok/s ctx 56320, on GPU, ~55 tok/s
4 ctx 13312, on GPU, ~55 tok/s ctx 33024, on GPU, ~54 tok/s

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 --fit off placement fix and tensor-parallel handling on top.

Closing in favor of #6718. Thanks again for the report and the original fix.

@oobabooga oobabooga closed this Jul 3, 2026
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.

[Bug] VRAM spill with automatically set context size

3 participants