weixin_72661020/chinese-news-title-sentiment-c3-v2
模型介绍文件和版本Pull Requests讨论分析

yangjiurong/chinese-news-title-sentiment-c3-v2 on Ascend NPU

1. 简介

本文档记录 yangjiurong/chinese-news-title-sentiment-c3-v2 中文新闻文本情感三分类模型在华为昇腾 Ascend NPU 环境上的适配与验证结果。

本模型基于 hfl/chinese-roberta-wwm-ext 微调,支持对中文新闻文本进行情感三分类:负面、中性、正面。

模型相关信息:

  • 模型架构:BertForSequenceClassification
  • 参数量:~110M
  • 分类数:3类(负面、中性、正面)
  • 框架:PyTorch + transformers
  • 词表大小:21128
  • 最大序列长度:512

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/yangjiurong/chinese-news-title-sentiment-c3-v2

2. 验证环境

组件版本
NPUAscend910 (25.5.2)
torch2.6.0
torch_npu2.6.0
transformers4.47.1
Python3.11.14
  • NPU:Ascend910_9362
  • 模型路径:/opt/atomgit/yangjiurong/chinese-news-title-sentiment-c3-v2
  • 推理设备:npu:0

3. 模型加载

from 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()

4. 推理验证

使用 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

5. 性能参考

单样本延迟

指标数值
平均延迟6.10 ms
P505.92 ms
P956.93 ms
P996.97 ms

批处理吞吐量

Batch Size吞吐量 (samples/sec)
1172.79
4568.44
81142.38
162321.22
324070.81

6. 精度评测

使用 eval/run_accuracy.py 进行精度评测。

指标数值
测试用例数12
正确数9
准确率75.00%

7. 交付物清单

  • 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:性能评测日志

8. 注意事项

  • 加载模型时使用 torch.float32(原始训练精度)
  • 推理时使用 torch.inference_mode() 提高性能
  • 使用 torch_npu 在 Ascend NPU 上进行加速
  • 文本长度超过 512 时需进行截断处理
  • 模型为 BertForSequenceClassification,不适用 vLLM 部署
下载使用量0