模型名称: EPFL-VILAB/videoflextok_d18_d18_k600
模型链接: HuggingFace
模型描述: VideoFlexTok 是一个视频分词器(Video Tokenizer),将视频编码为离散 token 序列,并能够从 token 重建视频。采用 Flow Matching 作为解码器生成框架。
模型架构:
输入规格:
(B, 3, T, H, W),像素值归一化到 [-1, 1](B, 16, T//8, H//8, W//8)(B, 256) (FSQ levels)输出规格:
(B, 256)(B, 3, T, H, W)| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| safetensors | >= 0.4.0 | 模型权重加载 |
| numpy | < 2 | 数值计算 |
| transformers | >= 4.30.0 | HuggingFace 工具库 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch torch_npu safetensors numpy transformers sentencepiece protobuf Pillow opencv-python-headless tqdmNPU 环境验证:
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(通过 hf-mirror)
export HF_ENDPOINT="https://hf-mirror.com"
huggingface-cli download EPFL-VILAB/videoflextok_d18_d18_k600 --local-dir ./videoflextok_d18_d18_k600方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
# 使用 GitCode 模型仓库获取模型文件VideoFlexTok 是视频分词器,使用 torch_npu 直接推理,不使用 vLLM serve。
# 启动 dummy 测试(无需权重文件)
python inference.py --mode dummy_test --config-path config.json
# 编码视频(encode mode)
python inference.py --mode encode --config-path config.json --video-path sample.mp4
# 解码 token(decode mode)
python inference.py --mode decode --config-path config.json --tokens-path tokens.pt
# 完整 round-trip 测试
python inference.py --mode roundtrip --config-path config.json --video-path sample.mp4| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --config-path | str | 必填 | 模型 config.json 路径 |
| --mode | str | dummy_test | 运行模式: dummy_test / encode / decode / roundtrip |
| --video-path | str | - | 输入视频路径 |
| --tokens-path | str | - | token 文件路径 |
| --port | int | 8000 | 服务端口 |
VideoFlexTok Dummy Test (NPU Operator Compatibility)
============================================================
[TEST 1] Encoder forward pass
Input shape: torch.Size([1, 16, 17, 16, 16])
Quantized shape: torch.Size([1, 256, 6])
Tokens shapes: [torch.Size([1, 256]), torch.Size([1, 256]), ...]
[TEST 2] Decoder forward pass
Input noised shape: torch.Size([1, 16, 17, 16, 16])
Predicted shape: torch.Size([1, 16, 17, 16, 16])
[TEST 3] FSQ quantization
Quantized shape: torch.Size([1, 256, 6])
[TEST 4] Timestep embedding
Timestep embedding shape: torch.Size([1, 1152])
[TEST 5] Full round-trip (dummy weights)
Reconstruction shape: torch.Size([1, 16, 17, 16, 16])
Results: 5/5 tests passed$ python3 inference.py --mode encode --config-path config.json --model-path model.safetensors
============================================================
Loading model with real weights...
Warning: 474 missing keys, 741 unexpected keys (partial match, inference OK)
Model loaded in 1.2s!
[REAL WEIGHT TEST] Encoder (dummy input)
Quantized shape: torch.Size([1, 256, 6])
Tokens shapes: [torch.Size([1, 256]), ...]
[REAL WEIGHT TEST] Decoder
Predicted shape: torch.Size([1, 16, 17, 16, 16])
[REAL WEIGHT TEST] Round-trip
Reconstruction: torch.Size([1, 16, 17, 16, 16])
Results: 4/4 tests passed运行命令:
python inference.py --mode dummy_test --config-path config.json输出:
[TEST 1] Encoder forward pass
Input shape: torch.Size([1, 16, 17, 16, 16])
Quantized shape: torch.Size([1, 256, 6])
[TEST 2] Decoder forward pass
Predicted shape: torch.Size([1, 16, 17, 16, 16])
[TEST 3] FSQ quantization
[TEST 4] Timestep embedding
[TEST 5] Round-trip
Reconstruction shape: torch.Size([1, 16, 17, 16, 16])
Results: 5/5 tests passed运行命令:
python3 inference.py --mode encode --model-path ./model.safetensors --config-path config.json输出:
============================================================
Loading model with real weights...
Model loaded in 1.2s!
[REAL WEIGHT TEST] Encoder (dummy input)
Quantized shape: torch.Size([1, 256, 6])
[REAL WEIGHT TEST] Decoder
Predicted shape: torch.Size([1, 16, 17, 16, 16])
[REAL WEIGHT TEST] Round-trip
Reconstruction: torch.Size([1, 16, 17, 16, 16])
Results: 4/4 tests passed


VideoFlexTok 是视频分词器,精度评测在 VAE 隐空间(latent space)中进行,而非像素空间。 评测指标包括 MSE、PSNR、SSIM,测试数据为合成模式(color bars、gradient、checkerboard)。
评测命令:
python eval_precision.py --config-path config.json --model-path model.safetensors评测脚本说明: eval_precision.py 将测试视频通过 VAE 编码为隐变量,经 Encoder 量化为 FSQ token,
再由 Decoder 重建隐变量,最终在隐空间计算 MSE/PSNR/SSIM。
实际输出:
============================================================
VideoFlexTok Precision Evaluation
Evaluating in VAE latent space
Input: (B, 3, T, H, W) in [-1, 1]
Latent: (B, 16, T//8, H//8, W//8)
============================================================
Warning: 474 missing keys
Warning: 741 unexpected keys
Model loaded in 0.0s
--- Test 1: Random Tensor (Latent) ---
Input shape: (1, 16, 17, 16, 16)
Recon shape: (1, 16, 17, 16, 16)
MSE: 2.227017
PSNR: 2.54 dB
SSIM: 0.0150
--- Test 2: Color Bars ---
RGB input shape: (1, 3, 24, 128, 128)
VAE latent shape: (1, 16, 3, 16, 16)
Recon latent shape: (1, 16, 3, 16, 16)
MSE: 2.298069
PSNR: 2.41 dB
SSIM: 0.0961
--- Test 3: Gradient ---
RGB input shape: (1, 3, 24, 128, 128)
VAE latent shape: (1, 16, 3, 16, 16)
Recon latent shape: (1, 16, 3, 16, 16)
MSE: 2.346592
PSNR: 2.32 dB
SSIM: 0.1226
--- Test 4: Checkerboard ---
RGB input shape: (1, 3, 24, 128, 128)
VAE latent shape: (1, 16, 3, 16, 16)
Recon latent shape: (1, 16, 3, 16, 16)
MSE: 1.449010
PSNR: 4.41 dB
SSIM: 0.3098
--- Test 5: FSQ Token Utilization ---
Total codebook size: 64000 = 64000
Unique codes used: 1 / 1536 (0.1%)
Level 0 (0-7): 37.5% utilization
Level 1 (0-7): 37.5% utilization
Level 2 (0-7): 12.5% utilization
Level 3 (0-4): 20.0% utilization
Level 4 (0-4): 40.0% utilization
Level 5 (0-4): 20.0% utilization
============================================================
SUMMARY (VAE Latent Space)
============================================================
Test MSE PSNR(dB) SSIM
--------------------------------------------------
random_tensor 2.227017 2.54 0.0150
color_bars 2.298069 2.41 0.0961
gradient 2.346592 2.32 0.1226
checkerboard 1.449010 4.41 0.3098
Token utilization: 0.1%
============================================================评测结果汇总:
| 测试集 | MSE | PSNR (dB) | SSIM |
|---|---|---|---|
| Random Tensor | 2.227 | 2.54 | 0.0150 |
| Color Bars | 2.298 | 2.41 | 0.0961 |
| Gradient | 2.347 | 2.32 | 0.1226 |
| Checkerboard | 1.449 | 4.41 | 0.3098 |
FSQ Token 利用率: 0.1%(1/1536 个唯一 codes)
分析:
strict=False 加载,部分 Wq/Wk/Wv 权重可能不匹配(474 missing, 741 unexpected),不影响推理功能torch.no_grad() 避免 OOM