混元翻译模型在 intel 平台上的使用

openlab_96bf3613 更新于 2天前

1. 模型简介

在全球化沟通日益频繁的今天,高效、精准的跨语言翻译工具成为刚需。Hunyuan-MT 系列模型正是为满足这一需求而生的专业翻译解决方案 —— 不仅专注于 33 种语言的互译能力,更特别支持 5 种民汉语言的顺畅转换,同时通过集成模型的优化设计,能将多个翻译结果融合升级,呈现更自然、准确的输出效果。

除了核心翻译功能,本文更聚焦于 “落地实用性”:手把手教你如何借助 OpenVINO™ 工具,将这款优秀的翻译模型成功部署在 Intel 平台上实现高效推理。内容由浅入深、层层递进,既覆盖了老版本 7B 参数量模型的经典用法,也带来了 HY-MT1.5 版本 1.8B 参数量模型的尝鲜体验,全程围绕 optimum-intel 和 GenAI 两大工具链,详细拆解原始模型下载、格式转换、推理代码编写与功能验证的完整流程。

老版原始模型地址链接 https://modelscope.cn/models/Tencent-Hunyuan/Hunyuan-MT-7B

新版原始模型地址连接 https://www.modelscope.cn/models/Tencent-Hunyuan/HY-MT1.5-1.8B

注:以下全部流程均在 Windows 11 Pro 24H2 系统中完成。Python 版本为 3.12.3。

2. 使用 OpenVINO™的优势

使用 OpenVINO™可以充分利用 intel 的硬件资源,可以分别部署到 CPU、iGPU 或者 NPU 上。让您电脑中平时大多数时间都处于空闲的 NPU,作为您的翻译助手,并且不会占用 CPU 和 iGPU 的资源。

3. 新模型尝鲜 HY-MT1.5-1.8B

首先设置用户权限,以管理员身份打开终端,输入:

Set-ExcutionPolicy –Scope CurrentUser

然后在弹出的 ExcutionPolicy: 提示符的后面输入 RemoteSigned,然后按回车即可

注意:此操作会更改该用户的系统权限,如果不想更改系统设置,可以使用 cmd 命令后,再继续后面的步骤即可。

创建 python3 的虚拟环境

python -m venv envenv\Scripts\activate

提示:如果在 PS 前缀的环境中使用,则执行的为 activate.ps1,如果是 cmd 环境中使用,则执行的是 activate.bat

然后安装 modelscope

pip install modelscope

然后下载新模型的原始模型

modelscope download --model "Tencent-Hunyuan/HY-MT1.5-1.8B" --local_dir "HY-MT1.5-1.8"

当原版模型下载好后,需要转换为 IR 格式,才能使用 OpenVINO™进行推理。首先我们要安装转换的相关依赖

注意:需要在这些步骤之前完成 git 与 git-lfs 的安装。我这里的安装包如下

参考下载链接:

https://github.com/git-for-windows/git/releases/download/v2.52.0.windows.1/Git-2.52.0-64-bit.exe

https://github.com/git-lfs/git-lfs/releases/download/v3.7.1/git-lfs-windows-v3.7.1.exe

然后继续执行下面的安装命令

pip install torch --index-url="https://download.pytorch.org/whl/cpu"pip install openvino nncfpip install git+https://github.com/openvino-dev-samples/optimum-intel.git@hunyuanpip install git+https://github.com/huggingface/optimum-onnx.gitpip install transformers==4.57.3

然后上述命令的执行过程中,可能会出现一些错误,可以忽略

然后对模型进行转换

optimum-cli export openvino --model HY-MT1.5-1.8 --task text-generation-with-past --weight-format int4 --group-size 128 --ratio 0.8 HY-MT1.5-1.8-int4-ov

然后运行推理,这里我们简单编写一段推理的代码,该代码基于 optimum-intel。

from transformers import AutoModelForCausalLM, AutoTokenizerfrom optimum.intel import OVModelForCausalLMimport osmodel_name_or_path = "C:\\AIGC\\20251223_HunyuanMT\\HY-MT1.5-1.8-int4-ov "  ##  改成您的模型保存的路径tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)model = OVModelForCausalLM.from_pretrained(model_name_or_path, device="GPU", device_map="auto")  # You may want to use bfloat16 and/or move to GPU heremessages = [    {"role": "user", "content": "Translate the following segment into Chinese, without additional explanation.\n\nIt’s on the house."},]tokenized_chat = tokenizer.apply_chat_template(    messages,    tokenize=True,    add_generation_prompt=False,    return_tensors="pt")
output*****odel.generate(tokenized_chat.to(model.device), max_new_tokens=2048)output_text = tokenizer.decode(outputs[0])##  打印原始输出print(output_text)
def extract_content(text: str) -> str:    start_marker = "<|hy_place▁holder▁no▁8|>"    end_marker = "<|hy_place▁holder▁no▁2|>"    start_index = text.find(start_marker)    if start_index == -1:        return ""  # 未找到开始标记        # 从开始标记之后查找结束标记    end_index = text.find(end_marker, start_index + len(start_marker))    if end_index == -1:        return ""  # 未找到结束标记       # 返回两个标记之间的内容    return text[start_index + len(start_marker):end_index]##  只打印翻译结果print(extract_content(output_text))

