Opus-MT.AXERA
Helsinki-NLP OPUS-MT 中英互译模型(Marian)的 Axera AX650N 量化推理包: 中文 → 英文、英文 → 中文。
- Python 推理(
translate.py+python/opus_mt_ax.py) - C++ 可执行文件(
bin/opus_mt_ax650,aarch64) - 一键运行脚本(
run_ax650.sh) - 200 句 Tatoeba 精度/延迟评测
源码(ONNX 导出、Pulsar2 量化、C++ 源码)见 GitHub: ZY-2012/Opus-MT.AXERA
上游模型: Helsinki-NLP/opus-mt-zh-en · Helsinki-NLP/opus-mt-en-zh
模型
| 方向 | 目录 | 模型 | AXMODEL |
|---|---|---|---|
| 中文 → 英文 | models/zh-en/ |
opus-mt-zh-en | encoder 83 MiB + decoder 124 MiB |
| 英文 → 中文 | models/en-zh/ |
opus-mt-en-zh | encoder 83 MiB + decoder 124 MiB |
量化:Pulsar2 7.0-patch1,AX650,NPU1,U16 激活 + S8 权重。 静态 shape:source 128 token,target 64 token。
精度(Tatoeba test 200 句,greedy)
| 方向 | 路径 | BLEU | chrF | 与 FP32 输出一致 |
|---|---|---|---|---|
| zh→en | PyTorch FP32 | 33.48 | 54.73 | — |
| zh→en | AX650 U16 | 32.78 | 53.91 | 179/200 |
| en→zh | PyTorch FP32 | 28.63 | 24.49 | — |
| en→zh | AX650 U16 | 28.72 | 24.65 | 168/200 |
逐张量 cosine(20 组真实输入):encoder 0.99950.9998,decoder logits 0.99720.9996,
next-token argmax 20/20。详见 reports/accuracy.md。
速度(AX650N,batch=1,已预热)
| 方向 | encoder | decoder 单步 | 端到端 (C++) | 端到端 (Python) |
|---|---|---|---|---|
| zh→en | 4.90 ms | 5.41 ms | 58.5 ms/句 | 69.1 ms/句 |
| en→zh | 4.89 ms | 5.41 ms | 47.3 ms/句 | 56.2 ms/句 |
C++ 与 Python 200 句输出逐句一致。详见 reports/performance.md。
目录结构
Opus-MT.AXERA/
├── models/
│ ├── zh-en/ encoder.axmodel decoder.axmodel
│ │ vocab.json source.spm target.spm model_meta.json config.json
│ └── en-zh/ ...
├── python/opus_mt_ax.py # 纯 Python 运行时(numpy + sentencepiece + axengine)
├── translate.py # Python CLI
├── bin/opus_mt_ax650 # 预编译 C++ 可执行文件(aarch64)
├── run_ax650.sh # 一键运行(C++ / Python)
├── samples/ # 中英文示例输入
└── reports/ # 精度 / 性能报告
运行(AX650N 板端)
# C++(默认,最快),中文→英文,自带示例
bash run_ax650.sh
# 英文→中文(自动加 >>cmn_Hans<<)
bash run_ax650.sh cpp en-zh "This is a test."
# Python
bash run_ax650.sh python zh-en "这是一个测试。"
# 批量:每行一句
bash run_ax650.sh cpp zh-en --input in.txt --output out.txt --stats
手动调用:
export LD_LIBRARY_PATH=/soc/lib:$LD_LIBRARY_PATH
./bin/opus_mt_ax650 --direction zh-en --model-root models \
--text "这是一个测试。" --stats
python3 translate.py --direction en-zh --model-root models \
--text "This is a test." --stats
参数:
--direction zh-en | en-zh--text单句,或--input/--output批量文件--max-new-tokens(默认 64)--stats打印 encoder/decoder/step 延迟
作为 Python 库
import sys
sys.path.insert(0, "python")
from opus_mt_ax import Translator
tr = Translator("zh-en", model_root="models") # 或 "en-zh"
print(tr.translate("这是一个测试。"))
限制
- source 最多 128 token(超出截断,
stats["truncated"]为 true) - 输出最多 64 token
- 仅 greedy 解码
en-zh默认>>cmn_Hans<<(简体中文);其他中文变体可自行传前缀- C++ 源码与模型转换流程在 GitHub;本仓只放模型、Python 运行时和预编译二进制
致谢 / References
- 上游模型:Helsinki-NLP/opus-mt-zh-en · Helsinki-NLP/opus-mt-en-zh
- OPUS-MT:Helsinki-NLP/OPUS-MT · OPUS-MT-train · Tatoeba-Challenge
- 测试集:zho-eng · eng-zho · Tatoeba
- Marian / Transformers:huggingface/transformers
- Pulsar2:文档 · 发布页
- 板端运行时:AXERA-TECH/pyaxengine
- AX650N BSP:AXERA-TECH/ax650n_bsp_sdk
- 工作流参考:AXERA-TECH/Magnetar
- SentencePiece:google/sentencepiece
- JSON:nlohmann/json
- 部署工程模板:ZY-2012/FS-EEND.AXERA
- AXERA 开源主页:github.com/AXERA-TECH
License
推理代码 Apache-2.0。模型权重:opus-mt-zh-en CC-BY-4.0,
opus-mt-en-zh Apache-2.0。