Skip to content

Commit 84d5d49

Browse files
committed
fix(mag_cache): correct per-transformer step count for Wan 2.2 two-stage denoising
1 parent 9159a58 commit 84d5d49

5 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/diffusers/hooks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .hooks import HookRegistry, ModelHook
2424
from .layer_skip import LayerSkipConfig, apply_layer_skip
2525
from .layerwise_casting import apply_layerwise_casting, apply_layerwise_casting_hook
26-
from .mag_cache import MagCacheConfig, apply_mag_cache
26+
from .mag_cache import MagCacheConfig, apply_mag_cache, update_mag_cache_num_steps
2727
from .pyramid_attention_broadcast import PyramidAttentionBroadcastConfig, apply_pyramid_attention_broadcast
2828
from .smoothed_energy_guidance_utils import SmoothedEnergyGuidanceConfig
2929
from .taylorseer_cache import TaylorSeerCacheConfig, apply_taylorseer_cache

src/diffusers/hooks/mag_cache.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def __post_init__(self):
130130
if not torch.is_tensor(self.mag_ratios):
131131
self.mag_ratios = torch.tensor(self.mag_ratios)
132132

133+
self._original_mag_ratios = self.mag_ratios.clone()
133134
if len(self.mag_ratios) != self.num_inference_steps:
134135
logger.debug(
135136
f"Interpolating mag_ratios from length {len(self.mag_ratios)} to {self.num_inference_steps}"
@@ -407,6 +408,7 @@ def apply_mag_cache(module: torch.nn.Module, config: MagCacheConfig) -> None:
407408
# Initialize registry on the root module so the Pipeline can set context.
408409
HookRegistry.check_if_exists_or_initialize(module)
409410

411+
module._mag_cache_config = config
410412
state_manager = StateManager(MagCacheState, (), {})
411413
remaining_blocks = []
412414

@@ -466,3 +468,15 @@ def _apply_mag_cache_block_hook(
466468

467469
hook = MagCacheBlockHook(state_manager, is_tail, config)
468470
registry.register_hook(hook, _MAG_CACHE_BLOCK_HOOK)
471+
472+
473+
def update_mag_cache_num_steps(module: torch.nn.Module, num_steps: int) -> None:
474+
config: MagCacheConfig = getattr(module, "_mag_cache_config", None)
475+
if config is None:
476+
return
477+
original_ratios = getattr(config, "_original_mag_ratios", config.mag_ratios)
478+
config.num_inference_steps = num_steps
479+
if original_ratios is not None:
480+
config.mag_ratios = nearest_interp(original_ratios, num_steps)
481+
482+

src/diffusers/pipelines/wan/pipeline_wan.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,15 @@ def __call__(
586586
else:
587587
boundary_timestep = None
588588

589+
if boundary_timestep is not None:
590+
from ...hooks.mag_cache import update_mag_cache_num_steps
591+
n_steps_t1 = sum(1 for t in timesteps if t >= boundary_timestep)
592+
n_steps_t2 = len(timesteps) - n_steps_t1
593+
if self.transformer is not None:
594+
update_mag_cache_num_steps(self.transformer, n_steps_t1)
595+
if self.transformer_2 is not None:
596+
update_mag_cache_num_steps(self.transformer_2, n_steps_t2)
597+
589598
with self.progress_bar(total=num_inference_steps) as progress_bar:
590599
for i, t in enumerate(timesteps):
591600
if self.interrupt:

src/diffusers/pipelines/wan/pipeline_wan_i2v.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,15 @@ def __call__(
741741
else:
742742
boundary_timestep = None
743743

744+
if boundary_timestep is not None:
745+
from ...hooks.mag_cache import update_mag_cache_num_steps
746+
n_steps_t1 = sum(1 for t in timesteps if t >= boundary_timestep)
747+
n_steps_t2 = len(timesteps) - n_steps_t1
748+
if self.transformer is not None:
749+
update_mag_cache_num_steps(self.transformer, n_steps_t1)
750+
if self.transformer_2 is not None:
751+
update_mag_cache_num_steps(self.transformer_2, n_steps_t2)
752+
744753
with self.progress_bar(total=num_inference_steps) as progress_bar:
745754
for i, t in enumerate(timesteps):
746755
if self.interrupt:

src/diffusers/pipelines/wan/pipeline_wan_vace.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,15 @@ def __call__(
955955
else:
956956
boundary_timestep = None
957957

958+
if boundary_timestep is not None:
959+
from ...hooks.mag_cache import update_mag_cache_num_steps
960+
n_steps_t1 = sum(1 for t in timesteps if t >= boundary_timestep)
961+
n_steps_t2 = len(timesteps) - n_steps_t1
962+
if self.transformer is not None:
963+
update_mag_cache_num_steps(self.transformer, n_steps_t1)
964+
if self.transformer_2 is not None:
965+
update_mag_cache_num_steps(self.transformer_2, n_steps_t2)
966+
958967
with self.progress_bar(total=num_inference_steps) as progress_bar:
959968
for i, t in enumerate(timesteps):
960969
if self.interrupt:

0 commit comments

Comments
 (0)