Skip to content

Studio: route Qwen-Image-Edit / Edit-2509 / Edit-2511 and FLUX.1-Kontext GGUFs to the native sd.cpp engine - #12776

Merged
danielhanchen merged 14 commits into
mainfrom
studio/sdcpp-edit-routes
Oct 6, 2026
Merged

danielhanchen merged 14 commits into
mainfrom
studio/sdcpp-edit-routes

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Stacked on #12755 (explicit FLUX cfg on sd.cpp): merge that first. One extra commit here gives #12755's two Kontext cfg tests a source image, since Kontext is edit-only on this engine now.

Problem

The native sd.cpp engine refuses every instruction-editing GGUF: Qwen-Image-Edit, Qwen-Image-Edit-2509, Qwen-Image-Edit-2511 and FLUX.1-Kontext-dev. On main, a load fails before any download with:

ValueError: Family 'qwen-image-edit' has no native sd.cpp asset mapping.
ValueError: Family 'flux.1-kontext' has no native sd.cpp asset mapping.
  • begin_load (sd_cpp_backend.py:2660) and the engine router (diffusion_engine_router.py:337, :532) both gate on family_sd_cpp_supported. That only checks sd_cpp_vae and sd_cpp_text_encoders, and the qwen-image-edit and flux.1-kontext entries in diffusion_families.py set neither.
  • The pinned sd.cpp build (master-813-bfbef5b-u1d02858) runs all four models. They take the source image as a reference image (-r / ref_images): sd.cpp docs/qwen_image_edit.md, docs/kontext.md, docs/edit.md.
  • The native engine already had reference-image transport on both paths, but only for the unified Qwen-Image-2.1 edit. Edit-only families never got there: generate() refused any input image without an explicit workflow, _native_edit_ready required unified_edit, and status() advertised txt2img even for a model that cannot do text-to-image.

Change

Asset mapping (diffusion_families.py)

  • qwen-image-edit (one entry covers the original, 2509 and 2511):
    • Qwen-Image's own VAE and the Qwen2.5-VL-7B Q4_K_M encoder (--qwen2vl), the same files the qwen-image route already fetches.
    • The encoder's vision projector mmproj-F16.gguf from the same repo (--llm_vision). All three checkpoints read the source through the VLM, and a GGUF encoder has no vision weights of its own.
    • euler, flow shift 3: the documented sd.cpp invocation, the same as qwen-image.
    • 2511 needs no --model-args qwen_image_zero_cond_t=true. sd.cpp enables it from the __index_timestep_zero__ tensor the GGUF carries (qwen_image.hpp:37). Checked on the public Q4_K_M: qwen_image: num_layers = 60, zero_cond_t = true.
  • flux.1-kontext: the FLUX.1 VAE and the CLIP-L + T5-XXL pair, the same files as flux.1.

Guidance (_map_guidance)

  • Kontext: cfg 1.0 plus the embedded guidance, like FLUX.1-dev. The reference workflow runs cfg 1 / guidance 2.5, and Studio's default for Kontext is 2.5. The cfg is sent explicitly because sd.cpp's own default is 7.0.
  • Qwen edit: unchanged real CFG, the value the diffusers path passes as true_cfg_scale.

Edit-only workflow (sd_cpp_backend.py), matching the diffusers engine:

  • An input image with no workflow is an edit. The source goes out as the single --ref-image (one-shot) or ref_images entry (server).
  • The output takes the source size snapped to the 16 px grid (the diffusers _snap_to_multiple rounding), and the source is resized to it. A source larger than the family's bounds is scaled down to fit, not refused.
  • status() advertises only edit, as _family_workflows does for these families.
  • _native_edit_ready accepts an edit-only family when its projector is loaded (when it declares one). The Qwen-Image-2.1 unified gate is unchanged.
  • Text-to-image, the reference workflow, extra reference images and masks are refused with the diffusers engine's messages.

sd_cpp_args._TE_FLAGS_BY_FAMILY gains both families. No signature changes; other families' argv, request body and workflows are unchanged.

