本文档记录 iic/nlp_structbert_emotion-classification_chinese-tiny 在昇腾 Ascend910 环境的适配、验证与部署结果。
nlp_structbert_emotion-classification_chinese-tiny 是一个基于 StructBERT 的中文情感分类模型,参数量 33.5M,支持 7 类情感的多标签分类:
模型轻量小巧(hidden_size=256, 4 层 Transformer),非常适合边缘部署和实时推理场景。
相关获取地址:
| 组件 | 版本 |
|---|---|
| NPU | Ascend910 (64GB HBM) |
| CANN | 8.5.1 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0 (CANN 8.5.1) |
| transformers | 4.25.1 (最低) |
| Python | 3.11.14 |
~/iic/nlp_structbert_emotion-classification_chinese-tiny/model/iic/nlp_structbert_emotion-classification_chinese-tiny由于该模型为 BERT 分类模型(非生成式模型),直接使用 transformers 库加载推理,无需启动 vLLM 服务。
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
device = "npu:0" if hasattr(torch, "npu") and torch.npu.is_available() else "cpu"
model_dir = "~/iic/nlp_structbert_emotion-classification_chinese-tiny/model/iic/nlp_structbert_emotion-classification_chinese-tiny"
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir, torch_dtype=torch.float32)
model = model.to(device)
model.eval()
text = "今天天气真好,心情非常愉快!"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probs = torch.sigmoid(outputs.logits) # multi_label_classification
id2label = {0: "恐惧", 1: "愤怒", 2: "厌恶", 3: "喜好", 4: "悲伤", 5: "高兴", 6: "惊讶"}
for i, prob in enumerate(probs.cpu().numpy()[0]):
print(f"{id2label[i]}: {prob:.4f}")直接运行推理脚本:
cd ~/iic/nlp_structbert_emotion-classification_chinese-tiny
python3 inference.py预期输出(部分示例):
--- Input: 今天天气真好,心情非常愉快! ---
高兴: 0.8448
喜好: 0.0879
Predicted emotions: ['高兴']测试条件:单样本推理,batch_size=1,max_length=128。
| 指标 | 数值 |
|---|---|
| 平均推理延迟 | 2.56 ms |
| P50 延迟 | 2.55 ms |
| P90 延迟 | 2.63 ms |
| 吞吐量 | 340 samples/sec |
| 模型参数量 | 33.5M |
| 模型大小 | 33.5 MB (pytorch_model.bin) |
文本长度对延迟影响极小(短/中/长文本差异 < 0.1ms),表明模型推理主要由计算开销决定而非 IO。
使用 10 个自定义中文情感测试用例进行评估(多标签分类,sigmoid 阈值 0.5)。
| 指标 | 数值 |
|---|---|
| 平均精度 (Accuracy@0.5) | 0.40 |
| 测试用例数 | 10 |
| 情感类别数 | 7 |
典型正确分类:
注:多标签分类中当多个情感同时存在时,单一预测可能无法覆盖所有标签,实际使用时建议降低阈值或人工校验。
multi_label_classification 问题类型,输出层使用 sigmoid 激活函数