Repository navigation
fix: resize Qwen Image Edit vision references - #2433
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
) ## 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.
Summary
384 × 384pixels, then align it to the vision encoder’s patch grid.Related Issue / Discussion
Fix #2427.
Additional Information
N/A
Checklist
Summary by CodeRabbit
Improvements
Documentation