Skip to content

Studio: add image generation with Unsloth GGUFs - #6658

Closed
oobabooga wants to merge 42 commits into
unslothai:mainfrom
oobabooga:image-generation
Closed

oobabooga wants to merge 42 commits into
unslothai:mainfrom
oobabooga:image-generation

Conversation

@oobabooga

@oobabooga oobabooga commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

This PR adds image generation support through diffusers, using Unsloth GGUFs.

Everything is contained in a new Image tab, which has

  • A menu for downloading Unsloth GGUFs for image generation models
  • Input elements (prompt, steps, resolution, etc)
  • An output image component with a gallery for past generations
image

How it works

The model menu is the same picker used for chat. In the Image tab it lists only image-generation GGUFs, and in the chat tab it excludes them, so neither lists the other's models.

Only the transformer ships as a GGUF, so the matching VAE, text encoder(s) and scheduler are pulled from the model's base diffusers repo. That base repo is resolved automatically from the GGUF repo's base_model tag.

The GPU is shared with the chat backend: loading an image model automatically evicts the loaded chat model and vice versa, so only one ever sits in VRAM.

Model architectures

The following models have been tested and integrated. Detection is by architecture, so one entry covers every quant and variant:

Tested but not integrated:

  • ERNIE-Image: its diffusers transformer has no single-file GGUF loader yet (ComfyUI-only for now)
  • FLUX.2-dev: its 48GB Mistral text encoder is too big to sit on one GPU with the transformer, and the sequential CPU offload that would fit it is broken for GGUF transformers in diffusers 0.38
  • Editing models (Qwen-Image-Edit, FLUX.1-Kontext): they need an input image, this tab is text-to-image only

Features

  • Disk-backed gallery with infinite scroll
  • Output images with embedded metadata for the generation settings (prompt, model, steps, etc), so any past image's settings can be reloaded
  • A slider for the batch size (parallel generation) and runs (sequential generations)
  • Reproducible seeds: a random seed is recorded and can be replayed
  • Resolution presets and aspect ratio controls
  • Live download progress for the model and its base repo while loading, and a per-step progress bar with ETA while generating

Not covered

  • Video generation
  • Text encoder quantization (the text encoder always loads in full precision)
  • LoRA support
  • Inpainting support
  • Image to image
  • Image editing
  • CPU offloading option (that would be better to add using the UI in Feat/model picker per model config #6647)
  • torch.compile option (same as above)

Prior work

The single-GPU handoff between chat and diffusion is adapted from #5754 by @danielhanchen. #5872 is a related WIP that also adds diffusion (including video).

oobabooga added 26 commits June 23, 2026 00:39
@oobabooga
oobabooga requested a review from danielhanchen as a code owner June 25, 2026 03:21
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@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: 030a565b06

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

Copy link
Copy Markdown
Member

/gemini review

@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 local text-to-image (diffusion) backend to the application, featuring model loading, image generation, disk-backed gallery persistence, and a single-GPU arbiter to coordinate VRAM usage between chat and image generation tasks. It also adds the corresponding frontend UI, routing, and comprehensive unit tests. The review feedback identifies a critical pagination bug in the image gallery where slicing is performed before filtering out invalid files, and points out a violation of model detection rules in the family detection logic due to raw substring matching instead of segment-boundary checks.

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: 0607351a81

ℹ️ 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 thread studio/backend/routes/models.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

/gemini review

@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 local text-to-image diffusion backend, featuring a single-GPU arbiter to manage VRAM allocation between chat and image generation, a disk-backed image gallery, and a new 'Images' frontend page with custom controls. Feedback on these changes highlights two key improvements: first, preventing an API exception when repo_id is a local path in _estimate_download_bytes to ensure companion files are properly prefetched; second, tracking the mount status of the ImagesPage component to prevent sequential generation requests from continuing in the background if a user navigates away.

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: 0607351a81

ℹ️ 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 thread studio/backend/routes/models.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

Went through the new round of Codex and Gemini comments. One code change, the rest left alone with reasoning below.

Changed

  • pickers.tsx: completed the Images picker GGUF gate. The earlier fix gated the default ("All") format view but not the explicit format branch. So switching the format dropdown to Safetensors or MLX in the Images picker still surfaced non-GGUF rows the backend can't load, and clicking them was a silent no-op. The gate now applies across every format selection, matching the search (Hub) path.

Left as-is, with reasoning

  • On-device non-curated diffusion GGUF shows in the Images picker then 400s (raised a few times across both bots): it's already an honest 400 with a "Supported: Z-Image, Qwen-Image, FLUX.1, FLUX.2-klein" message. Validation runs before the GPU is taken, so nothing is evicted. A clean fix isn't cheap or safe here. The text-to-image tag has to stay broad because it's also what hides diffusion GGUFs from the chat picker, so telling "loadable in Images" apart from "any diffusion GGUF" needs a separate backend signal. Inferring the family from GGUF metadata doesn't work for FLUX either: FLUX.1 and FLUX.2-klein both report general.architecture='flux', which is exactly why detection uses the repo name to tell them apart. The curated rail the UI recommends resolves families correctly.
  • batch_size missing from the saved recipe: the recipe is a documentary record, not a reproduce button. "Restore these settings" reapplies prompt, steps, guidance, seed, and dimensions only, never the model or the batch, so persisting batch_size alone wouldn't reproduce a batched image. Not worth the extra fields for no reproduction path.
  • The remaining comments re-raise items already covered in my earlier summary: gallery pagination (needs a foreign or hand-dropped PNG in the internal images dir), _estimate_download_bytes and the local-path load branch (never reached from the UI, which only sends curated HF repo ids), generation continuing after navigation (it finishes server-side and the images land in the gallery), and detect_family matching (it already splits on segment boundaries, not raw substrings). The multi-user gallery-auth note doesn't apply since Studio is a single-user local app.

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

ℹ️ 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 thread studio/backend/routes/inference.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

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

ℹ️ 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 thread studio/backend/routes/models.py Resolved

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

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

} catch {
// Transient poll failure: keep trying.
}
pollTimer.current = setTimeout(() => void pollLoadProgress(), 1000);

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 Stop rescheduling load polls after unmount

