Skip to content

Studio: floor the GGUF VRAM fit reserve so tight cards keep KV on the GPU - #6704

Closed
oobabooga wants to merge 8 commits into
unslothai:mainfrom
oobabooga:studio-vram-cushion-6682
Closed

oobabooga wants to merge 8 commits into
unslothai:mainfrom
oobabooga:studio-vram-cushion-6682

Conversation

@oobabooga

Copy link
Copy Markdown
Member

On a 24 GB desktop card, loading a 27B GGUF would pin -ngl -1, then silently offload layers back to CPU at runtime and run at roughly half speed. Reported in #6682 (Qwen3.6-27B-MTP, ~14 tok/s instead of 30-35).

Closes #6682

Problem

The GGUF auto-fit (#6312) reserves a flat 5% of total VRAM before sizing context. On a 24 GB card that is only ~1.2 GB, too thin to absorb what the byte estimate can't capture exactly: the CUDA context and compute buffers, and the gap between nvidia-smi's reported free and the VRAM CUDA actually exposes. So auto-fit sizes the context right up against the card's real usable limit. At that edge part of the load ends up on host RAM and generation roughly halves, even with nothing else on the card.

Clean-card context sweep for Qwen3.6-27B-MTP UD-Q4_K_XL, q8 KV, on an idle RTX 3090 (24 GB):

context tok/s
32k-92k 60 (full speed)
102k 47
113k (what #6312 picks) ~35
131k 27

At the ~113k that 5% advertises, the 27B is already down to ~35 of 60 tok/s with the GPU sitting ~1 GB short of full and host RAM climbing (the load is not staying fully on the device). Backing the context off restores full speed. A desktop or browser sharing the GPU makes it worse: at -c 112128 with a 3.5 GB desktop held, it ran at 14.6 tok/s (reporter: 14.3).

Fix

Floor the reserve: min(max(0.05*total, 3072 MiB), 0.125*total). So the reserve is 5% base, floored at 3 GiB, capped at 12.5%:

  • <= 24 GB: 12.5% (3 GiB on a 24 GB card)
  • 24-60 GB: flat 3 GiB (a shrinking fraction)
  • >= 60 GB: 5% (datacenter cards unchanged)

The floor is a per-pool cushion. The desktop drift and the one-lump layer-split compute buffer it covers are not per-device, so it is applied once across the pool, not per GPU. It lives in one helper (_pooled_usable_mib) at the single place the auto-context budget is computed. Ranking and the tensor-parallel paths are untouched.

Speed (RTX 3090, 24 GB)

Same model and card, measured idle and with a 1 GB desktop sharing the GPU:

reserve context tok/s (idle) tok/s (1 GB desktop)
5% (#6312) 113408 ~35 (spilling) ~23
floored (this PR) 66560 60 60

The fix holds full speed through a ~1 GB desktop. It is a fixed cushion, not a guarantee. A 27B with a ~3 GB desktop on 24 GB is at the card's edge (the fix gives 21.9 vs 14.6 there), and a steady desktop gets sized down to a smaller full-speed context instead.

Generalizes across architecture and cache type:

No context regression for models that already fit

Base (#6312) vs fix, same hardware. Only the two 27Bs that were spilling get reduced. Everything else is byte-identical:

model #6312 ctx fix ctx
Qwen3-1.7B 40960 40960
gemma-4-12b 262144 262144
gemma-3-27b 122880 (spilled) 78592
Qwen3.6-27B 113408 (spilled) 66560
Qwen3.6-27B on a 49 GB card 262144 262144

Multi-GPU

The per-pool floor matters here. On a constrained 49+24 GB pool (neither card fits the model alone), a naive per-device floor would reserve 3 GiB per card and drop to the 4096 fallback with --fit. The per-pool floor pins both GPUs at 36352, 74 tok/s, no spill.

Verification

  • 6 new unit tests on the reserve helpers: floor, cap, and datacenter regimes, single-GPU pool, floor-applied-once-across-pool (the multi-GPU case), and the unknown-total (MIG/vGPU) carve-out.
  • VRAM, fit, MTP, tensor-parallel, and datacenter suites green (666 passed).
  • End-to-end through Studio on real hardware (RTX 3090 and RTX 6000 Ada): the loads in the tables above, plus the loaded 27B serves 102/102 spec-compliance tests at 67 tok/s with no spill.

Code change is ~50 lines (two helpers and one call-site swap). The rest is tests.

@oobabooga
oobabooga requested a review from danielhanchen as a code owner June 26, 2026 12:43

@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 introduces a VRAM reserve floor and cap mechanism to prevent auto-fit context spilling to CPU on consumer cards with thin VRAM cushions. It refactors the multi-GPU pooled budget calculation to apply the VRAM reserve floor once across the entire pool rather than per device, and adds comprehensive unit tests to verify the new behavior on consumer, datacenter, and small cards, as well as multi-GPU pools. 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.

@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: 328e5b4665

ℹ️ 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: 4a5657304e

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

Comment on lines +819 to +820
return min(
max((1.0 - frac) * total_mib, _MIN_VRAM_RESERVE_MIB), _MAX_VRAM_RESERVE_FRAC * total_mib

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 the floored reserve for pinned selections too

This new floor is only applied through _pool_budget_mib, but the explicit-context and file-size-only branches still call _select_gpus(..., total_by_idx=...), whose usable calculation subtracts only (1 - usable_fraction) * total. On a tight 24 GB card with an explicit --ctx-size or missing KV metadata, _select_gpus can still return use_fit=False based on the old ~1.2 GiB cushion, so Studio emits -ngl -1 instead of --fit on and spends the 3 GiB headroom this patch is trying to protect. Please make _select_gpus use the same floored reserve when totals are known.

Useful? React with 👍 / 👎.

@oobabooga oobabooga closed this Jun 27, 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

2 participants