yuvalkirstain/PickScore_v1 on Ascend NPU
1. 模型简介
PickScore_v1 是基于 CLIP ViT-H/14 的人类偏好评分模型,由 yuvalkirstain 基于 openai/clip-vit-h-14 微调,用于文生图质量的人类偏好预测。输入为图像与文本 prompt,输出为图像-文本对的偏好分数(logit)。本适配在 Ascend910B 单卡 npu:0 上完成真实权重推理与验证。
2. 验证环境
- NPU: Ascend910_9362 (2 devices, tested on npu:0)
- npu-smi: OK, driver 25.5.5
- torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04
- torch.npu.is_available(): true, device_count: 2, device_name: Ascend910_9362
- CANN: 8.5.1
- 显存: 61.27 GiB total, used ~8.41 GiB (bs=1, FP32)
- dtype: float32 (torch.float32)
- 模型参数设备: npu:0
3. 安装依赖
pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch_npu==2.9.0.post1
# transformers==4.57.6
# Pillow>=10.0.0
# numpy
4. NPU 推理
python inference.py
# 可选:
python inference.py --mode validate # 仅验证
python inference.py --mode benchmark # 仅压测
默认从本地目录 /opt/atomgit/adapt-npu-agent/models/PickScore_v1 加载真实权重,不依赖运行时联网下载。输入为固定 224x224 RGB 测试图(蓝底红方块)与 4 条候选文本。
5. 真实推理结果
固定输入:
- Image: 224x224 RGB, color(73,109,137) background + red square [50,50,174,174]
- Texts (4, 顺序固定):
- "a photo of a cat"
- "a photo of a dog"
- "a photo of a beautiful landscape"
- "a photo of a red square on blue background" # 正确标签
真实 NPU 输出 (npu:0, FP32, transformers CLIPModel):
- image_embeds: [1, 1024]
- text_embeds: [4, 1024]
- logits_per_image: [1, 4] = [[18.8094, 18.8221, 17.8158, 21.3278]]
- softmax probs (CLIP distribution): [0.0676, 0.0685, 0.0250, 0.8389]
- Ranking (按 PickScore logit 从高到低):
- "a photo of a red square on blue background" -> 21.3278
- "a photo of a dog" -> 18.8221
- "a photo of a cat" -> 18.8094
- "a photo of a beautiful landscape" -> 17.8158
- Top-1 正确且符合图像语义;首次编译延迟 ~269 ms,稳定推理 ~26 ms。
6. CPU-NPU 一致性验证
相同权重、输入、预处理、dtype 与 batch=1 对比 CPU vs NPU (FP32):
- image_embeds cosine: 0.999997 (>=0.9999 PASS)
- text_embeds cosine min: 0.999998 mean: 0.999999 (>=0.9999 PASS)
- logits max_abs: 0.006813 mean_abs: 0.004777
- CPU logits: [[18.8163, 18.8288, 17.8187, 21.3250]]
- NPU logits: [[18.8094, 18.8221, 17.8158, 21.3278]]
- CPU ranking: [3, 1, 0, 2] NPU ranking: [3, 1, 0, 2] 完全一致
- Top-1 匹配: True, Overlap: 100%, full ranking match (n=4)
阈值: FP32 cosine >=0.9999, Top-1 必须一致, 候选<10 时全排序一致。结论: PASS。
7. 性能测试
- 设备: npu:0, dtype: float32, batch=1, image 224x224, 4 texts
- 预热 3 次, 正式 10 次, 计时区间前后
torch.npu.synchronize()
- 端到端: avg 25.97 min 25.58 max 26.68 p50 25.75 p90 26.61 p95 26.65 ms (stable)
- 首次编译: ~267-270 ms (含算子编译)
- 图像编码: avg 13.40 ms
- 文本编码 (4 texts): avg 12.31 ms
- 相似度计算已包含在端到端
- 峰值显存: ~8.41 GiB / 61.27 GiB
- 吞吐: ~38.5 images/s (e2e)
8. 自验证截图
三张 PNG 由 scripts/render_xterm_evidence.mjs --style raw 从真实日志渲染,含命令、退出码、时间、日志 SHA-256,提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$。
9. 已知限制
- 仅验证单图+4文本的零样本 scoring,非完整数据集评测;PickScore 官方评测需多图人类偏好成对比较,本次为单样本一致性验证。
- 使用 FP32,FP16/BF16 未测但 Huge 模型可按需切换并记录。
- 仅在 npu:0 上验证,未测试多卡或不同 batch/分辨率。
- 输入图像为合成固定图,非自然数据集;真实检索/排序性能需按官方 pipeline 在目标数据上复测。
10. 标签
Hardware: NPU, NPU, Ascend, Ascend910, CLIP, dual-encoder, PickScore, vision, image-text