w
weasonlee/PickScore_v1
模型介绍
文件和版本
Pull Requests
讨论
分析

PickScore_v1 — Ascend NPU 适配

模型简介

PickScore_v1 是在 Pick-a-Pic 数据集(开源用户文生图偏好数据集)上微调 CLIP-H(ViT-H/14)得到的文生图偏好打分函数:输入 prompt 与生成图像,输出偏好分数(logit 尺度的图文嵌入点积)。可用作通用打分函数、人类偏好预测、模型评估与图像排序等。模型规模 986.1M 参数。

原始模型:yuvalkirstain/PickScore_v1 官方代码:yuvalkirstain/PickScore 论文:Pick-a-Pic: An Open Dataset of User Preferences for Text-to-Image Generation

加载方式:标准 transformers API(AutoModel + AutoProcessor)。本仓库随权重附带 CLIP-H 的 processor 文件(preprocessor_config / tokenizer / merges / vocab),无需再从 laion/CLIP-ViT-H-14 下载 processor,完全离线可用。

环境要求

  • 昇腾 Ascend 910(本仓库在 910 ×2 上验证,单卡即可推理)
  • Python 3.11
  • torch 2.9.0 + torch-npu 2.9.0.post1
  • transformers 4.57.6(huggingface_hub 需 <1.0)

部署步骤

# 1. 克隆仓库
git clone https://gitcode.com/weasonlee/PickScore_v1.git
cd PickScore_v1

# 2. 安装依赖
pip install -r requirements.txt

# 3. 下载权重到 models/(已 .gitignore 排除;model.safetensors ~3.9GB)
mkdir -p models
HF_ENDPOINT=https://hf-mirror.com HF_HUB_DOWNLOAD_TIMEOUT=120 python -c "
from huggingface_hub import hf_hub_download
hf_hub_download('yuvalkirstain/PickScore_v1', 'model.safetensors', local_dir='models')
hf_hub_download('yuvalkirstain/PickScore_v1', 'config.json', local_dir='models')
# processor 文件(若克隆仓库未含)
for f in ['preprocessor_config.json','tokenizer.json','merges.txt','vocab.json','tokenizer_config.json','special_tokens_map.json']:
    hf_hub_download('yuvalkirstain/PickScore_v1', f, local_dir='models')"

# 4. 推理(NPU)
python inference.py --device npu
# 或 CPU
python inference.py --device cpu

推理示例

$ python inference.py --device npu
torch.npu.is_available() = True
torch.npu.device_count() = 2
[info] device = npu:0, torch = 2.9.0+cpu
[load] CLIPModel (PickScore_v1, ViT-H/14): 986.1M params
[info] 3 prompts x 3 image variants, synthetic PIL compositions
[time] avg: 37 ms (3 images + 3 prompts per pass)
[out ] preference logits (rows=images, cols=prompts):
  clean composition    [19.833, 17.315, 17.725]
  shifted composition  [18.957, 17.086, 17.809]
  noisy composition    [16.281, 16.180, 16.818]
[out ] 'a photo of a mountain at sunset' -> best image: clean composition
[out ] 'a photo of a house in a forest' -> best image: clean composition
[out ] 'digital art of a spaceship in orbit' -> best image: shifted composition

===== CPU vs NPU comparison (preference logits 3x3) =====
cos_sim      = 0.99999998
max_abs_err  = 9.554e-03
mean_abs_err = 5.717e-03
ranking agreement = 100%

输入:3 个 prompt × 3 种确定性 PIL 合成图变体(clean / shifted / noisy composition,固定种子,无外部图片依赖);打分为 logit_scale·(归一化图文嵌入点积)。CPU 与 NPU 输入完全一致。模型行为合理:噪声图得分明显最低,山景 prompt 下 clean 构图得分最高。

CPU vs NPU 精度对比

指标值
cos_sim(3×3 偏好 logits)0.99999998
max_abs_err9.554e-03
mean_abs_err5.717e-03
图像排序一致率100%
单次打分耗时(3 图 + 3 prompt,含同步,3 次均值)NPU ~37 ms vs CPU ~19000 ms(~500× 加速)

结论:NPU(Ascend 910, torch-npu 2.9.0.post1)与 CPU 的偏好打分一致性达 0.9999999(fp32 正常差异水平),全部 prompt 的图像排序完全一致(100%),适配成功。

仓库结构

├── inference.py        # 推理脚本(合成图打分 + NPU/CPU 对比)
├── readme.md
├── requirements.txt
├── models/             # 权重目录(需自行下载,已 gitignore)
├── assets/             # 运行截图
│   ├── agent_workflow.png
│   ├── npu_device_call.png
│   └── model_result.png
└── results/            # 每次运行的打分/耗时产物(gitignore)

参考文献

Kirstain Y. 等. "Pick-a-Pic:一个用于文本到图像生成的用户偏好开放数据集." NeurIPS 2023. arXiv:2305.01569.