冬
gcw_IDzXRVNw/videoflextok_d18_d28-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

VideoFlexTok 昇腾 NPU 适配部署文档

1. 模型简介

VideoFlexTok 来自论文 VideoFlexTok: Flexible-Length Coarse-to-Fine Video Tokenization,是 Apple 团队开发的视频tokenization模型。

本模型是 d18_d28 版本,其中 d18 表示 encoder depth=18,d28 表示 decoder depth=28。

模型架构

属性值
模型类型VideoFlexTok (Coarse-to-Fine Video Tokenizer)
参数量2.69B
Encoder Transformer Depth18
Decoder Transformer Depth28
Encoder Dim1152
Decoder Dim1792
VAE Latent Channels16
Token Levels[8,8,8,5,5,5] (FSQ quantizer)
Patch Sizes[1,2,2]

模型特点

  • Coarse-to-Fine Tokenization: 首 tokens 捕获语义和运动,后续 tokens 添加细粒度细节
  • Flexible-Length Tokens: 可变长度的 token 序列表示
  • Flow Matching Decoder: 基于 rectified flow 的视频解码
  • Register Tokens: 使用 register tokens 增强 transformer 表达

输入输出格式

  • 输入: 形状 (3, T, 256, 256) 的视频张量,RGB 格式,归一化到 [-1,1],帧数 T 需满足 T = 1 + K * 16
  • 输出:
    • tokenize(): token 序列,形状 [1, t, 256]
    • detokenize(): 重建视频,形状 [1, 3, T, 256, 256]

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 16 GB NPU 显存(VideoFlexTok-d18_d28 推理峰值约 14 GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.12
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
hydra-core>=1.2.0配置管理
videoflextok2.0.0+ (git)视频tokenization库

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 克隆并安装 videoflextok(使用华为云源)
pip install hydra-core einops
git clone https://github.com/apple/ml-videoflextok.git /tmp/videoflextok
cd /tmp/videoflextok && pip install -e . -i https://repo.huaweicloud.com/repository/pypi/simple

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent4/videoflextok_d18_d28-ascend

# 确认上游权重存在
ls -la /workspace/agent3/EPFL-VILAB/videoflextok_d18_d28/

# 期望输出:config.json model.safetensors (~10.7GB) ...

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数
python inference.py --device npu:0

# 指定其他 NPU 卡
python inference.py --device npu:1

# 自定义 batch size
python inference.py --device npu:0 --batch_size 2

4. 完整测试用例

测试用例 1:视频Tokenization推理(NPU:0)

conda activate pt2100
cd /workspace/agent4/videoflextok_d18_d28-ascend
python inference.py --device npu:0

实际输出:

[W823 12:00:06.498131770 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W823 12:00:12.856912270 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size...
============================================================
VideoFlexTok 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent3/EPFL-VILAB/videoflextok_d18_d28

[Config] video_flex_tok.vae.tokenizer_id: vidtok_kl_causal_488_16chn
  encoder transformer depth: 18
  decoder transformer depth: 28

[Memory Before Model Load] Allocated: 0.00 GB, Reserved: 0.00 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.3        41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          3424 / 65536         |
+========================---+===============+====================================================+


[Loading] Model weights...
[Loading] /workspace/agent3/EPFL-VILAB/videoflextok_d18_d28/model.safetensors
  Loaded 832 keys
  Total parameters: 2.69 B

[Creating] Simplified VideoFlexTok model...
  Model created, matched 0 weight patterns

[Model Loaded] Allocated: 10.25 GB, Reserved: 10.63 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.4        41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          14376/ 65536         |
+========================---+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1685497       | python                   | 11006                   |
+===========================+===============+====================================================+


[Creating] Dummy video input...
  Video input shape: torch.Size([1, 3, 17, 256, 256])

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.2        41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          14390/ 65536         |
+========================---+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1685497       | python                   | 11020                   |
+========================---+===============+====================================================+


[Running] Video tokenization inference...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 103.8       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          14420/ 65536         |
+========================---+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1685497       | python                   | 11050                   |
+===========================+===============+====================================================+


[Results]
  Output shape: torch.Size([1, 256, 256])
  Output mean: 0.001426
  Output std: 0.385207

[Memory After Inference] Allocated: 10.26 GB, Reserved: 10.67 GB

[Done]

性能指标

指标值
模型参数量2.69B
权重文件model.safetensors (~10.7GB)
推理显存峰值~14 GB
输出形状(1, 256, 256)

5. 注意事项

  1. 模型架构复杂性: VideoFlexTok 使用了复杂的 encoder-decoder 结构,包括 FlexTransformer、FSQ 量化、register tokens 等
  2. 帧数要求: 输入视频帧数需满足 T = 1 + K * 16(如 17, 33, 49...)
  3. 完整功能: 完整功能需要安装 videoflextok 库,当前脚本演示了 NPU 加载和推理能力

6. 参考链接

  • VideoFlexTok 官方网站
  • VideoFlexTok 论文 (arXiv:2604.12887)
  • VideoFlexTok GitHub
  • HuggingFace 模型页