hf_mirrors/Wan2.2
Model Introduction
Files & versions
Analysis

Wan2.2推理指导

目录

  • 一、准备运行环境
  • 二、下载权重
  • 三、Wan2.2使用
    • 3.1 开始前必读
    • 3.2 下载到本地,安装模型依赖
    • 3.3 Wan2.2-T2V-A14B
    • 3.4 Wan2.2-I2V-A14B
    • 3.5 Wan2.2-TI2V-5B
    • 3.6 MagCache 算法优化
    • 3.7 MindIE-SD 异步 CPU Offload
  • 四、W8A8量化功能支持
  • 五、W4A4量化功能支持
  • 六、推理结果参考
  • 七、VBench精度测试
  • 八、NPU Profiler 采集
  • 九、声明
  • 十、常见问题

一、准备运行环境

表 1 版本配套表

配套版本环境准备指导
Python3.11.10-
torch2.9.0-

注意:

  • 该模型也支持torch 2.1.0等版本

1.1 获取CANN&MindIE安装包&环境准备

  • 设备支持 Atlas 800I/800T A2(8*64G)
  • 环境准备指导

1.2 CANN安装

# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构,{soc}表示昇腾AI处理器的版本。
chmod +x ./Ascend-cann-toolkit_{version}_linux-{arch}.run
chmod +x ./Ascend-cann-kernels-{soc}_{version}_linux.run
# 校验软件包安装文件的一致性和完整性
./Ascend-cann-toolkit_{version}_linux-{arch}.run --check
./Ascend-cann-kernels-{soc}_{version}_linux.run --check
# 安装
./Ascend-cann-toolkit_{version}_linux-{arch}.run --install
./Ascend-cann-kernels-{soc}_{version}_linux.run --install

# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh
source /usr/local/Ascend/nnal/atb/set_env.sh

1.3 MindIE安装

# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构。
chmod +x ./Ascend-mindie_${version}_linux-${arch}.run
./Ascend-mindie_${version}_linux-${arch}.run --check

# 方式一:默认路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install
# 设置环境变量
cd /usr/local/Ascend/mindie && source set_env.sh

# 方式二:指定路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install-path=${AieInstallPath}
# 设置环境变量
cd ${AieInstallPath}/mindie && source set_env.sh

1.4 Torch_npu安装

请从昇腾社区资源下载中心下载与当前PyTorch版本、Python版本和CPU架构匹配的pytorch_v{pytorchversion}_py{pythonversion}.tar.gz软件包。Torch_npu版本配套关系可参考Ascend Extension for PyTorch。

安装前可通过以下命令确认Python版本和CPU架构:

python3 --version
uname -m

解压并安装Torch_npu:

tar -xzvf pytorch_v{pytorchversion}_py{pythonversion}.tar.gz
# 解压后,先确认whl包的实际文件名
find . -name "*.whl"
# 安装Torch_npu依赖
pip install pyyaml setuptools
# 安装与当前Python版本和CPU架构匹配的whl包,文件名以实际解压结果为准
pip install torch_npu-{pytorchversion}.xxxx.{arch}.whl

安装完成后执行验证:

python3 -c "import torch; import torch_npu; print('torch:', torch.__version__); print('torch_npu:', torch_npu.__version__); print('NPU available:', torch.npu.is_available())"

1.5 gcc、g++安装

# 若环境镜像中没有gcc、g++,请用户自行安装
yum install gcc
yum install g++

# 导入头文件路径
export CPLUS_INCLUDE_PATH=/usr/include/c++/12/:/usr/include/c++/12/aarch64-openEuler-linux/:$CPLUS_INCLUDE_PATH

注:若使用openeuler镜像,需要配置gcc、g++环境,否则会导致fatal error: 'stdio.h' file not found

二、下载权重

2.1 Wan2.2 权重及配置文件说明

  • Huggingface
模型链接
Wan2.2-T2V-A14B🤗huggingface
Wan2.2-I2V-A14B🤗huggingface
Wan2.2-TI2V-5B🤗huggingface
  • Modelers
模型链接
Wan2.2-T2V-A14B Modelers
Wan2.2-I2V-A14B Modelers
Wan2.2-TI2V-5B Modelers

2.2 Wan2.2 支持分辨率说明

模型支持分辨率
Wan2.2-T2V-A14B720*1280, 1280*720, 480*832, 832*480
Wan2.2-I2V-A14B720*1280, 1280*720, 480*832, 832*480
Wan2.2-TI2V-5B704*1280, 1280*704

三、Wan2.2使用

3.1 开始前必读

ALGO配置说明

ALGO用于选择FA计算方式,请在执行python或torchrun命令前根据设备及推理方式进行配置:

场景ALGO配置
昇腾Ascend 950设备浮点推理export ALGO=0
A2、A3设备浮点推理export ALGO=1
仅使能W8A8/W4A4 MXFP8量化export ALGO=0
使能W8A8/W4A4 MXFP8 + attention FP8量化export ALGO=3