Results

B200, Studio's own SdCppDiffusionBackend in process, on the pinned sd.cpp build. main (f865421355) against this branch.

  • Inputs: the public unsloth Q4_K_M and BF16 GGUFs of each model. The same 768x768 portrait, prompt "turn the background into a sunny beach", seed 42, 20 steps, guidance 2.5. These are the loadability campaign's settings.
  • Mode: server mode, plus a one-shot run (sd-server hidden) for Kontext and 2511.
model main, BF16 / Q4_K_M this PR, BF16 / Q4_K_M sent (server body) LPIPS Q4_K_M vs BF16, vgg / alex LPIPS this PR BF16 vs campaign sd-cli BF16, vgg / alex
Qwen-Image-Edit refused / refused renders / renders txt_cfg 2.5, euler, shift 3, 1 ref 768x768 0.093 / 0.058 0.790 / 0.824
Qwen-Image-Edit-2509 refused / refused renders / renders same 0.203 / 0.103 0.404 / 0.305
Qwen-Image-Edit-2511 refused / refused renders / renders same 0.036 / 0.021 0.245 / 0.168
FLUX.1-Kontext-dev refused / refused renders / renders txt_cfg 1.0, distilled_guidance 2.5, 1 ref 768x768 0.099 / 0.055 0.545 / 0.521
  • On main, every arm fails in begin_load with the no native sd.cpp asset mapping error above. On this branch, status()["workflows"] is ["edit"] for all four, and every output is 768x768: the source size, as on the diffusers engine.
  • One-shot and server give byte-identical images (Kontext and 2511 Q4_K_M, same md5). The one-shot argv carries --ref-image, --width 768 --height 768, and for Kontext --cfg-scale 1 --guidance 2.5; for 2511 --cfg-scale 2.5 --sampling-method euler --flow-shift 3.0, --qwen2vl and --llm_vision.
  • Sanity column: the campaign ran sd-cli directly at 1024x1024 with Q8_0 encoders (Qwen2.5-VL Q8_0 + mmproj Q8_0, T5 Q8_0). Here the output is 768 px and the encoders are Q4_K_M + mmproj F16 (Qwen) and T5 fp16 (Kontext). So this is a different noise shape and composition, not a parity check; the images are scaled to 768 for scoring.
    • 2509, 2511 and Kontext keep the subject and framing and replace the background with a beach, like the sd-cli renders.
    • The original Qwen-Image-Edit at 768 px reframes to a half-body shot on the beach, while the 1024 sd-cli render keeps the close-up. That is the high LPIPS in its row.
  • Every edit is a coherent beach edit of the same person (contact sheet below). No image is black, white, constant or noise.
  • Render times (BF16 / Q4_K_M, server): Qwen edit 86 to 93 s / 89 to 143 s, Kontext 42 / 64 s. The GPUs were shared, so treat these as indicative only.

sd.cpp edit routes

Columns: input, the campaign's sd-cli direct BF16 render (1024 px), Studio's diffusers GGUF route BF16 render from the same campaign (768 px), this PR BF16, this PR Q4_K_M.

