Skip to content

Studio: fit an oversized DiffusionGemma GGUF to the GPU in Auto mode - #13312

Merged
danielhanchen merged 8 commits into
mainfrom
studio-diffusion-auto-fit-ngl
Oct 12, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
studio-diffusion-auto-fit-ngl

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes #6456

Problem

Studio serves DiffusionGemma GGUFs through the diffusion runner (the unsloth_zoo shim driving llama-diffusion-gemma-visual-server). In Auto GPU memory mode the runner always puts every layer on the GPU. The GPU-layer control is hidden for diffusion models in the model picker, so Auto is the only path a UI user has.

When the GGUF is larger than the card (diffusiongemma-26B-A4B Q4_K_M is 16.8 GB, the report is an 8 GB RTX 4060):

#7575 made a Manual layer split reach the runner, but left Auto unchanged, and the picker does not offer Manual for diffusion.

Change

In Auto mode, before launching the runner, Studio checks whether the GGUF fits the runner's device (same _vram_usable_mib budget, Windows sysmem-fallback reserve included, plus 512 MiB for the runner's own CUDA context). Only when it does not fit does it pass --ngl N, with N the number of layers that fit while leaving room for the runner's compute buffer (1.75x the [n_head, N, N] fp32 scores, measured on a T4: 1752 MiB at 4096) and the 8% of VRAM the runner's context probe keeps free. When it splits and the context was left to auto-size, --maxtok 4096 is pinned too: the runner's second sizing pass probes against host RAM, which on Windows would again be backed by shared memory.

Unchanged:

  • Every GGUF that fits the card today: no --ngl, same --maxtok, byte-identical argv.
  • Manual splits, CPU-only / forced-CPU loads, DG_GPU / UUID tokens, Vulkan builds.
  • An older unsloth_zoo shim without --ngl: logs a warning and keeps today's launch.
  • Unpinned multi-GPU hosts without CUDA_DEVICE_ORDER=PCI_BUS_ID: sized against the roomiest card, so a load that fits any card is never cut down.
  • /status still reports Auto, and reload dedup compares the request, not the fit.

Composes with #13291 (multi-GPU split): that PR widens --gpu to a comma list, which this fit leaves alone.

Evidence

Colab T4 (15 GB), Linux, install.sh --local from the merge base, same venv, llama.cpp and unsloth_zoo for both arms; only the Studio backend tree differs. diffusiongemma-26B-A4B-it-Q4_K_M.gguf, Auto mode, no GPU pick:

arm runner argv result
base (n_ctx 4096) --gpu 0 --maxtok 4096 llama_model_load: error loading model: unable to allocate CUDA0 buffer, load fails
head (n_ctx 4096) --gpu 0 --maxtok 4096 --ngl 20 loads: CUDA0 weights 10376 MiB, CPU mapped 6225 MiB, compute 1752 MiB, runner holds 12232 MiB; chat returns "The three primary colors are red, yellow, and blue."
head (n_ctx 0, auto-size) --gpu 0 --maxtok 4096 --ngl 20 same as above, MAXTOK=4096 budget=requested

Splits the fit picks for this GGUF at a 4096 context: RTX 4060 8 GB on Windows 6 layers, on Linux 8; 6 GB card 4; T4 20; 20 GB, 24 GB and larger cards unchanged (all layers).

Tests: studio/backend/tests/test_diffusion_auto_fit_ngl.py (16 cases: fit decisions, device selection, Vulkan / failed probe, and the real _start_diffusion_server argv for split, fits, old shim and manual). test_differential.py against the merge base: the two launch tests fail on base and pass here; the fits / old-shim / manual launch tests pass on both. The 35 existing backend and tests/studio files touching diffusion: 3498 passed, 0 failed on both base and head.

Not covered here: the Windows sysmem-fallback path itself (no Windows NVIDIA runner); the split is sized with that reserve and the context pinned, but it was measured on Linux only.

@danielhanchen danielhanchen self-assigned this Oct 11, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T20:01:57.848615Z b61a279 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-11T10:56:28.202619Z 195aa6b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 53bb12c3d5

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

Comment on lines +19452 to +19454
if not pinned and len(rows) > 1 and os.environ.get("CUDA_DEVICE_ORDER") != "PCI_BUS_ID":
row = max(
rows,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Size unpinned launches against a card they may actually use

On a heterogeneous multi-GPU host without CUDA_DEVICE_ORDER=PCI_BUS_ID, this replaces the candidate corresponding to gpu with the roomiest card, even though nothing guarantees CUDA's fastest-first selection will choose that card. This also ignores an explicit DG_GPU selection because only gpu_ids sets pinned; for example, selecting an 8 GiB GPU while another card has 24 GiB makes the fit return no split, then the child still launches on the selected 8 GiB card and reproduces the allocation failure this change is intended to prevent. Resolve the actual selected device or conservatively size against every device it could denote rather than taking the maximum.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Deliberate: sizing an ambiguous unpinned device against the smallest card would cut layers from loads that fit today. Taking the roomiest card is never worse than before this PR (no split = today's launch); DG_GPU with mixed cards is an operator override outside the picker.

