Skip to content

Studio: Qwen-Image-Layered on the diffusers engine, GGUF included - #12775

Merged
danielhanchen merged 6 commits into
mainfrom
studio/gguf-qwen-layered
Oct 6, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio/gguf-qwen-layered

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Problem

Studio refuses every Qwen-Image-Layered checkpoint on the diffusers engine, including the public unsloth/Qwen-Image-Layered-GGUF BF16 file:

ValueError: 'unsloth/Qwen-Image-Layered-GGUF' is not a supported diffusion image model. Supported families: flux.1, ..., sdxl.

There was no family for it at all, GGUF or full repo:

  • detect_family (diffusion_families.py) matches qwen-image for a qwen-image-layered id, then _EDIT_KEYWORDS = ("edit", "kontext", "inpaint", "layered") rejects it, because layered is not in the qwen-image tokens. The tests asserted that refusal (test_detect_family_rejects_layered).
  • So there was no pipeline class, base repo, VAE or output handling to route to. family_override="qwen-image" would have loaded QwenImagePipeline, which cannot run this transformer.

Adding the family exposed a second bug on the GGUF route. The public GGUFs store time_text_embed.addition_t_embedding (an nn.Embedding(2, 3072)) in BF16. diffusers' GGUF quantizer only dequantises inside the nn.Linear layers it swaps for GGUFLinear, and BF16 stays packed (numpy has no bf16). The embedding therefore returns raw bytes, and the first step dies with The size of tensor a (3072) must match the size of tensor b (6144). The BF16 and UD-Q4_K_XL files both hit it.

Change

  • New qwen-image-layered family (diffusion_families.py):
    • QwenImageLayeredPipeline + QwenImageTransformer2DModel, base Qwen/Qwen-Image-Layered. A GGUF pick reads the transformer config from the base through config=, which is where use_additional_t_cond / use_layer3d_rope come from. The RGBA VAE also comes from the base.
    • edit=True: an input image is required and the only workflow is edit, as for Kontext and Qwen-Image-Edit.
    • condition_image_mode="RGBA", because the layered VAE encodes 4 channels.
    • ComfyUI's Image to Layers template values: 20 steps, cfg 2.5 (real CFG via true_cfg_scale), ModelSamplingAuraFlow shift 1 (comfy_flow_shift=1.0), 2 layers, input scaled to 640.
    • Two new family fields, layer_count (2) and layer_resolution (640). Every other family has layer_count=0.
    • The family name outranks qwen-image in detect_family. layered stays in _EDIT_KEYWORDS, so a layered variant of any other family is still refused.
  • Generation (diffusion.py, only when layer_count is set):
    • The pipeline gets layers and resolution.
    • The input is decoded at the family's condition mode, so its alpha is kept. Kontext and Qwen-Image-Edit still decode RGB.
    • Each returned RGBA layer is its own output image and carries the decomposition's seed. The route already persists every returned image to the gallery, and the Images page already shows every image in the response, so no route or UI change is needed.
    • One decomposition per forward: the pipeline groups its decoded layers per prompt with a stride that is only right for one input.
    • The compile-cache shape and the activation guard use the pipeline's real canvas (640 x 640 area at the input's aspect ratio, 32 px grid) and count the extra layer frames.
  • _dequantize_gguf_outside_linears (diffusion.py): after a GGUF transformer load, any GGUF-packed parameter outside a GGUFLinear is dequantised to the compute dtype. These are embedding tables and norms, a few KB.
    • A CPU census of the UD-Q4_K_XL files through Studio's own single-file shims finds such a parameter only in Qwen-Image-Layered.
    • Qwen-Image-2512, Qwen-Image-Edit-2511, Z-Image-Turbo, FLUX.1-dev, FLUX.1-Kontext and FLUX.2-klein-4B have none, so for them the call is a no-op.
  • Listing / loader agreement: a local pipeline folder named qwen-image-layered whose model_index.json declares a plain QwenImagePipeline stays refused in both the picker listing (catalog_classification._local_model_task) and the loader (detect_family_for_pick). Without this, the name would now route it to the layered pipeline. A real saved QwenImageLayeredPipeline resolves from its index.
  • Tables:
    • _TRUSTED_NON_GGUF_REPOS gains qwen/qwen-image-layered.
    • _PIPELINE_MIN_DIFFUSERS gains QwenImageLayeredPipeline: 0.37.0. It is absent from the 0.36.0 wheel's diffusers/__init__.py and exported by 0.37.0's.
    • The bf16 size, TE int8 skip, int8 exclude and scheme deny rows copy qwen-image-edit's (same DiT and encoder).
    • Generation defaults are 20 / 2.5.
    • UI: the generation defaults, the family label, and the picker's supported-edit keyword list.
  • sd.cpp: the family's sd_cpp_* fields are left empty, so the native engine still refuses it. That route is separate work.

