-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
6365601
support wan-animate-2
kelseyee 14f482c
Merge branch 'huggingface:main' into main
kelseyee a54425a
Refactor Wan-Animate-2 to diffusers conventions
2b722ed
Free per-segment KV cache and latents at the segment boundary
yiyixuxu 270a84e
Let `load_video` report the video's frame rate
yiyixuxu fc0c23b
Preprocess Wan-Animate-2 with the image/video processors
yiyixuxu 54dabb7
Match Wan-Animate-2 preprocessing to the reference implementation
yiyixuxu 918c239
Add Wan-Animate-2 modular pipeline
yiyixuxu cc5c239
Repack Wan-Animate-2 modular blocks into canonical standalone steps
yiyixuxu e0291f8
Refine Wan-Animate-2 modular blocks: in-loop VAE encode, v1-style ten…
yiyixuxu 1b81e87
Add Wan-Animate-2 modular tests, fill docstrings, self-assemble block…
yiyixuxu b9a1586
Add Wan-Animate-2 docs (modular pipeline + transformer)
yiyixuxu 1aa8d2f
Merge branch 'main' into animate2-refactor
yiyixuxu 1be4ed4
Run make style / make quality
yiyixuxu c307609
Point Wan-Animate-2 tests at the hf-internal-testing tiny repos
yiyixuxu d8965f7
Merge branch 'animate2-refactor' of github.com:yiyixuxu/diffusers int…
yiyixuxu 6dc53de
Import the video processor lazily to break a circular import
yiyixuxu 80938b2
Default distilled sampling to 10 steps; fix docstring/signature misma…
yiyixuxu 3d1046a
Pin Wan-Animate-2 block defaults; extend the defaults test for it
yiyixuxu 0eca946
Document per-preset sampling defaults; testing guide for the defaults…
yiyixuxu 612648c
Remove the standard WanAnimate2Pipeline
yiyixuxu e38e13d
Give the distilled Wan-Animate-2 preset its own model name
yiyixuxu 1551f50
feat: add tests for transformer.
sayakpaul 68fa61f
refactor transformer to avoid var mutation
sayakpaul e3db999
Merge pull request #1 from huggingface/transformer-wan-animate-tests
yiyixuxu e6e45c8
Apply suggestion from @yiyixuxu
yiyixuxu 198a639
Merge pull request #2 from huggingface/yiyixuxu-animate2-refactor
yiyixuxu 0275a2b
Apply suggestions from code review
yiyixuxu 3485751
Document that compiling the blocks is required for the flex attention…
yiyixuxu 38caa8f
Use the official demo assets in the doc example
yiyixuxu 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
| 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 |
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <!-- 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. Compiling the blocks is | ||
| # required as the in-context attention runs on the flex backend | ||
| pipe.transformer.enable_group_offload( | ||
| onload_device=torch.device("cuda"), | ||
| offload_device=torch.device("cpu"), | ||
| offload_type="block_level", | ||
| use_stream=True, | ||
| ) | ||
| pipe.text_encoder.to("cuda") | ||
| pipe.image_encoder.to("cuda") | ||
| pipe.vae.to("cuda") | ||
| pipe.transformer.compile_repeated_blocks(fullgraph=False) | ||
|
|
||
| # The first demo from the official repository: https://github.com/Wan-Video/Wan-Animate-2 | ||
| demo = "https://raw.githubusercontent.com/Wan-Video/Wan-Animate-2/main/examples/demo1" | ||
| image = load_image(f"{demo}/reference.png") | ||
| driving_video, driving_video_fps = load_video(f"{demo}/template.mp4", return_fps=True) | ||
| prompt = "人物外观描述:一只银灰色虎斑纹的小猫,拥有圆润的脸庞、竖立的耳朵和巨大的圆形眼睛。它身穿一套深蓝色的制服套装,包括一件带有金色纽扣的西装外套和一条百褶裙。外套里面搭配着白色衬衫,领口处系着一个红色的蝴蝶结,袖口露出白色的衬衫边缘。背景描述:背景为纯白色,光线均匀明亮,无其他杂物或装饰。" | ||
|
|
||
| videos = pipe( | ||
| image=image, | ||
| driving_video=driving_video, | ||
| driving_video_fps=driving_video_fps, | ||
| prompt=prompt, | ||
| 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 | ||
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
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.
Curious: what causes the graph break?