-
Notifications
You must be signed in to change notification settings - Fork 83
Optimize WAN pipelines: Reduce host overhead, hoist transposes, and fix Cache bugs #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Perseus14
wants to merge
1
commit into
main
Choose a base branch
from
wan-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,7 @@ def __call__( | |
| use_cfg_cache: bool = False, | ||
| use_sen_cache: bool = False, | ||
| use_kv_cache: bool = False, | ||
| output_type: str = "pil", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| ): | ||
| config = getattr(self, "config", None) | ||
| if max_sequence_length is None: | ||
|
|
@@ -203,6 +204,9 @@ def __call__( | |
| latents.block_until_ready() | ||
| trace["denoise_total"] = time.perf_counter() - t_denoise_start | ||
|
|
||
| if output_type == "latent": | ||
| return latents, trace | ||
|
|
||
| t_decode_start = time.perf_counter() | ||
| video = self._decode_latents_to_video(latents, trace=trace) | ||
| if hasattr(video, "block_until_ready"): | ||
|
|
@@ -252,6 +256,19 @@ def run_inference_2_2( | |
| do_classifier_free_guidance = guidance_scale_low > 1.0 or guidance_scale_high > 1.0 | ||
| bsz = latents.shape[0] | ||
|
|
||
| data_shards = 1 | ||
| try: | ||
| if hasattr(latents, "sharding") and hasattr(latents.sharding, "mesh"): | ||
| data_shards = latents.sharding.mesh.shape["data"] * latents.sharding.mesh.shape.get("fsdp", 1) | ||
| except Exception: | ||
| pass | ||
|
|
||
| if use_cfg_cache and do_classifier_free_guidance and bsz % data_shards != 0: | ||
| 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." | ||
| ) | ||
| use_cfg_cache = False | ||
|
|
||
| prompt_embeds_combined = ( | ||
| jnp.concatenate([prompt_embeds, negative_prompt_embeds], axis=0) if do_classifier_free_guidance else prompt_embeds | ||
| ) | ||
|
|
@@ -279,6 +296,8 @@ def run_inference_2_2( | |
| high_transformer = nnx.merge(high_noise_graphdef, high_noise_state, high_noise_rest) | ||
| kv_cache_high, encoder_attention_mask_high = high_transformer.compute_kv_cache(prompt_embeds_combined) | ||
|
|
||
| timesteps = jnp.array(scheduler_state.timesteps, dtype=jnp.int32) | ||
|
|
||
| # ── SenCache path (arXiv:2602.24208) ── | ||
| if use_sen_cache and do_classifier_free_guidance: | ||
| timesteps_np = np.array(scheduler_state.timesteps, dtype=np.int32) | ||
|
|
@@ -303,16 +322,18 @@ def run_inference_2_2( | |
| num_train_timesteps = float(scheduler.config.num_train_timesteps) | ||
|
|
||
| # SenCache state | ||
| ref_noise_pred = None # y^r: cached denoiser output | ||
| ref_latent = None # x^r: latent at last cache refresh | ||
| ref_timestep = 0.0 # t^r: timestep (normalized to [0,1]) at last cache refresh | ||
| accum_dx = 0.0 # accumulated ||Δx|| since last refresh | ||
| accum_dt = 0.0 # accumulated |Δt| since last refresh | ||
| reuse_count = 0 # consecutive cache reuses | ||
| cache_count = 0 | ||
| ref_noise_pred = jnp.zeros( | ||
| (bsz * 2, latents.shape[1], latents.shape[2], latents.shape[3], latents.shape[4]), dtype=latents.dtype | ||
| ) | ||
| ref_latent = jnp.zeros_like(latents) | ||
| ref_timestep = jnp.array(0.0, dtype=jnp.float32) | ||
| accum_dx = jnp.array(0.0, dtype=jnp.float32) | ||
| accum_dt = jnp.array(0.0, dtype=jnp.float32) | ||
| reuse_count = jnp.array(0, dtype=jnp.int32) | ||
| cache_count = jnp.array(0, dtype=jnp.int32) | ||
|
|
||
| for step in range(num_inference_steps): | ||
| t = jnp.array(scheduler_state.timesteps, dtype=jnp.int32)[step] | ||
| t = timesteps[step] | ||
| t_float = float(timesteps_np[step]) / num_train_timesteps # normalize to [0, 1] | ||
|
|
||
| # Select transformer and guidance scale | ||
|
|
@@ -358,10 +379,10 @@ def run_inference_2_2( | |
| ) | ||
| ref_noise_pred = noise_pred | ||
| ref_latent = latents | ||
| ref_timestep = t_float | ||
| accum_dx = 0.0 | ||
| accum_dt = 0.0 | ||
| reuse_count = 0 | ||
| ref_timestep = jnp.array(t_float, dtype=jnp.float32) | ||
| accum_dx = jnp.array(0.0, dtype=jnp.float32) | ||
| accum_dt = jnp.array(0.0, dtype=jnp.float32) | ||
| reuse_count = jnp.array(0, dtype=jnp.int32) | ||
| latents, scheduler_state = scheduler.step(scheduler_state, noise_pred, t, latents).to_tuple() | ||
| continue | ||
|
|
||
|
|
@@ -375,12 +396,10 @@ def run_inference_2_2( | |
| score = alpha_x * accum_dx + alpha_t * accum_dt | ||
|
|
||
| if score <= sen_epsilon and reuse_count < max_reuse: | ||
| # Cache hit: reuse previous output | ||
| noise_pred = ref_noise_pred | ||
| reuse_count += 1 | ||
| cache_count += 1 | ||
| else: | ||
| # Cache miss: full CFG forward pass | ||
| latents_doubled = jnp.concatenate([latents] * 2) | ||
| timestep = jnp.broadcast_to(t, bsz * 2) | ||
| noise_pred, _, _ = transformer_forward_pass_full_cfg( | ||
|
|
@@ -470,7 +489,7 @@ def run_inference_2_2( | |
| cached_noise_uncond = None | ||
|
|
||
| for step in range(num_inference_steps): | ||
| t = jnp.array(scheduler_state.timesteps, dtype=jnp.int32)[step] | ||
| t = timesteps[step] | ||
| is_cache_step = step_is_cache[step] | ||
|
|
||
| # Select transformer and guidance scale based on precomputed schedule | ||
|
|
@@ -607,8 +626,6 @@ def low_noise_branch(operands): | |
| ) | ||
|
|
||
| if scan_diffusion_loop: | ||
| timesteps = jnp.array(scheduler_state.timesteps, dtype=jnp.int32) | ||
|
|
||
| scheduler_state = scheduler_state.replace(last_sample=jnp.zeros_like(latents), step_index=jnp.array(0, dtype=jnp.int32)) | ||
|
|
||
| def scan_body(carry, t): | ||
|
|
@@ -657,7 +674,7 @@ def scan_body(carry, t): | |
| profiler = max_utils.Profiler(config) | ||
| profiler.start() | ||
|
|
||
| t = jnp.array(scheduler_state.timesteps, dtype=jnp.int32)[step] | ||
| t = timesteps[step] | ||
|
|
||
| if step_uses_high[step]: | ||
| graphdef, state, rest = ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default the method returns
np.array, notpil, but correct me if I'm wrong. In general, it would be nice to make checks thatoutput_typeis within supported output types.