切换设备或量化方式后,请重新确认ALGO配置。

独立配置FA量化格式

在Ascend 950设备上设置ALGO=3后,可通过环境变量单独控制T2V Self Attention的Dense FA量化格式,不要求量化DiT权重或传入quant_dit_path。

环境变量可选值说明
FA_PRECISIONfp8、mxfp8、mxfp4设置Dense FA量化格式;不设置时使用checkpoint注入的FA模块或浮点FA
FA_FP8_MODEHIGH_PRECISION、C8V16_TILING512仅在FA_PRECISION=fp8时生效;不设置时使用MindIE-SD默认模式

以FP8 C8V16模式为例:

export ALGO=3
export FA_PRECISION=fp8
export FA_FP8_MODE=C8V16_TILING512

使用RainFusion v3时,Dense阶段由FA_PRECISION控制,Sparse阶段由rainfusion_precision控制。

常见问题速查

若推理过程中出现OOM、HCCL端口占用、AOE目录不存在或torchvision版本冲突等问题,请参考九、常见问题。

3.2 下载到本地,安装模型依赖

git clone https://modelers.cn/MindIE/Wan2.2.git
cd Wan2.2
pip3 install -r requirements.txt

3.3 Wan2.2-T2V-A14B

使用上一步下载的权重

model_base="./Wan2.2-T2V-A14B/"

3.3.1 等价优化

3.3.1.1 单卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

python generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--offload_model False \
--base_seed 0 

参数说明:

  • ALGO: 为0表示默认FA算子; 设置为1表示使用高性能FA算子, 昇腾Ascend 950设备使用ALGO=0, A2、A3设备使用ALGO=1
  • FAST_LAYERNORM: 为0表示使用原生layernorm计算方式, 为1表示使用高性能layernorm算子
  • task: 任务类型。
  • ckpt_dir: 模型的权重路径
  • size: 生成视频的分辨率,支持(1280,720)、(832,480)分辨率
  • frame_num: 生成视频的帧数
  • sample_steps: 推理步数
  • prompt: 文本提示词
  • offload_model: 是否开启cpu offload
  • base_seed: 随机种子

3.3.1.2 8卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 --master_port=23459 generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--base_seed 0

参数说明:

  • dit_fsdp: dit使能fsdp, 用以降低显存占用
  • t5_fsdp: t5使能fsdp, 用以降低显存占用
  • cfg_size: cfg并行数
  • ulysses_size: ulysses并行数
  • vae_parallel: 使能vae并行策略

3.3.1.3 16卡性能测试

推荐配置:

torchrun --nproc_per_node=16 generate.py \
--cfg_size 2 \
--ulysses_size 8 \

3.3.2 算法优化-稀疏FA

3.3.2.1 8卡性能测试

执行命令:

export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 --master_port=23459 generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--use_rainfusion \
--rainfusion_type "v2" \
--sparsity 0.8 \
--sparse_start_step 15 \
--base_seed 0

参数说明:

  • use_rainfusion: 使能稀疏flash attention计算
  • rainfusion_type: Atlas 800I A2&A3使用v2,Ascend 950PR使用v3。默认为'v2', 'v1'将不再维护。
  • sparsity: 稀疏度,值为[0, 1), 稀疏度越大,加速比越高,相应精度损失更大
  • sparse_start_step: 开始稀疏的时间步,通常需要保证不小于1/4的总时间步数

3.3.3 RFv3稀疏量化FA

RainFusion v3将Self Attention分为Dense和Sparse两个阶段,两者的精度分别配置:

  • Dense阶段由ALGO、FA_PRECISION和FA_FP8_MODE控制。
  • Sparse阶段由rainfusion_precision控制,通过EagleQBSA执行块稀疏Attention。

本节示例使用Dense BF16和Sparse mix:前置Dense步骤执行BF16 FA,进入Sparse阶段后,Q/K按块量化为INT8,V量化为FP8。该功能当前仅支持Ascend 950设备,并要求rainfusion_type=v3。

如需组合Dense FP8 C8V16和Sparse mix,可将示例中的ALGO=0替换为独立配置FA量化格式中的三个环境变量;rainfusion_precision=mix保持不变。

3.3.3.1 8卡性能测试

执行命令:

export ALGO=0
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 --master_port=23459 generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--ring_size 1 \
--vae_parallel \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--use_rainfusion \
--rainfusion_type "v3" \
--rainfusion_precision "mix" \
--sparsity 0.8 \
--sparse_start_step 15 \
--mask_refresh_interval 1 \
--base_seed 0

参数说明:

rainfusion_precision设置Sparse阶段的计算精度:

