本文档记录 putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast 在华为昇腾 Ascend NPU 环境上的适配与验证结果。
该模型是一个基于 StructBERT 的中文情感分类模型,对输入文本进行正面/负面二分类。
相关获取地址:
| 组件 | 版本 |
|---|---|
NPU | Ascend 910 |
torch | 2.5.1 |
torch-npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
Python | 3.11.14 |
~/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast/model/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast/使用提供的推理脚本直接运行:
cd /opt/atomgit/putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fast
python3 inference.py推理脚本会自动完成以下操作:
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| Batch Size | 平均延迟 (ms) | P50 (ms) | P90 (ms) | P99 (ms) | 吞吐量 (samples/sec) |
|---|---|---|---|---|---|
| 1 | 2.680 | 2.684 | 2.730 | 2.759 | 373.14 |
| 4 | 2.601 | 2.603 | 2.635 | 2.650 | 1537.61 |
| 8 | 2.639 | 2.621 | 2.643 | 3.284 | 3031.83 |
| 16 | 2.629 | 2.638 | 2.659 | 2.665 | 6086.98 |
精度验证方法:在 CPU 和 NPU 上分别运行相同输入,对比输出概率值。
| 指标 | 数值 |
|---|---|
| 测试样本数 | 10 |
| 最大概率差异 | 0.000146 (0.0146%) |
| 误差阈值 | < 1% |
| 是否通过 | 是 |
| 预测一致率 | 10/10 (100%) |
所有样本的 CPU 与 NPU 推理结果完全一致,最大概率差异仅 0.0146%,远低于 1% 的精度阈值。
putaoshu/nlp_structbert_sentiment-classification_chinese-tiny-fastencoder. 前缀在加载时需映射为 bert.,head.classifier. 前缀映射为 classifier.,以兼容 HuggingFace BertForSequenceClassification