Results

B200, Studio's in-process DiffusionBackend on the pick unsloth/Qwen-Image-Layered-GGUF, main (f865421355) against this branch. Same input (object.png, 768 x 768 RGB), prompt "A product photo of an object on a plain studio background", seed 42, 20 steps, guidance 2.5, speed_mode=off, text_encoder_quant=none. BF16 is the public file through Studio's own resolver. UD-Q4_K_XL is the repacked public-candidate file.

file main this PR output
qwen-image-layered-BF16.gguf (public) refused: not a supported diffusion image model renders 2 RGBA layers, 640 x 640, alpha mean 1.000 / 0.881
qwen-image-layered-UD-Q4_K_XL.gguf refused, same error renders 2 RGBA layers, 640 x 640, alpha mean 1.000 / 0.880
qwen-image-layered-Q4_K_M.gguf (public) refused, same error renders 2 RGBA layers, 640 x 640, alpha mean 1.000 / 0.880

LPIPS, each layer alpha-composited over mid-gray, plus the composite of all layers over gray:

pair layer 1 vgg / alex layer 2 vgg / alex composite vgg / alex
UD-Q4_K_XL vs BF16, this PR 0.003 / 0.000 0.010 / 0.002 0.011 / 0.003
public Q4_K_M vs BF16, this PR 0.003 / 0.000 0.010 / 0.006 0.011 / 0.006
this PR BF16 vs the reference workflow's BF16 render (same input, prompt, seed number, 2 layers, 640) 0.057 / 0.001 0.028 / 0.010 0.036 / 0.015
reference workflow, its own UD-Q4_K_XL vs BF16 (context) 0.040 / 0.000 0.014 / 0.002 -
  • Both runtimes split the input the same way: layer 1 is the white background, layer 2 is the object with transparency around it.
  • The scheduler is rebuilt at static shift 1.0. The log shows dequantised 1 non-linear parameter(s): time_text_embed.addition_t_embedding.weight for both files.
  • Render time: 24 s BF16, 76 s UD-Q4_K_XL. These are shared GPUs, so treat the times as indicative only.
  • Sanity against the pinned sd-cli render from the same GGUF: LPIPS 0.70 to 0.89. That sd-cli run was text-to-layers with a different prompt, not image decomposition, so it only confirms that neither image is broken.

Qwen-Image-Layered on the GGUF route

Rows: this PR BF16, this PR UD-Q4_K_XL, reference workflow BF16. Columns: input, layer 1 over gray, layer 2 over gray, composite.

