opus-mt-fr-en 是 Helsinki-NLP 系列的多语言翻译模型,专门用于法语(FR)到英语(EN)的翻译任务,采用 6 层 Transformer 编码器 + 6 层解码器架构,参数量约 74M。
opus-mt-fr-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-17-1/opus-mt-fr-en/ 目录下:
pip install transformers torch_npu -i https://huaweimirror.com.cn/simpleRun the inference script for translation:
cd /data/ysws/agentsp/5-17-1/opus-mt-fr-en-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-17-1/opus-mt-fr-en-ascend/
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 翻译一致性 | 100% | 100% | PASS |
| 输出匹配 | True | True | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 2.127s |
| NPU 推理时间 | 0.132s |
| 加速比 | 16.14x |
| 输入 (法语) | 输出 (英语) |
|---|---|
| "Bonjour, comment allez-vous aujourd'hui?" | "Hello, how are you today?" |
| "Je suis très heureux de vous voir." | English translation |
| "Le temps est beau aujourd'hui." | English translation |
结果: CPU 和 NPU 输出的翻译结果完全一致
============================================================
OPUS-MT-FR-EN NPU Test
Model: Helsinki-NLP/opus-mt-fr-en (FR → EN)
Output: /data/ysws/agentsp/5-17-1/opus-mt-fr-en-ascend
============================================================
============================================================
OPUS-MT-FR-EN Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-17-1/opus-mt-fr-en/Helsinki-NLP/opus-mt-fr-en
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 256/256 [00:00<00:00, 4896.69it/s]
Input text: ["Bonjour, comment allez-vous aujourd'hui?"]
Input shape: torch.Size([1, 11])
Generated text: ['Hello, how are you today?']
Inference time: 0.949s
============================================================
Precision Test (CPU vs NPU)
============================================================
Loading tokenizer...
Loading model on CPU...
Loading weights: 100%|██████████| 256/256 [00:00<00:00, 4887.75it/s]
Running inference on CPU...
Loading model on NPU...
Loading weights: 100%|██████████| 256/256 [00:00<00:00, 7408.85it/s]
Running inference on NPU...
CPU inference time: 2.127s
NPU inference time: 0.132s
Speedup: 16.14x
CPU output: ['Hello, how are you today?']
NPU output: ['Hello, how are you today?']
Output texts match: True
Status: PASS
============================================================
Test Complete!
============================================================import torch
from transformers import MarianMTModel, MarianTokenizer
MODEL_DIR = "/data/ysws/agentsp/5-17-1/opus-mt-fr-en/Helsinki-NLP/opus-mt-fr-en"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()
src_texts = ["Bonjour, comment allez-vous aujourd'hui?"]
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 = [
"Bonjour, comment allez-vous aujourd'hui?",
"Je suis très heureux de vous voir.",
"Le temps est beau aujourd'hui."
]
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, tgt in zip(src_texts, translations):
print(f"{src} -> {tgt}")从 config.json 提取的关键参数:
{
"vocab_size": 59514,
"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,
"pad_token_id": 59513,
"eos_token_id": 0,
"bos_token_id": 0
}| Token | ID | 说明 |
|---|---|---|
| 0 | 序列结束 (eos_token_id) | |
| 59513 | 填充标记 (pad_token_id) |
A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。
A: 使用批处理可以显著提高吞吐量。另外,首次推理会有编译开销,后续推理会更快。
A: 本模型专门用于法语到英语的翻译。如需其他语言对,请访问 Helsinki-NLP 模型库。
本项目遵循 Apache-2.0 许可证