-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Wan-Animate-2 (authored by @kelseyee) #14413
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
base: main
Are you sure you want to change the base?
Changes from all commits
6365601
14f482c
a54425a
2b722ed
270a84e
fc0c23b
54dabb7
918c239
cc5c239
e0291f8
1b81e87
b9a1586
1aa8d2f
1be4ed4
c307609
d8965f7
6dc53de
80938b2
3d1046a
0eca946
612648c
e38e13d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!-- Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. --> | ||
|
|
||
| # WanAnimate2Transformer3DModel | ||
|
|
||
| A Diffusion Transformer model for 3D video-like data used in [Wan-Animate-2](https://github.com/Wan-Video/Wan2.2) by the Alibaba Wan Team. It animates a character image with the motion of a driving video through an in-context reference mechanism: each segment first runs a reference pass (`kv_cache_mode="extract"`) that caches every layer's reference K/V, then the denoising passes (`kv_cache_mode="cached"`) attend jointly over the generation tokens and the cached reference tokens through a flex `BlockMask`. | ||
|
|
||
| The model can be loaded with the following code snippet. | ||
|
|
||
| ```python | ||
| from diffusers import WanAnimate2Transformer3DModel | ||
|
|
||
| transformer = WanAnimate2Transformer3DModel.from_pretrained("Wan-AI/Wan2.2-Animate-2-14B-Diffusers", subfolder="transformer", dtype=torch.bfloat16) | ||
| ``` | ||
|
|
||
| ## WanAnimate2Transformer3DModel | ||
|
|
||
| [[autodoc]] WanAnimate2Transformer3DModel | ||
|
|
||
| ## Transformer2DModelOutput | ||
|
|
||
| [[autodoc]] models.modeling_outputs.Transformer2DModelOutput |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||||||||||||||||||||||||
| <!-- Copyright 2026 The HuggingFace Team. All rights reserved. | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||
| # limitations under the License. --> | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Wan-Animate-2 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| [Wan-Animate-2](https://github.com/Wan-Video/Wan2.2) by the Alibaba Wan Team animates a reference character image with the motion of a driving video. The driving video is processed in fixed-length segments: each segment runs a reference-extraction pass that caches the driving segment's K/V in every transformer layer, denoises against that cache, and is decoded inside the loop because the next segment conditions on the previous segment's decoded tail frames. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Two presets are available: the base checkpoint samples with classifier-free guidance, and the distilled checkpoint samples in few steps without it (its guider is pinned to `guidance_scale=1.0`). | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||
| from diffusers import ModularPipeline | ||||||||||||||||||||||||||||||
| from diffusers.utils import export_to_video, load_image, load_video | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| pipe = ModularPipeline.from_pretrained("Wan-AI/Wan2.2-Animate-2-14B-Diffusers") | ||||||||||||||||||||||||||||||
| pipe.load_components(dtype=torch.bfloat16) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # The transformer weights and the per-segment reference KV cache do not co-reside on one 80 GB | ||||||||||||||||||||||||||||||
| # card at the default resolution, so stream the transformer's blocks. The in-context attention | ||||||||||||||||||||||||||||||
| # runs on the flex backend; compiling fuses it. | ||||||||||||||||||||||||||||||
| from diffusers.hooks import apply_group_offloading | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| apply_group_offloading( | ||||||||||||||||||||||||||||||
| pipe.transformer, | ||||||||||||||||||||||||||||||
| onload_device=torch.device("cuda"), | ||||||||||||||||||||||||||||||
| offload_device=torch.device("cpu"), | ||||||||||||||||||||||||||||||
| offload_type="block_level", | ||||||||||||||||||||||||||||||
| use_stream=True, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
Comment on lines
+34
to
+40
Member
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.
Suggested change
|
||||||||||||||||||||||||||||||
| pipe.text_encoder.to("cuda") | ||||||||||||||||||||||||||||||
| pipe.image_encoder.to("cuda") | ||||||||||||||||||||||||||||||
| pipe.vae.to("cuda") | ||||||||||||||||||||||||||||||
| pipe.transformer.compile_repeated_blocks(fullgraph=False) | ||||||||||||||||||||||||||||||
|
Member
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. Curious: what causes the graph break? |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| driving_video, driving_video_fps = load_video("driving.mp4", return_fps=True) | ||||||||||||||||||||||||||||||
|
Member
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. Let's use something that is directly loadable. We don't know where "driving.mp4" is coming from. |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| videos = pipe( | ||||||||||||||||||||||||||||||
| image=load_image("reference.png"), | ||||||||||||||||||||||||||||||
|
Member
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 as above. |
||||||||||||||||||||||||||||||
| driving_video=driving_video, | ||||||||||||||||||||||||||||||
| driving_video_fps=driving_video_fps, | ||||||||||||||||||||||||||||||
| prompt="A cat in a blue uniform, white background", | ||||||||||||||||||||||||||||||
| output="videos", | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| export_to_video(videos[0], "output.mp4", fps=24) | ||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| For the distilled checkpoint, load `Wan-AI/Wan2.2-Animate-2-14B-Distilled-Diffusers` the same way — nothing else changes. Each preset carries its own sampling defaults (40 steps for the base checkpoint, 10 for the distilled one), and no `guidance_scale` argument exists anywhere: guidance is owned by the pipeline's guider component (classifier-free guidance at 3.0 for the base preset, disabled for the distilled one). | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| `height` and `width` (defaults 800 and 640) set the target *area* of the generated video; the actual frame size keeps the reference image's aspect ratio, and the driving frames are letterboxed to it. Inputs that already sit at the target letterbox size pass through the preprocessing untouched, so preprocessing can also be done entirely outside the pipeline. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ## WanAnimate2ModularPipeline | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| [[autodoc]] WanAnimate2ModularPipeline | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ## WanAnimate2DistilledModularPipeline | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| [[autodoc]] WanAnimate2DistilledModularPipeline | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ## WanAnimate2Blocks | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| [[autodoc]] WanAnimate2Blocks | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ## WanAnimate2DistilledBlocks | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| [[autodoc]] WanAnimate2DistilledBlocks | ||||||||||||||||||||||||||||||
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.