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

发布于:2025-02-27 ⋅ 阅读:(18) ⋅ 点赞:(0)


在这里插入图片描述

引用:2月25日晚间,阿里云旗下视觉生成基座模型万相2.1(Wan)正式开源,此次开源采用最宽松的Apache2.0协议,14B和1.3B两个参数规格的全部推理代码和权重全部开源,同时支持文生视频和图生视频任务。

(零)在线体验

本地部署之前可以在线体验
在线可以图生视频呢。

在线的通义万相图生视频(2005届超女)示例

再到阿里自己的平台上看API的价格,顿时发现生成类都是天价,还是对话的AI便宜啊。

(一)本地部署

(1.1)克隆仓库

和其它开源项目类似,先克隆代码仓库

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

(1.2)安装依赖

国内环境先把Python换成国内源。
最好还是先建个虚拟环境。

> python -m venv venv

按照下面官方的方式直接装是不会成功的。

# Ensure torch >= 2.4.0
(venv) > pip install -r requirements.txt

(1.2.1)安装 flash-attention

依赖中有flash_attn这项,没指定版本,安装依赖时编译轮子总是失败。

查询其官方的二进制发布后发现最新版是v2.7.4.post1,且只有Linux版本。
发现第三方flash_attn-2.7.4.post1+cu124torch2.6.0cxx11abiFALSE-cp310-cp310-win_amd64.whl,下载后手动安装。

记下来这两个cu124,以及torch2.6.0

(1.2.2)重新安装依赖

这时候我们有flash-attention了,直接安装requirements.txt吧。

(venv) > pip install -r requirements.txt

(1.2.3)替换pytorch成CUDA版本

国内环境先把Pytorch换成国内源,或手动下载安装。

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

(1.3)下载模型

按照新闻上的说法,本地能部署Wan2.1-T2V-1.3B模型。
国内环境请换huggingface源,或手动去hf-mirror下载。

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

(1.4)CUDA和CUDNN

因为之前接触的项目都是CUDA11,所以我这部分也都得重装一次。
如果已经是CUDA12则无视。

请注意版本号对应。
正常都会向下兼容,所以我用了最新的CUDA12.8。

(1.4.1)CUDA Toolkit 12.8 和 Visual Studio 2022 v17.13.0 冲突

安装程序进度条会在nsight visual studio edition卡住。
且不报错,请参考

看到不是我一个人的问题,就放心了。
神仙打架,我等绕过吧。

(二)本地生成

(2.1)官方例子

两只拟人的猫咪穿着舒适的拳击装备,戴着鲜艳的拳击手套,在聚光灯照亮的舞台上激烈搏斗。

(venv) > 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."

(2.2)执行过程

[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]

我的电脑是

  • CPU:i9-12900F
  • RAM:64GB
  • 显卡:4060Ti16GB

预计生成时间超过2个小时。😓
所以我才有时间把踩过的坑记录下来。

CPU基本没占用,内存有一定的占用,显卡跑满。
现在我准备去看电视剧了……

在这里插入图片描述

(2.3)执行结果

(2.3.1)默认文件名无法保存

等了2个多小时,最后一步保存文件失败了???
仅仅是文件名的格式问题,蛤?
这小bug太折磨人了。

[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.
(venv) >

(2.3.2)成功

两个小时又悄悄的过去了。
增加--save_file参数后OK。

[2025-02-26 22:26:56,407] INFO: Generating video ...
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [2:15:32<00:00, 162.64s/it]
[2025-02-27 00:43:17,461] INFO: Saving generated video to D:\TestIt.mp4
[2025-02-27 00:43:17,918] INFO: Finished.
(venv) >

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