opus-mt_tiny_spa-glg 是 Helsinki-NLP 开发的小型西加利西亚机器翻译模型,基于 Transformer 架构优化后的 MarianMT 模型。该模型参数量较小 (tiny 版本),专门针对西班牙语到加利西亚语的翻译任务进行优化。
opus-mt_tiny_spa-glg-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_spa-glg/Helsinki-NLP/opus-mt_tiny_spa-glg/ 目录下:
pip install transformers torch_npu sacremoses -i https://pypi.huaweicloud.com/repository/pypi/simple/运行推理脚本进行西加利西亚翻译:
cd /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend/
python3 inference.py inference运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend/
python3 inference.py precision_testcd /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend/
python3 inference.py all| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式: inference, precision_test 或 all | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| CPU 推理时间 | 0.144s | - | - |
| NPU 推理时间 | 0.061s | - | - |
| 加速比 | 2.35x | > 1x | PASS |
| 输出文本一致性 | 完全一致 | - | PASS |
| CPU vs NPU 输出一致性 | True | - | PASS |
| 操作 | 耗时 |
|---|---|
| NPU 推理时间 | 1.027s |
| 精度测试 CPU 时间 | 0.144s |
| 精度测试 NPU 时间 | 0.061s |
| 输入 (西班牙语) | 输出 (加利西亚语) |
|---|---|
| Buenos dias, como estas hoy? | Bos días, como estás hoxe? |
| Estoy muy contento de verte. | Estou moi contento de verte. |
| La traduccion automatica es muy util. | A tradución automática é moi útil. |
| Hoy el tiempo es muy bueno. | Hoxe o tempo é moi bo. |
结果: CPU 和 NPU 输出的翻译结果完全一致,验证了 NPU 计算的正确性。
完整测试日志保存在 log.txt
============================================================
OPUS-MT-TINY-SPA-GLG NPU Test
Model: Helsinki-NLP/opus-mt_tiny_spa-glg
Output: /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend
============================================================
============================================================
OPUS-MT-TINY-SPA-GLG Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg/Helsinki-NLP/opus-mt_tiny_spa-glg
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 151/151 [00:00<00:00, 5221.96it/s]
Input text: ['Buenos dias, como estas hoy?']
Input shape: torch.Size([1, 10])
Generated text: ['Bos días, como estás hoxe?']
Inference time: 1.027s
Inference result saved to /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-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, 4793.45it/s]
Running inference on CPU...
Loading model on npu:0...
Loading weights: 100%|██████████| 151/151 [00:00<00:00, 4687.66it/s]
Running inference on NPU...
CPU inference time: 0.144s
NPU inference time: 0.061s
Speedup: 2.35x
CPU output: ['Bos días, como estás hoxe?']
NPU output: ['Bos días, como estás hoxe?']
Output texts match: True
Status: PASS
Precision result saved to /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend/precision_result.json
============================================================
Creating Test Sample
============================================================
Saved test sample: /data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg-ascend/test_sample.txt
1. Buenos dias, como estas hoy?
2. Estoy muy contento de verte.
3. La traduccion automatica es muy util.
4. Hoy el tiempo es muy bueno.
============================================================
Test Complete!
============================================================import torch
from transformers import MarianMTModel, MarianTokenizer
MODEL_DIR = "/data/ysws/agentsp/5-17/opus-mt_tiny_spa-glg/Helsinki-NLP/opus-mt_tiny_spa-glg"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()
src_texts = ["Buenos dias, como estas hoy?"]
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 = [
"Buenos dias, como estas hoy?",
"Estoy muy contenido de verte.",
"La traduccion automatica es muy 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.35 倍。
本项目遵循 Apache-2.0 许可证