Skip to content

fix: resize Qwen Image Edit vision references - #2433

Merged
leejet merged 1 commit into
masterfrom
fix/qwen-edit-vision-resize
Nov 26, 2026
Merged

leejet merged 1 commit into
masterfrom
fix/qwen-edit-vision-resize

Conversation

@leejet

@leejet leejet commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Resize each Qwen Image Edit reference image to a target area of 384 × 384 pixels, then align it to the vision encoder’s patch grid.

Related Issue / Discussion

Fix #2427.

Additional Information

N/A

Checklist

Summary by CodeRabbit

  • Improvements

    • Reference images for Qwen Image are now resized toward a 384 × 384 pixel target while preserving their aspect ratio and aligning dimensions for processing.
    • Reference-image latent resizing continues to follow the output size.
  • Documentation

    • Added notes describing reference-image sizing and related limitations.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Qwen Image reference-image preprocessing now targets a 384 × 384 area for the vision encoder while preserving aspect ratio up to patch alignment. The model documentation also describes the separate output-size-based resize for VAE reference latents.

Changes: Qwen Image reference resizing

Layer / File(s) Summary
Vision reference preprocessing
src/conditioning/conditioner.hpp, docs/models/qwen_image_edit.md
Qwen Image reference dimensions now target a 384 × 384 area, rounded to the vision factor and clamped to at least one factor. Other versions keep the existing factor-rounded dimensions. The documentation describes this vision resize and the separate output-size-based VAE resize.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: stduhpf, wbruna

Merge Risk: Minimal · up to 9f458

