Skip to content

Commit f67717a

Browse files
committed
Tiled VAE: fix bug with pathologic size (tile size - overlap + 1)
This fixes the error: > The size of tensor a (128) must match the size of tensor b (0) > at non-singleton dimension 0
1 parent e708c31 commit f67717a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/refiners/foundationals/latent_diffusion/auto_encoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ def _generate_latent_tiles(size: _ImageSize, tile_size: _ImageSize, overlap: int
415415
"""
416416
tiles: list[_Tile] = []
417417

418-
for x in range(0, size.width, tile_size.width - overlap):
419-
for y in range(0, size.height, tile_size.height - overlap):
418+
for x in range(0, max(size.width - overlap, 1), tile_size.width - overlap):
419+
for y in range(0, max(size.height - overlap, 1), tile_size.height - overlap):
420420
tile = _Tile(
421421
top=max(0, y),
422422
left=max(0, x),

tests/foundationals/latent_diffusion/test_autoencoders.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ def test_tiled_autoencoder_rectangular_image(autoencoder: LatentDiffusionAutoenc
107107
ensure_similar_images(sample_image, result, min_psnr=37, min_ssim=0.985)
108108

109109

110+
@no_grad()
111+
@pytest.mark.parametrize("tile_w", [240, 242, 244, 254, 256, 258])
112+
def test_tiled_autoencoder_pathologic_sizes(
113+
refiners_sd15_autoencoder: SD1Autoencoder,
114+
sample_image: Image.Image,
115+
test_device: torch.device,
116+
tile_w: int,
117+
):
118+
# 242 is the true pathologic case, a tile just larger than (tile size - overlap).
119+
# 242 * 4 = 968 = (128 - 8 + 1) * 8
120+
121+
autoencoder = refiners_sd15_autoencoder.to(device=test_device, dtype=torch.float32)
122+
123+
sample_image = sample_image.crop((0, 0, tile_w, 400))
124+
sample_image = sample_image.resize((sample_image.width * 4, sample_image.height * 4))
125+
126+
with autoencoder.tiled_inference(sample_image, tile_size=(1024, 1024)):
127+
encoded = autoencoder.tiled_image_to_latents(sample_image)
128+
result = autoencoder.tiled_latents_to_image(encoded)
129+
130+
ensure_similar_images(sample_image, result, min_psnr=37, min_ssim=0.985)
131+
132+
110133
def test_value_error_tile_encode_no_context(autoencoder: LatentDiffusionAutoencoder, sample_image: Image.Image) -> None:
111134
with pytest.raises(ValueError):
112135
autoencoder.tiled_image_to_latents(sample_image)

0 commit comments

Comments
 (0)