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

opus-mt-en-gmw Ascend NPU 部署指南

项目简介

opus-mt-en-gmw 是 Helsinki-NLP 开发的多语言机器翻译模型,支持将英语(English)翻译成日耳曼语族(Germanic languages)语言,包括德语、荷兰语等。该模型基于 Transformer 架构的 MarianMT 模型,参数量约 220M。

特性

  • 支持 Ascend NPU 推理加速
  • CPU vs NPU 精度对比测试 (译文完全一致)
  • 多语言翻译支持 (英语 → 日耳曼语族)
  • Beam search 解码
  • 兼容 HuggingFace transformers

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.0+ with torch_npu
  • transformers: 4.8+

目录结构

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

使用方式

方式一:普通推理模式

cd /data/ysws/agentsp/5-20-1/opus-mt-en-gmw-ascend/

python3 inference.py

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

cd /data/ysws/agentsp/5-20-1/opus-mt-en-gmw-ascend/

python3 inference.py --precision_test

测试验证

精度测试结果

指标实测值阈值状态
译文匹配率100%100%PASS
NPU 加速比11.33x-显著加速

性能数据

操作耗时
平均 CPU 推理时间 (单句)1.4927s
平均 NPU 推理时间 (单句)0.1318s
NPU 加速比11.33x
8 句批量翻译总耗时1.1767s

推理结果示例

输入句子输出翻译
Hello, how are you today?Hallo, hoe geht es Ihnen heute?
Good morning!Guten Morgen!
Where is the bathroom?Waar is de badkamer?

结果: CPU 和 NPU 输出的翻译结果完全一致,NPU 相比 CPU 获得约 11.33x 加速

完整测试日志

============================================================
opus-mt-en-gmw Ascend NPU 部署测试
============================================================
MODEL_DIR: /data/ysws/agentsp/5-20-1/Helsinki-NLP/opus-mt-en-gmw
OUTPUT_DIR: /data/ysws/agentsp/5-20-1/opus-mt-en-gmw-ascend
Mode: precision_test

============================================================
创建测试样本
============================================================
测试句子已保存到: /data/ysws/agentsp/5-20-1/opus-mt-en-gmw-ascend/test_sentences.txt
共 8 句

============================================================
opus-mt-en-gmw NPU 推理测试
============================================================
Device: npu:0
Model loaded successfully!

测试句子数量: 8
  [1] Hello, how are you today?
  [2] Good morning!
  [3] Thank you very much.
  [4] Where is the bathroom?
  [5] I love you.
  [6] See you tomorrow!
  [7] How much does this cost?
  [8] I don't understand.

开始翻译 (device: npu:0)...

翻译结果:
  [1] 原文: Hello, how are you today?
      译文: Hallo, hoe geht es Ihnen heute?
  [2] 原文: Good morning!
      译文: Guten Morgen!
  [3] 原文: Thank you very much.
      译文: Vielen Dank.
  [4] 原文: Where is the bathroom?
      译文: Waar is de badkamer?
  [5] 原文: I love you.
      译文: Ich liebe dich.
  [6] 原文: See you tomorrow!
      译文: Bis morgen!
  [7] 原文: How much does this cost?
      译文: Hoeveel kost dit?
  [8] 原文: I don't understand.
      译文: Ik begrijp het niet.

总耗时: 1.1767s
平均每句: 0.1471s

============================================================
opus-mt-en-gmw 精度测试 (CPU vs NPU)
============================================================
Device: npu:0

加载 CPU 模型...
CPU 模型加载完成

加载 NPU 模型...
NPU 模型加载完成

测试句子数量: 3

--- 句子 1 ---
原文: Hello, how are you today?
CPU 译文: Hallo, hoe geht es Ihnen heute?
CPU 耗时: 1.9004s
NPU 译文: Hallo, hoe geht es Ihnen heute?
NPU 耗时: 0.1809s
译文匹配: True

--- 句子 2 ---
原文: Good morning!
CPU 译文: Guten Morgen!
CPU 耗时: 1.0754s
NPU 译文: Guten Morgen!
NPU 耗时: 0.0939s
译文匹配: True

--- 句子 3 ---
原文: Where is the bathroom?
CPU 译文: Waar is de badkamer?
CPU 耗时: 1.5023s
NPU 译文: Waar is de badkamer?
NPU 耗时: 0.1204s
译文匹配: True

============================================================
精度测试结果汇总
============================================================
译文完全匹配: PASS
平均 CPU 推理时间: 1.4927s
平均 NPU 推理时间: 0.1318s
NPU 加速比: 11.33x

精度阈值: 1.0%
译文匹配率: PASS

总体状态: PASS

============================================================
测试完成!
============================================================

Python API 使用示例

import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

MODEL_DIR = "/data/ysws/agentsp/5-20-1/Helsinki-NLP/opus-mt-en-gmw"

tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()

texts = ["Hello, how are you today?"]
inputs = tokenizer(texts, return_tensors="pt", padding=True)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}

with torch.no_grad():
    gen_ids = model.generate(
        inputs["input_ids"],
        attention_mask=inputs["attention_mask"],
        max_length=100,
        num_beams=4,
        early_stopping=True
    )

translations = tokenizer.batch_decode(gen_ids, skip_special_tokens=True)
print(translations)

模型结构

  • 架构类型: MarianMT (Transformer Encoder-Decoder)
  • 编码器: 6 层 Transformer
  • 解码器: 6 层 Transformer
  • 隐藏层维度: 512
  • 注意力头数: 8
  • 参数量: ~220M
  • 源语言: 英语 (en)
  • 目标语言: 日耳曼语族 (gmw)

参考链接

  • 原始模型: https://huggingface.co/Helsinki-NLP/opus-mt-en-gmw
  • Helsinki-NLP: https://github.com/Helsinki-NLP
  • HuggingFace Transformers: https://huggingface.co/transformers

许可证

本项目遵循 Apache-2.0 许可证