When the Images page is left while getDiffusionLoadProgress() is still in flight, the cleanup has no timeout to clear; once that await returns, this line unconditionally schedules another poll. That leaves a hidden polling loop running after unmount and can later call state setters or show the “Model loaded”/error toast on another page, despite the cleanup intending to dismiss the load toast. Guard the callback with an unmounted/cancelled flag or abort the poll before rescheduling.

Useful? React with 👍 / 👎.

Comment on lines +775 to +776
for (let i = 0; i < runs; i++) {
const res = await generateDiffusionImage({

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 Stop multi-run generation when the page unmounts

If the user starts a multi-run generation (count > 1) and navigates away after the first request is sent, the cleanup only clears the progress interval; this async loop keeps issuing the remaining /images/generate calls because nothing checks an unmounted/cancelled flag between iterations. A long “Runs” value can therefore continue consuming the GPU and saving gallery images from a page the user left, so the loop should stop on unmount or before starting the next run.

Useful? React with 👍 / 👎.

return
self._state = None
del state
clear_gpu_cache()

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 Clear the MPS cache when unloading image models

On Apple Silicon this backend can load the diffusion pipeline on mps, but this unload path delegates to clear_gpu_cache(), which is keyed off the global hardware detector that reports MLX on those hosts and takes the MLX no-op branch. Unloading or replacing an Images model there deletes Python references without releasing PyTorch MPS cached memory, making subsequent image loads or training more likely to OOM; clear based on state.device or call torch.mps.empty_cache() for MPS pipelines.

Useful? React with 👍 / 👎.

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

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

# holding. No-op when diffusion isn't loaded.
from core.inference.gpu_arbiter import acquire_for, CHAT

await asyncio.to_thread(acquire_for, CHAT)

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 Defer chat GPU acquisition until validation passes

When an Images model is loaded, this acquires the chat owner and evicts diffusion before the request has passed the ModelConfig.from_identifier validation and the chat/training VRAM guard below. A bad or unsupported chat selection, or a later 400/409, therefore unloads a working image pipeline even though no chat model ends up loaded; the image load path already validates before acquiring for the same reason. Move the acquisition until after the later rejection paths, while still acquiring before an actual chat load or already-loaded success needs ownership.

Useful? React with 👍 / 👎.

# Local image generation (Images page): diffusers' GGUFQuantizationConfig needs
# the gguf reader. diffusers itself is torch-bound and comes via the ML stack;
# gguf is torch-free so it lives here, where it's installed in every mode.
gguf

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 Pin diffusers to a version with these pipelines

Adding only gguf leaves the ML stack free to keep any already-satisfying diffusers version, but load_pipeline() now dynamically requires APIs such as GGUFQuantizationConfig and Flux2KleinPipeline. In an environment that resolves or already has an older diffusers release, the Images tab installs cleanly but selecting the new curated FLUX.2/GGUF models fails at load time; add an appropriate minimum diffusers version alongside this dependency change.

Useful? React with 👍 / 👎.

@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: 4b4e1345ec

ℹ️ 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 +3092 to +3093
"qwen_image",
"qwenimage",

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 Z-Image GGUFs as image models

When a supported Z-Image GGUF is discovered from the HF cache or a local/custom folder by general.architecture, _repo_gguf_task/_local_model_task route through this allow-list before the Images picker sees it. The backend supports Z-Image (including the zimage/z_image aliases in detect_family), but those architecture keys are absent here, so such on-device Z-Image files are tagged as text-generation, hidden from Images, and can be surfaced to the chat picker where they are not loadable; add the Z-Image architecture values to this set.

Useful? React with 👍 / 👎.

@oobabooga

Copy link
Copy Markdown
Member Author

Moved to #6763

@oobabooga oobabooga closed this Jun 30, 2026
danielhanchen added a commit that referenced this pull request Jul 8, 2026
…bf16_supported

torch.cuda.is_bf16_supported() defaults to counting pre-Ampere bf16 EMULATION as
supported, so on a T4/V100/RTX 20xx the DiT-training bf16 gates all passed even
though the trainer requires native Ampere-or-newer bf16: /diffusion/info advertised
the DiT precision modes, /diffusion/start's preflight let the run through and freed
resident GPU models, then the trainer child hit the real unsupported bf16 path. The
inference device resolver already fixed this (issue #6658) by gating NVIDIA on
capability major >= 8; the training path never got it. Add a shared
native_bf16_supported() helper (NVIDIA cap major >= 8; ROCm keeps the trustworthy
is_bf16_supported()) and use it in the three DiT bf16 sites -- train_precision_modes,
bf16_unsupported_reason, and the trainer guard -- so a pre-Ampere card is offered
nf4 only and never advertises/evicts-then-fails. Tests now exercise the emulation
case (is_bf16_supported True but capability < 8).
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.

3 participants