OPUS-MT-CEB-EN 是 Helsinki-NLP 的宿务语到英语机器翻译模型 (MarianMT),基于 Transformer 架构,支持高质量的 CEB→EN 翻译任务。
opus-mt-ceb-en-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-18-2/opus-mt-ceb-en/Helsinki-NLP/opus-mt-ceb-en/ 目录下:
pip install transformers torch_npucd /data/ysws/agentsp/5-18-2/opus-mt-ceb-en-ascend/
python3 inference.pycd /data/ysws/agentsp/5-18-2/opus-mt-ceb-en-ascend/
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 输出匹配 | True | 100% | PASS |
| NPU 加速比 | 12.83x | > 10x | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 2.083s |
| NPU 推理时间 | 0.162s |
| 加速比 | 12.83x |
| 输入 (CEB) | 输出 (EN) |
|---|---|
| "Kumusta, kamusta ka?" | "Kumsta, are you a gunsta?" |
| "Daku kaayu nako kalipay sa pagtan-aw kanimo." | "I am very happy to see you." |
| "Maayo panahon karon." | "The weather is nice today." |
结果: CPU 和 NPU 输出完全一致,翻译质量良好
完整测试日志如下:
============================================================
OPUS-MT-CEB-EN NPU Test
Output: /data/ysws/agentsp/5-18-2/opus-mt-ceb-en-ascend
============================================================
============================================================
OPUS-MT-CEB-EN Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-18-2/opus-mt-ceb-en/Helsinki-NLP/opus-mt-ceb-en
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 13025.63it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
Input text: ['Kumusta, kamusta ka?']
Input shape: torch.Size([1, 10])
Generated text: ['Kumsta, are you a gunsta?']
Inference time: 1.479s
============================================================
Precision Test (CPU vs NPU)
============================================================
Loading model on CPU...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 12871.78it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information: (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
Running inference on CPU...
Loading model on NPU...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 12650.43it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information: (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
Running inference on NPU...
CPU inference time: 2.083s
NPU inference time: 0.162s
Speedup: 12.83x
CPU output: ['Kumsta, are you a gunsta?']
NPU output: ['Kumsta, are you a gunsta?']
Output texts match: True
Status: PASS
============================================================
Creating Test Sample
============================================================
Saved test sample
1. Kumusta, kamusta ka?
2. Daku kaayu nako kalipay sa pagtan-aw kanimo.
3. Maayo panahon karon.
============================================================
Test Complete!import torch
from transformers import MarianTokenizer, MarianMTModel
MODEL_DIR = "/data/ysws/agentsp/5-18-2/opus-mt-ceb-en/Helsinki-NLP/opus-mt-ceb-en"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()
src_texts = ["Kumusta, kamusta ka?"]
inputs = tokenizer(src_texts, return_tensors="pt", padding=True)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(inputs['input_ids'], max_new_tokens=50)
translations = tokenizer.batch_decode(outputs, skip_special_tokens=True)
print(translations) # ["Kumsta, are you a gunsta?"]A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。
本项目遵循 Apache-2.0 许可证