
# ComfyUI套件下Wan2.2模型首尾帧基于ModelArts轻量算力节点适配NPU推理指导（6.5.910）
#### 方案概览
在视频生成领域，ComfyUI框架因其灵活性和强大的功能而受到广泛欢迎。然而，用户在使用ComfyUI框架进行视频生成时，经常会遇到模型性能不足的问题，尤其是在处理首尾帧工作流时。如何在保持模型精度的同时，实现性能的显著提升，是用户面临的主要挑战。为此，我们针对Wan2.2-14B模型进行了线性层w8a8量化和sage attention优化，旨在提高性能。通过这些优化措施，用户在使用ComfyUI-WanVideoWrapper个性化节点时，能够显著提升模型的处理速度和效率，特别是在加载包含lora模型的主模型时，性能提升尤为明显。我们提供了详细的优化方案，包括量化逻辑代码和sage attention逻辑代码，确保用户能够轻松集成这些优化措施，从而在实际应用中获得更好的性能表现。
本方案介绍了在NPU上使用ComfyUI套件推理部署Wan2.2-14B模型的详细过程。支持多卡部署，支持序列并行，量化，sageattention优化，方案专门适配ComfyUI套件下的ComfyUI-WanVideoWrapper节点。
#### 资源规格要求
建议使用轻量算力节点环境中的Snt9b单机资源。
表1Snt9b环境要求 
| 名称      | 版本            |
|:---|:---|
| driver  | 24.1.0.6      |
| PyTorch | pytorch_2.5.1 |
   
 #### 获取软件和镜像
 表2获取软件和镜像 
