本文档记录 deepset/tinyroberta-squad2 在昇腾 Ascend 910B3 NPU 上的迁移适配、推理部署与精度评测结果。
tinyroberta-squad2 是一个基于 RoBERTa 的抽取式问答模型,在 SQuAD 2.0 数据集上微调,参数量约 81.53M(6 层 Transformer,768 隐藏维)。该模型接受上下文文本和问题作为输入,输出答案在上下文中的起始和结束位置。
本次适配工作包括:
inference.pyeval.py相关获取地址:
参考文档:
| 组件 | 版本 |
|---|---|
Python | 3.9.13 |
torch | 2.8.0+cpu |
torch_npu | 2.8.0.post4 |
transformers | 4.57.6 |
numpy | 1.24.4 |
Ascend 910B3 × 8 逻辑卡25.5.2tinyroberta-squad2 使用标准 RoBERTa 架构,transformers 库原生支持。NPU 适配无需修改模型结构或权重,仅需将模型和输入张量迁移到 NPU 设备。
已验证通过的适配流程:
import torch
import torch_npu
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
model = AutoModelForQuestionAnswering.from_pretrained("deepset/tinyroberta-squad2")
model = model.npu()
model.eval()
tokenizer = AutoTokenizer.from_pretrained("deepset/tinyroberta-squad2")
inputs = tokenizer(question, context, return_tensors="pt")
inputs = {k: v.npu() for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
start = outputs.start_logits.cpu().argmax()
end = outputs.end_logits.cpu().argmax()
answer = tokenizer.decode(inputs["input_ids"][0][start:end+1])pip install torch torch_npu transformers -i https://repo.huaweicloud.com/repository/pypi/simple/
export HF_ENDPOINT=https://hf-mirror.com# NPU 推理
python inference.py --context "Python was created by Guido van Rossum in 1991." --question "Who created Python?"
# CPU 推理
python inference.py --context "..." --question "..." --device cpu
# 批量推理(JSON 文件)
python inference.py --input samples.json --output results.json
# 交互模式
python inference.py --interactivepython inference.py --context "Python was created by Guido van Rossum in 1991." --question "Who created Python?"预期输出:
QA Results (NPU)
--- QA #1 ---
Q: Who created Python?
A: Guido van Rossum测试条件:batch_size=8,max_seq_len=512,float32 精度,连续 20 次取平均。
| 指标 | CPU | NPU (Ascend 910B3) |
|---|---|---|
| 平均推理时间 (8 QA pairs) | 697.79 ms | 11.03 ms |
| 单问答对平均耗时 | 87.22 ms | 1.38 ms |
| 加速比 | 1x | 63.28x |
| 参数量 | 81.53M | 81.53M |
| 模型大小 | 311.0 MB | 311.0 MB |
使用 8 组问答对进行评测:
| 指标 | Start Logits | End Logits | 要求 | 结果 |
|---|---|---|---|---|
| MSE | 1.58e-6 | 1.60e-6 | - | - |
| Max Absolute Error | 5.12e-3 | 6.44e-3 | - | - |
| Cosine Similarity | 1.00000024 | 1.00000000 | > 0.99 | ✓ |
| Prob Mean Diff | 0.000105% | 0.000072% | < 1% | ✓ PASS |
| Prob Max Diff | 0.014085% | 0.008792% | < 1% | ✓ |
| Span Agreement | 8/8 (100%) | 8/8 (100%) | > 99% | ✓ |
结论:NPU 精度误差满足要求(< 1%),模型在 NPU 上的推理结果与 CPU 一致。
详细评测日志见 eval_log.txt。
.safetensors / .bin 文件,无需修改--max_length)torch_npu 版本:确保与 torch 版本匹配