Skip to content

Fix HunyuanImage refiner VAE tiling mixing latent and sample space - #15455

Open
newcloud123 wants to merge 1 commit into
huggingface:mainfrom
newcloud123:fix-hunyuan-image-refiner-tiling
Open

newcloud123 wants to merge 1 commit into
huggingface:mainfrom
newcloud123:fix-hunyuan-image-refiner-tiling

Conversation

@newcloud123

Copy link
Copy Markdown

The bugs

AutoencoderKLHunyuanImageRefiner mixes latent-space and sample-space quantities in both tiling paths.

tiled_encode cuts its tiles out of the sample tensor but advances with a latent-space step:

overlap_height = int(tile_latent_min_height * (1 - self.tile_overlap_factor))  # 256 // 16 * 0.75 == 12
...
for i in range(0, height, overlap_height):   # height is a sample-space dimension
    tile = x[..., i : i + self.tile_sample_min_height, ...]

With the default tile of 256 samples and a stride of 192 samples, the loop advances 12 samples instead of 192. That encodes ~16x more tiles than needed and concatenates them into a latent far larger than the input, so encode returns the wrong shape.

tiled_decode does the opposite: it walks the latent tensor correctly, but blends and crops the decoded tiles with latent-space numbers:

blend_height = int(tile_latent_min_height * self.tile_overlap_factor)  # 16 * 0.25 == 4
row_limit_height = tile_latent_min_height - blend_height              # 12
...
decoded = self.decoder(tile)                                          # 256 samples tall
result_row.append(tile[:, :, :, :row_limit_height, :row_limit_width]) # keeps 12 of 256

Each decoded tile keeps 12 pixels and drops the other 244, so decode returns a fraction of the image.

Both paths also ignore tile_sample_stride_height/tile_sample_stride_width, which enable_tiling stores and which every other tiled VAE (AutoencoderKLHunyuanVideo15, AutoencoderKLWan, ...) uses.

Reproduction

Tiny model, spatial compression 2, 32x32 input (16x16 latent), tiles of 16 samples with a stride of 12:

import torch
from diffusers import AutoencoderKLHunyuanImageRefiner

model = AutoencoderKLHunyuanImageRefiner(
    in_channels=3, out_channels=3, latent_channels=4,
    block_out_channels=(4, 8), layers_per_block=1,
    spatial_compression_ratio=2, temporal_compression_ratio=1,
).eval()
x = torch.randn(1, 3, 1, 32, 32)

with torch.no_grad():
    z = model.encode(x).latent_dist.mode()
    expected_image = model.decode(z).sample
    model.enable_tiling(
        tile_sample_min_height=16, tile_sample_min_width=16,
        tile_sample_stride_height=12, tile_sample_stride_width=12, tile_overlap_factor=0.25,
    )
    print(model.encode(x).latent_dist.mode().shape, model.decode(z).sample.shape)
encoded latent decoded image
non-tiled (1, 4, 1, 16, 16) (1, 3, 1, 32, 32)
tiled on main (1, 4, 1, 29, 29) (1, 3, 1, 18, 18)
tiled with this PR (1, 4, 1, 16, 16) (1, 3, 1, 32, 32)

The fix

  • tiled_encode: advance by self.tile_sample_stride_height/width (sample space, matching the cut), and derive the latent blend/crop from tile_latent_min_* minus the latent stride.
  • tiled_decode: keep stepping by the latent stride, but blend by tile_sample_min_* - tile_sample_stride_* and crop to tile_sample_stride_*, because the blend happens on decoded tiles.

The defaults are unchanged: with tile_sample_min = 256, tile_sample_stride = 192 and tile_overlap_factor = 0.25, the latent blend is 16 - 12 = 4 and the sample-space blend is 256 - 192 = 64, i.e. exactly what the old int(size * factor) produced in the right space.

Test

New tests/models/autoencoders/test_models_autoencoder_hunyuanimage_refiner.py with two shape assertions (tiled encode/decode vs the non-tiled paths). Both fail on main (29x29 != 16x16 and 18x18 != 32x32) and pass with the fix.

@github-actions github-actions Bot added models tests size/M PR with diff < 200 LOC labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Hi @newcloud123, thanks for the PR! It does not appear to link an issue it fixes.

  • If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked.
  • If you are the author of the model/pipeline this PR relates to, or part of the team we work with on it, tick the Are you the author (or part of the team we work with) of the model/pipeline this PR relates to? box in the PR description (if you removed the template, paste that line back, checked). This reminder then no longer applies.
  • Otherwise there is nothing you need to do: a maintainer will look at the PR and add the no-issue-needed label to silence this reminder if it should stay open without an issue.

See the contribution guide for more details.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an existing issue, the author box is ticked, or the no-issue-needed label is added, you can ignore this message — it stays here as a comment, but it no longer applies.

@newcloud123

Copy link
Copy Markdown
Author

Same situation as #15454 re the pr-link-issue-reminder: this is a self-found bug with no pre-existing issue. The PR body has the minimal reproduction and a shape table (encoded latent 16x16 vs 29x29, decoded image 32x32 vs 18x18 before the fix), plus regression tests.

Could a maintainer add no-issue-needed, or should I open an issue and add a closing keyword? Happy to do whichever fits the workflow. Thanks!

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

models size/M PR with diff < 200 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant