基于香橙派AI pro的Qwen3-0.6B的om模型推理部署
收藏回复举报
基于香橙派AI pro的Qwen3-0.6B的om模型推理部署
发表于2026-01-05 19:08:59
0 查看

基于香橙派AI pro的Qwen3-0.6B的om模型推理部署

本项目已开源于: gitcode.com/BB_out_snow/qwen-ascend-llm

项目推理的模型为Qwen3-0.6B。

未作推理效果评测,未作模型优化。

本项目实现了Qwen3-0.6B模型在香橙派AI pro的边缘设备上的推理部署。并将转换为OM模型,使其适配昇腾的框架进行部署推理。

一、运行环境

1.1 项目基础环境

组件版本
昇腾开发套件香橙派AI pro 8T 16G
昇腾芯片310B4
操作系统系统openeluer22.03
Ascend-cann-toolkit版本8.3.RC1
Ascend-cann-kernels-310b版本8.3.RC1
npu版本npu-smi 23.0.0
python版本python>=3.9

1.2 python环境参考

python版本为 3.9

参考依赖包如下:

onnx==1.16.1
onnxruntime==1.18.1
transformers==4.37.0 #当前项目只能使用该版本,该版本中没有qwn3的模型配置文件,需要手动创建,已手动修改脚本

# torch-npu==2.1.0.post6
# torch==2.1.0.post6 需要从华为官方的参考连接中下载,防止版本不适配
# https://www.hiascend.com/document/detail/zh/Pytorch/600/configandinstg/instg/insg_0006.html
# 本项目测试使用的是torch_npu-2.1.0.post17-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 
# 本项目测试使用的是torch-2.1.0+cpu-cp39-cp39-linux_x86_64.whl

tqdm
fastapi
uvicorn
sse_starlette==1.6.5
pydantic==2.1.1
openai

二、下载项目文件以及模型转换

2.0 增加交换空间

# 创建 16GB 的 Swap 文件(路径可自定义,如 /swapfile、/mnt/swapdisk/swapfile)
sudo fallocate -l 16G /swapfile
# 设置权限(仅 root 可读写)
sudo chmod 600 /swapfile
# 格式化 Swap 文件
sudo mkswap /swapfile
# 启用 Swap
sudo swapon /swapfile
# 验证是否生效
free -h
swapon --show
# 持久化配置(重启后自动挂载)
# 将 Swap 文件写入 /etc/fstab
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
# 验证 /etc/fstab 是否正确
cat /etc/fstab | grep swapfile
# 验证总体资源概况
htop
# openeluer 系统下的root用户使用cann环境
source ~/.bashrc

2.1 香橙派的环境配置

1.切换到主目录

cd /home/HwHiAiUser

2.克隆本项目

git clone https://gitcode.com/BB_out_snow/qwen-ascend-llm.git

3.赋权文件夹

#不用赋权也行
chmod -R 777 /home/HwHiAiUser/qwen-ascend-llm

4.切换到本项目的工作目录

cd /home/HwHiAiUser/qwen-ascend-llm

5.创建python环境

conda create  -n ascend-llm python=3.9

6.激活环境

conda activate ascend-llm

7.下载python环境的!!!部分依赖!!!

pip install -r requirements.txt

8.根据华为昇腾官网资料提示下载,防止版本不对

# 需要从华为官方的参考连接中下载,防止版本不适配
# torch-npu==2.1.0.post6
# torch==2.1.0.post6 
# https://www.hiascend.com/document/detail/zh/Pytorch/600/configandinstg/instg/insg_0006.html
# 本项目测试使用的是torch_npu-2.1.0.post17-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 
# 本项目测试使用的是torch-2.1.0+cpu-cp39-cp39-linux_x86_64.whl

2.2Windows的环境配置

模型从转换需要在上位机上面转换为onnx,在香橙派上转换耗时过长。

1.切换到主目录

cd /d D:\

2.克隆本项目

git clone https://gitcode.com/BB_out_snow/qwen-ascend-llm.git

4.切换到本项目的工作目录

cd /d D:\qwen-ascend-llm

5.创建python环境

conda create  -n ascend-llm python=3.9

6.激活环境

conda activate ascend-llm

7.下载python环境的依赖

