上游仓库 Lightricks/LTX-Video 2B DiT 蒸馏版(distilled,8 步采样)文本生成视频模型,在昇腾 Ascend 910 NPU 上完成 CPU / NPU 双跑推理验证。
Lightricks/LTX-Video 仓库只有裸权重 + scheduler/scheduler_config.json
(无 diffusers-format 组件目录),因此采用手动组合管线:
LTXVideoTransformer3DModel + AutoencoderKLLTXVideo + T5EncoderModel + T5TokenizerFast + FlowMatchEulerDiscreteScheduler,全部来自单个 4.5G 权重文件
(含 transformer + VAE + per_channel_statistics,944 张量)。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 对比见下文)。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)。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)。.
├── 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| 指标 | 值 |
|---|---|
| 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 上 2B DiT 的 8 步去噪约 126 秒(512×320×3帧,12.5s/步);但 BF16 VAE 解码在 CPU 单核卷积上极慢(>20 分钟未完成),因此本适配实际执行:
若 CPU 去噪超过 40 分钟,可改用更小分辨率(如 256×160×3 帧)或更少步数;本适配 512×320×3帧×8步 实测 CPU 去噪 125.7s,在预算内。
bitsandbytes 相关报错:T5-XXL-8bit 的 config 含 quantization_config,inference.py
已自动移除并按 bf16 加载。curl -L -C - 断点续传。