本仓库记录 maple77/bce-reranker-base_v1 在华为昇腾 Ascend910 NPU 上的适配与验证结果。
bce-reranker-base_v1 是一个基于 XLM-RoBERTa 的 Cross-encoder 重排序模型(Reranker),用于对 query-passage 对进行相关性评分。分数越高表示相关性越强。
XLMRobertaForSequenceClassification相关获取地址:
| 组件 | 版本 |
|---|---|
| NPU | Ascend910 (2卡) / 25.5.2 |
| PyTorch | 2.x |
| torch_npu | 2.x |
| transformers | 4.35.0+ |
| Python | 3.x |
~/maple77/bce-reranker-base_v1/model/maple77/bce-reranker-base_v1/由于本模型是 Cross-encoder Reranker(非 Causal LM),不能使用 vLLM 部署,使用原生 PyTorch + torch_npu 推理。
已验证通过的推理方式:
python3 ~/maple77/bce-reranker-base_v1/inference.py核心推理代码:
import torch
import torch_npu
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# 加载模型
model_path = "~/maple77/bce-reranker-base_v1/model/maple77/bce-reranker-base_v1"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path, num_labels=1)
model = model.npu()
model.eval()
# 编码输入
inputs = tokenizer(query, passage, padding=True, truncation=True,
max_length=512, return_tensors="pt")
inputs = {k: v.npu() for k, v in inputs.items()}
# 推理
with torch.no_grad():
outputs = model(**inputs)
score = outputs.logits.squeeze().item()python3 ~/maple77/bce-reranker-base_v1/inference.py预期输出:
Loading tokenizer...
Loading model...
Moving model to NPU...
Running inference...
Query: What is the capital of China?
Passage: Beijing is the capital of the People's Republic of China.
Relevance Score: 0.5448
Inference successful!测试条件:输入长度 ~30 tokens(query + passage),单次推理,NPU 推理 100 次迭代。
| 指标 | 数值 |
|---|---|
| 硬件 | Ascend910 |
| 平均延迟 | 6.84 ms |
| P50 延迟 | 6.76 ms |
| P90 延迟 | 7.16 ms |
| P95 延迟 | 7.27 ms |
| P99 延迟 | 7.87 ms |
| 最小延迟 | 6.52 ms |
| 最大延迟 | 7.87 ms |
详细性能结果见 eval/performance.json。
| 文件 | 说明 |
|---|---|
README.md | 本文档 |
inference.py | NPU 推理脚本 |
eval/accuracy_eval.py | 精度评测源代码 |
eval/performance_eval.py | 性能评测源代码 |
eval/accuracy.json | 精度结果 |
eval/performance.json | 性能结果 |
eval/run_log.txt | 完整运行日志 |
transformers 的 AutoModelForSequenceClassification 加载.npu())torch_npu 和 transformersNPU 推理验证:
| 指标 | 数值 |
|---|---|
| 测试用例数 | 5 |
| 预测一致性 | 5/5 (100.0%) |
| 精度结论 | ✅ 通过 (准确率 100.0%) |
精度评测源代码和日志详见 eval/ 目录。