pip install -r requirements_win.txt
#本项目测试使用的是torch为2.1.0+cpu版本。来源于https://download.pytorch.org/whl/cpu/torch-2.1.0%2Bcpu-cp39-cp39-win_amd64.whl#sha256=6b3c36d1597ec2a72e7e996b999a95372821292c0d2f86e9404d89f1d1fac557

2.3 下载模型文件

1.切换到工作目录

win

cd /d D:\qwen-ascend-llm

linux香橙派

cd  /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.下载模型

win

huggingface-cli download --repo-type model Qwen/Qwen3-0.6B --local-dir ./download/Qwen3-0.6B

linux 香橙派

HF_ENDPOINT=https://hf-mirror.com huggingface-cli download --repo-type model Qwen/Qwen3-0.6B --local-dir ./download/Qwen3-0.6B

4.查看原始模型文件

win

#下载完成后,会在D:\qwen-ascend-llm\download\Qwen3-0.6B目录下面看到下载好Qwen3-0.6B
cd /d D:\qwen-ascend-llm\download/Qwen3-0.6B

linux 香橙派

#下载完成后,会在/home/HwHiAiUser/qwen-ascend-llm/download/Qwen3-0.6B目录下面看到Qwen3-0.6B文件夹
cd  /home/HwHiAiUser/qwen-ascend-llm/download/Qwen3-0.6B

2.4 换为onnx模型

1.切换到工作目录

win

cd /d D:\qwen-ascend-llm

linux香橙派

cd /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行模型转换脚本

win (仅支持--dtype=float32 )

#使用cpu转换的需要时间
python export/export_onnx3.py --device_str=cpu --dtype=float32 --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_cpu/Qwen3-0.6B.onnx" --kv_cache_length=2048

linux香橙派(建议--dtype=float16)

# 建议使用昇腾的npu进行转换,即使耗费时间了,但其输出的模型效果较好
python export/export_onnx3.py --device_str=npu --dtype=float16 --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_npu/Qwen3-0.6B.onnx" --kv_cache_length=1024

4.查看生成的onnx模型文件

5.win

#转换完成后,会在D:qwen-ascend-llm\output\onnx_cpu目录下看到生成的Qwen3-0.6B.onnx模型的一系列文件
cd /d D:qwen-ascend-llm\output\onnx_cpu

在win电脑上使用cpu模型转换输出结果,参考如下

(ascend-llm) D:\qwen-ascend-llm>python export/export_onnx3.py --device_str=cpu --dtype=float32 --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_cpu/Qwen3-0.6B.onnx" --kv_cache_length=2048
Warning: transformers.models.qwen3.Qwen3Config not found. Using local Qwen3Config.
new model config save ok in  ./download/Qwen3-0.6B
Qwen3 attention_bias setting: False
begin export onnx3
The argument `trust_remote_code` is to be used with Auto classes. It has no effect here and is ignored.
D:\qwen-ascend-llm\export\modeling_qwen3.py:156: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if seq_len > self.max_seq_len_cached:
  
(ascend-llm) D:\qwen-ascend-llm\output\onnx_cpu>dir
2026/01/05  17:25    <DIR>          ..
2026/01/05  17:25       622,329,856 model.embed_tokens.weight
....................................................................
此处省略
....................................................................
2026/01/05  17:25         2,008,091 Qwen3-0.6B.onnx
2026/01/05  17:25        20,971,520 _model_layers.0_self_attn_rotary_emb_Constant_5_attr__value
2026/01/05  17:25        20,971,520 _model_layers.0_self_attn_rotary_emb_Constant_attr__value
             200 个文件  2,427,888,667 字节
               2 个目录 188,569,636,864 可用字节

6.linux 香橙派

#下载完成后,会在/home/HwHiAiUser/qwen-ascend-llm/output/onnx_npu目录下面看到文件夹下有且仅有一个文件Qwen3-0.6B.onnx文件
cd /home/HwHiAiUser/qwen-ascend-llm/output/onnx_npu

在香橙派上使用NPU模型转换输出结果,参考如下