| 分类    | 名称                                                                                                                                                                                                                                         | 获取路径                                                                                                                                                                                                                                                                                                                                                                                             |
|:---|:---|:---|
| 插件代码包 | AscendCloud-AIGC-6.5.910-xxx.zip。 说明： 包名中的xxx表示具体的时间戳，以包名的实际时间为准。                    | 获取路径：[Support-E](https://support.huawei.com/enterprise/zh/cloud-computing/modelarts-pid-23404305/software/)，在此路径中查找下载ModelArts **6.5.910.6**版本。 说明： 如果上述软件获取路径打开后未显示相应的软件信息，说明您没有下载权限，请联系您所在企业的华为方技术支持下载获取。 |
| 基础镜像  | Snt9B：西南-贵阳一： swr.cn-southwest-2.myhuaweicloud.com/atelier/pytorch_ascend:pytorch_2.5.1-cann_8.2.rc2-py_3.11-hce_2.0.2509-aarch64-snt9b-20251105194139-a70c13c | 从SWR拉取。                                                                                                                                                                                                                                                                                                                                                                                          |
   
#### 约束限制
- 本文档适配ModelArts 6.5.910版本，请参考[表2]获取配套版本的软件包和镜像，请严格遵照版本配套关系使用本文档。
- 确保容器可以访问公网。
 
#### 步骤一：准备环境
1. 请参考[轻量算力节点资源开通](https://support.huaweicloud.com/resmgmt-modelarts/usermanual-server-0005.html)，购买资源，并确保机器已开通，密码已获取，能通过SSH登录，不同机器之间网络互通。
   ![](https://support.huaweicloud.com/bestpractice-modelarts/public_sys-resources/note_3.0-zh-cn.png)
   当容器需要提供服务给多个用户，或者多个用户共享使用该容器时，应限制容器访问Openstack的管理地址（169.254.169.254），以防止容器获取宿主机的元数据。具体操作请参见[禁止容器获取宿主机元数据](https://support.huaweicloud.com/bestpractice-cce/cce_bestpractice_0318.html#section4)。
   
2. SSH登录机器后，检查NPU设备检查。运行如下命令，返回NPU设备信息。
   ```
   npu-smi info                    # 在每个实例节点上运行此命令可以看到NPU卡状态
   npu-smi info -l | grep Total    # 在每个实例节点上运行此命令可以看到总卡数
   ```
   如出现错误，可能是机器上的NPU设备没有正常安装，或者NPU镜像被其他容器挂载。请先正常[安装固件和驱动](https://support.huaweicloud.com/resmgmt-modelarts/usermanual-server-0011.html#section4)，或释放被挂载的NPU。
   
3. 检查docker是否安装。
   ```
   docker -v   #检查docker是否安装
   ```
   如尚未安装，运行以下命令安装docker。
   ```
   yum install -y docker-engine.aarch64 docker-engine-selinux.noarch docker-runc.aarch64
   ```
   
4. 配置IP转发，用于容器内的网络访问。执行以下命令查看net.ipv4.ip_forward配置项的值，如果为1，可跳过此步骤。
   ```
   sysctl -p | grep net.ipv4.ip_forward
   ```
   如果net.ipv4.ip_forward配置项的值不为1，执行以下命令配置IP转发。
   ```
   sed -i 's/net\.ipv4\.ip_forward=0/net\.ipv4\.ip_forward=1/g' /etc/sysctl.conf
   sysctl -p | grep net.ipv4.ip_forward
   ```
   
 
#### 步骤二：获取基础镜像
建议使用官方提供的镜像部署服务。镜像地址{image_url}参见[表2]。
```
docker pull {image_url}
```
如需登录，请[登录SWR控制台](https://console.huaweicloud.com/swr/#/swr/dashboard)，参考以下图示获取登录指令。
![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002472473282.png "点击放大")
#### 步骤三：启动容器镜像
1. 启动容器镜像。启动前请先按照参数说明修改${}中的参数。 Snt9b容器启动命令：
   ```
   export work_dir="自定义挂载的工作目录"
   export container_work_dir="自定义挂载到容器内的工作目录"
   export container_name="自定义容器名称"
   export image_name="镜像名称或ID"
   // 启动一个容器去运行镜像 
   docker run  -itd --network=host \
       --device=/dev/davinci0 \
       --device=/dev/davinci1 \
       --device=/dev/davinci2 \
       --device=/dev/davinci3 \
       --device=/dev/davinci4 \
       --device=/dev/davinci5 \
       --device=/dev/davinci6 \
       --device=/dev/davinci7 \
       --device=/dev/davinci_manager \
       --device=/dev/devmm_svm \
       --device=/dev/hisi_hdc \
       --shm-size=256g \
       -v /usr/local/dcmi:/usr/local/dcmi \
       -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
       -v /var/log/npu/:/usr/slog \
       -v /usr/local/sbin/npu-smi:/usr/local/sbin/npu-smi \
       -v ${work_dir}:${container_work_dir} \
       --name ${container_name} \
       ${image_name} \
       /bin/bash
   ```
   **参数说明：**
   - -v ${work_dir}:${container_work_dir}：代表需要在容器中挂载宿主机的目录。宿主机和容器使用不同的文件系统。work_dir为宿主机中工作目录，目录下可存放项目所需代码、数据等文件。container_work_dir为要挂载到的容器中的目录。为方便两个地址可以相同。
     ![](https://support.huaweicloud.com/bestpractice-modelarts/public_sys-resources/note_3.0-zh-cn.png)
     - 容器不能挂载到/home/ma-user目录，此目录为ma-user用户家目录。如果容器挂载到/home/ma-user下，拉起容器时会与基础镜像冲突，导致基础镜像不可用。
     
     
     
     - driver及npu-smi需同时挂载至容器。
       
   
   - --name ${container_name}：容器名称，进入容器时会用到，此处可以自己定义一个容器名称。
   
   - ${image_name}：对应机型的基础镜像的名称，具体参见[表2]。
   
   - --device=/dev/davinci0 ：挂载对应卡到容器，当需要挂载多卡，请依次添加多项该配置。
    
2. 通过容器名称进入容器中。
   Snt9b默认使用ma-user用户，后续所有操作步骤都在ma-user用户下执行。
   ```
   docker exec -it ${container_name} bash
   ```
   
 
#### 步骤四：安装依赖和软件包
1. 安装AscendX_Video软件包。
   1. 将获取到的AscendX_Video软件包AscendCloud-AIGC-\*.zip文件上传到容器的/home/ma-user目录下。获取路径参见[获取软件和镜像]。
   
   2. 解压AscendCloud-AIGC-\*.zip文件，解压后按照步骤安装Python依赖，执行以下命令即可。
      ```
      cd /home/ma-user
      unzip AscendCloud-AIGC-*.zip -d ./AscendCloud
      cp -r /home/ma-user/AscendCloud/aigc_inference/torch_npu/ascendx_video ./
      cd /home/ma-user/ascendx_video
      pip install seal-*-linux_aarch64.whl
      pip install check_device-*-linux_aarch64.whl
      pip install ascendx_video-*-none-any.whl
      ```
      
   
   3. 安装算子环境。 执行如下命令进入安装目录。
      ```
      cd /home/ma-user/AscendCloud/opp/A2
      ```
      执行如下命令安装算子。
      ```
      unzip AscendCloud-OPP-*.zip
      unzip AscendCloud-OPP-*-torch-2.5.1-py311-*.zip -d ./AscendCloud_OPP
      cd AscendCloud_OPP
      pip install *.whl
      mkdir -p /home/ma-user/operate
      bash ./ascend_cloud_ops_ascend_turbo-*_linux_aarch64.run --install-path=/home/ma-user/operate
      bash ./ascend_cloud_ops_custom_opp-*_linux_aarch64_ascend910b_ascend910_93.run --install-path=/home/ma-user/operate
      cd ..
      unzip AscendCloud-OPS-ADV-*.zip -d ./AscendCloud_OPS-ADV
      cd AscendCloud_OPS-ADV
      bash ./CANN-custom_ops-*-linux.aarch64.run --install-path=/home/ma-user/operate
      ```
      
    
2. 初始化环境变量。注意每次进入容器需要重新初始化环境。
   ```
   source /home/ma-user/operate/AscendTurbo/set_env.bash
   source /home/ma-user/operate/vendors/customize/bin/set_env.bash
   source /home/ma-user/operate/vendors/customize_cloud/bin/set_env.bash
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/ma-user/anaconda3/envs/PyTorch-2.5.1/lib/python3.11/site-packages/torch_npu/lib:/home/ma-user/anaconda3/envs/PyTorch-2.5.1/lib/python3.11/site-packages/torch/lib/
   export TORCH_DEVICE_BACKEND_AUTOLOAD=0
   ```
   
 
#### 步骤五：下载ComfyUI套件
在/home/ma-user执行以下列命令：
```
cd /home/ma-user
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI
git reset --hard 9b151559721ff6c8d93150f3d8a53259a23911cd
pip install -r  requirements.txt
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-WanVideoWrapper.git
cd ComfyUI-WanVideoWrapper
git reset --hard 37365817e82c3d6057fcb0a5d7f952f7376f096a
pip install -r  requirements.txt
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite.git
cd ComfyUI-VideoHelperSuite
git reset --hard 6e7f63867584bb1fb4944d36172e7f98436da9a1
pip install -r  requirements.txt
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/jamesWalker55/comfyui-various.git
cd comfyui-various
git reset --hard 5bd85aaf7616878471469c4ec7e11bbd0cef3bf2
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle.git
cd ComfyUI_LayerStyle
git reset --hard 42ccdd8f75ab312285eaa77073a5cc20bdba484c
pip install -r requirements.txt --user
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-KJNodes.git
cd ComfyUI-KJNodes
git reset --hard 0adab07d1ad3d0780afc97319eaf37c3681af37d
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/comfyorg/comfyui-essentials.git
cd comfyui-essentials
git reset --hard b728fc0252c0cdc0833245895f2f722c14441a04
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes.git
cd ComfyUI_Comfyroll_CustomNodes
git reset --hard d78b780ae43fcf8c6b7c6505e6ffb4584281ceca
 
cd /home/ma-user/ComfyUI/custom_nodes
git clone https://github.com/chrisgoringe/cg-use-everywhere.git
cd cg-use-everywhere
git reset --hard 07157142dde3cc48373237c29d0859cbb144d884
```
#### 步骤六：下载模型权重
下载权重文件至容器目录，需要用到的模型地址如下。
表3模型权重地址 
| 模型权重文件                                                          | 下载地址                                                                                                                                                                                                                                         |
|:---|:---|
| wan2.2_i2v_high_noise_14B_fp16.safetensors                      | <https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/blob/main/split_files/diffusion_models/wan2.2_i2v_high_noise_14B_fp16.safetensors>                                                                                              |
| wan2.2_i2v_low_noise_14B_fp16.safetensors                       | <https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/blob/main/split_files/diffusion_models/wan2.2_i2v_low_noise_14B_fp16.safetensors>                                                                                               |
| open-clip-xlm-roberta-large-vit-huge-14_visual_fp16.safetensors | [https://huggingface.co/Kijai/WanVideo_comfy/blob/main/open-clip-xlm-roberta-large-vit-huge-14_visual_fp16.safetensors](https://support.huaweicloud.com/bestpractice-modelarts/modelarts_wan22_infer_5910.html#ZH-CN_TOPIC_0000002483133440) |
| umt5_xxl_fp16.safetensors                                       | <https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/text_encoders/umt5_xxl_fp16.safetensors>                                                                                                                  |
| wan_2.1_vae.safetensors                                         | <https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/vae/wan_2.1_vae.safetensors>                                                                                                                              |
| Wan21_I2V_14B_lightx2v_cfg_step_distill_lora_rank64.safetensors | <https://huggingface.co/lightx2v/Wan2.1-I2V-14B-480P-StepDistill-CfgDistill-Lightx2v/blob/main/loras/Wan21_I2V_14B_lightx2v_cfg_step_distill_lora_rank64.safetensors>                                                                        |
   
将权重放到 /home/ma-user/ComfyUI/models/目录下，例如：
```
models
└──diffusion_models
    ├──wan2.2_i2v_high_noise_14B_fp16.safetensors
    ├──wan2.2_i2v_low_noise_14B_fp16.safetensors
└──clip_vision
    ├──open-clip-xlm-roberta-large-vit-huge-14_visual_fp16.safetensors
└──text_encoders
    ├──umt5_xxl_fp16.safetensors
└──vae
    └──wan_2.1_vae.safetensors
└──loras
    ├──Wan21_I2V_14B_lightx2v_cfg_step_distill_lora_rank64.safetensors
```
#### 步骤七：ComfyUI部署
将AIGC\*.zip解压后的代码包放在ComfyUI原生代码相应目录下然后执行：
```
cp -r /home/ma-user/AscendCloud/aigc_inference/torch_npu/ascendx_video/comfyui/custom_nodes/ComfyUI-WanVideoWrapper/comfyui_ascendx /home/ma-user/ComfyUI/custom_nodes/ComfyUI-WanVideoWrapper
cd /home/ma-user/ComfyUI/custom_nodes/ComfyUI-WanVideoWrapper/comfyui_ascendx
sh patch_apply.sh
cp /home/ma-user/AscendCloud/aigc_inference/torch_npu/ascendx_video/comfyui/custom_nodes/ComfyUI-VideoHelperSuite/video_save_adaptor.patch /home/ma-user/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite
cd /home/ma-user/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite
sed -i 's/\r//g' video_save_adaptor.patch
git apply video_save_adaptor.patch
pip install numpy==1.26.0
cp /home/ma-user/AscendCloud/aigc_inference/torch_npu/ascendx_video/comfyui/main_multi.py /home/ma-user/ComfyUI/
```
在解压后的AIGC\*.zip里获取aigc_inference/torch_npu/ascendx_video/comfyui/**Wan2.2首尾帧720P加速版.json** ，并准备**首尾帧图片**。
#### 步骤八：启动服务
1. 进入目录。
   ```
   cd /home/ma-user/ComfyUI
   ```
   
2. 启动服务命令如下，--listen后的ip改为{IP地址}。 端口号：设置为无进程使用的端口
   IP地址：使用宿主机IP作为{IP地址}。
   ```
   python main.py --port {端口号} --listen {IP地址}   #单卡启动
   torchrun --nproc_per_node=2 --master_addr 127.0.0.1 --master_port 29505  main_multi.py --port {端口号} --listen {IP地址}   #双卡启动
   ```
   单卡启动成功后打印：
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002505703195.png "点击放大")
   双卡启动成功后打印：
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002505543251.png "点击放大")
   
3. 使用http://{宿主机ip}:{端口号}，如http://7.150.9.xxx:8183可以访问前端页面。 进入前端页面，不选用提示模板。
   将工作流按照如下图方式打开。工作流在AscendCloud-AIGC-xxx.zip内：aigc_inference/torch_npu/ascendx_video/comfyui/**Wan2.2首尾帧720P加速版.json**
   图1打开工作流，按照workflow进行推理即可。   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000002473383288.png) 
 
