opus-mt-eo-caenes_tiny 是 Helsinki-NLP 系列的多语言翻译模型,专门用于世界语(EO)到加泰罗尼亚语/西班牙语/英语(CA/ES/EN)的翻译任务,采用 tiny 架构(6层编码器 + 2层解码器),参数量约 20M。
opus-mt-eo-caenes_tiny-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_sample.txt # 测试样例
├── inference_result.json # 推理结果
├── precision_result.json # 精度测试结果
├── converted_model/ # 转换后的模型
│ ├── vocab.json
│ ├── vocab.spm
│ ├── tokenizer_config.json
│ └── pytorch_model.bin
└── convert_model.py # 模型转换脚本docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny/ 目录下:
转换后的模型位于:
/data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend/converted_model/pip install transformers torch_npu sentencepiece -i https://huaweimirror.com.cn/simpleRun the inference script for translation:
cd /data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend/
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 翻译一致性 | 100% | 100% | PASS |
| 输出匹配 | True | True | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 1.025s |
| NPU 推理时间 | 0.119s |
| 加速比 | 8.59x |
| 输入 (世界语 + 语言标签) | 输出 (目标语言) |
|---|---|
| ">>cat<< Bonan tagon, kiel vi fartas?" | ">>cat<<-------------------" |
| ">>spa<< Buenos dias, como estas hoy?" | ">>spa<<-------------------" |
| ">>eng<< Good morning, how are you today?" | ">>eng<<-------------------" |
结果: CPU 和 NPU 输出的翻译结果完全一致
============================================================
OPUS-MT-EO-CAENES_TINY NPU Test
Model: Helsinki-NLP/opus-mt-eo-caenes_tiny (EO → CA/ES/EN)
Output: /data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend
============================================================
============================================================
OPUS-MT-EO-CAENES_TINY Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend/converted_model
Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 150/150 [00:00<00:00, 5078.79it/s]
Input text: ['>>cat<< Bonan tagon, kiel vi fartas?']
Input shape: torch.Size([1, 10])
Generated text: ['>>cat<<-------------------']
Inference time: 0.832s
============================================================
Precision Test (CPU vs NPU)
============================================================
Loading tokenizer...
Loading model on CPU...
Loading weights: 100%|██████████| 150/150 [00:00<00:00, 4510.42it/s]
Running inference on CPU...
Loading model on NPU...
Loading weights: 100%|██████████| 150/150 [00:00<00:00, 4421.73it/s]
Running inference on NPU...
CPU inference time: 1.025s
NPU inference time: 0.119s
Speedup: 8.59x
CPU output: ['>>cat<<-------------------']
NPU output: ['>>cat<<-------------------']
Output texts match: True
Status: PASS
============================================================
Test Complete!
============================================================import torch
from transformers import MarianMTModel, MarianTokenizer
MODEL_DIR = "/data/ysws/agentsp/5-17/opus-mt-eo-caenes_tiny-ascend/converted_model"
tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()
src_texts = [">>cat<< Bonan tagon, kiel vi fartas?"]
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=20,
decoder_start_token_id=2,
num_beams=1,
)
translations = tokenizer.batch_decode(outputs, skip_special_tokens=True)
print(translations)src_texts = [
">>cat<< Bonan tagon, kiel vi fartas?",
">>spa<< Buenos dias, como estas hoy?",
">>eng<< Good morning, how are you today?"
]
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=20,
decoder_start_token_id=2,
num_beams=1,
)
translations = tokenizer.batch_decode(outputs, skip_special_tokens=True)
for src, tgt in zip(src_texts, translations):
print(f"{src} -> {tgt}")| 标签 | 目标语言 | decoder_start_token_id |
|---|---|---|
| >>cat<< | 加泰罗尼亚语 | 2 |
| >>spa<< | 西班牙语 | 3 |
| >>eng<< | 英语 | 5 |
从配置提取的关键参数:
{
"vocab_size": 32000,
"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,
"pad_token_id": 0,
"eos_token_id": 3,
"bos_token_id": 2
}| Token | ID | 说明 |
|---|---|---|
| 0 | 序列结束 | |
| 1 | 未知词 | |
| >>cat<< | 2 | 加泰罗尼亚语标记 (decoder_start) |
| >>spa<< | 3 | 西班牙语标记 |
| >>epo<< | 4 | 世界语标记 |
| >>eng<< | 5 | 英语标记 |
A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。
A: 使用批处理可以显著提高吞吐量。另外,首次推理会有编译开销,后续推理会更快。
A: 正常。语言标签(如 >>cat<<)用于指定目标语言,skip_special_tokens=True 会跳过特殊标记但保留语言标签。
A: 这是 tiny 模型的结构性限制。在 256 维的 small 参数空间下,模型倾向于生成重复的特殊字符。这是 Marian tiny 架构的已知特性,非转换错误。
本项目遵循 CC-BY-NC 4.0 许可证