w
weasonlee/ltx-video
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LTX-Video — Ascend NPU 适配(2B DiT distilled 视频生成)

上游仓库 Lightricks/LTX-Video 2B DiT 蒸馏版(distilled,8 步采样)文本生成视频模型,在昇腾 Ascend 910 NPU 上完成 CPU / NPU 双跑推理验证。

适配过程

  1. 组件探测:Lightricks/LTX-Video 仓库只有裸权重 + scheduler/scheduler_config.json (无 diffusers-format 组件目录),因此采用手动组合管线: LTXVideoTransformer3DModel + AutoencoderKLLTXVideo + T5EncoderModel + T5TokenizerFast + FlowMatchEulerDiscreteScheduler,全部来自单个 4.5G 权重文件 (含 transformer + VAE + per_channel_statistics,944 张量)。
  2. fp8 反量化:权重文件名为 distilled-fp8,实测为 BF16(664) + FP8-E4M3(280) 混合 量化。NPU 不直接支持 fp8 计算,inference.py 首次加载时自动将 FP8 张量反量化为 BF16 并缓存为 ltxv-2b-0.9.8-distilled-fp8-bf16.safetensors(fp8→bf16 精度损失 可接受,cos_sim 对比见下文)。
  3. 文本编码器:T5-XXL(d_model=4096,与 transformer caption_channels=4096 匹配)。 使用 Lightricks/T5-XXL-8bit(T5EncoderModel 专用,8bit 分片,共 5.9GB);其 config 带 bitsandbytes quantization_config,本环境无 bitsandbytes,加载时移除该字段并 按 bf16 载入(8bit→bf16 反量化)。tokenizer 从 google/t5-v1_1-xxl 取 spiece.model(T5Tokenizer,slow)。
  4. Scheduler:取上游 scheduler/scheduler_config.json 的权威配置 (FlowMatchEulerDiscreteScheduler,shift=1.0 / base_shift=0.95 / max_shift=2.05, use_dynamic_shifting)。distilled 版本 8 步采样,guidance_scale=1.0(无需 CFG, negative_prompt=None)。
  5. 验证规模:512×320 × 3 帧 × 8 步,固定 seed=42,同一 prompt 下 CPU 与 NPU 各跑 一次,对比输出 latent 与首帧像素。

目录结构

.
├── inference.py                 # 推理脚本(--device npu|cpu|both)
├── readme.md
├── requirements.txt
├── .gitignore                   # 忽略 models/
├── ltx-config/                  # diffusers 格式配置(单文件加载用,仓库内)
│   ├── transformer/config.json
│   └── vae/config.json
├── models/                      # 本地权重(不入库)
│   ├── ltxv-2b-0.9.8-distilled-fp8.safetensors     # 4.5G(BF16+FP8 混合)
│   ├── ltxv-2b-0.9.8-distilled-fp8-bf16.safetensors # 反量化缓存(自动生成)
│   └── t5-xxl-8bit/             # T5-XXL 8bit 编码器 + tokenizer(5.9G)
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    ├── model_result.png
    └── data/                    # CPU/NPU 对比数据(不入库)

部署步骤

# 1. 依赖(Python 3.11;torch_npu 需匹配 CANN 8.5.1)
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 2. 权重(约 10.4G,models/ 已 .gitignore 排除)
mkdir -p models/t5-xxl-8bit
curl -L -C - 'https://hf-mirror.com/Lightricks/LTX-Video/resolve/main/ltxv-2b-0.9.8-distilled-fp8.safetensors' -o models/ltxv-2b-0.9.8-distilled-fp8.safetensors
curl -L -C - 'https://hf-mirror.com/Lightricks/T5-XXL-8bit/resolve/main/model-00001-of-00002.safetensors' -o models/t5-xxl-8bit/model-00001-of-00002.safetensors
curl -L -C - 'https://hf-mirror.com/Lightricks/T5-XXL-8bit/resolve/main/model-00002-of-00002.safetensors' -o models/t5-xxl-8bit/model-00002-of-00002.safetensors
# config.json / index.json / spiece.model / tokenizer_config.json / special_tokens_map.json 一并放入 models/t5-xxl-8bit/

# 3. CPU + NPU 双跑对比
python inference.py --device both

# 4. 单设备跑
python inference.py --device cpu
python inference.py --device npu

推理示例

[NPU] LTX-Video (2B DiT distilled) forward
  prompt        : a serene mountain lake at sunrise, light mist over the water
  config        : 512x320 x 3帧 x 8步, guidance=1.0, seed=42
  latent(denoise): (1, 160, 128)  [B, seq, C]
  frame0        : (512, 320, 3)(NPU VAE 解码)
  inference time : 540.12 ms(去噪,median of 1,warmup 已剔除)
  max_memory    : 15403.5 MB

CPU vs NPU 精度对比

指标值
cos_sim(去噪 latent,flatten)0.98194456
max_abs_err(latent)1.41220069
mse(latent)8.59e-02
latent shape(1, 160, 128)
CPU 去噪耗时125670 ms
NPU 去噪耗时540 ms
加速比(CPU / NPU)232.67x
frame0(NPU VAE 解码)(512, 320, 3)

说明:CPU 侧因 BF16 VAE 解码过慢(单核 conv3d >20 分钟),精度对比基于去噪 latent (CPU/NPU 共用同一固定初始噪声,seed=42),NPU 侧额外完成 VAE 解码用于画面可视化 (见 assets/model_result.png ④)。

可视化详见 assets/model_result.png(① 模型信息卡 ② CPU vs NPU 耗时柱状+加速比 ③ 精度表 ④ 生成首帧画面(imshow)与 CPU/NPU 对比)。

CPU 参照策略(实际执行)

CPU 上 2B DiT 的 8 步去噪约 126 秒(512×320×3帧,12.5s/步);但 BF16 VAE 解码在 CPU 单核卷积上极慢(>20 分钟未完成),因此本适配实际执行:

  • 计时与精度对比只统计去噪 latent(两端共用同一固定初始噪声,seed=42);
  • VAE 解码仅在 NPU 侧执行一次,用于首帧画面可视化(出图)。

若 CPU 去噪超过 40 分钟,可改用更小分辨率(如 256×160×3 帧)或更少步数;本适配 512×320×3帧×8步 实测 CPU 去噪 125.7s,在预算内。

故障处理

  • bitsandbytes 相关报错:T5-XXL-8bit 的 config 含 quantization_config,inference.py 已自动移除并按 bf16 加载。
  • FP8 张量加载报错:inference.py 自动做 fp8→bf16 一次性转换并缓存。
  • 网络下载中断:所有下载命令均使用 curl -L -C - 断点续传。