m
mxy-yy/tinyroberta-squad2-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

tinyroberta-squad2 Ascend NPU 部署指南

项目简介

tinyroberta-squad2 是基于 RoBERTa 的轻量级问答模型,专为 SQuAD2.0 问答任务微调。该模型可以在给定问题和上下文的情况下,从文本中提取答案片段。

特性

  • 支持 Ascend NPU 推理加速
  • CPU 与 NPU 精度对比测试(误差 < 1%)
  • 问答任务(Question Answering)
  • 兼容 HuggingFace transformers
  • 96.36 倍加速比

环境信息

项目版本/内容
设备Ascend 910B

文件结构

tinyroberta-squad2-ascend/
├── inference.py          # 推理测试脚本
├── test.log              # 测试日志
├── README.md             # 本文档

部署步骤

1. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2. 准备模型文件

模型文件位于 /opt/atomgit/mxy/tinyroberta-squad2/ 目录下:

  • model.safetensors - 模型权重
  • config.json - 模型配置
  • tokenizer.json / vocab.json / merges.txt - 分词器文件

3. 安装依赖

pip install transformers torch_npu

4. 执行推理

cd tinyroberta-squad2-ascend/
python3 inference.py

Usage

Method 1: Normal Inference Mode

Run the inference script for question answering:

cd tinyroberta-squad2-ascend/
python3 inference.py --mode inference

Method 2: Precision Test Mode (CPU vs NPU)

Run the precision comparison test:

cd tinyroberta-squad2-ascend/
python3 inference.py --mode precision_test

命令行参数说明

参数说明默认值
--mode测试模式: all, inference 或 precision_testall

测试验证

精度测试结果

指标实测值阈值状态
最大相对误差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!
============================================================

Python API 使用示例

基本推理

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}")

模型结构

组件说明
robertaRoBERTa 编码器
qa_outputs问答输出层 (start/end logits)

推理参数配置

参数值
hidden_size768
num_hidden_layers6
num_attention_heads12
vocab_size50265
max_position_embeddings514

注意事项

  1. 模型使用 NPU 进行推理加速
  2. 支持 CPU 和 NPU 双模式推理
  3. 精度验证通过 (< 1% 误差)
  4. 96x 加速比