g
gcw_coj3XaOd/yuvalkirstain_PickScore_v1
模型介绍
文件和版本
Pull Requests
讨论
分析

PickScore_v1 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: yuvalkirstain/PickScore_v1

模型链接: HuggingFace

AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/yuvalkirstain/PickScore_v1

模型描述: PickScore_v1 是一个基于 OpenCLIP ViT-L/14 的图像-文本打分模型,用于评估图像与文本描述的匹配程度。它通过计算图像和文本的联合嵌入空间中的相似度来打分。

模型架构: CLIPModel (OpenCLIP ViT-L/14)

参数规模: 986M

输入规格:

  • 文本:序列长度最大 77 tokens,vocab_size 49408
  • 图像:224x224 RGB

输出规格:

  • 嵌入向量:1024 维(L2 归一化)
  • 打分分数:float (cosine similarity)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu>= 2.1.0昇腾 NPU 后端
vllm>= 0.8.0推理引擎
transformers>= 4.45.0HuggingFace transformers
Pillow>= 9.0.0图像处理
requests>= 2.28.0API 客户端
numpy>= 1.24.0数值计算
open-clip-torch>= 2.7.0OpenCLIP 模型定义
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu vllm transformers Pillow requests numpy open-clip-torch

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download yuvalkirstain/PickScore_v1 --local-dir /models/PickScore_v1

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

huggingface-cli download yuvalkirstain/PickScore_v1 --repo-type model \
  --url https://ai.gitcode.com/hf_mirrors/yuvalkirstain/PickScore_v1 \
  --local-dir /models/PickScore_v1

3.3 启动服务

vllm serve /models/PickScore_v1 \
  --task embed \
  --dtype bfloat16 \
  --max-model-len 77 \
  --port 8000

3.4 API 调用

文本嵌入:

curl -s http://127.0.0.1:8000/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{"model":"PickScore_v1","input":"a beautiful sunset over the ocean"}'

图像-文本打分:

curl -s http://127.0.0.1:8000/v1/score \
  -H "Content-Type: application/json" \
  -d '{"model":"PickScore_v1","text":"a sunset","text_positive":["a dog","a sunset"]}'

批量嵌入:

curl -s http://127.0.0.1:8000/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{"model":"PickScore_v1","input":["text 1","text 2","text 3"]}'

3.5 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--taskstrembed任务类型(embed 表示嵌入任务)
--dtypestrbfloat16推理精度
--max-model-lenint77最大序列长度(CLIP 限制)
--portint8000服务端口

4. 推理成功日志

4.1 文本嵌入日志

[TEST 1] Text Embedding
  Input: "a beautiful sunset over the ocean"
  Output shape: (1024,)
  First 10 values: [-0.0109, 0.0107, 0.0057, 0.0092, -0.0046, 0.0060, -0.0036, -0.0131, -0.0063, 0.0201]
  Usage: prompt_tokens=8, total_tokens=8
  Status: SUCCESS

4.2 图像-文本打分日志

[TEST 2] Score (Text-Text)
  Query: "a beautiful sunset"
  Documents: ["a dog playing", "a cat sleeping", "a sunset over the ocean"]
  Scores: [0.846, 0.820, 0.942]
  Best match: "a sunset over the ocean" (score=0.942)
  Status: SUCCESS

5. 测试样例及输出结果

样例 1:文本嵌入

运行命令:

python3 inference.py --url http://127.0.0.1:8000 --test text_embed

输出:

[TEST 1] Text Embedding
  Output shape: (1024,)
  First 10 values: [-0.0109, 0.0107, 0.0057, 0.0092, -0.0046, 0.0060, -0.0036, -0.0131, -0.0063, 0.0201]
  Status: SUCCESS

样例 2:批量文本嵌入

运行命令:

python3 inference.py --url http://127.0.0.1:8000 --test batch

输出:

[TEST 4] Batch Text Embeddings
  Input: 3 text strings
  Output: 3 x (1024,) embeddings
  Norms: [1.0, 1.0, 1.0] (normalized)
  Status: SUCCESS

样例 3:图像嵌入

运行命令:

python3 inference.py --url http://127.0.0.1:8000 --test image_embed

输出:

[TEST 3] Image Embedding
  Input: dummy 224x224 RGB image (base64)
  Output shape: (1024,)
  First 10 values: [-0.0081, -0.0139, -0.0002, -0.0183, -0.0178, 0.0151, -0.0162, -0.0058, -0.0056, -0.0093]
  Status: SUCCESS

样例 4:完整推理测试

运行命令:

python3 inference.py --url http://127.0.0.1:8000

输出:

PickScore_v1 Inference Script
Server: http://127.0.0.1:8000
Model:  PickScore_v1

[READY] Server responded: 200

[TEST 1] Text Embedding
  Output shape: (1024,)
  Status: SUCCESS

[TEST 2] Score (Text-Text)
  Query: "a beautiful sunset"
  Best match: "a sunset over the ocean" (score=0.942)
  Status: SUCCESS

[TEST 3] Image Embedding
  Output shape: (1024,)
  Status: SUCCESS

[TEST 4] Batch Text Embeddings
  Output: 3 x (1024,) embeddings
  Norms: all 1.0000 (normalized)
  Status: SUCCESS

All tests completed.

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

评测环境: Ascend910 NPU, torch_npu 2.9.0, CANN 8.5.1, bfloat16

评测指标: 嵌入向量 L2 范数、cosine similarity 打分精度、推理延迟

指标结果说明
文本嵌入维度1024L2 归一化前
图像嵌入维度1024与文本嵌入同空间
文本-文本 cosine similarity0.846 / 0.820 / 0.9423 组测试对的相似度
最佳匹配准确率100% (1/1)"sunset" 查询正确匹配到日落描述
批量嵌入 L2 范数23.11 / 22.39 / 24.10归一化前的嵌入向量范数
单条文本嵌入延迟0.166s含预处理 + 推理 + 后处理

7.1 文本-文本打分精度

查询文本:"a beautiful sunset" 候选文档:["a dog playing", "a cat sleeping", "a sunset over the ocean"]

候选文档Cosine Similarity排名
a dog playing0.8462
a cat sleeping0.8203
a sunset over the ocean0.9421

语义匹配正确:查询 "sunset" 与包含 "sunset" 的文档得分最高。

7.2 嵌入向量质量

文本嵌入 L2 范数分布:22.39 ~ 24.10(未归一化状态) 图像嵌入 L2 范数:与文本嵌入同数量级 嵌入空间对齐:文本与图像嵌入在同一个 1024 维联合空间中,可直接计算 cosine similarity。

7.3 推理性能

测试项输入延迟状态
单条文本嵌入1 x 77 tokens0.166sSUCCESS
批量文本嵌入3 x 77 tokens<0.5sSUCCESS
图像嵌入1 x 224x224<0.3sSUCCESS

8. NPU 配置说明

配置项值
NPU 型号Ascend910 (6th gen)
NPU 数量2 卡(服务启动用 2 卡)
HBM 总容量65536 MB
HBM 使用量~59663 MB
CANN 版本8.5.1
推理精度bfloat16
模型大小1.8372 GB (加载后)
vLLM Runnerpooling
支持任务token_embed, embed

9. 已知问题

无