(ascend-llm) sh-5.1# python export/export_onnx3.py --device_str=npu --dtype=float16 --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx/Qwen3-0.6B.onnx" --kv_cache_length=1024
Warning: transformers.models.qwen3.Qwen3Config not found. Using local Qwen3Config.
new model config save ok in  ./download/Qwen3-0.6B
Qwen3 attention_bias setting: False
begin export onnx3
./home/HwHiAiUser/qwen-ascend-llm_qwen/export/modeling_qwen3.py:156: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if seq_len > self.max_seq_len_cached:
......................Warning: tiling offset out of range, index: 32
.................................................................Warning: tiling offset out of range, index: 32
.Warning: tiling offset out of range, index: 32
..Warning: tiling offset out of range, index: 32
.Warning: tiling offset out of range, index: 32
.Warning: tiling offset out of range, index: 32
..Warning: tiling offset out of range, index: 32

2.5 运行脚本推理onnx模型

1.切换到工作目录

win

cd /d D:\qwen-ascend-llm

linux香橙派

cd  /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行推理onnx脚本

win

# win cpu 运行推理脚本
python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_cpu/Qwen3-0.6B.onnx" --dtype="float32" --cpu_thread=4 --max_input_length=1024 --max_output_length=2048

linux香橙派(onnx模型直接爆了,跑不了)

# ascend_310b cpu+npu 运行推理脚本
python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_npu/Qwen3-0.6B.onnx" --dtype="float16" --cpu_thread=4 --max_input_length=1024 --max_output_length=1024

2.6 改变onnx结构适配om的一些算子结构(如果未使用npu转换,可以在此作优化)

1.切换到工作目录

win

cd /d D:\qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行改变onnx结构适配om的结构的脚本

win

python export/change_node.py --input_model_path="./output/onnx/Qwen3-0.6B.onnx" --output_model_path="./output/onnx3/Qwen3-0.6B.onnx"

等待几十分钟后,生成的onnx系列文件参考如下

#   2,384,171,008 44034c07-e3c2-11f0-af79-dc2148ef45ec
#       43,730,559 Qwen3-0.6B.onnx

4.查看生成的onnx模型文件

win

#转换完成后,会在D:qwen-ascend-llm\output\onnx3目录下看到生成的Qwen3-0.6B.onnx模型的一系列文件
cd /d D:qwen-ascend-llm\output\onnx3

2.7 将onnx模型为om模型

1.切换到工作目录

cd /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行将onnx模型为om模型的脚本

4.windows的cpu导出onnx模型转换为om模型(--dtype=float32 )

python export/onnx2om.py --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx3/Qwen3-0.6B.onnx" --om_model_path="./output/model/Qwen3-0.6B" --kv_cache_length=1024 --cpu_thread=1 --max_prefill_length=2

windows的cpu导出onnx模型转换为om模型(--dtype=float32 )

等待 近半个小时的转换后,终端输出参考如下

-sh-5.1# python export/onnx2om.py --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx3/Qwen3-0.6B.onnx" --om_model_path="./output/model/Qwen3-0.6B" --kv_cache_length=1024 --cpu_thread=2 --max_prefill_length=2
[INFO] soc_version is `auto`, will auto detect soc version
[INFO] {'soc_full_name': 'Ascend310B4', 'soc_short_name': 'Ascend310B'}
============ run command ==============
export MS_DEV_FORCE_ACL=1 &&  export MS_ENABLE_GE=1 &&  export TE_PARALLEL_COMPILER=2 && export MAX_COMPILE_CORE_NUMBER=2 && atc --framework=5 --model="./output/onnx3/Qwen3-0.6B.onnx" --output="./output/model/Qwen3-0.6B" --soc_version=Ascend310B4 --precision_mode_v2=mixed_float16 --modify_mixlist=/home/HwHiAiUser/ascend_llm/qwen-ascend-llm/ops_info.json --input_format=ND --input_shape="input_ids:1,-1;attention_mask:1,-1;position_ids:1,-1;past_key_values:1,-1,448,128" --dynamic_dims "1,513,1,512;2,514,2,512;1,1025,1,1024;2,1026,2,1024"
=======================================
ATC start working now, please wait for a moment.


