PickScore_v1(yuvalkirstain/PickScore_v1)是基于 CLIP ViT-H/14 的图文偏好打分模型(人类偏好奖励模型,用于文生图结果评估与排序)。本仓库提供其在华为昇腾 NPU(Ascend 910B,npu:0 单卡)上的真实推理脚本 inference.py,包含:
--mode benchmark)。| 项目 | 配置 |
|---|---|
| 硬件 | Atlas A2 系列 Ascend910B(Ascend910_9362,64GB HBM,单卡 npu:0) |
| 模型权重 | model.safetensors(FP32,3.67 GiB,986.1M 参数,CLIPModel ViT-H/14) |
| torch / torch_npu | 2.9.0+cpu / 2.9.0.post1+gitee7ba04 |
| CANN | 8.5.1 |
| transformers | 5.9.0(加载后端:AutoModel -> CLIPModel + CLIPProcessor) |
| dtype | float32 |
pip install -r requirements.txt模型权重需提前准备到本地目录(推荐通过环境变量镜像下载,不依赖运行时隐式下载):
# 使用 HF_ENDPOINT=https://hf-mirror.com 时自动走国内镜像直连
python -c "from huggingface_hub import snapshot_download; snapshot_download('yuvalkirstain/PickScore_v1', local_dir='./models/PickScore_v1')"默认命令即可执行真实 NPU 推理(固定样例 + 一致性验证 + PASS/FAIL 判定):
export PICKSCORE_MODEL_DIR=/path/to/PickScore_v1 # 本地权重目录;或用 --model-dir 指定;未提供时尝试 snapshot_download 自动下载
python inference.py
# 仅性能测试:
python inference.py --mode benchmark --benchmark-iters 10固定输入:1 张真实 RGB 图片 assets/testdata/cats.jpg(640x480,中心裁剪至 224x224),5 条候选 prompt(含正确描述 "two cats sitting together"),tokenizer max_length=77,batch size=1,float32。
NPU(npu:0)上对固定图片的候选 prompt 完整排序与真实 PickScore 分数:
| rank | prompt | PickScore(NPU) | softmax 概率 |
|---|---|---|---|
| 1 | two cats sitting together | 20.7848 | 0.942644 |
| 2 | a photo of a dog | 17.5532 | 0.037231 |
| 3 | a rocket launching into space | 16.5970 | 0.014309 |
| 4 | a bowl of hot noodle soup | 15.3242 | 0.004008 |
| 5 | an empty parking lot at night | 14.5284 | 0.001808 |
语义正确:正确描述 "two cats sitting together" 以显著优势排名第一。完整输出见 assets/model_result.png。
相同权重、相同输入、相同预处理下对比 CPU 与 npu:0 的 FP32 输出:
| 指标 | 结果 | 阈值/判定 |
|---|---|---|
| image embedding cosine(CPU, NPU) | 0.999998 | >= 0.9999 通过 |
| text embedding 最小 cosine(5 条) | 0.999998 | >= 0.9999 通过 |
| PickScore 打分 MAE | 0.00559 | — |
| PickScore 打分最大绝对误差 | 0.01155 | <= 0.5 通过 |
| 候选概率最大绝对误差 | 0.000586 | — |
| Top-1 是否一致 | 是 | 必须一致 通过 |
| 全排序是否一致(5<10) | 是 | 全部一致 通过 |
结论:最终判定 VALIDATION PASS。该验证为固定单样例一致性 smoke test,不等价于完整数据集精度评测。
测试条件:npu:0,FP32,batch size=1,图像 224x224,文本 5 条(padding 至 77 token),预热 3 次,正式测试 10 次,每次计时区间前后调用 torch.npu.synchronize()。
| 阶段 | avg | min | p50 | p90/p95 |
|---|---|---|---|---|
| 图像编码(bs=1, 224x224) | 18.98 ms | 18.68 ms | 18.75 ms | 19.56 ms |
| 文本编码(5 条, 77 token) | 14.92 ms | 14.78 ms | 14.90 ms | 15.04 ms |
| 端到端打分(双塔编码+相似度+softmax) | 34.28 ms | 34.05 ms | 34.22 ms | 34.50 ms |
峰值显存:验证流程 3.79 GiB;基准测试 4.55 GiB。首次编译(算子编译)发生在预热阶段,表中数值为稳定推理延迟。
assets/agent_workflow.png:环境检查、权重检查、架构侦察、NPU 推理与验证、性能测试各阶段真实日志。assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名与模型参数所在设备/dtype。assets/model_result.png:默认 python inference.py 完整输出(排序结果、一致性指标、PASS/FAIL)。get_image_features/get_text_features 返回 ModelOutput 对象,脚本直接使用 vision_model/text_model + 投影层获取张量,行为等价于官方用法。PICKSCORE_MODEL_DIR 或 --model-dir);未提供时回退为 huggingface_hub.snapshot_download 自动下载。Hardware: NPU、NPU、Ascend、Ascend910