问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

本地部署阿里万象2.1文生视频(Wan2.1-T2V-1.3B)模型

创作时间:
作者:
@小白创作中心

本地部署阿里万象2.1文生视频(Wan2.1-T2V-1.3B)模型

引用
CSDN
1.
https://blog.csdn.net/ddrfan/article/details/145887466

2025年2月25日,阿里云旗下视觉生成基座模型万相2.1(Wan)正式开源,此次开源采用最宽松的Apache2.0协议,14B和1.3B两个参数规格的全部推理代码和权重全部开源,同时支持文生视频和图生视频任务。本文将详细介绍如何在本地部署万相2.1文生视频模型(Wan2.1-T2V-1.3B),包括在线体验、环境搭建、模型下载和视频生成等步骤。

在线体验

在本地部署之前,可以先在线体验万相2.1的图生视频功能。通过在线平台,用户可以尝试将图片转换为视频的效果。值得注意的是,通过阿里云API生成视频的价格相对较高,相比之下,对话类AI服务的价格更为亲民。

本地部署

克隆仓库

首先需要克隆万相2.1的代码仓库。使用以下命令:

git clone https://github.com/Wan-Video/Wan2.1.git
cd Wan2.1

安装依赖

在安装依赖之前,建议创建一个虚拟环境:

python -m venv venv

按照官方说明直接安装依赖可能会遇到问题。其中,flash_attn依赖项没有指定版本,直接安装可能会失败。查询其官方二进制发布后发现最新版是v2.7.4.post1,且只有Linux版本。幸运的是,可以在第三方找到适用于Windows的版本:

flash_attn-2.7.4.post1+cu124torch2.6.0cxx11abiFALSE-cp310-cp310-win_amd64.whl

下载后手动安装,并注意版本信息:

  • cu124
  • torch2.6.0

安装完flash-attention后,可以重新安装其他依赖:

pip install -r requirements.txt

接下来需要替换PyTorch为CUDA版本。国内环境可以使用国内源或手动下载安装:

pip uninstall torch torchvision
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126

下载模型

根据新闻报道,本地可以部署Wan2.1-T2V-1.3B模型。国内环境可以使用huggingface源或手动从hf-mirror下载:

pip install "huggingface_hub[cli]"
huggingface-cli download Wan-AI/Wan2.1-T2V-1.3B --local-dir ./Wan2.1-T2V-1.3B

CUDA和CUDNN

由于之前接触的项目都是CUDA11,因此需要重新安装CUDA12。请注意版本号对应。正常情况下会向下兼容,因此可以使用最新的CUDA12.8。但是需要注意的是,CUDA Toolkit 12.8 和 Visual Studio 2022 v17.13.0可能存在冲突,安装程序进度条可能会在nsight visual studio edition卡住且不报错,可以参考相关解决方案。

本地生成

官方例子

使用官方提供的示例命令生成视频:

python generate.py  --task t2v-1.3B --size 832*480 --ckpt_dir ./Wan2.1-T2V-1.3B --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."

执行过程

执行过程中会显示详细的日志信息,包括模型配置、输入提示、生成进度等。生成过程可能需要较长时间,具体取决于硬件配置。以下是部分日志示例:

[2025-02-26 19:54:53,860] INFO: offload_model is not specified, set to True.
[2025-02-26 19:54:53,860] INFO: Generation job args: Namespace(task='t2v-1.3B', size='832*480', frame_num=81, ckpt_dir='./Wan2.1-T2V-1.3B', offload_model=True, ulysses_size=1, ring_size=1, t5_fsdp=False, t5_cpu=False, dit_fsdp=False, save_file=None, prompt='Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage.', use_prompt_extend=False, prompt_extend_method='local_qwen', prompt_extend_model=None, prompt_extend_target_lang='ch', base_seed=2819604094901642957, image=None, sample_solver='unipc', sample_steps=50, sample_shift=5.0, sample_guide_scale=5.0)
[2025-02-26 19:54:53,860] INFO: Generation model config: {'__name__': 'Config: Wan T2V 1.3B', 't5_model': 'umt5_xxl', 't5_dtype': torch.bfloat16, 'text_len': 512, 'param_dtype': torch.bfloat16, 'num_train_timesteps': 1000, 'sample_fps': 16, 'sample_neg_prompt': '色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手 指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走', 't5_checkpoint': 'models_t5_umt5-xxl-enc-bf16.pth', 't5_tokenizer': 'google/umt5-xxl', 'vae_checkpoint': 'Wan2.1_VAE.pth', 'vae_stride': (4, 8, 8), 'patch_size': (1, 2, 2), 'dim': 1536, 'ffn_dim': 8960, 'freq_dim': 256, 'num_heads': 12, 'num_layers': 30, 'window_size': (-1, -1), 'qk_norm': True, 'cross_attn_norm': True, 'eps': 1e-06}
[2025-02-26 19:54:53,860] INFO: Input prompt: Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage.
[2025-02-26 19:54:53,860] INFO: Creating WanT2V pipeline.
[2025-02-26 19:55:26,106] INFO: loading ./Wan2.1-T2V-1.3B\models_t5_umt5-xxl-enc-bf16.pth
[2025-02-26 19:55:34,677] INFO: loading ./Wan2.1-T2V-1.3B\Wan2.1_VAE.pth
[2025-02-26 19:55:35,471] INFO: Creating WanModel from ./Wan2.1-T2V-1.3B
[2025-02-26 19:55:42,221] INFO: Generating video ...
 40%|██████████████████████████████████████████████████████████████████████████████                                                                                                                     | 20/50 [52:05<1:18:05, 156.18s/it]

执行结果

在生成过程中,可能会遇到一些问题。例如,系统默认的文件名格式可能导致保存失败:

[2025-02-26 19:55:42,221] INFO: Generating video ...
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [2:02:40<00:00, 147.20s/it]
[2025-02-26 22:12:12,834] INFO: Saving generated video to t2v-1.3B_832*480_1_1_Two_anthropomorphic_cats_in_comfy_boxing_gear_and__20250226_221212.mp4
[out#0/mp4 @ 000002818aa85c40] Error opening output \t2v-1.3B_832*480_1_1_Two_anthropomorphic_cats_in_comfy_boxing_gear_and__20250226_221212.mp4: Invalid argument
Error opening output file \t2v-1.3B_832*480_1_1_Two_anthropomorphic_cats_in_comfy_boxing_gear_and__20250226_221212.mp4.
Error opening output files: Invalid argument
cache_video failed, error: result type Float can't be cast to the desired output type Byte
[2025-02-26 22:12:16,213] INFO: Finished.

通过增加--save_file参数可以解决这个问题:

python generate.py  --task t2v-1.3B --size 832*480 --ckpt_dir ./Wan2.1-T2V-1.3B --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage." --save_file D:\TestIt.mp4

最终生成的视频效果如下:

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号