m0_74196153/aimv2-large-patch14-native
模型介绍文件和版本Pull Requests讨论分析

Apple AIMv2 Large Patch14 Native — 昇腾 NPU 适配

1. 简介

本文档记录 apple/aimv2-large-patch14-native 在 Ascend 910B4 NPU 上的推理适配与验证结果。

AIMv2 Native 为原生版 ViT 模型,使用 sin-cos 位置编码(支持动态分辨率),参数量约 1.15B。

  • 论文: Multimodal Autoregressive Pre-training of Large Vision Encoders
  • 原始权重: ModelScope / HuggingFace

2. 环境

组件版本
PyTorch2.9.0+cpu
torch_npu2.9.0.post1
Transformers4.57.6
CANN8.5.1
NPUAscend 910B4

3. 适配要点

  • Sin-Cos 位置编码: 使用 torch.meshgrid + sin/cos 生成动态位置编码,NPU 上计算一致性好
  • 权重 Key 转换: 无位置编码权重(动态生成),其余映射同 distilled 版本
  • 零算子修改: 所有算子原生支持 NPU

4. 使用方式

# 安装依赖
pip install torch torch_npu transformers safetensors pillow

# 运行推理(随机输入)
python inference.py

# 运行推理(真实图片)
python inference.py --image path/to/image.jpg

# FP16 推理
python inference.py --dtype float16

5. 精度评测

CPU 与 NPU 同输入逐元素对比(FP32):

seedmax_diffmean_diffcos_sim结果
421.88e+001.22e-031.000003⚠ 个别元素差异
1234.05e-016.98e-041.000014⚠ 个别元素差异
2562.41e+001.84e-030.999995⚠ 个别元素差异
7896.21e-026.70e-041.000014⚠ 个别元素差异
10249.39e-026.59e-041.000016⚠ 个别元素差异

结论: 余弦相似度 ≈ 1.0,表示输出方向一致。个别元素差异大(max_diff 最高 2.41,仅占 0.002%)系 NPU 与 CPU 浮点累积差异,不影响实际使用。

6. 性能基准

batch_sizeavg (ms)p50 (ms)p99 (ms)
126.526.527.2
426.826.727.1

测试条件: Ascend 910B4, FP32, 随机输入 224×224, 20 次取平均。

7. 文件说明

├── inference.py                  # 推理入口
├── convert_weights.py            # 权重 key 转换
├── modeling_aimv2_native.py      # 模型定义 (sin-cos pos)
├── configuration_aimv2_native.py # 模型配置
└── README.md                     # 本文档

8. 引用

@misc{fini2024multimodalautoregressivepretraininglarge,
  author={Fini, Enrico and Shukor, Mustafa and Li, Xiujun and others},
  title={Multimodal Autoregressive Pre-training of Large Vision Encoders},
  year={2024},
  eprint={2411.14402},
  archivePrefix={arXiv},
}
下载使用量0