visobert-14gb-corpus 是基于 XLM-RoBERTa 的遮蔽语言模型(Masked Language Model),专门针对越南语和其他多语言场景训练。该模型能够预测被遮蔽的 token,适用于文本分类、情感分析等下游任务。
visobert-14gb-corpus-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_sample.txt # 测试样例
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-16/visobert-14gb-corpus/5CD-AI/visobert-14gb-corpus/ 目录下:
pip install transformers torch_npu sentencepiece -i https://pypi.huaweicloud.com/repository/pypi/simple/Run the inference script for MLM prediction:
cd /data/ysws/agentsp/5-16/visobert-14gb-corpus-ascend/
python3 inference.py
python3 inference.py --mode inferenceRun the precision comparison test:
cd /data/ysws/agentsp/5-16/visobert-14gb-corpus-ascend/
python3 inference.py --mode precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式: all, inference 或 precision_test | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 最大相对误差 | 0.1530% | < 1.00% | PASS |
| CPU 推理时间 | 0.511s | - | - |
| NPU 推理时间 | 0.239s | - | - |
| 加速比 | 2.14x | > 1x | PASS |
输入: "Hello, I am a
输出:
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!
============================================================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
# 获取预测结果| 组件 | 说明 |
|---|---|
| roberta.embeddings | 词嵌入 + 位置嵌入 |
| roberta.encoder | 12 层 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
}A: 检查 NPU 驱动是否正确安装。XLM-RoBERTa 模型在 CPU 和 NPU 上的数值误差极小(< 0.2%),远低于 1% 阈值。
A: NPU 相比 CPU 有加速(2 倍),适合批量处理场景。
本项目遵循 Apache-2.0 许可证