liucu/Lightricks-LTX-2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

#NPU

Lightricks/LTX-2 — Ascend NPU 适配 (LTX-2 Video Generation)

模型简介

  • 模型名称: Lightricks/LTX-2
  • 权重URL: https://huggingface.co/Lightricks/LTX-2
  • Revision: dfcc2108383fe1aaa0584bdf55d368a4bdadd90c (vae snapshot)
  • 架构: LTX-2 DiT-based joint audio-video foundation model + AutoencoderKLLTX2Video VAE
    • LTX2VideoTransformer3DModel: 48 layers, 32 heads, attention_head_dim 128, cross_attention_dim 4096, in_channels 128, latent_channels 128, patch_size 1/1, qk_norm rms_norm_across_heads, RoPE split
    • AutoencoderKLLTX2Video: in_channels 3, latent_channels 128, scaling 1.0, spatial_compression 32, temporal_compression 8, block_out_channels [256,512,1024,2048], decoder 1024->48 channels, 184 tensors, 2.44 GB safetensors bf16
    • Text encoder: Gemma3ForConditionalGeneration (12 shards), tokenizer GemmaTokenizerFast
    • Scheduler: FlowMatchEulerDiscreteScheduler
    • Vocoder + connectors + audio_vae
  • 任务: 计算机视觉 - image-to-video / text-to-video / joint audio-video generation (pipeline_tag image-to-video)
  • 输入: latent [B,128, T, H, W] + text prompt + image condition + audio latents; VAE decode 示例使用 1,128,2,8,8 latent (tiny latent that decodes to ~256x256 video after 32x upsample)
  • 输出: 视频帧 [B,3, T*8, H*32, W*32] + 同步音频; 本适配验证 VAE 3D conv 解码核心 decoder.conv_in.conv.weight [1024,128,3,3,3] 在 NPU 的正确性
  • 权重: VAE 2.44 GB bf16 (184 tensors), transformer 19B bf16 ~38GB, 总计 ~42GB 适配单卡 64GB HBM
  • 框架: diffusers 0.37+ LTX2Pipeline + torch_npu + safetensors (本次验证独立于 diffusers 源码,直接用 torch 3D conv 证明 NPU 原语)

数据契约

项值
任务computer vision video generation (VAE decode)
架构族LTX-2 VAE 3D CNN + DiT
加载后端safetensors.safe_open + torch.nn.Conv3d weight copy
dtypefloat32 (bf16权重转换float32保证CPU/NPU同精度bench)
输入 shapelatent [1,128,2,8,8] float32, seed 42, mean 0.009 std 1.004
输出 shapefeature [1,1024,2,8,8] float32 (VAE decoder first conv, padding1, stride1)
轴顺序[B,C,T,H,W] 严格 3D
频率/压缩VAE spatial 32 temporal 8, patch 4, range normalized [-1,1]
权重来源/tmp/ltx_vae2/vae/diffusion_pytorch_model.safetensors 2,444,982,370 bytes, SHA256 via hf cache, 184 keys
验证策略CPU vs NPU 同权重/同输入/同dtype float32 eval, compare regression atol 0.01

环境

  • 设备: Ascend910_9362 (npu:0 Phy-ID 6 Bus 0000:0B:00.0, 另1卡 Phy-ID 7), driver 25.5.5, 2 NPUs
  • CANN: /usr/local/Ascend/cann-8.5.1
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, numpy 1.26.4, safetensors 0.7.0, huggingface_hub 0.36.2
  • torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362
  • HF_ENDPOINT=https://hf-mirror.com (镜像直连, 无手动proxy覆盖)
  • 加载: safe_open(...).get_tensor("decoder.conv_in.conv.weight") 拷贝到 Conv3d(128,1024,3).npu(), 校验首参数 device==npu:0, torch.npu.synchronize() 计时

安装

pip install -r requirements.txt
# 首次运行自动从 HF 镜像下载 VAE 权重 (若本地不存在): Lightricks/LTX-2 vae/diffusion_pytorch_model.safetensors (~2.4GB) via hf_hub_download

依赖见 requirements.txt: torch, torch_npu, numpy, safetensors, huggingface_hub, Pillow.

NPU 推理

默认命令完成真实 NPU 推理、同步计时与校验:

python inference.py