Comment on lines +19436 to +19439
binary = self._find_llama_server_binary()
# Vulkan ordinals are not the CUDA ids the runner's mask takes.
if binary and self._is_vulkan_backend(binary):
return None

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 Classify the resolved visual-server binary

When DG_VISUAL_BIN points to a visual server whose backend differs from the normal llama-server installation, the fit classifies the unrelated llama-server binary instead. A Vulkan llama-server alongside an explicitly configured CUDA visual server therefore returns here without fitting and leaves an oversized model fully GPU-resident; the inverse configuration can probe CUDA memory for a Vulkan child, and the Windows sysmem-fallback classification below is similarly taken from the wrong installation. Pass the already resolved visual_bin into the fitter and use it for backend and fallback detection.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

DG_VISUAL_BIN pointing at a visual server from a different backend than the installed llama-server is a developer override, not a shipped layout; the prebuilt bundle installs both from one build.

Comment on lines +4920 to +4921
usable = _vram_usable_mib(
free_mib, total_mib, _active_vram_fraction(), sysmem_fallback = sysmem_fallback

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 Record the VRAM budget used by the diffusion fit

Auto diffusion placement now uses the active VRAM-budget fraction to decide both whether a split is needed and how many layers to offload, but the diffusion launch path still sets _vram_fraction_launched to None because it previously did not consume that setting. As a result, lowering the budget after a launch reports reload_required=false, and reapplying the identical model deduplicates against the running process instead of recomputing --ngl, leaving the old, higher VRAM allocation active. Capture the load's budget fraction and record it for diffusion launches that run this fit.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The VRAM budget control is not shown for diffusion models, and a stale split only lasts until the next load; the split stays safe either way. Not worth widening the dedup for.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 20fbebe9d8

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

Comment on lines +4925 to +4926
if file_mib <= usable:
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Include the context reserve before declaring a full fit

When the GGUF weights fit by themselves but the weights plus the requested context do not, this early return skips fitting before ctx_reserve is calculated. For example, the tested 16,806,810,208-byte Q4 model on a 20,475 MiB card with 19,500 MiB free is considered fully resident even at maxtok=8192, although the function's own estimate adds about 8,806 MiB of context/probe reserve; the runner can therefore still OOM or spill into system memory. Base the full-fit decision on the file plus the applicable context reserve, not the file alone.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Deliberate: when the weights fit, the runner's own context probe degrades the context rather than failing, and that is today's behaviour on every card that loads now. Moving layers to the CPU there would slow every borderline card for a context the runner already shrinks; this PR only changes loads that cannot hold the weights.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 930d09e113

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

# Auto: a GGUF too large for that device gets the split that fits it (#6456).
fit_ngl = None
if manual_ngl is None:
fit_ngl = self._diffusion_fit_ngl(model_path, gpu, maxtok, pinned = bool(gpu_ids))

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 Apply the auto split before the training VRAM guard

When diffusion training is active, _guard_chat_load_against_training still scales required_override_gb only for an explicit manual diffusion_ngl (routes/inference.py lines 16593-16604), so an oversized Auto DiffusionGemma is evaluated as fully GPU-resident and rejected with 409 before execution can reach this new fit. This makes the Auto fix unavailable in the supported chat-during-training flow—particularly significant because the UI hides the manual diffusion layer control—even when the fitted split would fit alongside training. Reuse the Auto planner in the preflight guard, or otherwise admit the load based on the split it will actually launch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Out of scope and unchanged from base: the guard refuses an Auto DiffusionGemma load during LLM training exactly as before this PR, which protects the running job. Admitting a CPU-heavy split beside an active trainer is a separate policy decision, not part of fixing the oversized Auto load.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 76e1cce4e8

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

@danielhanchen

Copy link
Copy Markdown
Member Author

Backend CI (Python 3.13, l-r) fails on tests/test_llama_cpp_wait_for_health.py::test_a_cancelled_diffusion_start_reaps_the_runner:

AssertionError: the cancelled runner was left running (kills=1)
assert 1 == 2

It passes on main, and this PR changes the DiffusionGemma start path in core/inference/llama_cpp.py, so the new Auto-fit start appears to skip one of the two kills a cancelled diffusion start is expected to issue (the runner is no longer reaped on cancel). The Chat UI Tests (chat) red looks unrelated (a permissions-step locator timeout; other PRs pass that job).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b61a279b5b

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

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: DiffusionGemma 26B-A4B exhibits Initialization Memory Saturation / Leak in Unsloth Studio

1 participant