利用Optimum-intel快速部署Qwen3-embedding系列模型
Qwen3-embedding示例:https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-embedding/qwen3-embedding.ipynb。完成模型转换后,我们同样可以利用Optimum-intel来进行模型部署,当创建以“OV”为前缀的模型任务对象后,Optimum-int
作为Qwen 模型家族的新成,Qwen3 Embedding 系列模型专为文本表征、检索与排序任务设计,基于 Qwen3 基础模型进行训练,充分继承了 Qwen3 在多语言文本理解能力方面的优势。在多项基准测试中,Qwen3 Embedding 系列在文本表征和排序任务中展现了卓越的性能。
Optimum-intel是面向Transformers库兼容的模型转换和部署工具,支持Intel OpenVINO™推理后端,这篇文章将分享如何利用Optimum-intel快速在Intel平台上部署Qwen3-embedding系列模型。
内容列表
1. 环境准备
2. 模型下载和转换
3. 模型部署
第一步,环境准备
通过以下命令可以搭建基于Python的模型部署环境。
%pip install -q "git+https://github.com/huggingface/optimum-intel.git"
%pip install -qU "openvino>=2025.2" "openvino_tokenizers>=2025.2"
第二步,模型下载和转换
在部署模型之前,我们首先需要将原始的PyTorch模型转换为OpenVINO™的IR静态图格式,并对其进行压缩,以实现更轻量化的部署和最佳的性能表现。通过Optimum提供的命令行工具optimum-cli,我们可以一键完成模型的格式转换和权重量化任务:
其中Embedding模型可以用<feature-extraction>任务类型导出:
optimum-cli export openvino --model Qwen3-Embedding-0.6B --trust-remote-code --task feature-extraction --weight-format fp16 Qwen3-Embedding-0.6B -ov
Reranker模型可以用<text-generation>任务类型导出:
optimum-cli export openvino --model Qwen3-Reranker-0.6B --trust-remote-code --task text-generation --weight-format fp16 Qwen3-Reranker-0.6B-ov
第三步,模型部署
完成模型转换后,我们同样可以利用Optimum-intel来进行模型部署,当创建以“OV”为前缀的模型任务对象后,Optimum-intel会使用OpenVINO™作为模型的后端执行推理,并通过device参数来指定模型部署硬件。
其中Embedding模型可以用< OVModelForFeatureExtraction>任务对象推理:
from optimum.intel import OVModelForFeatureExtraction
model = OVModelForFeatureExtraction.from_pretrained(model_dir, device=device)
Reranker模型可以用< OVModelForCausalLM >任务对象推理:
from optimum.intel import OVModelForCausalLM
model = OVModelForCausalLM.from_pretrained(model_dir, device=device)
完成OpenVINO™模型对象创建后,开发者可以该对象直接替换官方示例中的Transformers模型对象,复用官方示例进行模型部署。这里以Embedding模型为例,基于以下参考代码可以构建一个简单的文本相似度比较任务:
import torch
import torch.nn.functional as F
from torch import Tensor
from transformers import AutoTokenizer
def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
left_padding = attention_mask[:, -1].sum() == attention_mask.shape[0]
if left_padding:
return last_hidden_states[:, -1]
else:
sequence_lengths = attention_mask.sum(dim=1) - 1
batch_size = last_hidden_states.shape[0]
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
def get_detailed_instruct(task_description: str, query: str) -> str:
return f"Instruct: {task_description}\nQuery:{query}"
# Each query must come with a one-sentence instruction that describes the task
task = "Given a web search query, retrieve relevant passages that answer the query"
queries = [get_detailed_instruct(task, "What is the capital of China?"), get_detailed_instruct(task, "Explain gravity")]
# No need to add instruction for retrieval documents
documents = [
"The capital of China is Beijing.",
"Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
]
input_texts = queries + documents
tokenizer = AutoTokenizer.from_pretrained(model_dir, padding_side="left")
max_length = 8192
# Tokenize the input texts
batch_dict = tokenizer(
input_texts,
padding=True,
truncation=True,
max_length=max_length,
return_tensors="pt",
)
batch_dict.to(model.device)
outputs = model(**batch_dict)
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict["attention_mask"])
# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = embeddings[:2] @ embeddings[2:].T
print(scores.tolist())
更多示例可以参考:https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/qwen3-embedding
总结
可以看到,利用OpenVINO™工具套件,我们可以非常轻松地将转换后的Qwen3-embedding系列模型部署在Intel的硬件平台上,从而进一步在本地构建起各类基于LLM的服务和应用。
参考资料
Qwen3-embedding示例:https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-embedding/qwen3-embedding.ipynb
Qwen3-reranker示例: https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/qwen3-embedding/qwen3-reranker.ipynb
更多推荐
所有评论(0)