然后将上述代码保存为 infer1.py,然后执行

python infer1.py

就可以完成推理了。

 

4. 老版本的原始模型的下载 (7B)

从魔搭上下载原版格式的模型

modelscope download --model "Tencent-Hunyuan/Hunyuan-MT-7B" --local_dir "Hunyuan-MT-7B"

然后按照上述同样的方法进行转换和推理

如果一切顺利,就能看到日志如下:

5. 转换为 NPU 可以推理的格式

想让 NPU 运行的关键在于修改量化参数,具体命令如下

optimum-cli export openvino --model Hunyuan-MT-7B --task text-generation-with-past --weight-format int4 --sym --group-size -1 --ratio 1.0 Hunyuan-MT-7B-int4-ov-npu

6. 尝试在 NPU 上推理

想在 NPU 上推理,需要使用 OpenVINO™的 GenAI,首先要安装它

pip install openvino-genai

然后编写代码如下:

import openvino_genai as ov_genai
model_path = "C:\\AIGC\\20251223_HunyuanMT\\Hunyuan-MT-7B-int4-ov-npu"
device = "NPU"pipe = ov_genai.LLMPipeline(model_path, device)print(pipe.generate("Translate the following segment into Chinese, without additional explanation.\n\nIt’s on the house.”, max_new_tokens=2048))

然后将上述代码保存为 infer2.py,然后执行

python infer2.py

注意,如果在此时运行报错,可能是由于转换的步骤存在一定的随机性,可以尝试重新转换一个,再次尝试就可以成功推理。

此时可以看到 NPU 有负载,说明本次推理在 NPU 上进行

可以看到,使用 GenAI 的代码更加简洁,并且支持 NPU 推理。

7. 模型能力的粗略对比

我这里尝试了几种常用的中文英文的翻译短句,分别在两个模型上运行,然后人工得到一些运行结果:

source text:年龄上的差异。Hunyuan-MT-7B-int4-ov The difference in age.Hunyuan-MT-1.8B-int4-ov Age difference.
source text:但是在回望过去的时候,我希望今天我分享的一些人生故事,能够对你们有。Hunyuan-MT-7B-int4-ov But as I look back on the past, I hope that some of the life stories I shared today can be of some help to you.Hunyuan-MT-1.8B-int4-ov But looking back, I hope that some of the stories I told today will be of some use to you.
source text:些许的帮助。如果没有帮助的话,也希望你一会儿能带走一点点快乐,好吗?Hunyuan-MT-7B-int4-ov A little help would be appreciated. Even if there’s no real help, I hope you can take away a bit of joy with you later, okay?Hunyuan-MT-1.8B-int4-ov Some help is needed. If you don’t get any help, at least make yourself a little more happy, okay?
source text:我的大学呢其实是在西安上一个外国语大学,我们班当时有32个人,只有两个男生。Hunyuan-MT-7B-int4-ov My university was actually Xi'an International Studies University. There were 32 people in our class at that time, and only two of them were boys.Hunyuan-MT-1.8B-int4-ov I attended a university in Xi'an, which specializes in foreign languages. There were 32 students in our class, and only two of them were boys.
source text:然后另外一个同学呢,还因为身体不好,常年请假。Hunyuan-MT-7B-int4-ov Then there was another student who, due to poor health, took frequent leave from school throughout the year.Hunyuan-MT-1.8B-int4-ov Another student was unable to attend school due to health issues.
source text:呃,我印象特别深刻,当时大家都很关心我,老师也关心我。所以当时我经常上课的时候。Hunyuan-MT-7B-int4-ov Well, I really remember that it made a deep impression on me; everyone was very concerned about me at that time, and so were the teachers. That’s why I often attended classes regularly.Hunyuan-MT-1.8B-int4-ov Well, I remember very well how everyone was concerned about me, including my teachers. So I often had to attend classes during that period.
source text:老师环视一周。Hunyuan-MT-7B-int4-ov The teacher looked around the room.Hunyuan-MT-1.8B-int4-ov The teacher walked around the room.
source text:我藏在角落里。Hunyuan-MT-7B-int4-ov I hid in the corner.Hunyuan-MT-1.8B-int4-ov I hid in a corner.

8. 总结

通过实际操作与效果验证,HY-MT1.5-1.8B 模型的表现着实令人惊喜。相较于参数量更大的 7B 版本,它在资源占用上大幅 “瘦身”,却在翻译精度、语句流畅度等核心能力上毫不逊色 —— 无论是短句的精准转换,还是长文本的连贯表达,两者都各有亮点、难分伯仲。

这也正是轻量化模型的核心价值:以更小的硬件开销,实现几乎同等水平的功能输出。对于想要充分利用 Intel 平台闲置 NPU 资源(或 CPU、iGPU)搭建专属翻译助手的用户来说,无论是追求极致效率的 1.8B 版本,还是注重全面性能的 7B 版本,结合 OpenVINO™ 工具链都能实现便捷部署。希望本文的实操指南能为你打开思路,让优秀的翻译模型真正落地到日常使用场景中,为跨语言沟通带来更多便利。

如需咨询或交流相关信息,欢迎添加 OpenVINO 小助手微信(微信号 : OpenVINO-China),加入专属社群,与技术专家实时沟通互动。

0个评论