取值Sparse阶段的数据格式
bf16Q/K/V使用BF16
mixQ/K按块量化为INT8,V量化为FP8
fp8Q/K/V使用FP8量化BSA
mxfp4使用FP4 E2M1数据和E8M0微缩放

不设置rainfusion_precision时,依次读取BSA_PRECISION环境变量和量化资产;量化资产包含q_rot/k_rot时推导为fp8,否则使用bf16。Sparse阶段暂不支持mxfp8。

  • use_rainfusion:启用稀疏FA。
  • rainfusion_type:设置为v3,使用Ascend 950上的RFv3/EagleQBSA路径。
  • sparsity:稀疏度,合法范围为[0, 1)。数值越大,保留的Attention块越少。设置为0时保留全部块,但仍执行所选精度的BSA算子。
  • sparse_start_step:进入Sparse阶段前保留的Dense timestep数量。RFv3当前至少保留前8个timestep为Dense,block 0及索引为38、39的timestep也使用Dense FA。
  • mask_refresh_interval:每隔多少个Sparse timestep重新计算一次块稀疏mask;设置为0表示首次生成后持续复用。
  • ring_size:RFv3/EagleQBSA当前仅支持设置为1,并会关闭Quant All-to-All和All-to-All overlap路径。

mxfp4的量程由BSA_MXFP4_DST_TYPE_MAX控制:默认值7.25使用CX档,设置为0时使用OCP档。BSA_MXFP4_SCALE_ALG可进一步指定缩放算法。

3.4 Wan2.2-I2V-A14B

使用上一步下载的权重

model_base="./Wan2.2-I2V-A14B/"

3.4.1 等价优化

3.4.1.1 8卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 generate.py \
--task i2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--base_seed 0

参数说明:

  • ALGO: 为0表示默认FA算子; 设置为1表示使用高性能FA算子
  • FAST_LAYERNORM: 为0表示使用原生layernorm计算方式, 为1表示使用高性能layernorm算子
  • task: 任务类型。
  • ckpt_dir: 模型的权重路径
  • size: 生成视频的分辨率,支持(1280,720)、(832,480)分辨率
  • frame_num: 生成视频的帧数
  • sample_steps: 推理步数
  • dit_fsdp: dit使能fsdp, 用以降低显存占用
  • t5_fsdp: t5使能fsdp, 用以降低显存占用
  • cfg_size: cfg并行数
  • ulysses_size: ulysses并行数
  • vae_parallel: 使能vae并行策略
  • image: 输入图片路径
  • prompt: 文本提示词
  • base_seed: 随机种子

3.4.1.2 16卡性能测试

推荐配置:

torchrun --nproc_per_node=16 generate.py \
--cfg_size 2 \
--ulysses_size 8 \

3.4.2 算法优化--稀疏FA

3.4.2.1 8卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 generate.py \
--task i2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--use_rainfusion \
--rainfusion_type "v2" \
--sparsity 0.8 \
--sparse_start_step 15 \
--base_seed 0

参数说明:

  • use_rainfusion: 使能稀疏flash attention计算
  • rainfusion_type: Atlas 800I A2&A3使用v2,Ascend 950PR使用v3。默认为'v2', 'v1'将不再维护。
  • sparsity: 稀疏度,值为[0, 1), 稀疏度越大,加速比越高,相应精度损失更大
  • sparse_start_step: 开始稀疏的时间步,通常需要保证不小于1/4的总时间步数

3.4.3 RFv3稀疏量化FA

I2V的Dense/Sparse阶段划分、精度配置和参数含义与3.3.3 RFv3稀疏量化FA一致。以下示例同样使用Dense BF16和Sparse mix。

3.4.3.1 8卡性能测试

执行命令:

export ALGO=0
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

torchrun --nproc_per_node=8 generate.py \
--task i2v-A14B \
--ckpt_dir ${model_base} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--ring_size 1 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds." \
--use_rainfusion \
--rainfusion_type "v3" \
--rainfusion_precision "mix" \
--sparsity 0.8 \
--sparse_start_step 15 \
--mask_refresh_interval 1 \
--base_seed 0

参数说明:

  • rainfusion_type=v3:启用Ascend 950上的RFv3/EagleQBSA路径。
  • rainfusion_precision=mix:Sparse阶段使用Q/K INT8和V FP8混合量化。
  • 其余参数及其他精度取值见3.3.3 RFv3稀疏量化FA。

3.5 Wan2.2-TI2V-5B

使用上一步下载的权重

model_base="./Wan2.2-TI2V-5B/"

3.5.1 等价优化

3.5.1.1 单卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

python generate.py \
--task ti2v-5B \
--ckpt_dir ${model_base} \
--size 1280*704 \
--frame_num 121 \
--sample_steps 50 \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--offload_model False \
--base_seed 0 

