冬
gcw_IDzXRVNw/visobert-14gb-corpus-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

visobert-14gb-corpus Ascend NPU 部署指南

项目简介

visobert-14gb-corpus 是基于 XLM-RoBERTa 的遮蔽语言模型(Masked Language Model),专门针对越南语和其他多语言场景训练。该模型能够预测被遮蔽的 token,适用于文本分类、情感分析等下游任务。

特性

  • 支持 Ascend NPU 推理加速
  • CPU 与 NPU 精度对比测试(误差 < 1%)
  • 多语言支持(以越南语为主)
  • 兼容 HuggingFace transformers
  • 2.14 倍加速比

环境要求

  • 硬件:华为 Ascend 910 系列 NPU
  • CANN:8.0.RC1 或更高版本
  • PyTorch:2.0+ 并带有 torch_npu
  • transformers:4.38+
  • sentencepiece

目录结构

visobert-14gb-corpus-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/visobert-14gb-corpus/5CD-AI/visobert-14gb-corpus/ 目录下:

  • model.safetensors - 模型权重 (约 390MB)
  • config.json - 模型配置
  • sentencepiece.bpe.model - SentencePiece 分词器
  • tokenizer.json - tokenizer 配置

4. 安装依赖

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

Usage

Method 1: Normal Inference Mode

Run the inference script for MLM prediction:

cd /data/ysws/agentsp/5-16/visobert-14gb-corpus-ascend/

python3 inference.py

python3 inference.py --mode inference

Method 2: Precision Test Mode (CPU vs NPU)

Run the precision comparison test:

cd /data/ysws/agentsp/5-16/visobert-14gb-corpus-ascend/

python3 inference.py --mode precision_test

命令行参数说明

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

测试验证

精度测试结果

指标实测值阈值状态
最大相对误差0.1530%< 1.00%PASS
CPU 推理时间0.511s--
NPU 推理时间0.239s--
加速比2.14x> 1xPASS

推理结果示例

输入: "Hello, I am a model and I can help with language tasks."

输出:

  • 预测被遮蔽位置的 token
  • 概率分数

测试日志

visobert-14gb-corpus NPU Test
Model: 5CD-AI/visobert-14gb-corpus (XLM-RoBERTa MLM)
Output: /data/ysws/agentsp/5-16/visobert-14gb-corpus-ascend

============================================================
Inference Test (NPU)
============================================================
Device: npu:0
Loading model...
Model loaded successfully
Input: Hello, I am a <mask> model and I can help with language tasks.
Input shape: torch.Size([1, 21])
Inference time: 0.227s

============================================================
Precision Test (CPU vs NPU)
============================================================
NPU Device: npu:0
Loading model...
Input shape: torch.Size([1, 21])
Running on CPU...
Running on NPU...
CPU inference time: 0.511s
NPU inference time: 0.239s
Speedup: 2.14x
Max absolute error: 3.955507e-02
Max relative error: 0.1530% (threshold: 1.0%)
Status: PASS

============================================================
Precision Test Result: PASS
============================================================

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

Python API 使用示例

基本推理

import torch
from transformers import XLMRobertaForMaskedLM

MODEL_DIR = "/data/ysws/agentsp/5-16/visobert-14gb-corpus/5CD-AI/visobert-14gb-corpus"

model = XLMRobertaForMaskedLM.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()

text = "Hello, I am a <mask> model."
inputs = tokenizer(text, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}

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

logits = outputs.logits
mask_token_id = tokenizer.mask_token_id
# 获取预测结果

模型结构

  • 架构类型: XLMRobertaForMaskedLM
  • 编码器: 12 层 Transformer
  • 隐藏层维度: 768
  • 注意力头数: 12
  • 参数量: ~125M
  • 词汇表大小: 15004
组件说明
roberta.embeddings词嵌入 + 位置嵌入
roberta.encoder12 层 Transformer 编码器
lm_head遮蔽语言模型头

推理参数配置

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

{
  "hidden_size": 768,
  "intermediate_size": 3072,
  "num_attention_heads": 12,
  "num_hidden_layers": 12,
  "vocab_size": 15004,
  "max_position_embeddings": 514
}

常见问题

Q: 精度测试失败?

A: 检查 NPU 驱动是否正确安装。XLM-RoBERTa 模型在 CPU 和 NPU 上的数值误差极小(< 0.2%),远低于 1% 阈值。

Q: 如何提高推理速度?

A: NPU 相比 CPU 有加速(2 倍),适合批量处理场景。

参考链接

  • 原始模型: https://huggingface.co/5CD-AI/visobert-14gb-corpus
  • XLM-RoBERTa 论文: https://arxiv.org/abs/1911.02116
  • HuggingFace Transformers: https://huggingface.co/transformers

许可证

本项目遵循 Apache-2.0 许可证