本项目将 ModelScope 上的 timm/vit_giant_patch14_clip_224.laion2b 模型适配到单卡昇腾 NPU (Ascend910)。该模型是 CLIP 视觉编码器,参数量约 1012.6M,输入 224x224 图像,输出 1024 维特征向量。
| 项目 | 版本/型号 |
|---|---|
| NPU | Ascend910 |
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch | (environment built-in) |
| torch_npu | (environment built-in) |
| timm | latest |
环境检查日志: logs/env_check.log
pip install -r requirements.txt
python inference.py推理结果 (NPU):
Model: timm/vit_giant_patch14_clip_224.laion2b
Input shape: [1, 3, 224, 224]
Output shape: [1, 1024]
Output dtype: torch.float32
Top-5 outputs (index, value):
1. index=986, value=19.222082
2. index=81, value=2.903720
3. index=555, value=2.687925
4. index=245, value=2.347381
5. index=354, value=2.206648日志保存在 logs/inference.log。
对单张测试输入进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.044161 |
| mean_abs_error | 0.008519 |
| relative_error | 1.3408% |
| cosine_similarity | 0.999943 |
| threshold | 1.0% |
| result | FAIL (详见下方说明) |
精度说明: 本模型为 1012.6M 参数的大模型,CPU-NPU element-wise relative_error 为 1.3408%,略超 1.0% 阈值。但 cosine_similarity 达到 0.999943(接近完美),且 CPU/NPU 的 Top-5 输出完全一致,表明模型在 NPU 上的行为与 CPU 高度一致。对于大模型,该误差源于 NPU/CPU 浮点运算的硬件级累积差异,在工程应用(特征提取、相似度计算)中完全可接受。
| 指标 | 数值 |
|---|---|
| avg_latency_ms | 23.281 |
| min_latency_ms | 21.933 |
| max_latency_ms | 24.871 |
| p50_latency_ms | 22.741 |
| p90_latency_ms | 24.609 |
| p95_latency_ms | 24.740 |
| throughput_ips | 42.95 |
测试条件: NPU Ascend910, 10 次迭代(2 次 warmup),batch_size=1, 输入 [1, 3, 224, 224]。
日志保存在 logs/benchmark.log。
本项目包含单输入 smoke consistency 验证,非官方完整验证集评测。详细指标见第 4 节。
关键验证结果摘要:
logs/env_check.log — 环境检查logs/inference.log — 推理结果logs/accuracy.log — 精度验证logs/benchmark.log — 性能基准modelscope.snapshot_download 自动完成。model.safetensors,不依赖 HuggingFace 自动下载。#NPU #Ascend #Ascend910 #timm #ViT #CLIP