文档首页/ AI开发平台ModelArts/ 最佳实践/ 视频生成模型训练推理/ Wan2.1系列、HunyuanVideo和Cogvideo视频生成模型基于Lite Server适配Pytorch NPU的推理指导(6.5.906)
更新时间:2025-09-08 GMT+08:00
分享

Wan2.1系列、HunyuanVideo和Cogvideo视频生成模型基于Lite Server适配Pytorch NPU的推理指导(6.5.906)

方案概览

本文主要介绍如何在ModelArts的Lite Server环境中,使用NPU卡进行Wan2.1系列、HunyuanVideo和Cogvideo频生成模型进行文生视频推理或图生视频推理。完成本方案的部署,需要先联系您所在企业的华为方技术支持购买Server资源。

本案例支持Wan2.1系统(包含Wan2.1-T2V-14B-Diffusers, Wan2.1-T2V-1.3B-Diffusers, Wan2.1-I2V-14B-480P-Diffusers, Wan2.1-I2V-14B-720P-Diffusers), HunyuanVideo 和 Cogvideo 视频生成模型。

资源规格要求

建议使用Lite Server环境中的Ascend Snt9B单机或Ascend Snt9B23单机资源。

表1 Ascend Snt9B23 环境要求

名称

版本

driver

24.1.RC3.7

PyTorch

pytorch_2.5.1

表2 Ascend Snt9B 环境要求

名称

版本

driver

24.1.0.6

PyTorch

pytorch_2.5.1

获取软件和镜像

表3 获取软件和镜像

分类

名称

获取路径

插件代码包

AscendCloud-AIGC-6.5.906-xxx.zip软件包。

说明:

包名中的xxx表示具体的时间戳,以包名的实际时间为准。

获取路径:Support-E,在此路径中查找下载ModelArts 6.5.906.2 版本。

说明:

如果上述软件获取路径打开后未显示相应的软件信息,说明您没有下载权限,请联系您所在企业的华为方技术支持下载获取。

基础镜像

Snt9B23:乌兰一、华东二、西南-贵阳一

swr.cn-southwest-2.myhuaweicloud.com/atelier/pytorch_ascend:pytorch_2.5.1-cann_8.2.rc1-py_3.11-hce_2.0.2503-aarch64-snt9b23-20250717151727-8092d23

Snt9B:华东二、西南-贵阳一

swr.cn-southwest-2.myhuaweicloud.com/atelier/pytorch_ascend:pytorch_2.5.1-cann_8.2.rc1-py_3.11-hce_2.0.2503-aarch64-snt9b-20250717151727-8092d23

从SWR拉取。

约束限制

  • 本文档适配昇腾云ModelArts 6.5.906版本,请参考表3获取配套版本的软件包和镜像,请严格遵照版本配套关系使用本文档。
  • 确保容器可以访问公网。

步骤一:准备环境

  1. 请参考Lite Server资源开通,购买Server资源,并确保机器已开通,密码已获取,能通过SSH登录,不同机器之间网络互通。

    当容器需要提供服务给多个用户,或者多个用户共享使用该容器时,应限制容器访问Openstack的管理地址(169.254.169.254),以防止容器获取宿主机的元数据。具体操作请参见禁止容器获取宿主机元数据

  2. SSH登录机器后,检查NPU设备检查。运行如下命令,返回NPU设备信息。
    npu-smi info                    # 在每个实例节点上运行此命令可以看到NPU卡状态
    npu-smi info -l | grep Total    # 在每个实例节点上运行此命令可以看到总卡数

    如出现错误,可能是机器上的NPU设备没有正常安装,或者NPU镜像被其他容器挂载。请先正常安装固件和驱动,或释放被挂载的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}参见表3

docker pull {image_url}