Tests

  • New test_sd_cpp_edit_families.py, 23 cases:
    • Asset mapping: the planned assets for each of the three Qwen edit checkpoints and for Kontext; the companion-only repo set still excludes black-forest-labs/FLUX.1-schnell; layered and inpaint variants are still refused.
    • Guidance: Kontext gives (1.0, g); Qwen edit gives real CFG.
    • Workflows and readiness: only edit is advertised; Qwen edit without its projector is not edit-ready.
    • One-shot argv: --ref-image, the source size, cfg / guidance, the encoders, the projector and the flow shift.
    • Server body: one data:image/png;base64 ref image at the source size, txt_cfg / distilled_guidance, sampler and shift, and no img2img keys.
    • Sizing and refusals: source sizing matches the diffusers _snap_to_multiple, and an oversized source is fitted. The diffusers engine's refusals hold, a text-to-image family still refuses an input image, and the Qwen-Image-2.1 unified gate is unchanged.
  • On main, 15 of the new cases fail. Here all pass.
  • The set ran on both arms: test_sd_cpp_*.py, test_h3_native_server.py, test_diffusion_backend.py, test_diffusion_engine_router.py, test_cached_gguf_routes.py, test_diffusion_more_families.py, test_diffusion_gated_base.py, test_diffusion_gguf_compile.py, test_diffusion_gguf_offload_route.py, test_native_gguf_companion.py and test_diffusion_unified_edit.py.
    • This branch: 2155 passed, 1 failed, 1 skipped.
    • The one failure, test_generate_guard_uses_the_hint_the_load_planned_with, fails the same way on main. It reads live free VRAM on a shared GPU (ImageActivationShortfallError).
  • Combined with the open FLUX cfg PR (studio/sdcpp-flux-cfg), the two merge without conflicts. That PR's two Kontext cases in test_flux_oneshot_argv_sends_explicit_cfg / test_flux_server_request_sends_txt_cfg call generate() without an input image, which Kontext now refuses as it does on diffusers. Whichever lands second gives the Kontext case a source image (a three-line test change); with it the merged tree passes test_sd_cpp_backend.py + test_sd_cpp_edit_families.py, 155 passed.

