OPUS-MT-IS-EN 是 Helsinki-NLP 的冰岛语到英语机器翻译模型 (MarianMT),基于 Transformer 架构,支持高质量的 IS→EN 翻译任务。
opus-mt-is-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-1/opus-mt-is-en/Helsinki-NLP/opus-mt-is-en/ 目录下:
pip install transformers torch_npuRun the inference script for translation:
cd /data/ysws/agentsp/5-18-1/opus-mt-is-en-ascend/
# 使用默认测试句子
python3 inference.py
# 指定设备
python3 inference.py npu:0运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-18-1/opus-mt-is-en-ascend/
# 运行完整精度测试
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
mode | 测试模式: all, inference, precision_test | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 输出匹配 | True | 100% | PASS |
| NPU 加速比 | 13.83x | > 10x | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 1.845s |
| NPU 推理时间 | 0.133s |
| 加速比 | 13.83x |
| 输入 (IS) | 输出 (EN) |
|---|---|
| "Halló, hvernig hefurðu það í dag?" | "Hello, how are you today?" |
| "Ég er mjög ánægður að sjá þig." | "I'm very happy to see you." |
| "Veðrið er frábært í dag." | "The weather is nice today." |
结果: CPU 和 NPU 输出完全一致,翻译质量良好
============================================================
OPUS-MT-IS-EN NPU Test
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-18-1/opus-mt-is-en/Helsinki-NLP/opus-mt-is-en
Input text: ['Halló, hvernig hefurðu það í dag?']
Input shape: torch.Size([1, 9])
Generated text: ['Hello, how are you today?']
Inference time: 1.433s
============================================================
Precision Test (CPU vs NPU)
============================================================
CPU inference time: 1.845s
NPU inference time: 0.133s
Speedup: 13.83x
CPU output: ['Hello, how are you today?']
NPU output: ['Hello, how are you today?']
Output texts match: True
Status: PASS
============================================================
Test Sample
============================================================
1. Halló, hvernig hefurðu það í dag?
2. Ég er mjög ánægður að sjá þig.
3. Veðrið er gott í dag.
============================================================结果: CPU 和 NPU 输出完全一致,NPU 加速比 13.83x
import torch
from transformers import MarianTokenizer, MarianMTModel
MODEL_DIR = "/data/ysws/agentsp/5-18-1/opus-mt-is-en/Helsinki-NLP/opus-mt-is-en"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()
src_texts = ["Halló, hvernig hefurðu það í dag?"]
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) # ["Hello, how are you today?"]src_texts = [
"Halló, hvernig hefurðu það í dag?",
"Ég er mjög ánægður að sjá þig.",
"Veðrið er frábært í dag."
]
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)
for src, trans in zip(src_texts, translations):
print(f"{src} -> {trans}")| 组件 | 说明 |
|---|---|
| encoder | 6 层 Transformer 编码器 |
| decoder | 6 层 Transformer 解码器 |
| vocab | SentencePiece 词汇表 (~50k) |
从 config.json 提取的关键参数:
{
"hidden_size": 768,
"encoder_layers": 6,
"decoder_layers": 6,
"encoder_attention_heads": 12,
"decoder_attention_heads": 12,
"d_model": 768
}A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。OPUS-MT 模型输出是确定性的,CPU 和 NPU 输出应完全一致。
A: 使用批处理可以显著提高吞吐量。另外,首次推理会有编译开销,后续推理会更快。
A: 本模型专门用于 IS (冰岛语) → EN (英语) 翻译。其他语言方向需要使用对应的 OPUS-MT 模型。
本项目遵循 Apache-2.0 许可证