参数说明:

  • ALGO: 为0表示默认FA算子;设置为1表示使用高性能FA算子
  • FAST_LAYERNORM: 为0表示使用原生layernorm计算方式, 为1表示使用高性能layernorm算子
  • task: 任务类型。
  • ckpt_dir: 模型的权重路径
  • size: 生成视频的分辨率,支持(1280,720)、(832,480)分辨率
  • frame_num: 生成视频的帧数
  • sample_steps: 推理步数
  • image: 输入图片路径
  • prompt: 文本提示词
  • offload_model: 是否开启cpu offload,单卡默认开启
  • base_seed: 随机种子

3.5.1.2 8卡性能测试

执行命令:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
torchrun --nproc_per_node=8 generate.py \
--task ti2v-5B \
--ckpt_dir ${model_base} \
--size 1280*704 \
--frame_num 121 \
--sample_steps 50 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--base_seed 0

参数说明:

  • ALGO: 为0表示默认FA算子;设置为1表示使用高性能FA算子
  • FAST_LAYERNORM: 为0表示使用原生layernorm计算方式, 为1表示使用高性能layernorm算子
  • task: 任务类型。
  • ckpt_dir: 模型的权重路径
  • size: 生成视频的分辨率,支持(1280,720)、(832,480)分辨率
  • frame_num: 生成视频的帧数
  • sample_steps: 推理步数
  • dit_fsdp: dit使能fsdp, 用以降低显存占用
  • t5_fsdp: t5使能fsdp, 用以降低显存占用
  • cfg_size: cfg并行数
  • ulysses_size: ulysses并行数
  • vae_parallel: 使能vae并行策略
  • image: 输入图片路径
  • prompt: 文本提示词
  • base_seed: 随机种子

3.5.1.3 16卡性能测试

推荐配置:

torchrun --nproc_per_node=16 generate.py \
--cfg_size 2 \
--ulysses_size 8 \

3.5.2 算法优化

推荐配置:

--use_attentioncache \
--start_step 20 \
--attentioncache_interval 2 \
--end_step 47

参数说明:

  • use_attentioncache: 使能attentioncache策略
  • start_step: cache开始的step
  • attentioncache_interval: cache重计算间隔
  • end_step: cache结束的step

3.6 MagCache 算法优化

MagCache 通过动态计算不同网络层之间的特征幅度差异来决定是否跳过 Transformer block,可以在保证生成质量的前提下显著加速视频生成任务(支持 t2v、ti2v 和 i2v)。该特性已直接集成进 generate.py。

参数说明:

  • use_magcache: 使能 MagCache 策略
  • magcache_thresh: 决定是否跳过的累积误差阈值(默认 0.04)
  • magcache_K: 最大连续跳层步数限制(默认 2)
  • retention_ratio: 生成阶段前期不使用 MagCache 跳步的保护比例(默认 0.2)
  • magcache_calibration: (可选开发参数)标定模式,收集每步特征信息

3.7 MindIE-SD 异步 CPU Offload

通过动态将 DiT 的 Block 权重卸载到 CPU 并在需要时异步取回,极大降低模型推理期间对 NPU 显存的峰值占用。可配合单卡或多卡测试使用(不支持与 FSDP 联合使用)。

推荐配置:

--async_offload \
--min_reserved_blocks_count 2 \
--offload_model False 

参数说明:

  • async_offload: 开启基于 MindIE-SD 的块级别异步 cpu offload 策略
  • min_reserved_blocks_count: 指定常驻在 NPU 显存上的 DiT block 最小数量(默认值为2)
  • offload_model: 建议手动关闭官方的全量换入换出,以防发生内存冲突。程序检测到 async_offload 时也会自动关闭此功能。

四、W8A8量化功能支持

新增Wan2.2-T2V、Wan2.2-I2V、Wan2.2-TI2V的W8A8 量化支持,针对DiT模型进行量化,降低显存占用,提高模型推理性能

4.1 W8A8 MXFP8量化推理性能测试

4.1.1 安装量化工具msModelSlim

下载并安装msmodelslim工具

# 1. git clone msmodelslim 代码
git clone https://gitcode.com/Ascend/msmodelslim.git

# 2. 进入到 msmodelslim 的目录并运行安装脚本
cd msmodelslim
bash install.sh

4.1.2 导出量化权重

以Wan2.2-T2V-A14B模型为例,导出DiT的W8A8 MXFP8量化权重及描述文件

cd /path/to/msmodelslim

# 设置权重路径
model_base="./Wan2.2-T2V-A14B/"
# 设置权重保存路径
save_path="./quant_w8a8_mxfp8"

msmodelslim quant \
    --model_path ${model_base} \
    --save_path ${save_path} \
    --device npu \
    --model_type Wan2.2 \
    --trust_remote_code True \
    --config_path ./lab_practice/wan2_2/wan2_2_w8a8f8_mxfp_t2v.yaml

