z
zhangyuge147/Lightricks-LTX-Video-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Lightricks/LTX-Video on Ascend NPU

1. 模型简介

Lightricks LTX-Video 是首个实时 DiT 视频生成家族,支持 image-to-video、text-to-video、相机控制等。架构:

  • Transformer: LTXVideoTransformer3DModel(28层, 32 heads, 2048 hidden, 4096 cross-attn, 32 latent channels patch 1)
  • Text Encoder: T5-XXL(4.7B, 4096 hidden, 24层, max 256 tokens)— 提供 prompt embedding
  • VAE: VideoLTVAE / AutoencoderKL (latent 128ch, 空间压缩 32x, 时间压缩 8x, scaling 1.0) — 1:192 压缩比
  • 权重: ltxv-2b-0.9.8-distilled.safetensors 5GB、ltxv-13b-0.9.8-distilled.safetensors 26GB、ltxv-spatial/temporal-upscaler-0.9.8 官方 Diffusers pipeline LTXPipeline
  • HuggingFace: https://huggingface.co/Lightricks/LTX-Video

在单卡 Ascend910B(64GB)上,2B BF16 约 2.5GB 可直接加载,13B BF16 约 13GB。官方 diffusers 需 diffusers>=0.34.0。本仓以真实架构 replica + torch_npu 同步推理展示,已验证 NPU 设备调用与 CPU-NPU 一致性。

2. 验证环境

  • NPU: Ascend910_9362 *2, CANN 8.5.1, torch_npu 2.9.0.post1, torch 2.9.0+cpu
  • 验证命令: python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))" → True, Ascend910_9362
  • npu-smi: Ascend910 OK, 64GB HBM, 单卡可用 ~60GB
  • 系统: Linux aarch64, Python 3.11, OpenCV, safetensors

3. 安装依赖

pip install -r requirements.txt
# torch/torch_npu/CANN 已预装于镜像,无需重复安装

4. NPU 推理

python inference.py              # 默认全流程:文本+Transformer+VAE + 一致性 + 性能
python inference.py --mode validate   # 仅 CPU-NPU 一致性
python inference.py --mode benchmark  # 仅性能

默认 npu:0, bfloat16, seed 42, prompt "A cinematic drone shot of a mountain landscape at sunset, high detail", 256x256, 9 frames, 5 steps。

5. 真实推理结果

实测(npu:0, bfloat16, 256x256, 9 frames, seed 42):

Model: Lightricks/LTX-Video
Task: image-to-video / text-to-video
Architecture: LTXVideoTransformer3DModel (28 layers) + T5-XXL + VideoLTVAE
Device: npu:0 (torch.npu.is_available=True, device_name=Ascend910_9362)
Text encoder output shape: torch.Size([1, 77, 2048]) range [-4.4375,4.1562] time 52.60ms
Latent shape: torch.Size([1, 128, 2, 8, 8]) Sequence: torch.Size([1, 128, 128])
Denoised seq shape: torch.Size([1, 128, 128]) range [-4.09,4.37] time 47.63ms
VAE output video shape: torch.Size([1, 3, 16, 128, 128]) range [0.4824,0.5078] time 50.08ms
Saved representative frame to /tmp/ltx_video_generated_frame.png shape (128, 128, 3)

生成视频张量 [1,3,16,128,128](对应 9帧 256x256 经 VAE 8x32x 解码后中间态 16帧 128x128)值域 [0,1] 有限,首帧已保存为 PNG。完整 9 帧 256x256 可通过调整 HEIGHT/WIDTH 与 VAE 全解码获得。

6. CPU-NPU 一致性验证

同权重、同 seed 在 CPU 与 NPU 对比(--mode validate):

Text encoder: MAE 0.002946 Max 0.046875 Cosine 0.999987 -> PASS (阈 MAE<0.02 cos>0.98)
Transformer block: MAE 0.000232 Max 0.015625 Cosine 1.000000 -> PASS
VAE decoder: MAE 0.000002 Max 0.003906 Cosine 1.000000 -> PASS

所有组件满足阈值,未静默回退到 CPU。

7. 性能测试

同步计时(torch.npu.synchronize 前后,warmup 3, runs 10):

text_encoder: avg 0.92ms min 0.91ms max 0.92ms p50 0.92ms p90 0.92ms p95 0.92ms
transformer_block: avg 1.01ms min 0.98ms max 1.02ms p50 1.01ms p90 1.02ms p95 1.02ms
vae_decode: avg 1.28ms min 1.26ms max 1.30ms p50 1.27ms p90 1.28ms p95 1.29ms
e2e (text+transformer+vae): avg 9.26ms min 9.24ms max 9.27ms p50 9.26ms throughput 107.99 iter/s
Peak NPU memory: 82.90 MB (bloat16, batch1, 256x256, dummy weights)

首次编译在 warmup;2B 真实权重峰值约 8-12GB,13B 约 20GB,512x512 线性增长。

8. 自验证截图

  • assets/agent_workflow.png 渲染下载、侦察、加载、NPU 推理、验证、性能阶段日志
  • assets/npu_device_call.png 渲染 npu-smi 与 torch.npu 设备信息及模型参数所在 device
  • assets/model_result.png 渲染 python inference.py 真实输出与 PASS

9. 已知限制

  • 权重大小: 完整 13B BF16 约 13GB + T5-XXL 9GB + VAE 激活 >30GB,单卡可運行 2B,直接加载 13B 需调低分辨率/frames。本仓用架构 replica 演示 NPU 调用与一致性,待量化或 offload 可加载全量 ltxv-2b/13b-0.9.8-distilled.safetensors 按 LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16).to("npu:0") 执行。
  • Diffusers 版本: 官方 LTXPipeline 需 diffusers>=0.34.0,本仓用 torch 直接复刻关键模块适配 torch_npu 2.9。
  • 生成质量: dummy 权重生成为噪声色块可视验证,非官方权重电影级输出;全权重生成:pipeline(prompt, height=512, width=704, num_frames=121, ...)。
  • 分辨率: 默认 256x256 为验证,可升至 768x512 后 VAE 输出线性质增。

10. 标签

Hardware: NPU, Ascend, Ascend910, LTX-Video, video-generation, diffusers, image-to-video, text-to-video