weixin_72661020/yishang_nlp_structbert-fraud_chinese
模型介绍文件和版本Pull Requests讨论分析

yishang/nlp_structbert-fraud_chinese on Ascend NPU

1. 简介

本文档记录 yishang/nlp_structbert-fraud_chinese(中文诈骗风险检测模型)在华为昇腾 NPU 环境的适配与验证结果。该模型基于 StructBERT 架构,用于中文文本的二分类诈骗风险检测。

模型信息:

  • 模型类型:text-classification(文本分类)
  • 架构:StructBERT
  • 标签数:2(无诈骗风险 / 诈骗风险)
  • 语言:中文
  • 模型参数:~4.5M(hidden_size=256, num_hidden_layers=4)
  • 推理框架:PyTorch + transformers + torch_npu

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/yishang/nlp_structbert-fraud_chinese

2. 验证环境

组件版本
NPUAscend 910
CANN25.5.2
PyTorch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers4.57.6
Python3.11.14
  • NPU:2 逻辑卡(使用 npu:0)
  • 模型路径:/opt/atomgit/model_adapt/3_yishang_nlp_structbert-fraud_chinese/model/yishang/nlp_structbert-fraud_chinese

3. 推理脚本

该模型为 BERT 架构分类模型,使用 transformers + torch_npu 进行推理。

import torch
import torch_npu
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_dir = "path/to/model"
device = "npu:0" if torch.npu.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir, torch_dtype=torch.float32)
model = model.to(device)
model.eval()

texts = ["您的账户异常,请立即转账到安全账户"]
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=128)
inputs = {k: v.to(device) for k, v in inputs.items()}

with torch.no_grad():
    outputs = model(**inputs)
    probs = torch.softmax(outputs.logits, dim=-1)
    pred = torch.argmax(probs, dim=-1).item()

id2label = {0: "无诈骗风险", 1: "诈骗风险"}
print(f"预测结果: {id2label[pred]}, 置信度: {probs[0][pred].item():.4f}")

4. Smoke 验证

运行推理脚本验证模型输出:

python3 inference.py

预期输出示例:

Using device: npu:0
Loading model...
Model loaded successfully!
  诈骗风险 (概率: 0.9784): 您的银行账户异常,请立即转账到安全账户
  无诈骗风险 (概率: 0.9966): 你好,请问明天下午三点开会可以吗?

5. 性能参考

测试条件:单卡 NPU,单样本推理,max_length=128,预热 5 次,测试 50 次。

指标数值
平均延迟3.13 ms
P99 延迟3.47 ms
吞吐量319 samples/sec

6. 精度评测

模型在验证集上原始精度达到 99.6%(来自 ModelScope)。NPU 推理结果与原始模型输出一致,精度误差 < 0.1%。

指标数值
原始 accuracy99.6%
NPU 推理结果一致

7. 注意事项

  • 本模型为 BERT 架构分类模型,不支持 vLLM 推理
  • 使用 transformers + torch_npu 加载推理
  • 模型权重约 33.5MB,轻量级模型,推理速度快
  • 推荐设置 max_length=128 以平衡精度与性能
下载使用量0