-
...
ATC run success, welcome to the next use.
-sh-5.1# ls -la output/model/
total 6091596
drwxr-xr-x 2 root       root             4096 Apr 18 09:26 .
drwxr-xr-x 5 HwHiAiUser HwHiAiUser       4096 Apr 18 07:00 ..
-rw------- 1 root       root       6237780332 Apr 18 09:27 Qwen3-0.6B.om

5.linux香橙派的npu导出的onnx模型转换未om模型(--dtype=float16)

python export/onnx2om.py --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx3/Qwen3-0.6B.onnx" --om_model_path="./output/model/Qwen3-0.6B" --kv_cache_length=1024 --cpu_thread=1 --max_prefill_length=2

linux香橙派的npu导出的onnx模型转换未om模型(--dtype=float16)

等待 近半个小时的转换后,终端输出参考如下

(ascend-llm) sh-5.1# python export/onnx2om.py --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_npu/Qwen3-0.6B.onnx" --om_model_path="./output/model/Qwen3-0.6B20260105" --kv_cache_length=1024 --cpu_thread=1 --max_prefill_length=2
[INFO] soc_version is `auto`, will auto detect soc version
[INFO] {'soc_full_name': 'Ascend310B4', 'soc_short_name': 'Ascend310B'}
============ run command ==============
export MS_DEV_FORCE_ACL=1 &&  export MS_ENABLE_GE=1 &&  export TE_PARALLEL_COMPILER=1 && export MAX_COMPILE_CORE_NUMBER=1 && atc --framework=5 --model="./output/onnx/Qwen3-0.6B.onnx" --output="./output/model/Qwen3-0.6B20260105" --soc_version=Ascend310B4 --precision_mode_v2=mixed_float16 --modify_mixlist=/home/HwHiAiUser/ascend_llm/qwen-ascend-llm_qwen3/ops_info.json --input_format=ND --input_shape="input_ids:1,-1;attention_mask:1,-1;position_ids:1,-1;past_key_values:1,-1,448,128" --dynamic_dims "1,513,1,512;2,514,2,512;1,1025,1,1024;2,1026,2,1024"
=======================================
ATC start working now, please wait for a moment.
..................................................................................................
ATC run success, welcome to the next use.

6.查看生成的om模型文件

#windows的cpu导出onnx模型转换为om模型(--dtype=float32 )。转换完成后,会在/home/HwHiAiUser/qwen-ascend-llm/output/model目录下面看到名为Qwen3-0.6B.om的模型文件,大小大概3.42G

#linux香橙派的npu导出的onnx模型转换未om模型(--dtype=float16)。转换完成后,会在/home/HwHiAiUser/qwen-ascend-llm/output/model目录下面看到名为Qwen3-0.6B20260105.om的模型文件,大小大概2.29G
cd /home/HwHiAiUser/qwen-ascend-llm/output/model

三、香橙派AI pro 推理Qwen3-0.6B模型测试

3.1 onnx模型测试

1.切换到工作目录

cd /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行推理脚本

python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx/Qwen3-0.6B.onnx" --dtype="float16" --cpu_thread=4 --max_input_length=1024 --max_output_length=1024

4.直接爆了。onnx跑不了。

(ascend-llm) sh-5.1# python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_npu/Qwen3-0.6B.onnx" --dtype="float16" --cpu_thread=4 --max_input_length=1024 --max_output_length=1024
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] init success

欢迎使用Qwen聊天机器人,输入exit或者quit退出,输入clear清空历史记录
Input: 你是谁
Segmentation fault (core dumped)
(ascend-llm) sh-5.1# python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx_cpu/Qwen3-0.6B.onnx" --dtype="float32" --cpu_thread=4 --max_input_length=1024 --max_output_length=1024
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] init success

欢迎使用Qwen聊天机器人,输入exit或者quit退出,输入clear清空历史记录
Input: 你是谁
Segmentation fault (core dumped)
(ascend-llm) sh-5.1# python ./cli_chat3.py --session_type=onnx --hf_model_dir="./download/Qwen3-0.6B" --onnx_model_path="./output/onnx3/Qwen3-0.6B.onnx" --dtype="float32" --cpu_thread=4 --max_input_length=1024 --max_output_length=1024
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] init success

欢迎使用Qwen聊天机器人,输入exit或者quit退出,输入clear清空历史记录
Input: 你是谁
Segmentation fault (core dumped)

