本文档记录 Helsinki-NLP/opus-mt-zh-en 在 昇腾 Ascend NPU 环境的原生 PyTorch 部署与验证结果。
该模型为 MarianMTModel 架构(encoder-decoder 翻译模型),由于 vLLM 0.18.0 暂不支持 MarianMT 架构,因此采用 transformers + torch_npu 原生推理方式在 NPU 上运行。所有算子均由 torch_npu 自动映射到 Ascend CANN,无需手动替换或修改模型代码。
验证要点:
相关获取地址:
参考文档:
| 组件 | 版本 |
|---|---|
transformers | 4.57.6 |
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
modelscope | 1.35.3 |
2 逻辑卡(Ascend910_9362)/opt/atomgit/.cache/modelscope/hub/models/Helsinki-NLP/opus-mt-zh-en8.5.1模型权重与配置文件仅允许从 ModelScope 下载:
from modelscope import snapshot_download
model_dir = snapshot_download('Helsinki-NLP/opus-mt-zh-en', revision='master')
print(model_dir)
# /opt/atomgit/.cache/modelscope/hub/models/Helsinki-NLP/opus-mt-zh-en下载文件清单:
| 文件 | 大小 | 说明 |
|---|---|---|
config.json | 1.4 KB | 模型配置 |
pytorch_model.bin | 312 MB | PyTorch 权重 |
tokenizer_config.json | 44 B | 分词器配置 |
vocab.json | 1.6 MB | 词表 |
source.spm | 805 KB | 源语言 SentencePiece |
target.spm | 807 KB | 目标语言 SentencePiece |
generation_config.json | 293 B | 生成配置 |
from transformers import MarianMTModel, MarianTokenizer
import torch
import torch_npu
MODEL_PATH = "/opt/atomgit/.cache/modelscope/hub/models/Helsinki-NLP/opus-mt-zh-en"
# 1. 从 ModelScope 本地路径加载
tokenizer = MarianTokenizer.from_pretrained(MODEL_PATH)
model = MarianMTModel.from_pretrained(MODEL_PATH)
# 2. 迁移至 NPU
model = model.to("npu")
model.eval()
# 3. 推理
text = "你好,世界!"
inputs = tokenizer(text, return_tensors="pt").to("npu")
with torch.no_grad():
outputs = model.generate(**inputs)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result) # Hello, world!| 验证项 | 结果 |
|---|---|
| ModelScope 本地加载 | 通过 |
| NPU 设备迁移 | 通过 |
| 中文→英文单句翻译 | 通过 |
| CPU / NPU 输出一致性 | 5/5 完全一致 |
| 无算子报错 | 通过 |
| 中文输入 | CPU 输出 | NPU 输出 | 一致性 |
|---|---|---|---|
| 你好,世界! | Hello, world! | Hello, world! | 一致 |
| 今天天气很好。 | It's a nice day. | It's a nice day. | 一致 |
| 人工智能正在改变我们的生活。 | Artificial intelligence is changing our lives. | Artificial intelligence is changing our lives. | 一致 |
| 这只猫很可爱。 | This cat is cute. | This cat is cute. | 一致 |
| 华为昇腾NPU提供了强大的AI算力。 | China has provided the NPU with a powerful AI calculus. | China has provided the NPU with a powerful AI calculus. | 一致 |
测试条件:连续运行取稳定值,单句最大长度 512 tokens。
| 指标 | CPU | NPU (Ascend910B) |
|---|---|---|
Mean | 1765.64 ms | 202.66 ms |
P50 | 1764.40 ms | 201.24 ms |
P99 | 1772.12 ms | 211.44 ms |
Min | 1762.06 ms | 200.82 ms |
Max | 1772.12 ms | 211.44 ms |
| 加速比 | — | 8.71x |
| Batch Size | CPU (sentences/s) | NPU (sentences/s) | 加速比 |
|---|---|---|---|
1 | 1.82 | 13.56 | 7.44x |
2 | 2.05 | 19.21 | 9.37x |
4 | 1.76 | 22.03 | 12.52x |
8 | — | 39.79 | — |
| 指标 | 数值 |
|---|---|
Allocated | 305.7 MB |
Reserved | 468.0 MB |
模型参数量 | 77.9 M |
由于 MarianMT 为翻译模型,精度评测采用 CPU / NPU 输出一致性 方式进行对比验证。
| 指标 | 数值 |
|---|---|
| 评测方式 | CPU vs NPU 输出对比 |
| 测试样本数 | 5 |
| 一致样本数 | 5 |
| 一致率 | 100% |
| 差异样本数 | 0 |
结论:NPU 推理输出与 CPU 基准输出完全一致,算子精度无偏差。
vLLM 不支持:vLLM 0.18.0 的模型注册表中不包含 MarianMTModel,因此该模型无法通过 vLLM-Ascend 部署。如需在 NPU 上运行,请使用原生 transformers + torch_npu 方式。
torch_npu 内部警告:推理过程中可能出现以下警告,不影响功能与精度:
UserWarning: Cannot create tensor with internal format while allow_internal_format=False,
tensor will be created with base format.可通过设置环境变量消除:
export ALLOW_INTERNAL_FORMAT=1首次推理延迟:NPU 首次推理包含算子图编译开销,延迟与 CPU 接近;后续推理因算子缓存显著加速。
生产环境优化建议:
export TASK_QUEUE_ENABLE=1
export CPU_AFFINITY_CONF=1可进一步提升吞吐性能。
Tokenizer 依赖:首次加载 tokenizer 时可能提示 pip install sacremoses,该依赖为可选,不影响基础分词功能。