本文档记录 VTP-Large-f16d64(MiniMax 视觉分词器预训练模型)在华为昇腾 Ascend910B4 上的适配、推理与精度性能验证结果。
VTP(Visual Tokenizer Pre-training)是一个统一视觉分词器预训练框架,联合优化图像-文本对比学习、自监督学习和重建损失。该模型支持三种使用模式:
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch_npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
torchvision | 0.24.0 |
vtp | 源码安装(GitHub MiniMax-AI/VTP) |
1 逻辑卡 Ascend910B4/opt/atomgit/VTP-Large-f16d648.5.1# 使用华为源加速
pip install -i https://repo.huaweicloud.com/repository/pypi/simple \
torch torchvision torch_npu transformers \
omegaconf tqdm timm scipy pytorch_fid pillowgit clone https://github.com/MiniMax-AI/VTP.git --depth 1推理时将 VTP 源码路径加入 sys.path(inference.py 已通过 ../VTP 相对路径自动处理)。
# 方式一:HuggingFace
git lfs clone https://huggingface.co/MiniMaxAI/VTP-Large-f16d64
# 方式二:HuggingFace 镜像(国内推荐)
git lfs clone https://hf-mirror.com/MiniMaxAI/VTP-Large-f16d64
# 方式三:ModelScope
git lfs clone https://modelscope.cn/models/MiniMaxAI/VTP-Large-f16d64cd /opt/atomgit/VTP-Large-f16d64
# 全部模式(重建 + CLIP + 特征提取)
python3 inference.py --mode all
# 仅重建
python3 inference.py --mode reconstruction
# 仅 CLIP Zero-shot
python3 inference.py --mode clip
# 仅特征提取
python3 inference.py --mode feature
# 指定自定义图像
python3 inference.py --mode all --image /path/to/your/image.png基础检查:
测试条件:单张 256×256 图像输入,单卡 Ascend910B4,Warm-up 后取 3 次平均。
| 模式 | 平均耗时 | 输出 |
|---|---|---|
| Reconstruction (Encode+Decode) | 0.0991s | Latent [1, 64, 16, 16] → Image [1, 3, 256, 256] |
| CLIP Zero-shot | 0.0587s | Image Feature [1, 768], Text Probs [1, 4] |
| SSL Feature Extraction | 0.0587s | Last Layer + 4 Intermediate Layers |
使用本目录下的 accuracy_run_perf.py 对 NPU 输出与 CPU 输出进行逐层对比,计算相对误差(百分比)。
运行命令:
cd /opt/atomgit/VTP-Large-f16d64/VTP-Large-f16d64-ascend
python3 accuracy_run_perf.py --model_dir /opt/atomgit/VTP-Large-f16d64 --warmup_iters 2 --bench_iters 10| 评测项 | 最大相对误差 | 说明 |
|---|---|---|
| Reconstruction Latent | 6.105% | 内部瓶颈层编码精度差异,不影响最终重建 |
| Reconstruction Image | 0.798% ✅ | 关键输出:重建图像误差 < 1%,PSNR = 50.76 dB |
| CLIP Image Feature | 0.262% ✅ | 图像特征对齐良好 |
| CLIP Text Probs | 0.001% ✅ | 分类概率几乎一致 |
| SSL Last Layer CLS Token | 0.226% ✅ | CLS 特征对齐良好 |
| SSL Last Layer Patch Tokens | 6.105% | patch_tokens 与 latent 路径相同,受瓶颈层影响 |
| SSL Intermediate Features | 7.424% | 中间层特征受 NPU 算子差异影响较大 |
| 最终输出最大误差 | 0.798% ✅ | 重建图像、CLIP 特征、CLS Token 均 < 1% |
torch.device("npu:0") 替代 torch.device("cuda:0")。torch.cuda.amp.autocast() 改为 torch.autocast(device_type="npu")。vtp 未注册到 transformers 的 AutoConfig,需直接使用 VTPConfig.from_pretrained() + VTPModel.from_pretrained()。sys.path.insert(0, VTP_SRC) 导入。inference.py)单文件推理脚本,支持三种模式,参数说明:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--model_dir | str | 当前目录 | 模型权重路径 |
--image | str | dog.png | 输入图像路径 |
--mode | str | all | 推理模式 (all/reconstruction/clip/feature) |
--output_dir | str | ./output | 输出目录 |
accuracy_run_perf.py)自动完成 NPU vs CPU 精度对比和 NPU 性能基准测试,参数说明:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--model_dir | str | /opt/atomgit/VTP-Large-f16d64 | 模型权重路径 |
--image | str | dog.png | 输入图像路径 |
--output_dir | str | ./output_eval | 评测输出目录 |
--warmup_iters | int | 2 | Warm-up 迭代次数 |
--bench_iters | int | 10 | Benchmark 迭代次数 |
config.json 和 model.safetensors 需在同一目录。vtp/ 目录),inference.py 通过相对路径 ../VTP 引用,若 VTP 源码在其他位置需修改 VTP_SRC 变量。torch.autocast 在 Ascend NPU 上使用 device_type="npu"。若遇到精度问题可设置 torch.npu.config.allow_internal_format = False。