opus-mt_tiny_cat-eng 是 Helsinki-NLP 开发的小型加泰罗尼亚语-英语机器翻译模型,基于 Transformer 架构优化后的 MarianMT 模型。该模型参数量较小(tiny 版本),专门针对加泰罗尼亚语到英语的翻译任务进行优化。
opus-mt_tiny_cat-eng-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/opus-mt_tiny_cat-eng/Helsinki-NLP/opus-mt_tiny_cat-eng/ 目录下:
pip install transformers torch_npu sacremoses -i https://pypi.huaweicloud.com/repository/pypi/simple/Run the inference script for Catalan-English translation:
cd /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/
python3 inference.py inference运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/
python3 inference.py precision_testcd /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/
python3 inference.py all| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式:inference、precision_test 或 all | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| CPU 推理时间 | 0.141s | - | - |
| NPU 推理时间 | 0.060s | - | - |
| 加速比 | 2.34x | > 1x | PASS |
| 输出文本一致性 | 完全一致 | - | PASS |
| CPU vs NPU 输出一致性 | True | - | PASS |
| 操作 | 耗时 |
|---|---|
| NPU 推理时间 | 0.904s |
| 精度测试 CPU 时间 | 0.141s |
| 精度测试 NPU 时间 | 0.060s |
| 输入 (加泰罗尼亚语) | 输出 (英语) |
|---|---|
| Bon dia, com estas avui? | Good morning, how are you today? |
| Estic molt content de veure't. | I am very happy to see you. |
| La traduccio automatica es molt util. | Automatic translation is very useful. |
| Avui el temps es molt bo. | Today the weather is very good. |
结果: CPU 和 NPU 输出的翻译结果完全一致,验证了 NPU 计算的正确性。
完整测试日志保存在 log.txt
============================================================
OPUS-MT-TINY-CAT-ENG NPU Test
Model: Helsinki-NLP/opus-mt_tiny_cat-eng
Output: /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend
============================================================
============================================================
OPUS-MT-TINY-CAT-ENG Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng/Helsinki-NLP/opus-mt_tiny_cat-eng
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 151/151 [00:00<00:00, 4950.60it/s]
Input text: ['Bon dia, com estas avui?']
Input shape: torch.Size([1, 9])
Generated text: ['Good morning, how are you today?']
Inference time: 0.904s
Inference result saved to /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/inference_result.json
============================================================
Precision Test (CPU vs NPU)
============================================================
Using device: npu:0
Loading tokenizer...
Loading model on CPU...
Loading weights: 100%|██████████| 151/151 [00:00<00:00, 4472.83it/s]
Running inference on CPU...
Loading model on npu:0...
Loading weights: 100%|██████████| 151/151 [00:00<00:00, 5067.29it/s]
Running inference on NPU...
CPU inference time: 0.141s
NPU inference time: 0.060s
Speedup: 2.34x
CPU output: ['Good morning, how are you today?']
NPU output: ['Good morning, how are you today?']
Output texts match: True
Status: PASS
Precision result saved to /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/precision_result.json
============================================================
Creating Test Sample
============================================================
Saved test sample: /data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng-ascend/test_sample.txt
1. Bon dia, com estas avui?
2. Estic molt content de veure't.
3. La traduccio automatica es molt util.
4. Avui el temps es molt bo.
============================================================
Test Complete!
============================================================import torch
from transformers import MarianMTModel, MarianTokenizer
MODEL_DIR = "/data/ysws/agentsp/5-17/opus-mt_tiny_cat-eng/Helsinki-NLP/opus-mt_tiny_cat-eng"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()
src_texts = ["Bon dia, com estas avui?"]
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)
translations = tokenizer.batch_decode(outputs, skip_special_tokens=True)
print(translations)src_texts = [
"Bon dia, com estas avui?",
"Estic molt content de veure't.",
"La traduccio automatica es molt util."
]
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)
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 | 2 层 Transformer 解码器(tiny) |
| lm_head | 语言模型输出头 |
从 config.json 提取的关键参数:
{
"model_type": "marian",
"d_model": 256,
"encoder_layers": 6,
"decoder_layers": 2,
"encoder_attention_heads": 8,
"decoder_attention_heads": 8,
"encoder_ffn_dim": 1536,
"decoder_ffn_dim": 1536,
"vocab_size": 32001,
"max_position_embeddings": 256,
"pad_token_id": 32000,
"eos_token_id": 0,
"bos_token_id": 0
}A: 检查 NPU 驱动是否正确安装。MarianMT 模型在 CPU 和 NPU 上的输出完全一致,验证了计算的正确性。
A: tiny 版本虽然参数量小,但在基本日常对话翻译上表现良好。复杂句子可能需要 larger 模型。
A: 使用批处理可以显著提高吞吐量。NPU 推理比 CPU 快 2.34 倍。
本项目遵循 Apache-2.0 许可证