步骤三:启动容器镜像

  1. 启动容器镜像。启动前请先按照参数说明修改${}中的参数。
    Snt9B23容器启动命令:
    export work_dir="自定义挂载的工作目录"
    export container_work_dir="自定义挂载到容器内的工作目录"
    export container_name="自定义容器名称"
    export image_name="镜像名称或ID"
    // 启动一个容器去运行镜像 
    docker run  -itd --net=host \
        --privileged \
        --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

    Snt9B容器启动命令:

    export work_dir="自定义挂载的工作目录"
    export container_work_dir="自定义挂载到容器内的工作目录"
    export container_name="自定义容器名称"
    export image_name="镜像名称或ID"
    // 启动一个容器去运行镜像 
    docker run  -itd --net=bridge \
        --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为要挂载到的容器中的目录。为方便两个地址可以相同。
      • 容器不能挂载到/home/ma-user目录,此目录为ma-user用户家目录。如果容器挂载到/home/ma-user下,拉起容器时会与基础镜像冲突,导致基础镜像不可用。
      • driver及npu-smi需同时挂载至容器。
    • --name ${container_name}:容器名称,进入容器时会用到,此处可以自己定义一个容器名称。
    • ${image_name}:对应机型的基础镜像的名称,具体参见表3
    • --device=/dev/davinci0 :挂载对应卡到容器,当需要挂载多卡,请依次添加多项该配置。
  2. 通过容器名称进入容器中。

    Snt9B23使用root用户登录

    docker exec -it -u root ${container_name} bash
    Snt9B默认使用ma-user用户,后续所有操作步骤都在ma-user用户下执行。
    docker exec -it ${container_name} bash

步骤四:安装依赖和软件包

  1. git clone和git lfs下载大模型可以参考如下操作。
    1. 在浏览器中输入如下地址下载git-lfs压缩包并上传到容器的/home/ma-user目录下。
      https://github.com/git-lfs/git-lfs/releases/download/v3.2.0/git-lfs-linux-arm64-v3.2.0.tar.gz
      或直接下载到容器,这样在容器中可以直接使用。
      cd /home/ma-user
      wget https://github.com/git-lfs/git-lfs/releases/download/v3.2.0/git-lfs-linux-arm64-v3.2.0.tar.gz
    2. 进入容器,执行安装git lfs命令。
      cd /home/ma-user
      tar -zxvf git-lfs-linux-arm64-v3.2.0.tar.gz 
      cd git-lfs-3.2.0 
      sudo sh install.sh
    3. 设置git配置去掉ssl校验。
      git config --global http.sslVerify false
  2. 安装AscendX_Video软件包。
    1. 将获取到的AscendX_Video软件包AscendCloud-AIGC-xxx.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. 安装算子环境。

      如果使用的是Snt9B23机器,执行:

      cd /home/ma-user/AscendCloud/opp/A3

      如果使用的是Snt9B机器,执行:

      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

3. 初始化环境变量

每次进入容器需要重新初始化环境。

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

步骤五:下载模型权重

下载权重文件至容器目录,需要用到的模型地址如下。

将权重放到 /home/ma-user/ascendx_video/weights目录下,例如:

weights
└──Wan-AI
    ├──Wan2.1-I2V-14B-480P-Diffusers
    ├──Wan2.1-I2V-14B-720P-Diffusers
    ├──Wan2.1-T2V-14B-Diffusers
    └──Wan2.1-T2V-1.3B-Diffusers
├──HunyuanVideo
└──CogVideoX-5b

步骤六:Wan文生视频模型推理

在/home/ma-user/ascendx_video/scripts/目录中有如下脚本:

  • infer_wan_14b_t2v.sh:表示Wan文生视频模型Wan2.1-T2V-14B的推理脚本。
  • infer_wan_14b_i2v.sh:表示Wan图生视频模型Wan2.1-I2V-14B的推理脚本。
  • infer_wan_1.3b_t2v.sh:表示Wan文生视频模型Wan2.1-T2V-1.3B的推理脚本。

执行以下命令开始推理任务,以infer_wan_14b_t2v.sh为例。

cd /home/ma-user/ascendx_video/scripts/
bash infer_wan_14b_t2v.sh

文生视频推理脚本infer_wan_14b_t2v.sh参数介绍如下。infer_wan_1.3b_t2v.sh脚本参数和infer_wan_14b_t2v.sh类似。

export MASTER_ADDR=127.0.0.1
export MASTER_PORT=29501

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export MEMORY_FRAGMENTATION=1
export COMBINED_ENABLE=1
export TASK_QUEUE_ENABLE=2
export TOKENIZERS_PARALLELISM=false