3.2 om模型测试

1.切换到工作目录

cd /home/HwHiAiUser/qwen-ascend-llm

2.激活python环境

conda activate ascend-llm

3.运行推理脚本

python ./cli_chat3.py --session_type="acl" --hf_model_dir="./download/Qwen3-0.6B" --om_model_path="./output/model/Qwen3-0.6B20260105.om" --max_input_length=1024 --max_output_length=2048 --max_prefill_length=2

4.测试结果如下,勉强可以跑起来。

(ascend-llm) sh-5.1# python ./cli_chat3.py --session_type="acl" --hf_model_dir="./download/Qwen3-0.6B" --om_model_path="./output/model/Qwen3-0.6B20260105.om" --max_input_length=1024 --max_output_length=2048 --max_prefill_length=2
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] acl init
[INFO] acl set device, device_id: 0
[INFO] acl create context
[INFO] load model buffer 100.00%
[INFO] load model from memory, please wait a monment...
[INFO] load model duration:  18.365962505340576
[INFO] get model desc
[INFO] init success

欢迎使用Qwen聊天机器人,输入exit或者quit退出,输入clear清空历史记录
Input: 什么是MCP(Model Context Protocol)
Output: <think>

</think>


MCP(Model Context Protocols)是一种用于在机器学习模型(如Transformer模型)中管理上下文信息的技术。它主要用于在模型的输入过程中保持上下句之间的连贯性,从而帮助模型更好地理解和生成连续的自然语言内容。

MCP通常包括以下几种主要的机制:

1  **Context Tokenization**:这是MCP的核心概念,它负责对输入的文本进行上下文分隔。例如,在Transformer架构中,模型会将输入文本拆分为多个上下文块,每个上下文块包含与当前上下文相关的token。

这些机制使得MCP能够帮助模型在生成文本时保持与上下文的连贯性,从而提高生成内容的真实性和流畅性。

如果你有具体的应用场景或问题,可以告诉我,我会尽力提供帮助。
[INFO] first_token_lantency: 3.3379s,  decode_speed: 3.56 token/s,   total_speed(prefill+decode): 3.88 token/s
Input: 你是谁
Output: <think>
嗯,用户现在问“你是谁”。首先,我要确定他们之前的问题和对话历史。之前用户问过什么是MCP(Model Context Protocols),我回答过关于MCP的基本概念和主要机制。现在用户直接问“你是谁”,可能是在测试我的知识库,或者想进一步了解关于MCP的内容。

我需要确认用户的意图。用户可能是想了解MCP的全称和功能,但直接问“你是谁”可能意味着他们希望得到关于MCP的信息,而不仅仅是定义。不过根据之前的对话,用户已经询问了相关内容,可能现在需要更简洁的回答,或者确认之前的回答是否正确。

另外,用户可能在测试我是否能够正确理解问题,或者想进一步探讨MCP的具体应用或功能。考虑到之前回答中提及相关机制,比如上下文中token的处理,可能用户希望得到更详细的信息。但根据问题,用户直接问的是“你是谁”,这可能是一个简短的问候,或者可能想确认之前的回答。

需要保持友好的态度,避免让用户感到被询问。同时,确保回答准确,并符合之前提到的MPC机制。可能用户需要更简要的解释,或者希望理解MCP的整体作用。因此可以回复一个简洁的介绍,确认自己是提供MCP相关信息的助手,并感谢用户的问题。
</think>

我是您的AI助手,专注于机器理解和学习领域,为您提供帮助和支持。如果您有任何问题或需要解答,欢迎提问!
[INFO] first_token_lantency: 23.9902s,  decode_speed: 3.53 token/s,   total_speed(prefill+decode): 4.63 token/s
Input: quit
[INFO] acl reset device
[INFO] acl finalize
[INFO] destory context

四、主要参考项目

1.Tlntin的qwen-ascend-llm

https://github.com/Tlntin/qwen-ascend-llm

2.Qwen3

https://github.com/QwenLM/Qwen3

3.基于昇腾310芯片的大语言模型部署

https://gitee.com/yinghuo302/ascend-llm

4.昇腾

https://www.hiascend.com/

本帖最后由 匿名用户2026/01/05 19:28:59 编辑

我要发帖子