weixin_72661020/nlp_structbert_sentiment-classification_chinese-tiny-fast
模型介绍文件和版本Pull Requests讨论分析

putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast on Ascend NPU

1. 简介

本文档记录 putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast 在华为昇腾 Ascend NPU 环境上的适配与验证结果。

该模型是一个基于 StructBERT 的中文情感分类模型,对输入文本进行正面/负面二分类。

  • 模型类型:text-classification(文本分类)
  • 架构:StructBERT (BERT-based)
  • 语言:中文
  • 标签数:2(负面、正面)
  • 模型参数:hidden_size=256, num_hidden_layers=4, num_attention_heads=4
  • 推理框架:PyTorch + transformers + torch_npu

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast

2. 验证环境

组件版本
NPUAscend 910
torch2.5.1
torch-npu2.9.0.post1+gitee7ba04
transformers4.57.6
Python3.11.14
  • NPU 数量:2 逻辑卡
  • 模型路径:~/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast/model/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast/

3. 推理

使用提供的推理脚本直接运行:

cd /opt/atomgit/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast
python3 inference.py

推理脚本会自动完成以下操作:

  1. 加载模型配置和权重(自动映射 encoder. 前缀为 bert.,head.classifier. 前缀为 classifier.)
  2. 将模型加载到 NPU 设备
  3. 对测试文本进行情感分类推理
  4. 输出预测结果及概率

4. Smoke 验证

python3 inference.py

预期输出示例:

Using device: npu:0
NPU available: True

Text: 这个产品非常好用,我很喜欢!
Prediction: 正面 (label: 1)
Probabilities: 负面=0.0095, 正面=0.9905

Text: 质量太差了,完全不推荐购买。
Prediction: 负面 (label: 0)
Probabilities: 负面=0.9875, 正面=0.0125

5. 性能参考

Batch Size平均延迟 (ms)P50 (ms)P90 (ms)P99 (ms)吞吐量 (samples/sec)
12.6802.6842.7302.759373.14
42.6012.6032.6352.6501537.61
82.6392.6212.6433.2843031.83
162.6292.6382.6592.6656086.98

6. 精度评测

精度验证方法:在 CPU 和 NPU 上分别运行相同输入,对比输出概率值。

指标数值
测试样本数10
最大概率差异0.000146 (0.0146%)
误差阈值< 1%
是否通过是
预测一致率10/10 (100%)

所有样本的 CPU 与 NPU 推理结果完全一致,最大概率差异仅 0.0146%,远低于 1% 的精度阈值。

7. 注意事项

  • 模型权重下载自 ModelScope,路径为 putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast
  • 模型权重中 encoder. 前缀在加载时需映射为 bert.,head.classifier. 前缀映射为 classifier.,以兼容 HuggingFace BertForSequenceClassification
  • 推理时需安装 torch_npu 以及对应的 PyTorch 版本
  • 该模型为小型模型(4层 Transformer),推理速度快,单条推理延迟通常 < 5ms
下载使用量0