参数说明:

  • config_path: 指定量化算法配置yaml路径,msmodelslim工具中自带最佳实践

执行后,"./quant_w8a8_mxfp8"目录下会生成两个文件夹:

  • t2v_high_noise_model
    • quant_model_description_w8a8_mxfp8.json:量化配置描述文件
    • quant_model_weight_w8a8_mxfp8.safetensors:量化后的权重文件
  • t2v_low_noise_model
    • quant_model_description_w8a8_mxfp8.json:量化配置描述文件
    • quant_model_weight_w8a8_mxfp8.safetensors:量化后的权重文件

按照模型导入路径修改文件夹命名:

cd ./quant_w8a8_mxfp8
mv t2v_high_noise_model high_noise_model
mv t2v_low_noise_model low_noise_model

4.1.3 仅使能W8A8量化模型推理

以Wan2.2-T2V-A14B模型为例,执行量化推理

export ALGO=0
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

model_base="./Wan2.2-T2V-A14B/"
quant_dit_path="./quant_w8a8_mxfp8/"

torchrun --nproc_per_node=8 --master_port=23459 generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--quant_dit_path ${quant_dit_path} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--base_seed 0

4.1.4 使能 W8A8+attention FP8量化模型推理

以Wan2.2-T2V-A14B模型为例,执行量化推理

export ALGO=3
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

model_base="./Wan2.2-T2V-A14B/"
quant_dit_path="./quant_w8a8_mxfp8/"

torchrun --nproc_per_node=8 --master_port=23459 generate.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--quant_dit_path ${quant_dit_path} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." \
--base_seed 0

参数说明:

  • ALGO: 加载w8a8c8量化权重后,配置ALGO=3时使能W8A8 MXFP8 + attention FP8量化,配置ALGO=0时,仅使能W8A8 MXFP8量化

五、W4A4量化功能支持

新增Wan2.2-T2V、Wan2.2-I2V、Wan2.2-TI2V的W4A4量化支持,针对DiT模型进行量化,降低显存占用,提高模型推理性能

量化模型使用 CPU offload 或 DiT FSDP 时若出现内部格式转换、拷贝错误,请参见九、常见问题第 7 条。

5.1 W4A4 MXFP8量化推理性能测试

5.1.1 安装量化工具msModelSlim

下载并安装msmodelslim工具

# 1. git clone msmodelslim 代码
git clone https://gitcode.com/Ascend/msmodelslim.git

# 2. 进入到 msmodelslim 的目录并运行安装脚本
cd msmodelslim
bash install.sh

5.1.2 导出量化权重

以Wan2.2-I2V-A14B模型为例,导出DiT的W4A4 MXFP8量化权重及描述文件

cd /path/to/msmodelslim

# 设置权重路径
model_base="./Wan2.2-I2V-A14B/"
# 设置权重保存路径
save_path="./quant_w4a4_mxfp8"

msmodelslim quant \
    --model_path ${model_base} \
    --save_path ${save_path} \
    --device npu \
    --model_type Wan2.2 \
    --trust_remote_code True \
    --config_path ./lab_practice/wan2_2/wan2_2_w4a4f8_mxfp_i2v.yaml

参数说明:

  • config_path: 指定量化算法配置yaml路径,msmodelslim工具中自带最佳实践

执行后,"./quant_w4a4_mxfp8"目录下会生成两个文件夹:

  • i2v_high_noise_model
    • quant_model_description_w4a4_mxfp8.json:量化配置描述文件
    • quant_model_weight_w4a4_mxfp8.safetensors:量化后的权重文件
  • i2v_low_noise_model
    • quant_model_description_w4a4_mxfp8.json:量化配置描述文件
    • quant_model_weight_w4a4_mxfp8.safetensors:量化后的权重文件

按照模型导入路径修改文件夹命名:

cd ./quant_w4a4_mxfp8
mv i2v_high_noise_model high_noise_model
mv i2v_low_noise_model low_noise_model

5.1.3 仅使能W4A4量化模型推理

以Wan2.2-I2V-A14B模型为例,执行量化推理

export ALGO=0
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

model_base="./Wan2.2-I2V-A14B/"
quant_dit_path="./quant_w4a4_mxfp8/"

torchrun --nproc_per_node=8 --master_port=23458 generate.py \
--task i2v-A14B \
--ckpt_dir ${model_base} \
--quant_dit_path ${quant_dit_path} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--base_seed 0

5.1.4 使能 W4A4+attention FP8量化模型推理

以Wan2.2-I2V-A14B模型为例,执行量化推理

export ALGO=3
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

model_base="./Wan2.2-T2V-A14B/"
quant_dit_path="./quant_w4a4_mxfp8/"

