Skip to content

Commit 3a8f9e9

Browse files
committed
Disable CFG cache when batch size is indivisible by data shards to prevent ulysses attention crashes
1 parent 67c0c99 commit 3a8f9e9

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/maxdiffusion/pipelines/wan/wan_pipeline_2_2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ def run_inference_2_2(
256256
do_classifier_free_guidance = guidance_scale_low > 1.0 or guidance_scale_high > 1.0
257257
bsz = latents.shape[0]
258258

259+
data_shards = 1
260+
try:
261+
if hasattr(latents, "sharding") and hasattr(latents.sharding, "mesh"):
262+
data_shards = latents.sharding.mesh.shape["data"] * latents.sharding.mesh.shape.get("fsdp", 1)
263+
except Exception:
264+
pass
265+
266+
if use_cfg_cache and do_classifier_free_guidance and bsz % data_shards != 0:
267+
from maxdiffusion.max_logging import MaxLogging
268+
max_logging = MaxLogging()
269+
max_logging.log(f"Warning: Disabling CFG cache because batch size {bsz} is not divisible by data shards {data_shards}. This often happens with data_parallelism > 1 and per_device_batch_size = 1.")
270+
use_cfg_cache = False
271+
259272
prompt_embeds_combined = (
260273
jnp.concatenate([prompt_embeds, negative_prompt_embeds], axis=0) if do_classifier_free_guidance else prompt_embeds
261274
)

src/maxdiffusion/pipelines/wan/wan_pipeline_i2v_2p2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,19 @@ def run_inference_2_2_i2v(
371371
do_classifier_free_guidance = guidance_scale_low > 1.0 or guidance_scale_high > 1.0
372372
bsz = latents.shape[0]
373373

374+
data_shards = 1
375+
try:
376+
if hasattr(latents, "sharding") and hasattr(latents.sharding, "mesh"):
377+
data_shards = latents.sharding.mesh.shape["data"] * latents.sharding.mesh.shape.get("fsdp", 1)
378+
except Exception:
379+
pass
380+
381+
if use_cfg_cache and do_classifier_free_guidance and bsz % data_shards != 0:
382+
from maxdiffusion.max_logging import MaxLogging
383+
max_logging = MaxLogging()
384+
max_logging.log(f"Warning: Disabling CFG cache because batch size {bsz} is not divisible by data shards {data_shards}. This often happens with data_parallelism > 1 and per_device_batch_size = 1.")
385+
use_cfg_cache = False
386+
374387
prompt_embeds_combined = (
375388
jnp.concatenate([prompt_embeds, negative_prompt_embeds], axis=0) if do_classifier_free_guidance else prompt_embeds
376389
)

0 commit comments

Comments
 (0)