Skip to content

Commit 9af6a4d

Browse files
committed
fix(wan_i2v): repeat conditioning images along batch dimension in prepare_latents
1 parent 56a46f9 commit 9af6a4d

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/maxdiffusion/models/wan/autoencoder_kl_wan.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,19 @@ def __call__(self, x: jax.Array) -> jax.Array:
201201
n, h, w, c = in_shape
202202
target_h = int(h * self.scale_factor[0])
203203
target_w = int(w * self.scale_factor[1])
204-
if self.method == "nearest" and self.scale_factor[0] == int(self.scale_factor[0]) and self.scale_factor[1] == int(self.scale_factor[1]):
204+
if (
205+
self.method == "nearest"
206+
and self.scale_factor[0] == int(self.scale_factor[0])
207+
and self.scale_factor[1] == int(self.scale_factor[1])
208+
):
205209
scale_h = int(self.scale_factor[0])
206210
scale_w = int(self.scale_factor[1])
207211
out = jnp.repeat(jnp.repeat(x, scale_h, axis=1), scale_w, axis=2)
208212
else:
209213
if self.method == "nearest":
210-
max_logging.log(f"Warning: WanUpsample2D nearest method requested but scale_factor {self.scale_factor} is not integer. Falling back to jax.image.resize.")
214+
max_logging.log(
215+
f"Warning: WanUpsample2D nearest method requested but scale_factor {self.scale_factor} is not integer. Falling back to jax.image.resize."
216+
)
211217
out = jax.image.resize(x.astype(jnp.float32), (n, target_h, target_w, c), method=self.method)
212218
out = out.astype(input_dtype)
213219
return out
@@ -1234,7 +1240,10 @@ def scan_fn(carry, chunk):
12341240
if spatial_sharding is not None:
12351241
out_chunk = jax.lax.with_sharding_constraint(out_chunk, spatial_sharding)
12361242
next_feat_map = jax.tree_util.tree_map(
1237-
lambda x: jax.lax.with_sharding_constraint(x, spatial_sharding) if spatial_sharding is not None and hasattr(x, "shape") and x.ndim == len(spatial_sharding.spec) else x, next_feat_map
1243+
lambda x: jax.lax.with_sharding_constraint(x, spatial_sharding)
1244+
if spatial_sharding is not None and hasattr(x, "shape") and x.ndim == len(spatial_sharding.spec)
1245+
else x,
1246+
next_feat_map,
12381247
)
12391248
return next_feat_map, out_chunk
12401249

@@ -1333,7 +1342,9 @@ def scan_fn(carry, chunk_in):
13331342
if spatial_sharding is not None:
13341343
out_chunk = jax.lax.with_sharding_constraint(out_chunk, spatial_sharding)
13351344
next_feat_map = jax.tree_util.tree_map(
1336-
lambda x: jax.lax.with_sharding_constraint(x, spatial_sharding) if spatial_sharding is not None and hasattr(x, "shape") and x.ndim == len(spatial_sharding.spec) else x,
1345+
lambda x: jax.lax.with_sharding_constraint(x, spatial_sharding)
1346+
if spatial_sharding is not None and hasattr(x, "shape") and x.ndim == len(spatial_sharding.spec)
1347+
else x,
13371348
next_feat_map,
13381349
)
13391350
return next_feat_map, out_chunk

src/maxdiffusion/pipelines/wan/wan_pipeline_i2v_2p1.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,15 @@ def prepare_latents(
106106
last_image = last_image.detach().cpu().numpy()
107107
last_image = jnp.array(last_image)
108108

109-
if num_videos_per_prompt > 1:
110-
image = jnp.repeat(image, num_videos_per_prompt, axis=0)
111-
if last_image is not None:
112-
last_image = jnp.repeat(last_image, num_videos_per_prompt, axis=0)
109+
if batch_size % image.shape[0] != 0:
110+
raise ValueError(f"Batch size ({batch_size}) must be divisible by image batch size ({image.shape[0]}).")
111+
if image.shape[0] < batch_size:
112+
image = jnp.repeat(image, batch_size // image.shape[0], axis=0)
113+
if last_image is not None:
114+
if batch_size % last_image.shape[0] != 0:
115+
raise ValueError(f"Batch size ({batch_size}) must be divisible by last_image batch size ({last_image.shape[0]}).")
116+
if last_image.shape[0] < batch_size:
117+
last_image = jnp.repeat(last_image, batch_size // last_image.shape[0], axis=0)
113118

114119
num_channels_latents = self.vae.z_dim
115120
num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1

src/maxdiffusion/pipelines/wan/wan_pipeline_i2v_2p2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ def prepare_latents(
158158
last_image = last_image.detach().cpu().numpy()
159159
last_image = jnp.array(last_image)
160160

161+
if batch_size % image.shape[0] != 0:
162+
raise ValueError(f"Batch size ({batch_size}) must be divisible by image batch size ({image.shape[0]}).")
163+
if image.shape[0] < batch_size:
164+
image = jnp.repeat(image, batch_size // image.shape[0], axis=0)
165+
if last_image is not None:
166+
if batch_size % last_image.shape[0] != 0:
167+
raise ValueError(f"Batch size ({batch_size}) must be divisible by last_image batch size ({last_image.shape[0]}).")
168+
if last_image.shape[0] < batch_size:
169+
last_image = jnp.repeat(last_image, batch_size // last_image.shape[0], axis=0)
170+
161171
num_channels_latents = self.vae.z_dim
162172
num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1
163173
latent_height = height // self.vae_scale_factor_spatial

0 commit comments

Comments
 (0)