Tests

  • New test_diffusion_qwen_layered.py, 16 tests:
    • detection of the GGUF, full-repo, alias and local-filename ids;
    • neighbouring families unchanged, and other families' layered / inpaint variants still refused;
    • the family contract (workflows, RGBA, cfg kwarg, 2 layers at 640, shift 1, defaults 20 / 2.5, trusted base);
    • the GGUF transformer reading config="Qwen/Qwen-Image-Layered", subfolder="transformer";
    • the decomposition call through the real generate(), against a double with the pipeline's own signature: layers, resolution, true_cfg_scale, empty negative, RGBA input with its alpha, and two RGBA outputs with seeds [7, 7];
    • a seed list running one decomposition per forward;
    • an input image being required;
    • the registered canvas shape;
    • saved-pipeline index agreement and contradiction;
    • _dequantize_gguf_outside_linears on a BF16-packed embedding beside a GGUFLinear.
  • Updated: the three detection tests that asserted Qwen-Image-Layered is refused now assert it resolves, and they keep the refusal for a FLUX / Z-Image layered variant.
  • Results, test_diffusion_qwen_layered.py + test_diffusion_backend.py + test_cached_gguf_routes.py + test_diffusion_gguf_compile.py + test_diffusion_gguf_offload_route.py + test_gguf_route_cursor_reset.py + test_diffusion_unified_edit.py + test_diffusion_flow_shift.py + test_diffusion_auto_policy.py + test_diffusion_precision.py + test_diffusion_transformer_quant.py:
    • torch 2.14 / diffusers 0.41.0.dev0: main has 15 failures, all in the new file. This branch: 1421 passed.
    • torch 2.12 / diffusers 0.40.0.dev0: this branch has 1 failure, test_pipeline_class_guard_passes_every_shipped_family, the same one main has (Qwen-Image-2.1 needs diffusers 0.41).
  • Every test_diffusion_*.py, test_sd_cpp_*.py, *famil* and test_images* file (168 files): the failing test IDs on this branch are a subset of main's on the same interpreter. No new failures.

Not covered

  • The full Qwen/Qwen-Image-Layered repo load (non-GGUF) is unit-tested only; its 41 GB transformer was not downloaded.
  • One input image, prompt and seed. No repeated-run noise floor.
  • speed_mode=off only (no compiled path), on B200 only.
  • The layer count (2) and the 640 bucket are family defaults. There is no request field to change them.
  • Qwen-Image-Layered is not added to the curated model catalog. It is reachable through Hub search and the family override.
  • The native sd.cpp engine still refuses this family.
  • The UI changes were not type-checked or rendered here.

UI before / after

Images page model picker, searching "Qwen-Image-Layered" (base 192554dee vs this branch). Before, the picker hid every layered repo as an unsupported edit model; now the GGUF is listed.

Images picker before and after

Qwen-Image-Layered had no family, and the layered keyword guard routed
every layered id away from qwen-image, so the public GGUFs were refused
as unsupported. Add a qwen-image-layered family on
QwenImageLayeredPipeline with the ComfyUI Image to Layers defaults
(20 steps, cfg 2.5, shift 1, 2 layers at 640), an RGBA input, and each
decomposed RGBA layer returned as its own output image.

The public GGUFs store time_text_embed.addition_t_embedding (an
nn.Embedding) in BF16, which diffusers keeps packed outside its
GGUFLinear layers, so the first step read raw bytes. Dequantise packed
parameters that sit outside the quantizer's linears after a GGUF
transformer load; no other shipped family has any.

A local pipeline whose model_index declares a family that its folder
name rules out stays refused in both the listing and the loader.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 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 ⚠️ Failed 2026-10-06T03:52:52.343543Z ce0abde New commits
ℹ️ 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.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 5, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 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 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: e3af2c0c13

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

// backend's qwen-image-edit family.
const SUPPORTED_EDIT_KEYWORDS = ["qwen-image-edit", "kontext"] as const;
// backend's qwen-image-edit, flux.1-kontext and qwen-image-layered families.
const SUPPORTED_EDIT_KEYWORDS = ["qwen-image-edit", "kontext", "qwen-image-layered"] as const;

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 Whitelist the underscore alias in the picker

For a Hub repository named with the newly supported qwen_image_layered alias, none of these supported tokens matches, but the generic layered segment does. Consequently isImageEditModel() returns true and the Images picker hides a model that backend detection explicitly accepts; include the underscore spelling or normalize separators before applying the edit-model exclusion.

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.

Fixed in 542a09e: the picker's supported list now carries every spelling the backend accepts (qwen-image-layered, qwen_image_layered, qwenimagelayered).

Comment on lines +33 to +34
{ match: "qwen-image-layered", steps: 20, guidance: 2.5 },
{ match: "qwen_image_layered", steps: 20, guidance: 2.5 },

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 layered defaults to the compact alias

The family registry also accepts qwenimagelayered, but this defaults table only covers the hyphenated and underscored spellings. Selecting a repository with that compact alias therefore seeds the Studio controls with the fallback 9 steps and guidance 0 rather than the intended 20/2.5 recipe, which can materially degrade the decomposition; add the compact alias here and to the matching backend defaults table.

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.

