Intel OpenVINO™ Day0 完成阿里通义 Qwen3 快速布置
前语。
Qwen3 是阿里通义团队近期最新发布的文本生成系列模型,供给完好掩盖全参数和混合专家(MoE)架构的模型系统。经过海量数据练习,Qwen3 在逻辑推理、指令遵从、。智能。体才能及多言语支撑等维度完结突破性提高。而 OpenVINO 东西套件则能够协助。开发者。快速构建依据 LLM 的运用,充分运用。 AI。PC 异构算力,完结高效推理。
本文将以。Qwen3-8B。为例,介绍怎么运用 OpenVINO 的。 Python。A。PI。在。英特尔。途径(。GPU。, NPU)Qwen3 系列模型。
内容列表。
01 环境预备。
02 模型下载和转化。
03 模型布置。
01 Environment Preparation。
02 Model Downlo。ad。and Conversion。
03 Model Deployment。
环境预备。
Environment Preparation。
依据以下指令能够完结模型布置使命在 Python 上的环境装置。
Use the following commands to set up the Python environment for model deployment:。
python-m venv py-venv./py_venv/Scripts/。ac。ti。vate.batpip install--pre-U openvino openvino-tokenize。rs。--extra-index-urlhttp://sstorage.openvinotoolkit.org/。sim。ple/wheels/hightlypip intall nncfpip intallgit+https://github.com/openvino-dev-samples/optimum-。intel。.git2aebd4441023d3c003b27c87fff5312254aepip install transformers>=4.51.3。
模型下载和转化。
Model Download and Conversion。
在布置模型之前,咱们首要需要将原始的 PyTorch 模型转化为 OpenVINO 的 IR 静态图格局,并对其进行紧缩,以完结更轻量化的布置和最佳的功用体现。经过 Optimum 供给的指令行东西 optimum-cli,咱们能够一键完结模型的格局转化和权重量化使命:
Before deployment, we must convert the original PyTorch model to In。te。rmediate Representation (IR) format of OpenVINO and compress it for lightweight deployment and optimal pe。rf。ormance. Use the optimum-cli tool to perform model conversion and weight quantization in one step:。
optimum-cli export openvino --model Qwen/Qwen3-8B --task text-generation-with-past --weight-format int4 --group-size128--ratio0.8 Qwen3-8B-int4-ov。
开发者能够依据模型的输出成果,调整其间的量化参数,包含:
--model:为模型在 HuggingFace 上的 model id,这儿咱们也提早下载原始模型,并将 model id 替换为原始模型的本地途径,针对国内开发者,引荐运用 ModelScope 魔搭社区作为原始模型的下载途径,详细加载办法能够参阅 ModelScope 官方攻略:https://www.modelscope.cn/docs/models/download。
--weight-format:量化精度,能够挑选fp32,fp16,int8,int4,int4_sym_g128,int4_asym_g128,int4_sym_g64,int4_asym_g64。
--group-size:权重里同享量化参数的通道数量。
--ratio:int4/int8 权重份额,默以为1.0,0.6表明60%的权重以 int4 表,40%以 int8 表明。
--sym:是否敞开对称量化。
此外咱们主张运用以下参数对运转在NPU上的模型进行量化,以到达功用和精度的平衡。
Developers。 can。adjust quantization pa。ram。eters based on model output results, including:。
--model:The model ID on HuggingFace. For local models, replace it with the local path. For Chinese developers, ModelScope is。 recom。mended for model downloads.s。
--weight-format:Quantization precision (options: fp32, fp16, int8, int4, etc.).。
--group-size:Number of channels sharing quantization parameters.。
--ratio:int4/int8 weight ratio (default: 1.0).。
--sym:Enable symmetric quantization.。
For NPU-optimized quantization, use following command:。
optimum-cli export openvino--modelQwen/Qwen3-8B --tasktext-generation-with-past--weight-formatnf4--sym--group-size-1Qwen3-8B-nf4-ov--backup-precisionint8_sym。
模型布置。
Model Deployment。
OpenVINO 现在供给两种针对大言语模型的布置计划,假如您习惯于 Transformers 库的。接口。来布置模型,并想体会相对更丰厚的功用,引荐运用依据 Python 接口的 Optimum-intel 东西来进行使命建立。假如您想测验更极致的功用或是轻量化的布置办法,GenAI API 则是不贰的挑选,它一起支撑 Python 和。 C++。两种。编程。言语,装置容量不到200MB。
OpenVINO currently offers two deployment methods for large language models (LLMs). If you are accustomed to deploying models via the Transformers library interface and seek richer functionality, it is recommended to use the Python-based Optimum-intel tool for task implementation. For those aiming for peak performance or lightweight deployment, the GenAI API is the optimal choice. It supports both Python and C++ programming languages, with an installation footprint of less than 200MB.。
OpenVINO 为大言语模型供给了两种布置办法:
OpenVINO offers two deployment approaches for large language models:。
Optimum-intel 布置实例。
Optimum-intel Deployment Example。
from optimum.intel.openvino import OVModelForCausalLMfrom transformers import AutoConfig, AutoTokenizerov_model = OVModelForCausalLM.from_pretrained( llm_model_path, device='GPU',)tokenizer = AutoTokenizer.from_pretrained(llm_model_path)prompt ="Give me a short introduction tolarge language model."messages = [{"role":"user","content": prompt}]text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=True)model_inputs = tokenizer([text], return_tensors="pt")generated_ids = ov_model.generate(**model_inputs, max_new_tokens=1024)output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()try: index = len(output_ids) - output_ids[::-1].index(151668)except ValueError: index = 0thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("")content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("")print("thinking content:", thinking_content)print("content:", content)。
GenAI API 布置示例。
GenAI API Deployment Example。
importopenvino_genai asov_genaigeneration_config=ov_genai.GenerationConfig()generation_config.max_new_tokens =128generation_config.apply_chat_template =Falsepipe=ov_genai.LLMPipeline(llm_model_path,"GPU")result = pipe.generate(prompt, generation_config)。
这儿能够修正 device name 的办法将模型轻松布置到NPU上。
To deploy the model on NPU, you can replace the device name from “GPU” to “NPU”.。
pipe= ov_genai.LLMPipeline(llm_model_path,"NPU")。
当然你也能够经过以下办法完结流式输出。
To enable streaming mode, you can customize a streamer for OpenVINO GenAI pipeline.。
defstreamer(。sub。word):print(subword, end='', flush=True) sys.stdout.flush()returnFalsepipe.generate(prompt, generation_config, streamer=streamer)。
此外,GenAI API 供给了 chat 形式的构建办法,经过声明 pipe.start_chat()以及pipe.finish_chat(),多轮谈天中的历史数据将被以 kvcache 的形状,在内存中进行办理,然后提高运转功率。
Additionally, the GenAI API provides a chat mode implementation. By invoking pipe.start_chat() and pipe.finish_chat(), history data from multi-turn conversations is managed in memory as kvcache, which can significantly boost inference efficiency.。
pipe.start_chat()whileTrue: try: prompt =input('question:') exceptEOFError: break pipe.generate(prompt, generation, streamer) print('----------')pipe.finish_chat()。
Chat形式输出成果示例:
Output of Chat mode:。
总结。
Conclusion。
假如你对功用基准感兴趣,能够拜访全新上线的 OpenVINO 模型。中心。(Model Hub)。这儿供给了在 Intel。 CPU。、集成 GPU、NPU 及其他加速器上的模型功用数据,协助你找到最适合自己解决计划的硬件途径。
Whether using Optimum-intel or OpenVINO GenAI API, developers can effortlessly deploy the converted Qwen3 model on Intel hardware platforms, enabling the creation of diverse LLM-based services and applications locally.。
参阅资料。
Reference。
llm-chatbot notebook:。
https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-chatbot。
GenAI API:
https://github.com/openvinotoolkit/openvino.genai。
内容来源:https://bachduy.com/app-1/sohude tower,http://chatbotjud.saude.mg.gov.br/app-1/game-70-bet
(责任编辑:新闻)