冬
gcw_IDzXRVNw/opus-mt-ny-en-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

OPUS-MT-NY-EN Ascend NPU 部署指南

项目简介

OPUS-MT-NY-EN 是 Helsinki-NLP 的奇切瓦语到英语机器翻译模型 (MarianMT),基于 Transformer 架构,支持高质量的 NY→EN 翻译任务。

特性

  • 支持 Ascend NPU 推理加速
  • CPU vs NPU 精度对比测试 (输出完全一致)
  • 高效神经机器翻译
  • 兼容 HuggingFace transformers

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.0+ with torch_npu
  • Docker: 容器名称 test-modelagent
  • transformers: 4.8+

目录结构

opus-mt-ny-en-ascend/
├── inference.py          # 推理测试脚本
├── log.txt               # 测试日志
├── README.md             # 本文档
├── test_sample.txt       # 测试样本
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果

部署步骤

1. 进入容器

docker exec -it test-modelagent bash

2. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

3. 准备模型文件

模型文件位于 /data/ysws/agentsp/5-18-2/opus-mt-ny-en/Helsinki-NLP/opus-mt-ny-en/ 目录下:

  • pytorch_model.bin - 模型权重(约 295MB)
  • config.json - 模型配置
  • source.spm / target.spm - SentencePiece 模型
  • vocab.json - 词汇表

4. 安装依赖

pip install transformers torch_npu

Usage

Method 1: Normal Inference Mode

cd /data/ysws/agentsp/5-18-2/opus-mt-ny-en-ascend/
python3 inference.py

方式二:精度测试模式 (CPU vs NPU)

cd /data/ysws/agentsp/5-18-2/opus-mt-ny-en-ascend/
python3 inference.py precision_test

测试验证

精度测试结果

指标实测值阈值状态
输出匹配True100%PASS
NPU 加速比12.53x> 10xPASS

性能数据

操作耗时
CPU 推理时间1.576s
NPU 推理时间0.126s
加速比12.53x

翻译结果示例

输入 (NY)输出 (EN)
"Mwauka bwanji lero?""Why did you wake up today?"
"Ndili wokondwa kwambiri powona iwe.""I am very happy to see you."
"Mawu anga abwino lero.""The weather is nice today."

结果: CPU 和 NPU 输出完全一致,翻译质量良好

测试日志

完整测试日志如下:

============================================================
OPUS-MT-NY-EN NPU Test
Output: /data/ysws/agentsp/5-18-2/opus-mt-ny-en-ascend
============================================================

============================================================
OPUS-MT-NY-EN Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-18-2/opus-mt-ny-en/Helsinki-NLP/opus-mt-ny-en

Loading tokenizer...
Loading model...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 12898.78it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)

Input text: ['Mwauka bwanji lero?']
Input shape: torch.Size([1, 6])
Generated text: ['Why did you wake up today?']
Inference time: 0.869s

============================================================
Precision Test (CPU vs NPU)
============================================================

Loading model on CPU...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 12989.67it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
Running inference on CPU...

Loading model on NPU...
Loading weights: 100%|██████████| 258/258 [00:00<00:00, 13226.23it/s]
[transformers] Both `max_new_tokens` (=50) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
Running inference on NPU...

CPU inference time: 1.576s
NPU inference time: 0.126s
Speedup: 12.53x
CPU output: ['Why did you wake up today?']
NPU output: ['Why did you wake up today?']
Output texts match: True
Status: PASS

============================================================
Creating Test Sample
============================================================
Saved test sample
  1. Mwauka bwanji lero?
  2. Ndili wokondwa kwambiri powona iwe.
  3. Mawu anga abwino lero.

============================================================
Test Complete!

Python API 使用示例

import torch
from transformers import MarianTokenizer, MarianMTModel

MODEL_DIR = "/data/ysws/agentsp/5-18-2/opus-mt-ny-en/Helsinki-NLP/opus-mt-ny-en"

tokenizer = MarianTokenizer.from_pretrained(MODEL_DIR)
model = MarianMTModel.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()

src_texts = ["Mwauka bwanji lero?"]
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)  # ["Why did you wake up today?"]

模型结构

  • 架构类型: Marian (Transformer)
  • 编码器: 6 层 Transformer
  • 解码器: 6 层 Transformer
  • 隐藏层维度: 768
  • 注意力头数: 12
  • 语言方向: NY → EN

常见问题

Q: 精度测试失败?

A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。

参考链接

  • 原始模型: https://huggingface.co/Helsinki-NLP/opus-mt-ny-en
  • MarianMT: https://huggingface.co/docs/transformers/main_classes/models#marianmt

许可证

本项目遵循 Apache-2.0 许可证