export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
N_NPUS=8
torchrun --nproc_per_node=$N_NPUS --master_addr $MASTER_ADDR --master_port $MASTER_PORT ../infer.py \
         --model Wan2.1-T2V-14B \
         --pretrained_model_name_or_path "../weights/Wan-AI/Wan2.1-T2V-14B-Diffusers" \
         --save_path ./output.mp4 \
         --num_inference_steps 50 \
         --width 1280 \
         --height 720 \
         --frames 81 \
         --sp 8 \
         --fsdp \
         --vae_lightning \
         --turbo_mode next_faiz \
         --atten_a8w8 \
         --matmul_a8w8 \
         --prompt "A young boy with short brown hair, dressed in a dark blue t-shirt and red pants, is seen playing a KAWAI upright piano with skill and concentration. The piano's glossy black surface reflects the room's lighting, and its white and black keys are arranged in a standard layout, indicating a scene of musical practice or learning. The boy's hands move over the keys, suggesting he is engaged in playing or practicing a piece." \
         --negative_prompt "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"
  • ASCEND_RT_VISIBLE_DEVICES:使用的卡号。
  • N_NPUS: 使用卡数量。Wan2.1-I2V-14BWan2.1-T2V-14B支持8卡推理,Wan2.1-T2V-1.3B支持4卡推理。
  • model:支持的推理模型,目前支持: Wan2.1-T2V-14BWan2.1-I2V-14BWan2.1-T2V-1.3B
  • pretrained_model_name_or_path:对应模型的权重地址。
  • save_path:推理生成的视频保存路径。
  • num_inference_steps:推理步数。
  • frames,height,width:生成视频的尺寸,分别是帧数,高,宽。目前支持 81x480x832、121x480x832、81x720x1280、121x720x1280。
  • prompt,negative_prompt:生成视频的正向提示词和反向提示词。
  • sp:并行参数,推荐和推理卡数保持一致,Wan2.1-T2V-1.3B建议4Wan2.1-T2V-14BWan2.1-I2V-14B建议8
  • vae_lightning:VAE加速,该参数仅在多卡场景下支持。不设置此参数时,不启用VAE加速。VAE加速启用能够加速VAE性能。
  • quality_enhance_mode:效果增强。支持 "faiz" 模式,默认为"default"不启用。推荐使用faiz达到最优效果。不设置此参数时,表示不启用效果增强。quality_enhance_mode效果增强,会对精度造成轻微影响。
  • turbo_mode:加速模式。支持 "faiz","next_faiz" 模式,默认为"default"不启用。推荐使用 next_faiz 模式,达到最高性能。不设置此参数时,表示不启用加速模式。turbo_mode加速模式能加速视频推理,但会对精度造成轻微影响。
  • atten_a8w8: atten量化加速。推荐打开,达到最高性能。不设置此参数时,表示不启用atten量化加速。atten量化加速能加速视频推理,但会对精度造成轻微影响。
  • matmul_a8w8: matmul量化加速。推荐打开,达到最高性能。不设置此参数时,表示不启用matmul量化加速。matmul量化加速能加速视频推理,但会对精度造成轻微影响。

推理任务运行结束后,生成的视频文件output.mp4存放在设置的save_path目录下,脚本中默认放在/home/ma-user/ascendx_video/scripts目录,请查看推理结果。

步骤七:Wan图生视频模型推理

开始图生视频模型推理前,先下载示例图片,并将示例图片放在 /home/ma-user/ascendx_video/scripts 目录下。

图1 示例图片

执行以下命令开始推理任务,以infer_wan_14b_i2v.sh为例。

cd /home/ma-user/ascendx_video/scripts/
bash infer_wan_14b_i2v.sh

图生视频推理脚本infer_wan_14b_i2v.sh参数介绍如下:

export MASTER_ADDR=127.0.0.1
export MASTER_PORT=29501

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export MEMORY_FRAGMENTATION=1
export COMBINED_ENABLE=1
export TASK_QUEUE_ENABLE=2
export TOKENIZERS_PARALLELISM=false
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
N_NPUS=8
torchrun --nproc_per_node=$N_NPUS --master_addr $MASTER_ADDR --master_port $MASTER_PORT ../infer.py \
         --model Wan2.1-I2V-14B \
         --pretrained_model_name_or_path "../weights/Wan-AI/Wan2.1-I2V-14B-480P-Diffusers" \
         --task_type i2v \
         --i2v_image_path ./astronaut.jpg \
         --save_path ./output.mp4 \
         --num_inference_steps 40 \
         --width 832 \
         --height 480 \
         --frames 81 \
         --sp 8 \
         --fsdp \
         --vae_lightning \
         --turbo_mode next_faiz \
         --atten_a8w8 \
         --matmul_a8w8 \
         --prompt "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in the background. High quality, ultrarealistic detail and breath-taking movie-like camera shot." \
         --negative_prompt "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走"
  • task_type:推理任务分为t2v, i2v。文生视频,图生视频。默认为图生视频。
  • i2v_image_path:图生视频的图路径。
  • 其他参数和infer_wan_14b_t2v.sh参数一致,具体请参见步骤六:Wan文生视频模型推理中的参数解释。