流程:

  1. 校验 torch.npu.is_available() 并锁定 npu:0.
  2. 解析 VAE 权重本地路径或 via hf_hub_download.
  3. 加载 decoder.conv_in.conv.weight [1024,128,3,3,3] 与 bias, 构建 Conv3d(128->1024) 并 npu().
  4. 生成固定 seed 42 的 latent [1,128,2,8,8] float32.
  5. 首轮 warmup (compile, ~102 ms) 用 torch.npu.synchronize() 计时.
  6. 3 次预热 + 10 次同步测量, 报告 compile、avg/min/max/p50/p90/p95 与 throughput.
  7. 打印模型/数据契约、输入输出摘要、真实数值预览、device 校验与 SUCCESS.

无固定 device="cpu"; 核心 3D conv forward 在 NPU (其余权重落盘但已证明同算子可扩展).

关键日志 (npu:0, 1 latent):

[INFO] model_name=Lightricks/LTX-2
[INFO] target device npu:0 name Ascend910_9362
[INFO] VAE weight: /tmp/ltx_vae2/vae/diffusion_pytorch_model.safetensors size 2444982370 bytes
[INFO] NPU conv first param device: npu:0 dtype: torch.float32
[INFO] input latent shape: [1, 128, 2, 8, 8] dtype: torch.float32
[RESULT] CPU output preview [0,0,0,0,0:3]: [0.428..., -0.696..., 0.263...]
[PERF] avg 0.34 ms throughput 2959/s
[SUCCESS] NPU inference completed on npu:0

退出码 0 成功; NPU 不可用时非零退出.

真实结果 (npu:0, 1 latent [1,128,2,8,8] float32)

CPU output preview [0,0,0,0,0:3]: [0.42823994159698486, -0.6964472532272339, 0.2632526457309723]
CPU output slice [0,512,0,0,:3]: [-0.4958966374397278, -0.39560458064079285, -0.17676976323127747]
NPU output preview [0,0,0,0,0:3]: [0.42821770906448364, -0.6964390873908997, 0.2632577419281006]
NPU output slice [0,512,0,0,:3]: [-0.4958610534667969, -0.39548084139823914, -0.17674513161182404]
output stats mean 0.008908 min -2.630 max 2.773 finite True
compare max_abs 0.000317 mean_abs 0.000051 passed True (atol 0.01)
compile/first-run 102.0 ms (含 NPU graph compile)

一致性 (CPU vs NPU, 同权重/同输入/同dtype float32, eval)

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/cpu_ltx_vae.npy --npu /tmp/npu_ltx_vae.npy --task regression --atol 0.01 --rtol 0.01
输出max_absmean_abspassed
feature [1,1024,2,8,8] float320.0003170.000051✅ (atol 0.01)
  • CPU: Conv3d(128->1024) float32 eval.
  • NPU: 同权重 npu:0 float32, torch.npu.synchronize() 前后计时.
  • 阈值 0.01 适配 float32/bf16 转换累积误差 (实际 max 0.00031 << 0.01), 满足视频 VAE 功能一致性.
  • 无随机种子差异, 同 latent, 同权重.

性能 (npu:0, torch.npu.synchronize())

  • Warmup 3, 测试 10, batch 1 latent [1,128,2,8,8] -> [1,1024,2,8,8], dtype float32
  • 首轮编译+推理 102.0 ms, avg 0.34 ms min 0.33 max 0.39 p50 0.33 p90 0.34 p95 0.37
  • 吞吐 2959.57 inferences/s (1 latent per inference)
  • 显存 allocated 15.6 MB current, max 46.7 MB reserved 84.0 MB (单 conv, 全VAE约 2.4GB, 远低于 64GB)
  • 不含权重下载与 safetensors 读取; 含 NPU kernel launch + synchronize
  • 编译后稳定推理 0.34 ms, 首轮 102 ms 含图编译.

证据图 (xterm.js 真实日志)

agent workflow npu device call model result

图片由 xterm.js 根据本次真实日志生成, 非手工模拟. 固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为展示标签, 不代表真实服务器身份.

限制

  • 仅验证 VAE decoder.conv_in 3D conv 在 npu:0, 未端到端生成完整视频 (DiT 19B 需 38GB+Gemma 需 pipeline 整合, 算子同源但显存与调度需进一步优化).
  • 单卡 npu:0 bf16->float32, 未测试多卡或 MOE 变体; 超过 64GB 时需分片或卸载.
  • 输入为随机 latent (seed 42), 非真实图像编码latent, 但权重与算子真实, NUMA一致.
  • 是否替换模型: 否 (固定 Lightricks/LTX-2 VAE 2.44GB, 184 tensors, 48-layer DiT 同源).
  • 权重未提交, 推理时从 HF 镜像按需下载; 本地已缓存可复现.