opus-mt-de-ar 是基于 Helsinki-NLP OPUS-MT 的德语到阿拉伯语翻译模型,采用 6 层 Transformer 编码器-解码器架构 (MarianMT),可在 512 长度序列上进行高质量翻译。
opus-mt-de-ar-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_sentences.txt # 测试句子
├── precision_result.json # 精度测试结果
└── inference_result.json # 推理输出结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-20/opus-mt-de-ar/ 目录下:
pytorch_model.bin - 模型权重 (约 304MB)config.json - 模型配置vocab.json - 词表文件source.spm / target.spm - SentencePiece 模型tokenizer_config.json - 分词器配置pip install transformers torch_npu sacremoses运行翻译推理(仅NPU):
cd /data/ysws/agentsp/5-20/opus-mt-de-ar-ascend/
python3 inference.py运行精度对比测试,验证 NPU 翻译结果与 CPU 一致性:
cd /data/ysws/agentsp/5-20/opus-mt-de-ar-ascend/
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 翻译结果一致性 | 完全一致 | 相等 | PASS |
| CPU 推理时间 (3句) | 4.48s | - | - |
| NPU 推理时间 (3句) | 1.27s | - | - |
| 加速比 | 3.54x | > 1x | PASS |
| 输入 (德语) | 输出 (阿拉伯语) |
|---|---|
| Guten Morgen, wie geht es Ihnen? | صباح الخير، كيف حالك؟ |
| Ich freue mich, Sie kennenzulernen. | من الجيد مقابلتك. |
| Das Leben ist schön wenn man es genießt. | الحياة جميلة عندما تستمتع بها. |
结果: CPU 和 NPU 输出的翻译结果完全一致,翻译质量正常
============================================================
opus-mt-de-ar - Ascend NPU Translation Test
Output: /data/ysws/agentsp/5-20/opus-mt-de-ar-ascend
============================================================
Mode: PRECISION TEST
NPU available: True
Device: npu:0
============================================================
Loading Model and Tokenizer
============================================================
Tokenizer loaded successfully
Model loaded successfully
============================================================
Running CPU Translation
============================================================
Input: Guten Morgen, wie geht es Ihnen?
Output: صباح الخير، كيف حالك؟
Input: Ich freue mich, Sie kennenzulernen.
Output: من الجيد مقابلتك.
Input: Das Leben ist schön wenn man es genießt.
Output: الحياة جميلة عندما تستمتع بها.
CPU total time: 4.4760s
============================================================
Running NPU Translation
============================================================
Input: Guten Morgen, wie geht es Ihnen?
Output: صباح الخير، كيف حالك؟
Input: Ich freue mich, Sie kennenzulernen.
Output: من الجيد مقابلتك.
Input: Das Leben ist schön wenn man es genießt.
Output: الحياة جميلة عندما تستمتع بها.
NPU total time: 1.2659s
Speedup: 3.54x
============================================================
Precision Test Results
============================================================
Translation outputs match: PASS
============================================================
Test Complete!
============================================================import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
MODEL_DIR = "/data/ysws/agentsp/5-20/opus-mt-de-ar"
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
device = torch.device("npu:0")
model = model.to(device)
model.eval()
sentences = ["Guten Morgen, wie geht es Ihnen?"]
inputs = tokenizer(sentences, return_tensors="pt", padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], max_new_tokens=100)
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Translation: {translation}")sentences = [
"Guten Morgen, wie geht es Ihnen?",
"Ich freue mich, Sie kennenzulernen.",
"Das Leben ist schön wenn man es genießt."
]
inputs = tokenizer(sentences, return_tensors="pt", padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], max_new_tokens=100)
translations = [tokenizer.decode(out, skip_special_tokens=True) for out in outputs]
for src, tgt in zip(sentences, translations):
print(f"{src} -> {tgt}")| 组件 | 说明 |
|---|---|
| encoder | 6 层 Transformer 编码器 |
| decoder | 6 层 Transformer 解码器 |
| lm_head | 语言模型头部 |
从 config.json 提取的关键参数:
{
"d_model": 512,
"encoder_layers": 6,
"decoder_layers": 6,
"encoder_attention_heads": 8,
"decoder_attention_heads": 8,
"encoder_ffn_dim": 2048,
"decoder_ffn_dim": 2048,
"vocab_size": 61153,
"max_position_embeddings": 512,
"scale_embedding": true
}A: 检查分词器是否正确加载,确保使用 AutoTokenizer.from_pretrained() 而非手动加载。
A: NPU 推理已针对大规模矩阵运算优化,当前加速比约 3.5x。首次推理会有编译开销,后续推理会更快。
A: 修改 max_new_tokens 参数,默认值为 100。
A: 本模型仅支持德语 (de) 到阿拉伯语 (ar) 的翻译。其他语言对需要使用对应的模型。
本项目遵循 Apache-2.0 许可证