Skip to content

[Z-Image] Resize the init image to height/width in img2img and inpaint - #15452

Open
menglcai-amd wants to merge 1 commit into
huggingface:mainfrom
menglcai-amd:zimage-img2img-height-width
Open

menglcai-amd wants to merge 1 commit into
huggingface:mainfrom
menglcai-amd:zimage-img2img-height-width

Conversation

@menglcai-amd

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15337

This PR has two changes:

Fix 1. Resize the init image to height/width in ZImageImg2ImgPipeline and ZImageInpaintPipeline.

The noise is created at height/width, but the init image was preprocessed without them, so it kept its original size. If the two sizes differ, they can't be combined and scheduler.scale_noise raises a size-mismatch RuntimeError. Passing height/width to image_processor.preprocess fixes this, the same way SD3, Flux and QwenImage do it. The inpaint mask was already resized this way; now the image matches it.

Fix 2. Keep 16-channel latents passed as image working in img2img after Fix 1.

img2img also accepts an already-encoded latent as image. The image processor only skips resizing when it recognizes a latent by its channel count, and it was using the default of 4 channels, while Z-Image latents have 16. Before Fix 1 this didn't matter, since nothing was resized. After Fix 1, a latent would be stretched to pixel size and the pipeline would fail. Giving the processor the VAE's latent channel count, as SD3 and Flux img2img do, lets latents pass through untouched. As a side effect, a 16-channel tensor whose values are all non-negative is no longer normalized as if it were an image.

New tests:

  • test_height_width_differ_from_image (img2img and inpaint): covers Fix 1. Fails on main with the error from the issue, passes with this PR.
  • test_latent_image_with_height_width (img2img): covers Fix 2. Passes on main, fails with Fix 1 alone, passes with this PR.

Testing

Real model: Tongyi-MAI/Z-Image-Turbo, bf16, 8 steps, strength 0.5, on an AMD MI250 (ROCm). 512x512 input image.

Case main This PR
img2img, height/width = 768x512 RuntimeError 768x512
img2img, height/width = 256x384 RuntimeError 256x384
inpaint, height/width = 768x512 RuntimeError 768x512
inpaint, height/width = 256x384 RuntimeError 256x384
img2img, 16-channel latent as image + height/width = 512x512 OK OK, identical to main
img2img / inpaint without height/width; img2img with height/width equal to the image; 2 images + 2 prompts OK OK, same as main (*)

(*) 3 of these outputs are bit-identical to main. The other 2 differ by at most 0.0078; running the same code twice on this GPU differs by up to 0.0176, so this is run-to-run variation, not the change.

Unit tests: tests/pipelines/z_image/

CPU MI250
main 64 passed, 91 skipped -
main + new tests new tests only: 2 test_height_width_differ_from_image failed, latent test passed 2 failed (test_height_width_differ_from_image), 113 passed, 43 skipped
This PR 67 passed, 91 skipped 115 passed, 43 skipped
More input cases (tiny test components, CPU)

Compared main, Fix 1 alone, and this PR, using the components from the Z-Image tests (32x32 input).

Case main Fix 1 alone This PR
tensor / PIL / numpy input, height/width != image (img2img and inpaint) RuntimeError OK OK
2 images of different sizes + 2 prompts, height/width set RuntimeError OK OK
inpaint, mask size != image size RuntimeError OK OK
inpaint, user latents + height/width RuntimeError OK OK
img2img, user latents + height/width OK OK OK
img2img, 16-channel latent as image + height/width OK RuntimeError OK
no height/width OK OK OK
height/width not a multiple of the required size ValueError ValueError ValueError
latent as image without height/width; latent as image in inpaint fails fails fails (unchanged, not in scope)

Self-review notes

Final round on the full diff. Verdict: READY, no blocking issues, no dead code. ruff check, ruff format --check and utils/check_copies.py pass. Test results are in the Testing section above.

Left for the reviewer:

  1. latent_channels is a local in __init__ (as in SD3 img2img), not self.latent_channels (as in Flux / QwenImage img2img). Happy to switch if you prefer the attribute.
  2. The height/width docstrings say "defaults to 1024", but the input image size is used when they are not passed, and the image docstring doesn't mention that a latent can be passed. Both predate this PR; happy to fix them here if you prefer.
  3. Fix 2 is only in img2img: inpaint always VAE-encodes the init image, so it doesn't accept a latent image anyway.

Possible addition to .ai/references/pipelines.md: an img2img pipeline that passes height/width to image_processor.preprocess and accepts latents as image should build its VaeImageProcessor with vae_latent_channels, otherwise those latents get resized like images.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Who can review?

@yiyixuxu @sayakpaul

Signed-off-by: menglcai <menglcai@amd.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZImageImg2ImgPipeline / ZImageInpaintPipeline: explicit height/width different from the input image raises RuntimeError

1 participant