PE-Lang-L14-448(Perception Encoder Lang L/14 - 448px)是一个基于 ViT-L/14 架构的图像特征提取模型,输入分辨率为 448×448,产生 1024 维的 CLS token 嵌入向量。本仓库记录了该模型在华为昇腾 NPU(Ascend 910B4)上的适配、推理验证和精度评测结果。
模型架构要点:
相关获取地址:
| 组件 | 版本 |
|---|---|
PyTorch | 2.9.0 |
torch-npu | 2.9.0.post1 |
numpy | latest |
Pillow | latest |
torchvision | latest |
Ascend 910B4(1 逻辑卡)/opt/atomgit/PE-Lang-L14-448/PE-Lang-L14-448-ascend/opt/atomgit/PE-Lang-L14-448/PE-Lang-L14-448.ptPE-Lang-L14-448-ascend/
├── inference.py # 模型定义 + 权重加载 + 推理入口
├── eval.py # 精度与性能评测脚本
└── log.txt # 评测日志输出cd /opt/atomgit/PE-Lang-L14-448/PE-Lang-L14-448-ascend
# NPU 推理(bf16)
python3 inference.py \
--checkpoint ../PE-Lang-L14-448.pt \
--device npu:0 \
--image-path /path/to/image.jpg权重文件(.pt)的 key 格式为 OpenCLIP 风格:
transformer.resblocks.0.attn.in_proj_weight
transformer.resblocks.0.attn.in_proj_bias
transformer.resblocks.0.mlp.c_fc.weight脚本的 load_custom_weights() 方法自动将其映射为 PyTorch nn.Module 格式:
transformer.0.attn.in_proj.weight
transformer.0.attn.in_proj.bias
transformer.0.mlp.c_fc.weight其中 in_proj_weight / in_proj_bias 被拆分为 in_proj.weight / in_proj.bias 以匹配 nn.Linear 的参数字典。
python3 inference.py \
--checkpoint ../PE-Lang-L14-448.pt \
--device npu:0 \
--batch-size 1 \
--num-iterations 5 \
--num-warmup 3预期输出:
[PE-Lang] Model initialized: 291.42M parameters
[PE-Lang] Output shape: torch.Size([1, 1024]), dtype: torch.bfloat16
[PE-Lang] Inference complete. Embedding shape: torch.Size([1, 1024])测试条件:Ascend 910B4,batch_size=1/2/4,bf16,warmup=10,iterations=50。
| Batch Size | 延迟 (ms/batch) | 吞吐 (img/s) |
|---|---|---|
| 1 | 20.05 | 49.87 |
| 2 | 37.60 | 53.20 |
| 4 | 75.20 | 53.19 |
以 CPU float32 推理结果为参考,对比 NPU bf16 推理结果,共 2 张随机测试图像,逐一计算每条输出(1024 维向量)的余弦相似度和相对误差。
python3 eval.py| 指标 | 数值 | 阈值 | 是否通过 |
|---|---|---|---|
| Cosine similarity (mean) | 0.999983 | - | ✅ |
| Cosine similarity (min) | 0.999981 | - | ✅ |
| Relative error mean | 0.5801% | < 1.0% | ✅ |
| Relative error max | 0.6230% | < 1.0% | ✅ |
| Relative error min | 0.5372% | < 1.0% | ✅ |
| MSE | 0.00032346 | - | - |
| Max absolute error | 0.519245 | - | - |
精度结论:PASSED ✅(相对误差均值 0.58%,最大误差 0.62%,均低于 1% 阈值)
NPU bf16 相比 CPU float32 的微小误差(~0.6%)主要来源于:
# 运行完整精度与性能评测(输出自动写入 log.txt)
python3 eval.py.pt 文件必须包含完整的模型权重(~582MB)。如果文件大小仅为 100+ 字节,说明是 LFS 指针文件,需通过 git lfs pull 或从 HuggingFace / ModelScope 手动下载真实权重文件。eval.py 会在当前目录自动创建 log.txt,若重复运行建议先删除旧日志:rm -f log.txt。kernel_meta/(NPU 算子编译缓存),属于正常现象,可忽略。