torchrun --nproc_per_node=8 --master_port=23458 generate.py \
--task i2v-A14B \
--ckpt_dir ${model_base} \
--quant_dit_path ${quant_dit_path} \
--size 1280*720 \
--frame_num 81 \
--sample_steps 40 \
--cfg_size 2 \
--ulysses_size 4 \
--vae_parallel \
--image examples/i2v_input.JPG \
--prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside." \
--base_seed 0

参数说明:

  • ALGO: 加载w4a4c8量化权重后,配置ALGO=3时使能W4A4 MXFP8 + attention FP8量化,配置ALGO=0时,仅使能W4A4 MXFP8量化

六、推理结果参考

Atlas 800I A2(8*64G) 64核(arm)性能数据 (ALGO=1)

模型分辨率帧数迭代次数卡数E2E耗时
Wan2.2-T2V-A14B1280×72081408435.99s
Wan2.2-I2V-A14B1280×72081408436.42s
Wan2.2-TI2V-5B1280×70412150872.21s

七、VBench精度测试

VBench 1.0用于从主体一致性、背景一致性、运动平滑度、成像质量、语义一致性等维度评估视频生成质量。AISBench已适配VBench 1.0,具体说明请参考AISBench VBench 1.0使用指南。

本节以Wan2.2-T2V-A14B和VBench-1.0-mini为例,介绍评测环境安装、视频生成和评分流程。

说明:本节使用仓库根目录下的vbench.py批量生成评测视频。该脚本基于generate.py扩展,负责读取VBench数据集中的提示词,并按照VBench要求的目录结构保存视频。

7.1 安装AISBench及软件依赖

在Wan2.2仓库根目录执行:

git clone https://github.com/AISBench/benchmark.git
cd benchmark
pip install -v -e .

安装VBench部分评测维度依赖的Detectron2:

pip install -e ais_bench/third_party/detectron2 --no-build-isolation
cd ..

7.1.1 安装Decord

x86_64环境可直接安装:

pip install decord

ARM环境如果没有匹配的预编译包,可通过源码安装:

git clone https://github.com/dmlc/decord.git
cd decord
mkdir build
cd build

cmake .. -DUSE_CUDA=0 -DCMAKE_BUILD_TYPE=Release
make -j

cd ../python
python3 setup.py install --user
cd ../..

如果编译过程中提示无法找到FFmpeg,可安装FFmpeg后重新编译:

conda install -c conda-forge ffmpeg=4.4.2

如果安装过程中出现依赖版本兼容问题,可尝试使用以下已验证版本:

pip install "setuptools<=82.0.0" "timm==0.9.8"

7.2 下载VBench评测权重与资源

VBench在评测不同维度时需要加载多个辅助模型权重及相关资源文件。为避免评测过程中因网络问题导致文件下载失败,建议提前将这些文件下载到本地。所需文件及缓存说明请参考VBench缓存资源说明。

以下命令将评测所需的权重和资源下载到Wan2.2仓库根目录下的vbench_cache:

cd benchmark

export VBENCH_CACHE_DIR="../vbench_cache"
bash ais_bench/configs/vbench_examples/download_vbench_cache.sh

cd ..

后续执行评测时,需要将VBENCH_CACHE_DIR指向同一缓存目录:

export VBENCH_CACHE_DIR="./vbench_cache"

7.3 准备VBench数据集

VBench支持以下两种评测方式:

  • Standard:使用VBench官方提示词及维度定义,可用于标准化评测。
  • Custom:使用用户自定义提示词和视频,适合功能验证或特定场景评测。

建议首先使用VBench-1.0-mini验证完整流程。该数据集是从VBench 1.0标准数据集中采样得到的精简版本。

在Wan2.2仓库根目录执行:

git clone https://modelers.cn/AISBench/VBench-1.0-mini.git

下载完成后,确认数据集目录中包含批量生成和评测所需的元数据文件,例如:

VBench-1.0-mini/
├── VBench_kmeans_info.json
└── VBench_full_info.json

其中:

  • VBench_kmeans_info.json用于vbench.py读取提示词并生成视频。
  • VBench_full_info.json用于AISBench Standard模式匹配提示词和评测维度。

具体文件名请以下载的数据集实际内容为准,并相应修改后续命令中的路径。

7.4 生成Standard评测视频

执行批量生成前,根据设备类型配置ALGO,具体配置规则请参考3.1 开始前必读。

以下命令以4卡推理为例:

export ALGO=1
export PYTORCH_NPU_ALLOC_CONF='expandable_segments:True'
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=1
export TOKENIZERS_PARALLELISM=false
export FAST_LAYERNORM=1

model_base="./Wan2.2-T2V-A14B/"
vbench_dataset="./VBench-1.0-mini"
vbench_output="./results_vbench"

