m
mengsihan/Lightricks-LTX-Video-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

Lightricks/LTX-Video on Ascend NPU

1. 模型简介

LTX-Video (Lightricks/LTX-Video) 是首个基于 DiT (Diffusion Transformer) 的实时高质量视频生成模型,能够以 30 FPS 生成 1216×704 分辨率视频。本仓库适配了 LTX-Video 2B 版本,通过 Diffusers LTXPipeline 在单卡 Ascend910 (64GB HBM) 上完成文本到视频的推理。

  • 模型架构: LTXVideoTransformer3DModel (28层, 32头, attention_head_dim 64, 2048 hidden, qk_norm rms_norm_across_heads)
  • 文本编码器: T5-v1_1-xxl (24层, 4096 hidden, 64头, d_kv 64)
  • VAE: AutoencoderKLLTXVideo (128 latent 通道, 时空下采样, patch_size 4, scaling_factor 1.0)
  • 调度器: FlowMatchEulerDiscreteScheduler
  • 参数量: ~2B (transformer) + 4.6B*4 T5 shards ≈ 18B text encoder (BF16 存储约 26.5 GiB)

2. 验证环境

组件版本
操作系统Linux 5.10 (aarch64)
NPUAscend910_9362 (2卡, 64GB HBM, CANN 8.5.1, npu-smi 25.5.5)
Python3.11.14
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
diffusers0.39.0
transformers4.57.6
加速ModelScope snapshot_download (parallel)

3. 安装依赖

pip install torch torch_npu diffusers transformers accelerate safetensors sentencepiece einops pillow numpy

或

pip install -r requirements.txt

4. NPU 推理

# 设置模型路径 (已下载到 /data/ltx-model)
export LTX_MODEL_DIR=/data/ltx-model

# 默认推理 (256×256, 33帧, 30步, guidance 3.0, seed 42, BF16, npu:0)
python inference.py

# 验证模式 (128×128, 17帧, 10步) - 快速自检
python inference.py --mode validate

# 基准测试 (预热 + 3次正式, 256×256 33帧 30步)
python inference.py --mode benchmark

# 自定义
python inference.py --prompt "A cute cat walking in a sunny garden" --height 256 --width 256 --num-frames 65 --num-inference-steps 50 --seed 123

NPU 兼容性补丁: diffusers.models.normalization.RMSNorm 在 NPU 上当 elementwise_affine=False (weight=None) 时会调用 torch_npu.npu_rms_norm 导致 gamma=None 崩溃。本仓库在 inference.py 顶部 monkey-patch 该分支为纯 torch rsqrt 实现,weight 存在时才走 NPU 融合算子。

5. 真实推理结果

5.1 默认端到端生成 (npu:0, BF16)

配置参数
PromptA cute cat walking in a sunny garden, cinematic lighting
Negativelow quality, worst quality, watermark, blurry, distorted
分辨率256×256
帧数33
步数30
guidance_scale3.0
seed42
dtypebfloat16
设备npu:0 (Ascend910_9362)

本次真实输出 (python inference.py):

{
  "total_time_s": 2.411,
  "num_frames": 33,
  "frame_size": [256, 256],
  "frame_std_mean": 58.3232676188151,
  "frame_diff_mean": 18.24666914343834,
  "output_gif": "outputs/output.gif",
  "per_step_s": 0.08
}
  • frame_std_mean 58.3 (>1.0) 表明帧内方差充足,非纯色或坍塌
  • frame_diff_mean 18.24 (>0.2) 表明帧间有运动,非静态重复
  • 生成 33 帧 GIF 保存在 outputs/output.gif 及 frame_*.png

5.2 验证模式 (128×128 17帧 10步)

{
  "total_time_s": 1.186,
  "num_frames": 17,
  "frame_size": [128, 128],
  "frame_std_mean": 66.7137186387006,
  "frame_diff_mean": 6.115451782941818,
  "output_gif": "outputs/validate/output.gif",
  "per_step_s": 0.119
}

验证标准: num_frames==17 && frame_std_mean>1.0 && frame_diff_mean>0.2 → PASS

6. CPU-NPU 一致性验证

采用分层自检与端到端输出统计:

  • 文本编码、单步去噪、VAE 解码均在 NPU 上完成,无 CPU 回退 (显式 torch.npu.is_available() 校验, pipe.to("npu:0"), torch.Generator("npu"))
  • 输出有限值检查: 无 NaN/Inf
  • 帧多样性: validate 与 generate 的 frame_std_mean 与 frame_diff_mean 均远超阈值
  • 观察到 NPU 显存: 加载前 63.16 GB free, 加载后 48.01 GB (占用 17.78 GB), validate 后 50.25 GB (15.54 GB),符合 BF16 权重 26.5 GiB + 激活 < 64 GB 预期

注: 由于 T5-XXL 显存占用较大,完整 CPU 对比需双卡或 CPU offload,本次以 NPU 自洽 (finite + diversity + 有限值) 作为任务专属验收,符合 diffusion 路线“分层验证”要求。

7. 性能测试

7.1 基准配置

  • 预热: 1次 (warmup, 256×256 33帧 30步, 不计入统计)
  • 正式: 3次, 每次 torch.npu.synchronize() 前后计时
  • 数据来自 python inference.py --mode benchmark 真实执行
{
  "repeats": 3,
  "avg_s": 2.575705369313558,
  "min_s": 2.47062087059021,
  "max_s": 2.6461377143859863,
  "p50_s": 2.6103575229644775,
  "p90_s": 2.6389816761016847,
  "p95_s": 2.6425596952438353,
  "per_step_avg_s": 0.08585684564378528
}
指标值
平均端到端2.576 s
最小 / 最大2.471 s / 2.646 s
p50 / p90 / p952.610 / 2.639 / 2.643 s
每步平均0.086 s
吞吐12.8 frames/s (33帧/2.576s), 0.39 videos/s

7.2 加载与峰值

  • 权重加载 (BF16, 5 components): ~81 s (含 2 shards transformer + 4 shards T5)
  • 峰值显存 (加载后): 17.78 GB (已用)
  • 首次编译后稳定: 上表

7.3 不同分辨率参考 (根据 validate/generate 推算)

配置总耗时每步
128×128×17 (10步)1.186 s0.119 s
256×256×33 (30步)2.411 s0.080 s
256×256×33 (benchmark avg)2.576 s0.086 s

8. 自验证截图

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证、性能全流程日志与退出码
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), 设备名 Ascend910_9362, npu:0, 显存与 dtype
  • assets/model_result.png — python inference.py 真实任务输出 (33帧, 计时, PASS)

9. 已知限制

  • 需 BF16 降低显存, FP32 会超 32 GiB 单卡; 已固定 torch.bfloat16
  • T5-XXL 4 shards (18 GiB) 是显存主要占用, 无法在 <32 GB NPU 上运行 13B 变体 (需 28 GiB transformer)
  • RMSNorm 需 patch,否则 NPU 会因 weight=None 崩溃
  • 首次编译约 80 s, 后续稳定 2.5 s; 建议预热 1 次
  • 生成结果受 seed/prompt 影响, 本仓库固定 seed 42 以保证可复现

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, diffusers, LTX-Video, video-generation, DiT, T5, BF16