本文档记录 yangjiurong/chinese-news-title-sentiment-c3-v2 中文新闻文本情感三分类模型在华为昇腾 Ascend NPU 环境上的适配与验证结果。
本模型基于 hfl/chinese-roberta-wwm-ext 微调,支持对中文新闻文本进行情感三分类:负面、中性、正面。
模型相关信息:
相关获取地址:
| 组件 | 版本 |
|---|---|
| NPU | Ascend910 (25.5.2) |
| torch | 2.6.0 |
| torch_npu | 2.6.0 |
| transformers | 4.47.1 |
| Python | 3.11.14 |
/opt/atomgit/yangjiurong/chinese-news-title-sentiment-c3-v2from transformers import BertTokenizer, BertForSequenceClassification
import torch
import torch_npu
device = torch.device("npu:0")
model_id = "/path/to/model"
tokenizer = BertTokenizer.from_pretrained(model_id)
model = BertForSequenceClassification.from_pretrained(model_id, torch_dtype=torch.float32)
model.to(device)
model.eval()使用 inference.py 进行推理验证:
import torch
import torch_npu
from transformers import BertTokenizer, BertForSequenceClassification
device = torch.device("npu:0")
model_id = "/path/to/model"
tokenizer = BertTokenizer.from_pretrained(model_id)
model = BertForSequenceClassification.from_pretrained(model_id, torch_dtype=torch.float32)
model.to(device)
model.eval()
id2_label = {0: "负面", 1: "中性", 2: "正面"}
test_text = "这家酒店不太行,环境太差了!"
with torch.inference_mode():
inputs = tokenizer(test_text, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
logits = model(**inputs).logits
pred = torch.argmax(logits, dim=-1)
print(f"预测结果:{id2_label[pred.item()]}")运行命令:
python3 inference.py| 指标 | 数值 |
|---|---|
| 平均延迟 | 6.10 ms |
| P50 | 5.92 ms |
| P95 | 6.93 ms |
| P99 | 6.97 ms |
| Batch Size | 吞吐量 (samples/sec) |
|---|---|
| 1 | 172.79 |
| 4 | 568.44 |
| 8 | 1142.38 |
| 16 | 2321.22 |
| 32 | 4070.81 |
使用 eval/run_accuracy.py 进行精度评测。
| 指标 | 数值 |
|---|---|
| 测试用例数 | 12 |
| 正确数 | 9 |
| 准确率 | 75.00% |
inference.py:推理脚本eval/run_accuracy.py:精度评测脚本eval/run_perf.py:性能评测脚本eval/accuracy_results.json:精度评测结果eval/performance_results.json:性能评测结果eval/accuracy_run.log:精度评测日志eval/performance_run.log:性能评测日志torch.float32(原始训练精度)torch.inference_mode() 提高性能torch_npu 在 Ascend NPU 上进行加速