Skip to content

Fix MagVIT tiled encoding dropping the last tile row and column - #15454

Open
newcloud123 wants to merge 1 commit into
huggingface:mainfrom
newcloud123:fix-magvit-tiled-encode-crop
Open

newcloud123 wants to merge 1 commit into
huggingface:mainfrom
newcloud123:fix-magvit-tiled-encode-crop

Conversation

@newcloud123

Copy link
Copy Markdown

The bug

AutoencoderKLMagvit.tiled_encode crops every tile to the full-image latent size instead of the tile stride:

result_row.append(tile[:, :, :, :latent_height, :latent_width])

latent_height/latent_width are height // spatial_compression_ratio and width // spatial_compression_ratio of the whole input, i.e. the size of the final latent, not the size of one tile's contribution. Once the input is bigger than one tile, the crop is a no-op, the overlapping borders survive, and torch.cat(..., dim=4) places tile k at min_height * k instead of stride * k. The final crop then keeps the first latent_height entries, so the last tile row/column is silently discarded.

The neighbouring tiled_decode in the same file already crops to self.tile_sample_stride_height/width, and AutoencoderKLLTXVideo.tiled_encode crops to tile_latent_stride_*. Only the MagVIT encode path uses the full latent size.

Reproduction

Tiny MagVIT VAE (spatial compression 2, so a 16x16 input becomes an 8x8 latent), 2x2 tiles with half-tile overlap:

import torch
from diffusers import AutoencoderKLMagvit

model = AutoencoderKLMagvit(
    in_channels=3, out_channels=3, latent_channels=4,
    down_block_types=["SpatialDownBlock3D", "SpatialDownBlock3D"],
    up_block_types=["SpatialUpBlock3D", "SpatialUpBlock3D"],
    block_out_channels=[4, 8], layers_per_block=1, norm_num_groups=2,
).eval()
model.enable_tiling(
    tile_sample_min_height=8, tile_sample_min_width=8,
    tile_sample_stride_height=4, tile_sample_stride_width=4,
)

x = torch.randn(1, 3, 4, 16, 16)
perturbed = x.clone()
perturbed[:, :, :, 12:, 12:] += 3.0   # only the bottom-right tile changes

with torch.no_grad():
    a = model.encode(x).latent_dist.mode()
    b = model.encode(perturbed).latent_dist.mode()
print((a - b).abs().max())
max latent delta
main 0.000000 (bottom-right tile never reaches the latent)
this PR 0.212866

Tiled-vs-non-tiled error on a 16x16 input with tile_min=12, stride=8 also drops from 0.0698 mean abs to 0.0380.

The fix

Crop each tile to its stride, so the concatenation advances by stride and every tile lands where it belongs:

result_row.append(tile[:, :, :, :tile_latent_stride_height, :tile_latent_stride_width])

The final [:latent_height, :latent_width] crop stays, as it removes the tail padding.

Test

test_tiled_encode_keeps_the_last_tile in tests/models/autoencoders/test_models_autoencoder_magvit.py builds a tiny model, enables 2x2 tiling, and asserts that perturbing only the bottom-right tile changes the latent. It fails on main (max delta == 0) and passes with the fix.

Note: _encode/_decode also compare x.shape[-1] against tile_sample_min_height (height and width swapped) when deciding whether tiling is needed. That only affects when tiling triggers for non-square tiles, not correctness, so I left it out to keep this PR focused — happy to send it separately.

@github-actions github-actions Bot added models tests size/S PR with diff < 50 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

Re the pr-link-issue-reminder: this is a self-found bug — there is no existing issue for it, so I didn't invent a Fixes #N link. The PR body contains a minimal reproduction and before/after numbers (bottom-right tile sensitivity 0.000000 -> 0.212866; tiled-vs-nontiled MAE 0.0698 -> 0.0380), plus a regression test.

If a maintainer could add the no-issue-needed label, that should silence the reminder. Happy to open an issue and link it instead if that's the preferred workflow — just let me know. 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/S PR with diff < 50 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant