support-wan-animate-2 - #14412
Open
kelseyee wants to merge 2 commits into
Open
Conversation
Contributor
|
Hi @kelseyee, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add Wan-Animate-2 (In-Context Attention) Pipeline
Summary
This PR adds support for Wan-Animate-2, a character animation model that uses an in-context attention mechanism with KV cache and block mask (flex_attention), to the diffusers library.
Unlike the existing Wan-Animate v1 (which uses a motion encoder + face encoder), Wan-Animate-2 directly consumes driving video latents via a two-phase forward (reference encoding → generation with cached KV), eliminating intermediate motion extractors.
Model Architecture
IncontextAttentionBlock(forward_ref + forward_gen)Two-phase forward
forward_ref: Encodes the driving video, caches K/V per layer (40 layers × K+V)forward_gen: Generates video using cached K/V + block mask for frame-level sparse in-context attention, withscore_mod(log_scale) for reference attention weightingKey components (copied verbatim from original)
create_mask: Block mask creation logic for frame-level sparse attentionrope_params/rope_apply: RoPE computation (preserves float64 on CUDA)flash_attention/flex_attention: Attention functions (with lazytorch.compile)_score_mod_impl: log_scale score modificationNew files
src/diffusers/models/transformers/transformer_wan_animate_2.pyWanAnimate2Transformer3DModel— transformer model (~95% verbatim copy from original, 5% diffusers shell adaptation)src/diffusers/pipelines/wan/pipeline_wan_animate_2.pyWanAnimate2Pipeline— standard pipeline with segment-based generation, FPS resampling, letterbox resizesrc/diffusers/modular_pipelines/wan/modular_blocks_wan_animate_2.pyWanAnimate2Blocks/WanAnimate2ModularPipeline— modular pipeline blockstests/models/transformers/test_models_transformer_wan_animate_2.pytests/pipelines/wan/test_wan_animate_2.pyModified files
src/diffusers/__init__.pyWanAnimate2Transformer3DModel,WanAnimate2Pipelinesrc/diffusers/models/__init__.pysrc/diffusers/models/transformers/__init__.pysrc/diffusers/pipelines/__init__.pysrc/diffusers/pipelines/wan/__init__.pysrc/diffusers/modular_pipelines/wan/__init__.pysrc/diffusers/modular_pipelines/wan/modular_pipeline.pyWanAnimate2ModularPipelineclasssrc/diffusers/loaders/single_file_model.pysrc/diffusers/loaders/single_file_utils.pyconvert_wan_animate_2_transformer_to_diffuserssrc/diffusers/pipelines/pipeline_utils.pysrc/diffusers/utils/dummy_pt_objects.pysrc/diffusers/utils/dummy_torch_and_transformers_objects.pyUsage
Basic inference
Distilled model (10 steps, no CFG)
Key parameters
imagedriving_videopromptheight/widthclip_lennum_inference_stepsguidance_scaleflow_solver"dpm""dpm"or"euler")fpssample_shiftDesign decisions
Maximum code copying: ~95% of transformer computation logic is verbatim copied from the original
wan_animate_2_model.py. Only the outer shell (class inheritance,@register_to_config, class attributes, gradient checkpointing) is adapted.Preserves original behavior:
float64for RoPE (matchestransformer_wan.pyconditional dtype pattern)flex_attentionwithblock_mask+score_modcalled directly (not throughdispatch_attention_fn)torch.compilefor flex_attention (compiled on first call, not at import)Pipeline matches original:
decord+get_frame_indicesresize_by_area(keep aspect ratio + black padding)Shared components: VAE (
AutoencoderKLWan), text encoder (UMT5EncoderModel), CLIP (CLIPVisionModel), and scheduler (DPMSolverMultistepSchedulerwithflow_prediction) are the same as Wan2.1.Memory requirements
Use
pipe.enable_model_cpu_offload()to offload T5/CLIP/VAE to CPU when not in use.Checklist
WanAnimate2Transformer3DModel)WanAnimate2Pipeline)WanAnimate2Blocks,WanAnimate2ModularPipeline)from_single_file)__init__.pyregistrationsmake stylepassed (ruff check + format)make fix-copiespassedmake stylere-run after latest changesKnown limitations
flex_attentionrequirestorch.compile: Without compilation, falls back to dense (math) implementation which OOMs on large resolutions. Lazy compile is used to avoid import-time compilation.flash_attnis a hard dependency: Theflash_attentionfunction requiresflash_attnpackage. Not guarded withis_availablebecause it's essential for the model's attention computation.enable_model_cpu_offload()may break KV cache: The accelerate hooks may interfere with the KV cache dict. Use.to("cuda")for reliable operation, or ensure sufficient GPU memory.Context parallel not included: The original model supports FSDP/tensor parallel via
wanxiang.ops. This is not included in the initial PR. Multi-GPU inference requires manual FSDP setup.@yiyixuxu