torchrun --nproc_per_node=4 --master_port=23459 vbench.py \
--task t2v-A14B \
--ckpt_dir ${model_base} \
--size "1280*720" \
--frame_num 81 \
--sample_steps 40 \
--dit_fsdp \
--t5_fsdp \
--cfg_size 2 \
--ulysses_size 2 \
--vae_parallel \
--base_seed 0 \
--vbench_mini \
--vbench_mini_root ${vbench_dataset} \
--save_path ${vbench_output} \
--num_samples 1 \
--temporal_flickering_samples 1 \
--skip_existing

VBench相关参数说明:

参数说明
--vbench_mini启用VBench-1.0-mini批量生成模式
--vbench_mini_rootVBench-1.0-mini数据集根目录
--save_path评测视频输出根目录
--num_samples除时序闪烁外,每条提示词生成的视频数量
--temporal_flickering_samples时序闪烁维度每条提示词生成的视频数量
--skip_existing输出视频已存在时跳过生成,便于中断后继续执行

上述命令将两个采样参数均设置为1,仅用于快速验证生成和评测流程。正式评测建议使用VBench默认采样数量:

--num_samples 5 \
--temporal_flickering_samples 25

不同实验之间应保持模型权重、提示词、分辨率、帧数、推理步数、采样数量及随机种子配置一致,否则评测结果不宜直接比较。

7.5 执行Standard评测

进入AISBench目录并复制一份评测配置,避免直接修改原始示例:

cd benchmark

cp ais_bench/configs/vbench_examples/eval_vbench_standard.py \
ais_bench/configs/vbench_examples/eval_vbench_wan22.py

打开eval_vbench_wan22.py,根据当前目录结构设置以下配置:

DATA_PATH = "../results_vbench"
VBENCH_CACHE_DIR = "../vbench_cache"

vbench_eval_cfg = dict(
    load_ckpt_from_local=True,
    full_json_dir="../VBench-1.0-mini/VBench_full_info.json",
)

其中:

  • DATA_PATH为vbench.py生成的视频根目录。
  • VBENCH_CACHE_DIR为第7.2节下载的评测权重及资源缓存目录。
  • full_json_dir为当前数据集对应的VBench_full_info.json。

执行评测:

ais_bench ais_bench/configs/vbench_examples/eval_vbench_wan22.py \
--mode eval \
--max-num-workers 1 \
--work_dir ../outputs/vbench_wan22

可以通过增大--max-num-workers使用多个设备并行评测,但该值不应超过当前可用设备数量。

各维度评测结果默认保存在:

outputs/vbench_wan22/results/vbench_eval/

Standard模式将输出各维度分数,并汇总视频质量分数、语义一致性分数和总分。

7.6 使用Custom模式评测自定义视频

Custom模式适用于不属于VBench Standard Prompt Suite的自定义提示词和视频。首先创建视频文件与提示词的对应关系,例如:

{
  "sample_000.mp4": "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage.",
  "sample_001.mp4": "A lone cowboy rides his horse across an open plain at sunset."
}

将该文件保存为custom_prompts.json,并将待评测视频统一放入custom_videos目录。

复制Custom模式配置:

cd benchmark

cp ais_bench/configs/vbench_examples/eval_vbench_custom.py \
ais_bench/configs/vbench_examples/eval_vbench_custom_wan22.py

在复制后的配置文件中设置:

  • DATA_PATH为../custom_videos。
  • prompt_file为../custom_prompts.json。
  • VBENCH_CACHE_DIR为../vbench_cache。

执行评测:

ais_bench ais_bench/configs/vbench_examples/eval_vbench_custom_wan22.py \
--mode eval \
--max-num-workers 1 \
--work_dir ../outputs/vbench_custom_wan22

Custom模式支持的评测维度少于Standard模式,其结果适合自定义实验之间的对比,不应直接与Standard模式的完整评分进行比较。

八、NPU Profiler 采集

Wan2.2 支持通过 --profiler-config 参数开启 NPU 性能采集,用于分析 t2v/ti2v/i2v 推理过程中的算子性能、AI Core 指标等。该功能默认关闭,开启后不影响推理结果,仅在推理外层叠加 profiler 采集。

8.1 快速开始

在推理命令后追加 --profiler-config 参数即可开启采集,最简配置只需指定 "profiler": "torch":

python generate.py --task t2v-A14B --size 1280*720 ... \
  --profiler-config '{"profiler": "torch"}'

8.2 参数配置

参数类型默认值说明
profilerstr无采集开关,设为 "torch" 时开启
torch_profiler_dirstr./profiling_dict输出目录(父路径)
torch_profiler_activitieslist["CPU", "NPU"]采集活动,可选 CPU / NPU
torch_profiler_record_shapesbooltrue是否记录算子输入形状
torch_profiler_profile_memoryboolfalse是否采集内存
torch_profiler_with_stackboolfalse是否记录调用栈
torch_profiler_with_flopsboolfalse是否记录 FLOPs
torch_profiler_with_modulesboolfalse是否记录模块层级
torch_profiler_aic_metricsstrPipeUtilizationAI Core 性能指标,可选 AiCoreNone / PipeUtilization / ArithmeticUtilization / Memory / MemoryL0 / MemoryUB / ResourceConflictRatio / L2Cache / MemoryAccess
torch_profiler_levelstrLevel1采集等级,见 8.3
torch_profiler_l2_cacheboolfalse是否采集 L2 Cache
torch_profiler_data_simplificationboolfalse是否开启数据精简

