weixin_72661020/multilingual-sentiment-analysis
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

tabularisai/multilingual-sentiment-analysis 在昇腾 NPU 上的部署

1. 简介

本文档记录 tabularisai/multilingual-sentiment-analysis 在华为昇腾 Ascend910 NPU 环境的快速部署与验证结果。

tabularisai/multilingual-sentiment-analysis 是一个基于 DistilBERT 的多语言情感分析模型,支持 5 类情感分类:

  • Very Negative(极度负面)
  • Negative(负面)
  • Neutral(中性)
  • Positive(正面)
  • Very Positive(极度正面)

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/tabularisai/multilingual-sentiment-analysis
  • 权重下载地址(HuggingFace):https://huggingface.co/tabularisai/multilingual-sentiment-analysis

2. 验证环境

组件版本
NPUAscend910
PyTorch2.9.0
torch-npu2.9.0.post1+gitee7ba04
transformers适配版本
Python3.11.14
  • 模型路径:~/tabularisai/multilingual-sentiment-analysis/model/tabularisai/multilingual-sentiment-analysis

3. 模型推理

由于该模型为序列分类模型(非文本生成模型),直接使用 transformers 库进行推理:

import torch
import torch_npu
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model_path = "tabularisai/multilingual-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path, torch_dtype=torch.float32)
model = model.to("npu:0")
model.eval()

texts = ["I love this product!", "This is terrible."]
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
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)
id2label = model.config.id2label
for i, text in enumerate(texts):
    label = id2label.get(int(predictions[i])) or id2label.get(str(int(predictions[i])))
    print(f"Text: {text} -> Sentiment: {label}")

4. Smoke 验证

直接运行推理脚本验证:

cd ~/tabularisai/multilingual-sentiment-analysis
python3 inference.py

预期输出示例:

Text: This product is absolutely amazing and wonderful!
Predicted sentiment: Very Positive
Logits: [[-1.577, -1.576, -0.959, 0.658, 2.321]]

5. 性能参考

测试条件:10 条测试样本,单次推理,max_length=512。

指标CPUNPU (Ascend910)
平均推理时间55.65 ms8.28 ms
加速比1x6.72x

7. 注意事项

  • 模型为 float32 精度,可直接在 NPU 上运行,无需精度转换
  • 推理前需将模型移至 NPU 设备:model.to("npu:0")
  • 输入张量也需移至相同 NPU 设备
  • 首次推理后有 warmup 过程,性能测试需排除 warmup 时间
  • 该模型不支持 vLLM 推理,需直接使用 transformers 库

Ascend NPU 精度评测

NPU 推理验证:

指标数值
测试用例数10
精度结论---

精度评测源代码和日志详见 eval/ 目录。