Qwen Image Edit references sent to the vision encoder are now resized toward a 384×384 area, as the linked issue requests, and the docs describe this. The only known side effect is minor cropping for extremely elongated references. It is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix: resize Qwen Image Edit vision references" names the main change: reference images for the Qwen Image Edit vision encoder are now resized. The raw summary confirms the conditioner.hpp c…
Description check Passed The description includes the Summary, Related Issue / Discussion (Fix #2427), and Additional Information sections from the template, plus both checklist items marked as checked. The Summary states the…
Linked Issues check Passed Issue #2427 requires that each Qwen Image Edit Plus reference sent to the vision encoder is resized to about 384 x 384 area, keeps aspect ratio, and aligns to the patch grid, independently of the VAE …
Out of Scope Changes check Passed The PR changes two areas. First, src/conditioning/conditioner.hpp adds the 384x384 area resize for the Qwen Image path. This directly implements #2427. Second, docs/models/qwen_image_edit.md adds a No…
Full details: Title check

Explanation

The title "fix: resize Qwen Image Edit vision references" names the main change: reference images for the Qwen Image Edit vision encoder are now resized. The raw summary confirms the conditioner.hpp change for VERSION_QWEN_IMAGE scales reference images toward 384x384 and rounds dimensions to the vision factor. The title is specific and concise, and it does not describe anything absent from the changeset.

Full details: Description check

Explanation

The description includes the Summary, Related Issue / Discussion (Fix #2427), and Additional Information sections from the template, plus both checklist items marked as checked. The Summary states the change: resize each Qwen Image Edit reference image to a 384 x 384 target area, then align it to the vision encoder patch grid. The Additional Information section says only N/A, and it gives no verification notes or sample output. The description also does not mention the VAE reference handling or the docs update, though the raw summary says a Notes and limitations section was added to docs/models/qwen_image_edit.md. These gaps are minor, so the description is mostly complete.

Full details: Linked Issues check

Explanation

Issue #2427 requires that each Qwen Image Edit Plus reference sent to the vision encoder is resized to about 384 x 384 area, keeps aspect ratio, and aligns to the patch grid, independently of the VAE reference. The PR summary states that for VERSION_QWEN_IMAGE in src/conditioning/conditioner.hpp, reference preprocessing now scales toward a 384x384 target area and rounds each dimension to a multiple of the vision factor, with a minimum of one factor. This matches the requested behavior. The docs note in docs/models/qwen_image_edit.md states that the VAE reference uses a separate resize based on output size, which keeps the two paths independent. Limits of this assessment: the changed-blob diff could not be read, so the code behavior rests on the AI-generated summary and not on source lines. The summary also does not state whether VERSION_QWEN_IMAGE covers only Plus models or also Qwen Image Edit 2.1; the issue asks for Plus models, and the summary says other versions in the branch keep their prior rounding. No coding test is required by the issue, and none is needed to assess the requirement.

Full details: Out of Scope Changes check

Explanation

The PR changes two areas. First, src/conditioning/conditioner.hpp adds the 384x384 area resize for the Qwen Image path. This directly implements #2427. Second, docs/models/qwen_image_edit.md adds a Notes and limitations section that describes the vision and VAE resize behavior. This documents the change and is in scope. The summary reports no changes to public declarations. No unrelated change is identified. Limit: the file-level diff could not be read, so the absence of other hunks rests on the summary and the listed files.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9f6ca4e7-3b91-4243-99f3-c377ec1d77b0
Commits

Reviewing files that changed from the base of the PR and between 52491bd and 9f45824.

Files selected for processing: 2

  • docs/models/qwen_image_edit.md
  • src/conditioning/conditioner.hpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@leejet
leejet merged commit 456f52d into master Nov 26, 2026
11 checks passed
@leejet
leejet deleted the fix/qwen-edit-vision-resize branch November 26, 2026 16:10
danielhanchen added a commit to unslothai/unsloth that referenced this pull request Nov 27, 2026
)

## Summary

Native (sd.cpp) Qwen-Image-Edit now reads each reference image through
the Qwen2.5-VL vision encoder at a 384x384 pixel area, the size
`QwenImageEditPlusPipeline` uses (`CONDITION_IMAGE_SIZE = 384 * 384`)
and the size upstream sd.cpp now hard-codes
(leejet/stable-diffusion.cpp#2433).

The pinned build clamps each reference to 384..560 px per side instead,
so a 1024x1024 source is encoded at 560x560 (400 vision tokens). With
this change it is encoded at 392x392 (196 tokens), the same grid the
diffusers pipeline produces after Qwen2-VL's 28 px snap.

## Change

- `sd_cpp_args.py`: `ref_image_args_for_family()` returns
`vlm_size=147456` for the `qwen-image-edit` family when reference images
are present. `build_img_gen_request()` forwards it as `ref_image_args`
next to `ref_images`.
- `sd_cpp_backend.py`: both transports send it, `ref_image_args` in the
sd-server request and `--ref-image-args` for sd-cli.
- Kill switch: `UNSLOTH_SD_CPP_QWEN_EDIT_REF_AREA=0` keeps the binary's
own sizing.

The pinned build reads a user `vlm_size` as a pixel area, and min == max
takes its exact-area branch, which is the same arithmetic as the
upstream fix. Upstream builds that already hard-code 384x384 produce the
same size. Builds without `vlm_size` warn and ignore the key.
Text-to-image runs, other families and edits without references send
nothing.

## CPU timing

Setup: sd-cli built from the pinned tag `master-982-0518b34-u9455ee1`,
CPU only (8 vCPU AMD EPYC 7B12).
- Model: Qwen-Image-Edit-2511 Q8_0 GGUF, Qwen2.5-VL-7B Q4_K_M with
mmproj F16, Qwen-Image VAE.
- Input: 1024x1024 reference. Output: 256x256, 4 euler steps, cfg 4,
seed 42.
- Runs: 3 prompts, arms interleaved (pinned, this change, pinned again).
Both arms use the same binary and differ only by `--ref-image-args`.
- Engagement, from the verbose log of every run: pinned `1024x1024 ->
560x560`, this change `1024x1024 -> 392x392`.

| median of runs | pinned (6 runs) | this change (3 runs) | delta |
|---|---|---|---|
| text + vision encode | 92.5 s | 63.2 s | -31.6% |
| sampling | 841.2 s | 706.8 s | -16.0% |
| generate total | 940.3 s | 779.1 s | -17.1% |

Sampling also gets faster because the image tokens are part of the DiT's
text stream. The pinned-vs-pinned repeat varied by 1 to 9% on total
time; every run with this change was faster than every pinned run.

## Output quality

Setup:
- Build: the pinned prebuilt `master-982-0518b34-u9455ee1` (Linux CUDA
13) on one NVIDIA L4, `--diffusion-fa`, everything resident on the GPU.
- Model: Qwen-Image-Edit-2511 Q4_K_M GGUF. Q8_0 does not fit an L4, and
the ref-size argument is the only difference between arms, so the quant
is shared.
- Run: 1024x1024 reference, 20 euler steps, cfg 4, flow shift 3.0.
Outputs at 512x512 and 1024x1024, 3 edit prompts x 2 seeds.
- Arms: pinned, this change, pinned again.
- Engagement, confirmed in every run log: pinned `1024x1024 -> 560x560`,
this change `1024x1024 -> 392x392`.

| output | LPIPS this change vs pinned, median [min, max] | PSNR this
change vs pinned, median | pinned vs pinned repeat |
|---|---|---|---|
| 512x512 (6 cells) | 0.112 [0.007, 0.636] | 25.7 dB | bit-identical
(LPIPS 0) |
| 1024x1024 (6 cells) | 0.267 [0.017, 0.444] | 17.4 dB | bit-identical
(LPIPS 0) |

The build is deterministic, so every difference comes from the reference
size. The VLM conditioning changes, so the renders change too.

Side-by-side inspection of all 12 pairs:
- Every render in both arms carries out the instruction (watercolor,
forest background, red suit).
- Both arms keep the subject's identity and pose.
- The large-LPIPS cells are watercolor renders where the two arms pick a
different background wash, or a red-suit render where one arm also
colors the egg. Each arm does this in some cells.
- Neither arm has a systematic loss of detail or instruction following.

Timing on the L4, median:

| output | encode | sampling | total |
|---|---|---|---|
| 512x512 | -26.9% | -6.2% | -6.9% |
| 1024x1024 | -27.3% | -1.8% | -2.1% |

## Tests

- New: `studio/backend/tests/test_sd_cpp_qwen_edit_ref_area.py`, 10
tests. 3 of them fail with the backend change reverted.
- Existing: `test_sd_cpp_args.py`, `test_sd_cpp_backend.py`,
`test_sd_cpp_qwen21_edit.py`, `test_sd_cpp_qwen_layered.py` and
`test_sd_cpp_moss_uncond_skip.py` pass, 387 tests in total.
- A wider diffusion set has the same pre-existing failures with and
without this change.
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] Qwen Image Edit 2509/2511: reference images reach the vision encoder at full size; edits turn into a drawing above ~0.6 MP of references

1 participant