qq_34566203/tinyroberta-squad2-ascend
模型介绍文件和版本Pull Requests讨论分析

TinyRoBERTa-SQuAD2 QA on Ascend NPU

1. 简介

本文档记录 deepset/tinyroberta-squad2 在 Ascend 910B3 NPU 环境下的适配与验证结果。该模型是一个基于 RoBERTa 的问答模型,在 SQuAD 2.0 数据集上微调,支持抽取式问答任务。

本仓库提供:

  • inference.py:NPU 推理脚本,支持基于上下文的问答
  • eval.py:精度与性能评测脚本,对比 NPU 与 CPU 的输出差异
  • log.txt:评测运行日志

相关获取地址:

  • 模型权重(HuggingFace):https://huggingface.co/deepset/tinyroberta-squad2
  • 镜像加速:https://hf-mirror.com

2. 验证环境

组件版本
torch2.8.0
torch_npu2.8.0.post4
transformers4.57.6
  • NPU:Ascend 910B3,1 逻辑卡
  • 模型架构:RobertaForQuestionAnswering(6 层,768 隐藏维,12 注意力头)
  • 最大序列长度:384

3. 推理启动

环境准备

pip install torch torch_npu transformers
export ASCEND_RT_VISIBLE_DEVICES=0

单条问答

python inference.py \
  --context "Einstein developed the theory of relativity." \
  --question "What did Einstein develop?"

批量问答

# JSONL 格式: {"context": "...", "question": "..."}
python inference.py --input-file qa_pairs.jsonl

输出格式

{
  "model": "tinyroberta-squad2",
  "device": "npu:0",
  "num_questions": 1,
  "results": [
    {
      "question": "What did Einstein develop?",
      "context": "Einstein developed the theory of relativity...",
      "answer": "theory of relativity",
      "confidence": 0.9542
    }
  ]
}

4. Smoke 验证

python inference.py --context "Python was created by Guido van Rossum." --question "Who created Python?"

5. 性能参考

指标CPUNPU
平均推理时间0.9597 s0.0130 s
吞吐量8.34 q/s616.88 q/s
加速比-73.82x

6. 精度评测

精度评测采用答案区间(answer span)一致性和 logits 误差双指标。

指标数值
测试问题数8
Max start abs error8.7323e-03
Max end abs error1.0587e-02
答案区间一致率8/8 (100.00%)
错误率0.00%
精度要求(错误率 < 1%)通过

结论:NPU 与 CPU 的答案预测完全一致(100%),logits 误差极小,精度通过验证。

7. 注意事项

  1. 问答格式:模型为抽取式问答,即从给定上下文中提取答案片段,不生成新文本。
  2. 序列长度:默认最大长度为 384 token(问题和上下文合并),超出部分会被截断。
  3. SQuAD 2.0:模型支持不可回答问题的检测。
  4. 中文支持:模型主要针对英语训练,对中文效果有限。
下载使用量0