2
gcw_BOxvkDDf/videoflextok-d18-d28
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

VideoFlexTok D18 D28 — 昇腾 NPU 适配版

基于 EPFL-VILAB/videoflextok_d18_d28 的昇腾 NPU(torch_npu)适配实现。视频 tokenizer:输入视频帧,输出离散 FSQ token 序列。

该变体为 encoder 深度 18、decoder 深度 28(d18_d28),运行分辨率 256×256,VidTok VAE 潜变量网格 [5, 32, 32],patch [1, 2, 2],输出 token 序列形状 [1, 5, 256](共 1280 个 FSQ token)。

适配要点

  • CUDA 全量移除:代码中无任何 .cuda() / torch.device('cuda'),模型与输入张量全部迁移至 npu:0,并带 try-except NPU 初始化异常捕获。
  • FlexAttention 等价替换:原生 torch.nn.attention.flex_attention 的 ArgSort 内核算子在昇腾 NPU 上不可用,替换为等价的 dense bool-mask + torch.nn.functional.scaled_dot_product_attention 实现(该配置未使用 score_mod,替换无损)。
  • autocast 中性化:torch.amp.autocast('cuda') 全部替换为 'npu'。
  • 所有权重缓存于 ./model_weights/EPFL-VILAB/videoflextok_d18_d28/,优先加载本地缓存,避免重复下载。

使用

命令行推理

python inference.py --input demo_frames/     # 输入帧目录(或 zip / 单张图片)

真实输出示例(npu:0):

device: npu:0 | tokens shape: [1, 5, 256] | total FSQ tokens: 1280

FastAPI 服务

python inference.py --serve    # 监听 0.0.0.0:8000
  • GET /:上传页面(支持 zip / 单张图片 / JSON {"mode":"demo"})
  • POST /predict:返回推理 JSON

环境

  • 华为 Ascend910 × 2,CANN ≥ 8.0
  • PyTorch 2.9.0 + torch_npu
  • Python 3.11
  • 官方源码:https://github.com/apple/ml-videoflextok

验证

  • CLI:python inference.py --input demo_frames/ → exit 0,输出 token 序列 [1, 5, 256](1280 FSQ token,与 config n_max=256 一致)
  • FastAPI:GET / 200,POST /predict(JSON 与 zip 上传)200
  • 截图见 assets/:agent_workflow.png / npu_device_call.png / model_result.png