把 openai/clip-vit-base-patch32 视觉语言双塔模型真实运行在单卡昇腾 910(npu:0)上,完成零样本图像分类推理,并完成 CPU-NPU 一致性验证与性能测试。
openai/clip-vit-base-patch32 (https://huggingface.co/openai/clip-vit-base-patch32)CLIPModel — ViT-B/32 视觉编码器 (12 层, hidden 768, 12 heads, patch 32) + 文本 Transformer (12 层, hidden 512, 8 heads, vocab 49408, context 77) + logit_scale,投影维度 512,约 151M 参数pytorch_model.bin(605 MB, float32),本地快照加载,正式推理不依赖运行时隐式下载3×224×224 float32(CLIP 标准 resize+normalize)(5, 10) int64(padding/truncation)image_embeds (1, 512)、text_embeds (5, 512)(均从 npu:0 显式拷贝回 CPU 记录)logits_per_image (1, 5),softmax 得到候选标签概率与完整排序sample_input.jpg):logits [13.5911, 18.9903, 18.8662, 15.3534, 19.3868] → 排序 dog 0.4371 > horse 0.2940 > food 0.2597 > mountain 0.0077 > city 0.0013所有模型参数与输入输出张量显式置于 npu:0,torch.npu.synchronize() 包围计时;无 CPU fallback。
python inference.py # 默认 NPU 推理 + 候选排序
python inference.py --mode validate # CPU-NPU 一致性验证
python inference.py --mode benchmark # 性能测试(预热 3 次 + 正式 10 次)Ascend910_9362, CANN 8.5.1, 驱动 25.5.5, HBM 65536 MBa photograph of a dog = 0.437146a photograph of a horse = 0.294045a photograph of food on a table = 0.259736a photograph of a mountain landscape = 0.007744a photograph of a city street with cars = 0.001329RESULT: PASS(真实 python inference.py 输出,见 assets/model_result.png)相同权重、相同输入、相同预处理:
1.0000001190.0011330.000741dog = NPU dog(top1_match=True)npu:0, warmup 3, iter 10, FP32, batch 1, 224×224)assets/agent_workflow.png:下载 → 侦察 → 加载 → NPU 推理 → 一致性 → 性能 全流程assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名、模型参数 device/dtypeassets/model_result.png:默认 python inference.py 真实输出与 PASSpip install -r requirements.txt
# 无法访问 huggingface.co 时设置镜像:
export HF_ENDPOINT=https://hf-mirror.comNPU · Ascend · Ascend910 · Computer Vision · Vision-Language · CLIP · Zero-shot Classification · Image-Text Retrieval