-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Fix wan 2.1 i2v context parallel #12909
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
Conversation
|
@sayakpaul @DN6 PTAL~ |
Would it have any memory impact? |
| "blocks.*": { | ||
| "encoder_hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False), | ||
| }, | ||
| # Reference: https://github.com/huggingface/diffusers/pull/12909 |
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.
Is this specific to I2V only? If so, then this change is probably a little to intrusive no?
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.
@sayakpaul This is theoretically applicable to all wan series models and offers better performance. I conducted tests on the wan 2.1/2.2 t2v/i2v models, and the results were all correct. You can perform quick verification through examples in cache-dit. This patch has already been used in cache-dit to fix some precision issues. vipshop/cache-dit#639
pip3 install torch==2.9.1 transformers accelerate torchao bitsandbytes torchvision
pip3 install opencv-python-headless einops imageio-ffmpeg ftfy
pip3 install git+https://github.com/huggingface/diffusers.git # latest
pip3 install git+https://github.com/vipshop/cache-dit.git # latest
git clone https://github.com/vipshop/cache-dit.git && cd cache-dit/examples
# use `--cpu-offload` and `--parallel-text-encoder` for low VRAM device, e.g, < 48GiB
torchrun --nproc_per_node=4 generate.py wan2.1_t2v --parallel ulysses --parallel-text-encoder
torchrun --nproc_per_node=4 generate.py wan2.2_t2v --parallel ulysses --parallel-text-encoder --cpu-offload
torchrun --nproc_per_node=2 generate.py wan2.1_i2v --parallel ulysses --parallel-text-encoder --steps 16 --frames 21 --vae-tiling
torchrun --nproc_per_node=2 generate.py wan2.2_i2v --parallel ulysses --parallel-text-encoder --cpu-offload --steps 16 --frames 21 --vae-tiling
|
|
ohhreally cool! |
I'm not sure. It depends on whether the Attention implementation of other models is similar to Wan. I haven't tested it on other models yet. |
yiyixuxu
left a comment
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.
thanks!
|
@bot /style |
|
Style fix runs successfully without any file modified. |
|
@DefTruth can you run |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
…fusers into fix-wan-2.1-i2v-cp
done |
|
Thanks for your contributions! |
follows up #12660, and also fixed vipshop/cache-dit#622
We need to disable the splitting of encoder_hidden_states because the image_encoder consistently generates 257 tokens for image_embed. This causes the shape of encoder_hidden_states—whose token count is always 769 (512 + 257) after concatenation—to be indivisible by the number of devices in the CP.
Since the key/value in cross-attention depends solely on encoder_hidden_states (text or img), the (q_chunk * k) * v computation can be parallelized independently. Thus, there is no need to pass the parallel_config for cross-attention. This change reduces redundant all-to-all communications—specifically (3+1)×2=8 for the two cross-attention operations (text and img)—thereby improving Wan’s performance under context parallelism.
@sayakpaul @yiyixuxu @DN6
reproduce
test cmds:
w/o this pr:
w/ this pr: