冬
gcw_IDzXRVNw/bert-emotion-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

bert-emotion Ascend NPU 部署指南

项目简介

bert-emotion 是基于 BERT-Mini 的轻量级情感分析模型,专门针对短文本情感检测优化。该模型能够将文本分类到 13 种情感类别,包括 happiness、sadness、anger、love 等。模型大小仅约 20MB,参数约 6M,适合边缘设备和实时应用。

特性

  • 支持 Ascend NPU 推理加速
  • CPU vs NPU 精度对比测试 (输出完全一致)
  • 13 类情感分类
  • 轻量级设计 (~20MB)
  • 2.5x 加速比

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.0+ with torch_npu
  • Docker: 容器名称 test-modelagent
  • transformers: 4.50+

目录结构

bert-emotion-ascend/
├── inference.py          # 推理测试脚本
├── log.txt               # 测试日志
├── README.md             # 本文档
├── test_sample.txt       # 测试样例
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果

部署步骤

1. 进入容器

docker exec -it test-modelagent bash

2. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

3. 准备模型文件

模型文件位于 /data/ysws/agentsp/5-16/bert-emotion/boltuix/bert-emotion/ 目录下:

  • model.safetensors - 模型权重 (约 45MB)
  • config.json - 模型配置
  • vocab.txt - 词汇表
  • tokenizer.json / tokenizer_config.json - 分词器文件

4. 安装依赖

pip install transformers torch_npu -i https://pypi.huaweicloud.com/repository/pypi/simple/

使用方式

方式一:普通推理模式

运行推理脚本进行情感分类:

cd /data/ysws/agentsp/5-16/bert-emotion-ascend/

python3 inference.py --mode inference

方式二:精度测试模式 (CPU vs NPU)

运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:

cd /data/ysws/agentsp/5-16/bert-emotion-ascend/

python3 inference.py --mode precision_test

方式三:完整测试 (推理 + 精度)

cd /data/ysws/agentsp/5-16/bert-emotion-ascend/

python3 inference.py --mode all

命令行参数说明

参数说明默认值
--mode测试模式: inference, precision_test 或 allall

测试验证

精度测试结果

指标实测值阈值状态
最大相对误差0.0110%< 1.00%PASS
最大绝对误差7.37e-04--
CPU 推理时间0.021s--
NPU 推理时间0.008s--
加速比2.46x> 1xPASS
预测标签一致性完全一致-PASS

性能数据

操作耗时
NPU 推理时间 (3 句)0.236s
精度测试 CPU 时间0.021s
精度测试 NPU 时间0.008s

分类结果示例

输入文本预测情感
"I am so happy today! Everything is going great!"happiness
"This is really disappointing and makes me sad."sadness
"I can't believe it! That's amazing news!"surprise

结果: 模型正确识别了文本中的情感类别。

测试日志

============================================================
BERT-Emotion NPU Test
Model: boltuix/bert-emotion
Output: /data/ysws/agentsp/5-16/bert-emotion-ascend
============================================================

============================================================
BERT-Emotion Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-16/bert-emotion/boltuix/bert-emotion
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 73/73 [00:00<00:00, 4817.25it/s]
Model loaded successfully
Input texts: ['I am so happy today! Everything is going great!', 'This is really disappointing and makes me sad.', "I can't believe it! That's amazing news!"]
Input shape: torch.Size([3, 15])
Logits shape: torch.Size([3, 13])
Predictions: [5, 0, 3]
Labels: ['happiness', 'sadness', 'surprise']
Inference time: 0.236s

Inference result saved to /data/ysws/agentsp/5-16/bert-emotion-ascend/inference_result.json

============================================================
Precision Test (CPU vs NPU)
============================================================
Using device: npu:0
Loading tokenizer...
Loading model on CPU...
Loading weights: 100%|██████████| 73/73 [00:00<00:00, 4589.30it/s]
Loading model on npu:0...
Loading weights: 100%|██████████| 73/73 [00:00<00:00, 4248.72it/s]
Running inference on CPU...
Running inference on NPU...
CPU inference time: 0.021s
NPU inference time: 0.008s
Speedup: 2.46x
Max absolute error: 7.374287e-04
Max relative error: 0.0110% (threshold: 1.0%)
CPU predictions: [5] -> happiness
NPU predictions: [5] -> happiness
Predictions match: True
Status: PASS

Precision result saved to /data/ysws/agentsp/5-16/bert-emotion-ascend/precision_result.json

============================================================
Creating Test Sample
============================================================
Saved test sample: /data/ysws/agentsp/5-16/bert-emotion-ascend/test_sample.txt

============================================================
Test Complete!
============================================================

Python API 使用示例

基本情感分类

import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

MODEL_DIR = "/data/ysws/agentsp/5-16/bert-emotion/boltuix/bert-emotion"

tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)

model = model.to("npu:0").eval()

texts = ["I am so happy today!", "This is really disappointing."]
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=128)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}

with torch.no_grad():
    outputs = model(**inputs)

predictions = torch.argmax(outputs.logits, dim=-1)
labels = [model.config.id2label[p.item()] for p in predictions]
print(labels)

实时情感检测

def detect_emotion(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
    inputs = {k: v.to("npu:0") for k, v in inputs.items()}

    with torch.no_grad():
        outputs = model(**inputs)

    prediction = torch.argmax(outputs.logits, dim=-1).item()
    return model.config.id2label[prediction]

emotion = detect_emotion("I'm feeling really anxious about this.")
print(f"Detected emotion: {emotion}")

模型结构

  • 架构类型: BertForSequenceClassification
  • 编码器层数: 4
  • 隐藏层维度: 256
  • 注意力头数: 4
  • 前馈网络维度: 1024
  • 词汇表大小: 30522
  • 分类类别数: 13
Emotion ID情感名称
0sadness
1anger
2love
3surprise
4fear
5happiness
6neutral
7disgust
8shame
9guilt
10confusion
11desire
12sarcasm

推理参数配置

从 config.json 提取的关键参数:

{
  "model_type": "bert",
  "hidden_size": 256,
  "num_hidden_layers": 4,
  "num_attention_heads": 4,
  "intermediate_size": 1024,
  "vocab_size": 30522,
  "max_position_embeddings": 512,
  "attention_probs_dropout_prob": 0.1,
  "hidden_dropout_prob": 0.1
}

常见问题

Q: 精度测试失败?

A: 检查 NPU 驱动是否正确安装。BERT-Emotion 模型在 CPU 和 NPU 上的输出完全一致,误差极小 (0.01%)。

Q: 如何提高推理速度?

A: 使用批处理可以提高吞吐量。NPU 推理非常快 (0.008s vs CPU 0.021s)。

Q: 模型支持中文吗?

A: 本模型专门针对英语文本的情感分析。如需中文支持,请查找对应语言的情感分析模型。

参考链接

  • 原始模型: https://huggingface.co/boltuix/bert-emotion
  • BERT 论文: https://arxiv.org/abs/1810.04805
  • HuggingFace Transformers: https://huggingface.co/transformers

许可证

本项目遵循 Apache-2.0 许可证