Fixed in 542a09e: qwenimagelayered added to both the frontend and backend defaults tables, with tests covering each spelling.

@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: 542a09e5a2

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

const SUPPORTED_EDIT_KEYWORDS = [
"qwen-image-edit",
"kontext",
"qwen-image-layered",

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 Keep unsupported layered variants out of the picker

For an ID such as org/Qwen-Image-Layered-Inpaint, this newly whitelisted token makes isImageEditModel() return false immediately, so the Images picker exposes the model despite the additional reserved inpaint segment. Backend detect_family() checks every _EDIT_KEYWORDS segment against the matched family and rejects this same ID, leaving users with a selectable model that cannot load. Only bypass the exclusion when all reserved variant segments are supported by the matched family rather than when any supported token is present.

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.

Not changing this: no such repo exists, and the allowlist has the same shape for the shipped qwen-image-edit and kontext entries; a hypothetical layered-inpaint id is outside the families this PR adds.

# ComfyUI's Image to Layers template: ModelSamplingAuraFlow 1, 2 layers, the input scaled to 640.
comfy_flow_shift = 1.0,
layer_count = 2,
layer_resolution = 640,

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 Expose the layered canvas instead of claiming source-sized output

For every Qwen-Image-Layered generation, the pipeline derives a fixed approximately 640² canvas from the source aspect ratio, not from the source's pixel dimensions. Because the family is exposed only as a generic edit workflow and layer_resolution is not included in the status capabilities, the Images page instead tells users that output size comes from the source and that uploading a smaller image will reduce it; neither is true for this family. Surface the fixed bucket/decomposition behavior so the UI can report the actual output sizing and an actionable memory remedy.

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.

Not changing this here: it is copy on the Images page, not a wrong render; the decomposition still runs at the pipeline's own canvas and returns correct layers.

if layer_count:
# Every decomposition denoises layers + 1 frames beside the encoded input, all at the canvas
# size: the frames past the first are extra tokens on top of the output-sized one.
guard_condition_pixels += (layer_count + 1) * guard_width * guard_height

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 Reserve layered frames when choosing the load placement

On cards where this large model needs weight offloading, load-time placement still reserves the generic single-frame 1024² headroom (about 8 GiB), while this generation-time calculation charges four 640² frames for the two-layer configuration (about 12.8 GiB). Qwen-Image-Layered has no calibrated placement entry, so measured_request_extra_mib() returns zero and cannot release additional resident groups; the subsequent activation guard can therefore reject every render after a successful default load even though offloading roughly another 5 GiB of weights would make it runnable. Include the family's fixed layer/canvas cost in load planning, including the unified-memory preflight.

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.

Fixed in 66c6311: the planning estimate for a qwen-image-layered load now reserves the decomposition's footprint (640 canvas, output + input + layers frames), so planned equals what the guard charges (12800 MiB both) and an offloaded load no longer refuses its default render. Load planning, the unified preflight and the guard's planned figure share that estimator; other families unchanged.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 66c63116dd

ℹ️ 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 pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 6, 2026
@danielhanchen
danielhanchen merged commit 7c16bcc into main Oct 6, 2026
@danielhanchen
danielhanchen deleted the studio/gguf-qwen-layered branch October 6, 2026 05:23
danielhanchen added a commit that referenced this pull request Oct 6, 2026
…erage in Backend CI (#12843)

#12766 added the VAE seam guard and #12775 / #12777 added the
qwen-image-layered family right after it, with no entry in
vae_tile_configs.json. The seam guard fails on main:
test_every_family_has_a_vae_config, then a KeyError in the geometry check.

Add Qwen/Qwen-Image-Layered's vae/config.json at revision 8f0ca708df (the
4-channel RGBA AutoencoderKLQwenImage). With it the full seam guard passes:
27 passed.

The coverage check lives in a module that importorskips torch and diffusers,
so it only ran in the path-filtered seam-guard job. A dependency-free copy
now runs in Backend CI, plus a shape check on each entry.
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.

1 participant