本文档记录 jinaai/jina-reranker-v1-turbo-en 在 Ascend 910B3 NPU 环境下的适配与验证结果。
jina-reranker-v1-turbo-en 是 Jina AI 发布的轻量级交叉编码器重排序模型,基于 JinaBERT 架构。JinaBERT 是 BERT 的变体,支持对称双向 ALiBi(Attention with Linear Biases)位置编码,可处理最长 8192 token 的输入序列。该模型通过知识蒸馏技术,在保持 competitive 精度的同时实现极快的推理速度。
本仓库提供:
相关获取地址:
参考文档:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 4.57.6 |
Ascend 910B3,1 逻辑卡JinaBertForSequenceClassification384126ALiBi(支持外推到 8192 token)61,05632M启动前可先检查 NPU 可用性:
python3 -c "import torch; print(f'NPU available: {torch.npu.is_available()}')"环境准备:
# 安装依赖
pip install torch torch_npu transformers
# 设置 NPU 可见设备(默认使用 0 号卡)
export ASCEND_RT_VISIBLE_DEVICES=0已验证通过的推理命令:
单对查询-文档评分:
python inference.py \
--query "What is machine learning?" \
--passage "Machine learning is a subset of AI that enables systems to learn from data."批量推理(JSONL 格式):
# pairs.jsonl 每行:{"query": "...", "passage": "..."}
python inference.py --input-file pairs.jsonlCPU 推理(参考基准):
python inference.py --device cpu --query "test" --passage "example"输出格式:
{
"model": "jinaai--jina-reranker-v1-turbo-en",
"device": "npu:0",
"num_pairs": 1,
"inference_time_seconds": 0.0021,
"pairs_per_second": 476.19,
"results": [
{
"query": "What is machine learning?",
"passage": "Machine learning is a subset of AI...",
"score": 4.527361
}
]
}python inference.py --query "test" --passage "example passage"预期输出:
score 字段为相关性得分(正数表示正相关)inference_time_seconds 应在毫秒级别(< 50ms)验证结果:
测试条件:8 对查询-文档,batch_size=8,max_length=8192。
| 指标 | CPU | NPU |
|---|---|---|
avg_time | 0.0960 s | 0.0116 s |
throughput | 83.32 pairs/s | 690.87 pairs/s |
speedup | - | 8.28x |
精度评测采用余弦距离(Cosine Distance)作为主要指标,对比 NPU 与 CPU 输出的评分向量。
| 指标 | 数值 |
|---|---|
| 测试样本数 | 8 |
| 最大绝对误差 | 3.9647e-03 |
| 平均绝对误差 | 1.5690e-03 |
| 最大相对误差 | 0.4236% |
| 余弦相似度 | 0.99999952 |
| 余弦距离 | 0.000048% |
| 精度要求(余弦距离 < 1%) | 通过 |
结论:NPU 与 CPU 输出的评分向量余弦距离仅 0.000048%,远低于 1% 的阈值,精度通过验证。
modeling_bert.py、configuration_bert.py),加载时需设置 trust_remote_code=True。num_labels=1 及 ignore_mismatched_sizes=True 以匹配权重形状。</s><s> 作为分隔符拼接(JinaBERT 的特定格式)。