Skip to content

Commit 1d595b5

Browse files
committed
docs: add O6 NPU zh docs
signed-off-by: "Morgan ZHANG" <[email protected]>
1 parent ae67cd9 commit 1d595b5

22 files changed

+1209
-0
lines changed

.typos.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ extend-ignore-identifiers-re = [
55
"g3caf61a44c2",
66
"des3_ede",
77
"hda",
8+
"noe",
9+
"NOE",
810
]
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
sidebar_position: 41
3+
---
4+
5+
# 应用开发
6+
7+
主要介绍上层应用开发,比如 NPU 应用开发等
8+
9+
<DocCardList />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# 人工智能
6+
7+
主要介绍使用 NPU SDK 进行人工智能硬件加速的应用开发
8+
9+
<DocCardList />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# CIX AI Model HUb
6+
7+
CIX AI Model Hub 是针对在 CIX SOC 上部署进行了优化的最先进的机器学习模型的集合。里面包含了多种领域的 AI 模型与针对 CIX SOC NPU 编译的配置文件,其中包括计算机视觉,语音识别,生成式 AI 等多种开源模型。
8+
9+
## 克隆 CIX AI Model Hub 仓库
10+
11+
通过 [CIX 早鸟计划](./npu-introduction#cix-早鸟计划) 申请通过后,会有邮件发送到注册邮箱,请按邮件内容获取 AI Model Hub 仓库
12+
13+
```bash
14+
git clone https://e.coding.net/g-uaqh1479/ai-model-hub/ai_model_hub.git
15+
```
16+
17+
## 配置环境
18+
19+
:::tip
20+
在 X86 工作站中请与 NOE 的环境进行隔离
21+
:::
22+
23+
```bash
24+
pip3 install -r requirements.txt
25+
```
26+
27+
## 模型例子目录
28+
29+
```bash
30+
├── datasets
31+
├── models
32+
│   ├── Audio
33+
│   │   └── Speech_Recognotion
34+
│   ├── ComputeVision
35+
│   │   ├── Face_Detection
36+
│   │   ├── Face_Recognition
37+
│   │   ├── Image_Classification
38+
│   │   ├── Object_Detection
39+
│   │   ├── Pose_Estimation
40+
│   │   ├── Semantic_Segmentation
41+
│   │   └── Super_Resolution
42+
│   └── Generative_AI
43+
│   ├── LLM
44+
│   └── Text_Image_Search
45+
└── utils
46+
├── evaluate
47+
└── label
48+
```
49+
50+
## 端到端例子
51+
52+
这些应用程序经过优化,成为易于遵循的示例,而不是为了最大限度地缩短预测时间。
53+
54+
端到端示例:
55+
56+
1. 将人类可读的输入预处理为模型输入
57+
2. 运行模型推理
58+
3. 将模型输出后处理为人类可读的格式
59+
60+
所有模型示例代码均可在 O6 上端到端运行模型:
61+
62+
```bash
63+
python3 inference_npu.py
64+
```
65+
66+
此外,还可以通过 OnnxRuntime 在 X86 工作站或 O6 上本地运行的端到端示例
67+
68+
```bash
69+
python3 inference_onnx.py
70+
```
71+
72+
## CIX AI Model Hub 预编译模型下载站
73+
74+
CIX AI Model Hub 里面的涉及的例子,用户可以直接从 [ModelScope](https://modelscope.cn/models/cix/ai_model_hub_24_Q4/files) 上下载提供的 onnx 模型和预编译好的模型,无需从头编译。
75+
76+
ModelScope 链接: https://modelscope.cn/models/cix/ai_model_hub_24_Q4/files
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
sidebar_position: 6
3+
---
4+
5+
# DeepLabv3 完整示例
6+
7+
此文档使用 CIX P1 NPU SDK 将 [deeplabv3](https://pytorch.org/vision/main/models/generated/torchvision.models.segmentation.deeplabv3_resnet50.html) 移植到 CIX SOC 内部的硬件加速模块实现使用 NPU 推理神经网络模型,
8+
请在参考此文档前请先在 X86 工作站按照 [安装 NPU SDK](./npu-introduction#安装-npu-sdk) 安装 NOE Compiler,在 X86 工作站与 Orion O6 按照 [下载 CIX AI Model Hub](./ai-hub#下载-cix-ai-model-hub) 文档配置所需环境。
9+
10+
论文链接: [Rethinking Atrous Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1706.05587)
11+
12+
## DeepLabv3 工程目录列表
13+
14+
在 CIX AI Model Hub 中包含了 DeepLabv3 的所需文件, 请用户按照 [下载 CIX AI Model Hub](./ai-hub#下载-cix-ai-model-hub) 下载
15+
16+
```bash
17+
cd ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3
18+
```
19+
20+
```bash
21+
.
22+
├── cfg
23+
│   └── onnx_deeplab_v3_build.cfg
24+
├── datasets
25+
│   └── calibration_data.npy
26+
├── graph.json
27+
├── inference_npu.py
28+
├── inference_onnx.py
29+
├── ReadMe.md
30+
├── test_data
31+
│   └── ILSVRC2012_val_00004704.JPEG
32+
└── Tutorials.ipynb
33+
```
34+
35+
## (可选)下载 CIX 模型
36+
37+
用户可无需从头编译模型,radxa 提供下载预编译好的 deeplab_v3.cix 模型方法
38+
39+
- 下载模型
40+
```bash
41+
wget https://modelscope.cn/models/cix/ai_model_hub_24_Q4/resolve/master/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3/deeplab_v3.cix
42+
```
43+
44+
## 编译模型
45+
46+
### 准备 onnx 模型
47+
48+
- 下载 onnx 模型
49+
50+
[deeplabv3_resnet50.onnx](https://modelscope.cn/models/cix/ai_model_hub_24_Q4/resolve/master/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3/model/deeplabv3_resnet50.onnx)
51+
52+
- 简化模型
53+
54+
这里使用 onnxsim 进行模型输入固化和模型简化
55+
56+
```bash
57+
pip3 install onnxsim onnxruntime
58+
onnxsim deeplabv3_resnet50.onnx deeplabv3_resnet50-sim.onnx --overwrite-input-shape 1,3,520,520
59+
```
60+
61+
### 编译模型
62+
63+
CIX SOC NPU 支持 INT8 计算,在编译模型前,我们需要使用 NOE Compiler 对模型进行 INT8 量化
64+
65+
- 准备校准集
66+
67+
- 使用 `datasets` 现有校准集
68+
69+
```bash
70+
.
71+
└── calibration_data.npy
72+
```
73+
74+
- 自行准备校准集
75+
76+
`test_data` 目录下已经包含多张校准集的图片文件
77+
78+
```bash
79+
.
80+
├── 1.jpeg
81+
└── 2.jpeg
82+
```
83+
84+
参考以下脚本生成校准文件
85+
86+
```python
87+
import sys
88+
import os
89+
import numpy as np
90+
_abs_path = os.path.join(os.getcwd(), "../../../../")
91+
sys.path.append(_abs_path)
92+
from utils.image_process import preprocess_image_deeplabv3
93+
from utils.tools import get_file_list
94+
# Get a list of images from the provided path
95+
images_path = "test_data"
96+
images_list = get_file_list(images_path)
97+
data = []
98+
for image_path in images_list:
99+
input = preprocess_image_deeplabv3(image_path)
100+
data.append(input)
101+
# concat the data and save calib dataset
102+
data = np.concatenate(data, axis=0)
103+
np.save("datasets/calib_data_tmp.npy", data)
104+
print("Generate calib dataset success.")
105+
```
106+
107+
- 使用 NOE Compiler 量化与编译模型
108+
109+
- 制作量化 cfg 配置文件, 请参考以下配置
110+
111+
```bash
112+
[Common]
113+
mode = build
114+
115+
[Parser]
116+
model_type = onnx
117+
model_name = deeplab_v3
118+
detection_postprocess =
119+
model_domain = image_segmentation
120+
input_model = ./deeplabv3_resnet50-sim.onnx
121+
input = input
122+
input_shape = [1, 3, 520, 520]
123+
output = output
124+
output_dir = ./
125+
126+
[Optimizer]
127+
output_dir = ./
128+
calibration_data = ./datasets/calib_data_tmp.npy
129+
calibration_batch_size = 1
130+
metric_batch_size = 1
131+
dataset = NumpyDataset
132+
quantize_method_for_weight = per_channel_symmetric_restricted_range
133+
quantize_method_for_activation = per_tensor_asymmetric
134+
save_statistic_info = True
135+
136+
[GBuilder]
137+
outputs = deeplab_v3.cix
138+
target = X2_1204MP3
139+
profile = True
140+
tiling = fps
141+
```
142+
143+
- 量化模型
144+
:::tip
145+
如果遇到 cixbuild 报错 `[E] Optimizing model failed! CUDA error: no kernel image is available for execution on the device ...`
146+
这意味着当前版本的 torch 不支持此 GPU,请完全卸载当前版本的 torch, 然后在 torch 官网下载最新版本。
147+
:::
148+
```bash
149+
cixbuild ./onnx_deeplab_v3_build.cfg
150+
```
151+
152+
## 板端部署
153+
154+
### NPU 推理
155+
156+
将使用 NOE Compiler 编译好的 .cix 格式的模型复制到 Orion O6 开发板上进行模型验证
157+
158+
```bash
159+
python3 inference_npu.py --images ./test_data/ --model_path ./deeplab_v3.ci
160+
```
161+
162+
```bash
163+
(.venv) radxa@orion-o6:~/NOE/ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3$ time python3 inference_npu.py --images ./test_data/ --model_path ./deeplab_v3.cix
164+
npu: noe_init_context success
165+
npu: noe_load_graph success
166+
Input tensor count is 1.
167+
Output tensor count is 1.
168+
npu: noe_create_job success
169+
save output: noe_ILSVRC2012_val_00004704.JPEG
170+
npu: noe_clean_job success
171+
npu: noe_unload_graph success
172+
npu: noe_deinit_context success
173+
174+
real 0m9.047s
175+
user 0m4.314s
176+
sys 0m0.478s
177+
```
178+
179+
结果保存在 `output` 文件夹中
180+
181+
![deeplab1.webp](/img/o6/deeplab1.webp)
182+
183+
### CPU 推理
184+
185+
使用 CPU 对 onnx 模型进行推理验证正确性,可在 X86 工作站或 O6 上运行
186+
187+
```bash
188+
python3 inference_onnx.py --images ./test_data/ --onnx_path ./deeplabv3_resnet50-sim.onnx
189+
```
190+
191+
```bash
192+
(.venv) radxa@orion-o6:~/NOE/ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3$ time python3 inference_onnx.py --images ./test_data/ --onnx_path ./deeplabv3_resnet50-sim.onnx
193+
save output: onnx_ILSVRC2012_val_00004704.JPEG
194+
195+
real 0m7.605s
196+
user 0m33.235s
197+
sys 0m0.558s
198+
199+
```
200+
201+
结果保存在 `output` 文件夹中
202+
![deeplab2.webp](/img/o6/deeplab2.webp)
203+
204+
可以看到 NPU 和 CPU 上推理的结果一致,但运行速度大幅缩短
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# NPU SDK 安装
6+
7+
## NPU 简介
8+
9+
瑞莎星睿 O6 (Radxa Orion O6) 拥有高达 28.8TOPs NPU (Neural Processing Unit) 算力,支持 INT4 / INT8 / INT16 / FP16 / BF16 和 TF32 类型的加速。
10+
此文档主要讲解用户如何使用 CIX P1 NPU SDK 运行基于 NPU 加速推理的人工智能模型和应用。包括模型编译工具,工具链和一些常见模型案例进行逐步讲解。
11+
12+
## CIX 早鸟计划
13+
14+
NPU SDK 包含 NOE Compiler 与 CIX AI Model Hub, 要获取 SDK 需要通过申请 CIX 早鸟计划获取。
15+
申请链接: [CIX 早鸟计划](https://support.cixtech.com/#/)
16+
17+
## X86 工作站安装 NPU SDK
18+
19+
:::tip
20+
CIX P1 NPU SDK 是安装在 X86 工作站中,并要求 python 版本为 3.8。
21+
22+
请用户准备好 python3.8 环境,推荐使用 [anaconda](https://www.anaconda.com/download)
23+
:::
24+
25+
### 下载 NPU SDK
26+
27+
通过 [CIX 早鸟计划](#cix-早鸟计划) 申请通过后,会有邮件发送到注册邮箱,请按邮件内容下载 SDK
28+
29+
```bash
30+
tar -xvf nor.tag.gz
31+
```
32+
33+
### 安装 NOE Compiler
34+
35+
NOE Compiler 用于模型的编译,让 ONNX 模型框架的模型格式转换为可以使用 NPU 进行加速推理的模型格式
36+
37+
```bash
38+
cd noe
39+
pip3 install -r requirements.txt
40+
pip3 install ./CixBuilder-6.1.2958.1-py3-none-any.whl
41+
```
42+
43+
## 板端安装 NOE UMD
44+
45+
:::tip
46+
NOE UMD 包以 deb 安装包形式发布,如使用 python api,在创建虚拟环境时请继承系统依赖: **`python3 -m venv --system-site-packages .venv`**
47+
:::
48+
通过 [CIX 早鸟计划](#cix-早鸟计划) 申请通过后,会有邮件发送到注册邮箱,请按邮件内容下载 UMD (User Mode Driver) deb 包
49+
50+
```bash
51+
sudo dpkg -i ./cix-noe-umd_0.01-1_arm64.deb
52+
```

0 commit comments

Comments
 (0)