8.3 采集等级(torch_profiler_level)

取值说明
Level_none不采集所有 Level 层级控制的数据,即关闭 profiler_level
Level0采集上层应用数据、底层 NPU 数据及 NPU 上执行的算子信息(官方默认值)
Level1在 Level0 基础上多采集 CANN 层 AscendCL 数据和 AI Core 性能指标,并自动开启 aic_metrics=PipeUtilization,生成 communication.json / communication_matrix.json / api_statistic.csv
Level2在 Level1 基础上多采集 CANN 层 Runtime 数据及 AI CPU 数据(data_preprocess.csv)

8.4 输出说明

采集结果输出到 ./profiling_dict/Wan_{task}_{env}_{time}/ 目录(若指定 torch_profiler_dir 则以其为父路径)。最终 trace 文件由 tensorboard_trace_handler 在该目录下生成 {worker}_{timestamp}_ascend_pt/ 子目录,核心数据位于 ASCEND_PROFILER_OUTPUT/trace_view.json。

九、声明

  • 本代码仓提到的数据集和模型仅作为示例,这些数据集和模型仅供您用于非商业目的,如您使用这些数据集和模型来完成示例,请您特别注意应遵守对应数据集和模型的License,如您因使用数据集或模型而产生侵权纠纷,华为不承担任何责任。
  • 如您在使用本代码仓的过程中,发现任何问题(包括但不限于功能问题、合规问题),请在本代码仓提交issue,我们将及时审视并解答。

十、常见问题

  1. 若出现OOM, 可添加环境变量 export T5_LOAD_CPU=1,以降低显存占用

  2. 当前仅TI2V支持attentioncache

  3. 若遇到报错: Directory operation failed. Reason: Directory [/usr/local/Ascend/mindie/latest/mindie-rt/aoe] does not exist,请设置环境变量unset TUNE_BANK_PATH

  4. 若使用openeuler镜像, 若没有配置gcc、g++环境,会遇到报错:fatal error: 'stdio.h' file not found,请参考1.5 gcc、g++安装

  5. 若循环跑纯模型推理,可能会因为HCCL端口未及时释放,导致因端口被占用而推理失败,报错:Failed to bind the IP port. Reason: The IP address and port have been bound already. HCCL function error :HcclGetRootInfo(&hcclID), error code is 7: 请配置export HCCL_HOST_SOCKET_PORT_RANGE="auto"不指定端口 HCCL function error :HcclGetRootInfo(&hcclID), error code is 11: 请配置sysctl -w net.ipv4.ip_local_reserved_ports=60000-60015预留端口

  6. 若环境中的torch为cpu版本,安装requirement.txt时,默认会安装GPU版本的torchvision,导致版本冲突,出现类似AttributeError: partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)的错误。可通过安装cpu版本的torchvision解决:pip install torchvision==0.24.0 --index-url https://download.pytorch.org/whl/cpu

  7. 量化模型开启 CPU offload 或 DiT FSDP 后出现内部格式转换、拷贝错误:

    量化模型开启 CPU offload(如 --offload_model True、--async_offload 等)或 DiT FSDP(--dit_fsdp)后,可能出现以下错误:

    RuntimeError: Current device only support aclnn operator, and current operator at_npu::native::NPUNativeFunctions::copy_ do not support internal format.

    当量化权重使用 NZ 等内部格式,而当前运行环境的相关拷贝、格式转换或分布式同步路径不支持该格式时,可能出现上述错误。已在本项目 W4A4 MXFP4 Dual-Scale 配置下观察到该问题,其加载 quant_model_description_w4a4_mxfp4_dualscale.json 的分支会设置 use_nz=True,将量化权重转换为 NZ 内部格式。其他量化配置若使用相同格式和操作路径,也可能受到影响;是否兼容需结合具体配置、运行环境和实现验证。

    请根据触发场景处理:

    • CPU offload:模型或模块卸载到 CPU 时触发。请关闭所有涉及将该量化权重卸载到 CPU 的 offload 特性,例如设置 --offload_model False、移除 --async_offload 参数。
    • DiT FSDP:初始化时同步模型参数和 buffers,在 dist._broadcast_coalesced 路径触发,即使未开启 CPU offload 也可能出现。请移除 --dit_fsdp;已在上述 Dual-Scale 配置及所测环境下确认移除后可正常运行。

    关闭相应特性后,请确保 NPU 内存充足。