推理任务运行结束后,生成的视频文件output.mp4存放在设置的save_path目录下,脚本中默认放在/home/ma-user/ascendx_video/scripts目录,请查看推理结果。

步骤八:HunyuanVideo文生视频模型推理

在/home/ma-user/ascendx_video/scripts/目录中有如下脚本:

  • infer_hunyuan_video_13b_t2v_sp.sh:表示Hunyuan Video文生视频模型HunyuanVideo-T2V-13B的推理脚本。

执行以下命令开始推理任务,以infer_hunyuan_video_13b_t2v_sp为例。

cd /home/ma-user/ascendx_video/scripts/
bash infer_hunyuan_video_13b_t2v_sp.sh

文生视频推理脚本infer_hunyuan_video_13b_t2v_sp.sh参数介绍如下:

export MASTER_ADDR=127.0.0.1
export MASTER_PORT=29502

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export MEMORY_FRAGMENTATION=1
export COMBINED_ENABLE=1
export TASK_QUEUE_ENABLE=2
export TOKENIZERS_PARALLELISM=false
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
N_NPUS=8
torchrun --nproc_per_node=$N_NPUS --master_addr $MASTER_ADDR --master_port $MASTER_PORT ../infer.py \
         --model HunyuanVideo-T2V-13B \
         --pretrained_model_name_or_path "../weights/HunyuanVideo" \
         --task_type t2v \
         --save_path ./output.mp4 \
         --width 544 \
         --height 960 \
         --frames 129 \
         --sp 8 \
         --guidance_scale 6 \
         --num_inference_steps 50 \
         --seed 42 \
         --fsdp \
         --vae_lightning \
         --turbo_mode next_faiz \
         --prompt "An astronaut floating in space, but instead of stars, there are massive, glowing jellyfish drifting through the void. Their tentacles ripple with shifting, colorful lights, creating a mesmerizing display in the darkness of space. The astronaut moves between them, touching their delicate, translucent bodies, which react to touch with bursts of light. The scene feels dreamlike, as if the astronaut has entered a cosmic undersea world." \
         --negative_prompt "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"

参数和infer_wan_14b_t2v.sh参数一致,具体请参见步骤六:Wan文生视频模型推理中的参数解释。

推理任务运行结束后,生成的视频文件output.mp4存放在设置的save_path目录下,脚本中默认放在/home/ma-user/ascendx_video/scripts目录,请查看推理结果。

步骤九:Cogvideo文生视频模型推理

在/home/ma-user/ascendx_video/scripts/目录中有如下脚本:

  • infer_cogvideo_5b.sh:表示Cogvideo文生视频模型CogVideoX-5b的推理脚本。

执行以下命令开始推理任务,以infer_cogvideo_5b.sh为例。

cd /home/ma-user/ascendx_video/scripts/
bash infer_cogvideo_5b.sh

文生视频推理脚本infer_cogvideo_5b.sh参数介绍如下:

export MASTER_ADDR=127.0.0.1
export MASTER_PORT=29501

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export MEMORY_FRAGMENTATION=1
export COMBINED_ENABLE=1
export TASK_QUEUE_ENABLE=2
export TOKENIZERS_PARALLELISM=false
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
N_NPUS=8
torchrun --nproc_per_node=$N_NPUS --master_addr $MASTER_ADDR --master_port $MASTER_PORT ../infer.py \
         --model CogVideoX-5b \
         --pretrained_model_name_or_path ../weights/CogVideoX-5b \
         --save_path ./output.mp4 \
         --width 832 \
         --height 480 \
         --frames 49 \
         --num_inference_steps 50 \
         --sp 8 \
         --vae_lightning \
         --turbo_mode next_faiz \
         --fsdp \
         --atten_a8w8 \
         --matmul_a8w8 \
         --prompt "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." \
         --negative_prompt "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"

参数和infer_wan_14b_t2v.sh参数一致,具体请参见步骤六:Wan文生视频模型推理中的参数解释。

推理任务运行结束后,生成的视频文件output.mp4存放在设置的save_path目录下,脚本中默认放在/home/ma-user/ascendx_video/scripts目录,请查看推理结果。

相关文档