tinyroberta-squad2 是基于 RoBERTa 的轻量级问答模型,专为 SQuAD2.0 问答任务微调。该模型可以在给定问题和上下文的情况下,从文本中提取答案片段。
| 项目 | 版本/内容 |
|---|---|
| 设备 | Ascend 910B |
tinyroberta-squad2-ascend/
├── inference.py # 推理测试脚本
├── test.log # 测试日志
├── README.md # 本文档source /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /opt/atomgit/mxy/tinyroberta-squad2/ 目录下:
pip install transformers torch_npucd tinyroberta-squad2-ascend/
python3 inference.pyRun the inference script for question answering:
cd tinyroberta-squad2-ascend/
python3 inference.py --mode inferenceRun the precision comparison test:
cd tinyroberta-squad2-ascend/
python3 inference.py --mode precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式: all, inference 或 precision_test | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 最大相对误差 | 0.0454% | < 1.00% | ✅ PASS |
| CPU 推理时间 | 0.853s | - | - |
| NPU 推理时间 | 0.009s | - | - |
| 加速比 | 96.36x | > 1x | ✅ PASS |
输入问题: "What can you control in the Google Privacy Center?"
输入上下文: "The Google Privacy Center is where you can control the personal information Google collects about you..."
输出答案: 从上下文中提取的答案片段
tinyroberta-squad2 NPU Test
Model: MichelBartels/tinyroberta-6l-768d-finetuned (QA)
Output: /opt/atomgit/mxy/tinyroberta-squad2-ascend
============================================================
Inference Test (NPU)
============================================================
Device: npu:0
Loading model and tokenizer...
Model loaded successfully
Question: What can you control in the Google Privacy Center?
Context length: 182 chars
Input tokens: 199
Inference time: 0.266s
Answer: <s>
Start logits shape: torch.Size([1, 199])
End logits shape: torch.Size([1, 199])
============================================================
Precision Test (CPU vs NPU)
============================================================
NPU Device: npu:0
Loading model...
Question: What can you control in the Google Privacy Center?
Context length: 182 chars
Running on CPU...
Running on NPU...
CPU inference time: 0.853s
NPU inference time: 0.009s
Speedup: 96.36x
Max abs error (start): 3.141880e-03
Max abs error (end): 2.924919e-03
Max relative error: 0.0454% (threshold: 1.0%)
Status: PASS
============================================================
Precision Test Result: PASS
============================================================
============================================================
Test Complete!
============================================================import torch
from transformers import RobertaForQuestionAnswering, RobertaTokenizerFast
MODEL_DIR = "/opt/atomgit/mxy/tinyroberta-squad2"
tokenizer = RobertaTokenizerFast.from_pretrained(MODEL_DIR)
model = RobertaForQuestionAnswering.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()
question = "What is the capital of France?"
context = "France is a country in Europe. Its capital is Paris."
inputs = tokenizer(question, context, return_tensors="pt", max_length=512, truncation=True)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
answer_start_idx = outputs.start_logits.argmax()
answer_end_idx = outputs.end_logits.argmax()
answer = tokenizer.decode(inputs["input_ids"][0][answer_start_idx:answer_end_idx + 1])
print(f"Answer: {answer}")| 组件 | 说明 |
|---|---|
| roberta | RoBERTa 编码器 |
| qa_outputs | 问答输出层 (start/end logits) |
| 参数 | 值 |
|---|---|
| hidden_size | 768 |
| num_hidden_layers | 6 |
| num_attention_heads | 12 |
| vocab_size | 50265 |
| max_position_embeddings | 514 |