Skip to content

fix(qwen-image): implement tiled encode and decode in QwenImageVAE - #1720

Merged
Artiprocher merged 1 commit into
modelscope:mainfrom
fusheng-ji:fix/qwen-image-vae-tiled
Oct 13, 2026
Merged

Artiprocher merged 1 commit into
modelscope:mainfrom
fusheng-ji:fix/qwen-image-vae-tiled

Conversation

@fusheng-ji

@fusheng-ji fusheng-ji commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1069

QwenImagePipeline passes tiled, tile_size and tile_stride to the VAE (e.g. qwen_image.py#L194, #L282), but QwenImageVAE.encode / decode accept them through **kwargs and ignore them. So tiled=True has no effect, and high-resolution encoding and decoding always use the full amount of memory.

This PR implements spatial tiling the same way WanVideoVAE does:

  • tile_size and tile_stride are measured in latent pixels (the pipeline defaults 128 / 64 mean 1024 / 512 image pixels).
  • Overlapping tiles are blended with linear masks.
  • The last tile in each direction is clipped to the image border, so any size works, including images smaller than one tile.
  • The number of output channels is taken from the first tile, so the RGBA VAE of Qwen-Image-Layered (image_channels=4) works too.

The default path (tiled=False) is unchanged.

Checking

Setup: real Qwen/Qwen-Image VAE weights, bf16, 1× B200, 2048×2048 input:

peak memory difference from non-tiled
encode, non-tiled 7.77 GiB –
encode, tiled=True (128 / 64) 2.15 GiB latent relative diff 6.2e-3
decode, non-tiled 8.53 GiB –
decode, tiled=True (128 / 64) 2.43 GiB PSNR 60.3 dB
decode, tiled=True (64 / 32) 0.92 GiB PSNR 58.1 dB

Before this PR, tiled=True peaked at 8.57 GiB for encode + decode, the same as non-tiled.

Other shapes:

  • 520×776 and 96×200 inputs (tiles clipped at the border, or smaller than one tile) give correct shapes and finite outputs.
  • A random-init QwenImageVAE(image_channels=4) with batch 2 also works.
  • With tile_stride == tile_size (no overlap, e.g. 64 / 64, 32 / 32, or 64×48 tiles), tiled encode and decode match the non-tiled result exactly; a tile_stride outside (0, tile_size] raises a ValueError. (Checked with stand-in encode / decode functions.) WanVideoVAE.build_1d_mask has the same zero-overlap problem on main; I left it unchanged here.

End-to-end with QwenImagePipeline. Setup: Qwen/Qwen-Image weights, bf16, 2048×2048, 30 steps, torch 2.11.0+cu130, 1× B200. I ran text-to-image (decode) and then image-to-image from that image with denoising_strength=0.6 (encode + decode), each with tiled=False and tiled=True (pipeline defaults 128 / 64). Peak memory is measured above the memory already in use when the VAE call starts:

step main, tiled=False main, tiled=True this PR, tiled=False this PR, tiled=True
VAE decode peak 8.26 GiB 8.26 GiB 8.26 GiB 2.11 GiB
VAE encode peak (i2i) 7.51 GiB 7.51 GiB 7.51 GiB 1.88 GiB
output vs tiled=False – bit-identical (tiling ignored) – PSNR 54.9 dB (t2i), 50.2 dB (i2i)
  • With tiled=False, outputs are bit-identical before and after this PR.
  • I visually checked the tiled outputs, including 512×512 crops around the image center where the tiles overlap, and saw no seams. The slightly lower i2i PSNR comes from the tiled encode changing the input latents a little, which then goes through 30 denoising steps.
Qwen-Image 2048x2048 text-to-image and image-to-image: tiled vs non-tiled VAE outputs and center crops, no visible seams Figure. Qwen/Qwen-Image at 2048×2048, 30 steps, with this PR. Top: full images (downscaled). Bottom: 512×512 crops at 1:1 from the image center, where tiles overlap. Tiled outputs match the non-tiled ones (PSNR 54.9 dB t2i, 50.2 dB i2i) with no visible seams, while VAE peak memory drops about 4×. On main, tiled=True is ignored: its output is bit-identical to non-tiled and uses the same memory.

Tiled decoding is a bit slower (0.52 s vs 0.36 s at 2048²).

QwenImagePipeline passes `tiled`, `tile_size` and `tile_stride` to
QwenImageVAE.encode / decode, but both methods accepted them through
**kwargs and ignored them, so `tiled=True` had no effect and high
resolution encoding / decoding used the full amount of memory.

Implement spatial tiling in the same way as WanVideoVAE: `tile_size` and
`tile_stride` are measured in latent pixels, overlapping tiles are blended
with linear masks, and the last tile in each direction is clipped to the
image border. Tiles without overlap (`tile_stride == tile_size`) are not
blended, and a `tile_stride` that is not in `(0, tile_size]` raises a
ValueError. The number of output channels is taken from the first tile,
so the RGBA VAE of Qwen-Image-Layered works as well. The default path
(`tiled=False`) is unchanged.

Fixes modelscope#1069
@fusheng-ji
fusheng-ji force-pushed the fix/qwen-image-vae-tiled branch from e04597c to 12bf35f Compare October 2, 2026 20:26
@Artiprocher

Copy link
Copy Markdown
Collaborator

@fusheng-ji Thank you for your suggestion. We will go ahead and merge this PR.

@Artiprocher
Artiprocher merged commit 39da7b9 into modelscope:main Oct 13, 2026
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.

QwenImageVAE tiled decode未实现?

2 participants