Not covered

  • One seed, one prompt and one input image per model. There is no noise floor, and the Q4_K_M vs BF16 LPIPS is a breakage check, not a quality ranking.
  • The edits ran on CUDA only. The CPU, Metal and Vulkan hosts this engine exists for were not run, and neither was a pre-sdcpp-API build. The 2511 auto-detection and Kontext support predate the sdcpp API every server-mode build needs.
  • The flow shift is 3.0 for all three Qwen edit checkpoints (sd.cpp's documented value), while the diffusers path uses ComfyUI's 3.1 for 2511 and the original. Not A/B'd.
  • The original Qwen-Image-Edit reframes at the 768 source size. Rendering at a 1 MP area instead was not tried; this PR keeps the diffusers engine's source sizing.
  • Qwen-Image-Layered has no Studio family at all, so it is not routed here.

_map_guidance returned no cfg for flux.1, flux.2-klein and flux.2-dev, so
the one-shot argv had no --cfg-scale and the server request had no
guidance.txt_cfg. sd.cpp then applied its own default cfg of 7.0, which
renders FLUX.1-dev and schnell dark or burnt and oversaturates FLUX.2.

Guidance-distilled FLUX (FLUX.1 dev / schnell / Kontext, FLUX.2-dev) now
runs one pass at cfg 1.0 with the embedded guidance. FLUX.2-klein has no
guidance embedder, so it takes the real-CFG mapping: the 4-step models
default to 1.0, the undistilled klein-base to its real CFG of 5.0.
…sd.cpp engine

The native engine refused every edit-only family at load ("has no native
sd.cpp asset mapping"), because qwen-image-edit and flux.1-kontext declared
no single-file VAE or text encoders, although the pinned sd.cpp build runs
all of them with the source image as a reference image.

- Asset mapping: Qwen-Image-Edit (original, 2509, 2511) takes Qwen-Image's
  VAE, the Qwen2.5-VL Q4_K_M encoder and its mmproj vision projector, euler,
  flow shift 3. 2511's zero-timestep conditioning is detected by sd.cpp
  from the __index_timestep_zero__ tensor the GGUF carries. Kontext takes
  the FLUX.1 VAE and CLIP-L + T5-XXL.
- Guidance: Kontext runs cfg 1.0 with the embedded guidance; the Qwen edit
  checkpoints keep real CFG, the value the diffusers path passes as
  true_cfg_scale.
- Edit-only workflow on both transports: an input image is an edit, the
  source is sent as the one --ref-image / ref_images entry, the output takes
  the source size snapped to the grid as on the diffusers engine, and
  status() advertises only "edit". Qwen edit needs its projector loaded.
- Same refusals as the diffusers engine for text-to-image, the reference
  workflow, reference images and masks on these families.
@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-06T04:53:32.793232Z 040190d 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.

@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-05T20:11:43.134426Z 86891ef 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.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 5, 2026
@danielhanchen danielhanchen self-assigned this Oct 6, 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: c75709f8e6

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

"Qwen2.5-VL-7B-Instruct-Q4_K_M.gguf",
"qwen2vl",
),
("unsloth/Qwen2.5-VL-7B-Instruct-GGUF", "mmproj-F16.gguf", "llm_vision"),

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 Require the projector when loading edit-only models offline

When a non-user-initiated/cache-only load has the Qwen edit GGUF, VAE, and text encoder cached but lacks mmproj-F16.gguf, the existing _fetch_assets logic silently skips every missing llm_vision asset because that was optional for Qwen-Image-2.1 text-to-image. For this newly mapped family the projector is mandatory and status() consequently exposes no workflows, so the load reports success but every generation is unusable. Make this projector optional only for families that retain a non-edit workflow, or fail this edit-only load with the existing actionable cache error.

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 243d9aa: a cache-only load of an edit-only family now fails when its projector is missing, instead of loading with no workflows. Qwen-Image-2.1 keeps skipping it.

# An edit-only family (Kontext, Qwen-Image-Edit) has no text-to-image mode: an input image
# with no explicit workflow is an edit, as on the diffusers engine. Re-checked against the
# state this call actually runs on below.
loaded = self._state

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 Infer the implicit edit workflow from the locked state

When a generation waits behind another generation while a model load commits, this unlocked read can inspect the old family while the later code runs against the replacement state. Local/unmanaged callers do not supply expected_load, so a request with an input image can either be rejected as unsupported after a text-to-image model is replaced by an edit model, or retain an inferred edit workflow after the opposite replacement. Derive workflow and conditioned after acquiring the generation/state locks, from the same state used for the run.

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: both swap directions end in a refusal (the init-image refusal for a txt2img model, or 'Image editing is not available' via _native_edit_ready under the lock), never a wrong render, and the Studio route passes expected_load for exactly this window (#9448).

Comment on lines +1863 to +1864
width = max(multiple, int(round(sw * fit / multiple)) * multiple)
height = max(multiple, int(round(sh * fit / multiple)) * multiple)

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 Preserve support for source images below 256 pixels

For an edit source whose snapped width or height is below 256 (for example the 64×64 input already accepted by the diffusers Qwen-Image-Edit path), these calculations leave that side below 256 and the subsequent check_output_size rejects the request. Since edit-only models ignore the requested width and height, callers cannot select a valid output size as a workaround, making small icons and narrow images work on diffusers but fail on the newly routed native engine. Source-sized edits should either follow the diffusers path without applying the text-to-image minimum or resize to a valid minimum.

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 243d9aa: a source-sized edit now scales a source below 256 px up to the minimum side (aspect kept), so it renders like on the diffusers engine instead of being refused.

Comment on lines +4392 to +4394
if getattr(state.family, "edit", False):
# Edit-only families have no text-to-image mode, so only "edit", as on the diffusers engine.
workflows = ["edit"] if self._native_edit_ready(state) else []

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 alpha disabled for RGB edit-only families

On a newer/custom sd.cpp binary carrying _REFERENCE_FIDELITY_MARKER, this new edit-only status path still reaches the unconditional conditioning["alpha"] = full_fidelity override below, so Kontext and Qwen-Image-Edit advertise alpha support even though both families decode their source in RGB and return RGB output. A client relying on the capability can therefore submit transparency expecting it to be preserved while it is flattened; retain conditioning_capabilities()'s family-derived False value for edit-only RGB families and apply the build-level override only to RGBA-capable unified editing.

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: the pinned build does not carry the fidelity marker, so alpha is False for these families on every shipped install; the advertised flag only moves on a custom newer binary, and the output stays RGB either way.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 243d9aa5ff

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

Base automatically changed from studio/sdcpp-flux-cfg to main October 6, 2026 04:52
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