Skip to content

Commit

Permalink
[v0.10.0 -> v0.13.0]Task16 (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: sparanoid <[email protected]>
  • Loading branch information
Anleeos and sparanoid authored Dec 11, 2023
1 parent ac10c94 commit 345abf6
Show file tree
Hide file tree
Showing 13 changed files with 1,135 additions and 325 deletions.
86 changes: 70 additions & 16 deletions docs/how_to/microtvm/01-aot.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: microTVM 主机驱动的 AoT
title: 3. microTVM AoT 编译
---

# microTVM 主机驱动的 AoT
# 3. microTVM AoT 编译

:::note
单击 [此处](https://tvm.apache.org/docs/how_to/work_with_microtvm/micro_aot.html#sphx-glr-download-how-to-work-with-microtvm-micro-aot-py) 下载完整的示例代码
Expand All @@ -12,14 +12,65 @@ title: microTVM 主机驱动的 AoT

本教程展示了 microTVM(使用 TFLite 模型)主机驱动的 AoT 编译。与 GraphExecutor 相比,AoTExecutor 减少了运行时解析图的开销。此外,我们可以通过提前编译更好地进行内存管理。本教程可以使用 C 运行时(CRT)在 x86 CPU 上执行,也可以在 Zephyr 支持的微控制器/板上的 Zephyr 平台上执行。

## 安装 microTVM Python 依赖项
TVM 不包含用于 Python 串行通信包,因此在使用 microTVM 之前我们必须先安装一个。我们还需要TFLite来加载模型。

```bash
pip install pyserial==3.5 tflite==2.1
```

```python
import os


# 本指南默认运行在使用 TVM 的 C 运行时的 x86 CPU 上,如果你想
# 在 Zephyr 实机硬件上运行,你必须导入 `TVM_MICRO_USE_HW` 环境
# 变量。此外如果你使用 C 运行时,你可以跳过安装 Zephyr。
# 将花费大约20分钟安装 Zephyr。
use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

```

## 安装 Zephyr

``` bash
# 安装 west 和 ninja
python3 -m pip install west
apt-get install -y ninja-build

# 安装 ZephyrProject
ZEPHYR_PROJECT_PATH="/content/zephyrproject"
export ZEPHYR_BASE=${ZEPHYR_PROJECT_PATH}/zephyr
west init ${ZEPHYR_PROJECT_PATH}
cd ${ZEPHYR_BASE}
git checkout v3.2-branch
cd ..
west update
west zephyr-export
chmod -R o+w ${ZEPHYR_PROJECT_PATH}

# 安装 Zephyr SDK
cd /content
ZEPHYR_SDK_VERSION="0.15.2"
wget "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${ZEPHYR_SDK_VERSION}/zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"
tar xvf "zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"
mv "zephyr-sdk-${ZEPHYR_SDK_VERSION}" zephyr-sdk
rm "zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"

# 安装 python 依赖
python3 -m pip install -r "${ZEPHYR_BASE}/scripts/requirements.txt"
```

## 导入 Python 依赖项

``` python
import numpy as np
import pathlib
import json
import os

import tvm
from tvm import relay
import tvm.micro.testing
from tvm.relay.backend import Executor, Runtime
from tvm.contrib.download import download_testdata
```
Expand All @@ -31,9 +82,8 @@ from tvm.contrib.download import download_testdata
**注意:**默认情况下,本教程使用 CRT 在 x86 CPU 上运行,若要在 Zephyr 平台上运行,需要导出 *TVM_MICRO_USE_HW* 环境变量。

``` python
use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))
MODEL_URL = "https://github.com/tlc-pack/web-data/raw/main/testdata/microTVM/model/keyword_spotting_quant.tflite"
MODEL_PATH = download_testdata(MODEL_URL, "keyword_spotting_quant.tflite", module="model")
MODEL_URL = "https://github.com/mlcommons/tiny/raw/bceb91c5ad2e2deb295547d81505721d3a87d578/benchmark/training/keyword_spotting/trained_models/kws_ref_model.tflite"
MODEL_PATH = download_testdata(MODEL_URL, "kws_ref_model.tflite", module="model")
SAMPLE_URL = "https://github.com/tlc-pack/web-data/raw/main/testdata/microTVM/data/keyword_spotting_int8_6.pyc.npy"
SAMPLE_PATH = download_testdata(SAMPLE_URL, "keyword_spotting_int8_6.pyc.npy", module="data")

Expand All @@ -56,25 +106,23 @@ relay_mod, params = relay.frontend.from_tflite(

## 定义 target

接下来定义 target、runtime 和 executor。本教程将详细介绍使用 AOT 主机驱动的执行器。这里使用的主机微 target,它使用 CRT runtime 在 x86 CPU 上运行模型,或者在 qemu_x86 模拟器单板上运行带有 Zephyr 平台的模型。对于物理微控制器,获取物理单板(例如 nucleo_l4r5zi)的 target 模型,并将其传递给 *tvm.target.target.micro*,从而创建完整的微目标
接下来定义 target、runtime 和 executor。本教程将详细介绍使用 AOT 主机驱动的执行器。这里使用的主机微 target,它使用 CRT runtime 在 x86 CPU 上运行模型,或者在 qemu_x86 模拟器单板上运行带有 Zephyr 平台的模型。对于物理微控制器,获取物理单板(例如 nucleo_l4r5zi)的 target 模型,并将其修改 `BOARD` 支持 Zephyr 单板

``` python
# 使用 C runtime(crt),并通过将 system-lib 设置为 True 来启用静态链接
RUNTIME = Runtime("crt", {"system-lib": True})

# 在主机上模拟一个微控制器。使用来自 `src/runtime/crt/host/main.cc [https://github.com/apache/tvm/blob/main/src/runtime/crt/host/main.cc](https://github.com/apache/tvm/blob/main/src/runtime/crt/host/main.cc)`_ 的 main()。
# 在主机上模拟一个微控制器。使用来自 `src/runtime/crt/host/main.cc`_ 的 main()。
# 若要使用物理硬件,请将「host」替换为与你的硬件匹配的内容。
TARGET = tvm.target.target.micro("host")
TARGET = tvm.micro.testing.get_target("crt")

# 使用 AOT 执行器,而非计算图或是虚拟机执行器。不要使用未打包的 API 或 C 调用风格。
EXECUTOR = Executor("aot")

if use_physical_hw:
boards_file = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json"
with open(boards_file) as f:
boards = json.load(f)
BOARD = os.getenv("TVM_MICRO_BOARD", default="nucleo_l4r5zi")
TARGET = tvm.target.target.micro(boards[BOARD]["model"])
SERIAL = os.getenv("TVM_MICRO_SERIAL", default=None)
TARGET = tvm.micro.testing.get_target("zephyr", BOARD)
```

## 编译模型
Expand Down Expand Up @@ -105,7 +153,13 @@ project_options = {} # 可以用选项通过 TVM 提供特定于平台的选项

if use_physical_hw:
template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr"))
project_options = {"project_type": "host_driven", "zephyr_board": BOARD}
project_options = {
"project_type": "host_driven",
"board": BOARD,
"serial_number": SERIAL,
"config_main_stack_size": 4096,
"zephyr_base": os.getenv("ZEPHYR_BASE", default="/content/zephyrproject/zephyr"),
}

temp_dir = tvm.contrib.utils.tempdir()
generated_project_dir = temp_dir / "project"
Expand Down Expand Up @@ -155,6 +209,6 @@ with tvm.micro.Session(project.transport()) as session:
Label is `left` with index `6`
```

[下载 Python 源代码:micro_aot.py](https://tvm.apache.org/docs/_downloads/f8a7209a0e66b246185bfc41bbc82f54/micro_aot.py)
[下载 Python 源代码:micro_aot.py](https://tvm.apache.org/docs/v0.13.0/_downloads/f8a7209a0e66b246185bfc41bbc82f54/micro_aot.py)

[下载 Jupyter Notebook:micro_aot.ipynb](https://tvm.apache.org/docs/_downloads/c00933f3fbcf90c4f584d54607b33805/micro_aot.ipynb)
[下载 Jupyter Notebook:micro_aot.ipynb](https://tvm.apache.org/docs/v0.13.0/_downloads/c00933f3fbcf90c4f584d54607b33805/micro_aot.ipynb)
81 changes: 63 additions & 18 deletions docs/how_to/microtvm/02-autotune_microtvm.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: 使用 microTVM 进行自动调优
title: 6. 使用 microTVM 进行模型调优
---

# 使用 microTVM 进行自动调优
# 6. 使用 microTVM 进行模型调优

:::note
单击 [此处](https://tvm.apache.org/docs/how_to/work_with_microtvm/micro_autotune.html#sphx-glr-download-how-to-work-with-microtvm-micro-autotune-py) 下载完整的示例代码
Expand All @@ -12,16 +12,61 @@ title: 使用 microTVM 进行自动调优

本教程介绍如何用 C runtime 自动调优模型。

``` python
## 安装 microTVM Python 依赖项
TVM 不包含用于 Python 串行通信包,因此在使用 microTVM 之前我们必须先安装一个。我们还需要TFLite来加载模型。

```bash
pip install pyserial==3.5 tflite==2.1
```

```python
# 如果下面的标志为 False,可以跳过下一节(安装 Zephyr)
# 安装 Zephyr 约花费20分钟
import os

use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

```
## 安装 Zephyr

``` bash
# 安装 west 和 ninja
python3 -m pip install west
apt-get install -y ninja-build

# 安装 ZephyrProject
ZEPHYR_PROJECT_PATH="/content/zephyrproject"
export ZEPHYR_BASE=${ZEPHYR_PROJECT_PATH}/zephyr
west init ${ZEPHYR_PROJECT_PATH}
cd ${ZEPHYR_BASE}
git checkout v3.2-branch
cd ..
west update
west zephyr-export
chmod -R o+w ${ZEPHYR_PROJECT_PATH}

# 安装 Zephyr SDK
cd /content
ZEPHYR_SDK_VERSION="0.15.2"
wget "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${ZEPHYR_SDK_VERSION}/zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"
tar xvf "zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"
mv "zephyr-sdk-${ZEPHYR_SDK_VERSION}" zephyr-sdk
rm "zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-x86_64.tar.gz"

# 安装 python 依赖
python3 -m pip install -r "${ZEPHYR_BASE}/scripts/requirements.txt"
```

## 导入 Python 依赖项

``` python
import json
import numpy as np
import pathlib

import tvm
from tvm.relay.backend import Runtime

use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))
import tvm.micro.testing
```

## 定义模型
Expand Down Expand Up @@ -62,19 +107,16 @@ params = {"weight": weight_sample}

``` python
RUNTIME = Runtime("crt", {"system-lib": True})
TARGET = tvm.target.target.micro("host")
TARGET = tvm.micro.testing.get_target("crt")

# 为物理硬件编译
# --------------------------------------------------------------------------
# 在物理硬件上运行时,选择描述硬件的 TARGET 和 BOARD。
# 下面的示例中选择 STM32L4R5ZI Nucleo。
if use_physical_hw:
boards_file = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json"
with open(boards_file) as f:
boards = json.load(f)

BOARD = os.getenv("TVM_MICRO_BOARD", default="nucleo_l4r5zi")
TARGET = tvm.target.target.micro(boards[BOARD]["model"])
SERIAL = os.getenv("TVM_MICRO_SERIAL", default=None)
TARGET = tvm.micro.testing.get_target("zephyr", BOARD)
```

## 提取调优任务
Expand Down Expand Up @@ -117,10 +159,10 @@ if use_physical_hw:
module_loader = tvm.micro.AutoTvmModuleLoader(
template_project_dir=pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")),
project_options={
"zephyr_board": BOARD,
"west_cmd": "west",
"board": BOARD,
"verbose": False,
"project_type": "host_driven",
"serial_number": SERIAL,
},
)
builder = tvm.autotvm.LocalBuilder(
Expand Down Expand Up @@ -182,10 +224,11 @@ if use_physical_hw:
lowered,
temp_dir / "project",
{
"zephyr_board": BOARD,
"west_cmd": "west",
"board": BOARD,
"verbose": False,
"project_type": "host_driven",
"serial_number": SERIAL,
"config_main_stack_size": 4096,
},
)

Expand Down Expand Up @@ -240,10 +283,12 @@ if use_physical_hw:
lowered_tuned,
temp_dir / "project",
{
"zephyr_board": BOARD,
"board": BOARD,
"west_cmd": "west",
"verbose": False,
"project_type": "host_driven",
"serial_number": SERIAL,
"config_main_stack_size": 4096,
},
)

Expand Down Expand Up @@ -273,6 +318,6 @@ tvmgen_default_fused_layout_transform tvmgen_default_fused_layout_transf
Total_time - 195.83 - - - - -
```

[下载 Python 源代码:micro_autotune.py](https://tvm.apache.org/docs/_downloads/9ccca8fd489a1486ac71b55a55c320c5/micro_autotune.py)
[下载 Python 源代码:micro_autotune.py](https://tvm.apache.org/docs/v0.13.0/_downloads/9ccca8fd489a1486ac71b55a55c320c5/micro_autotune.py)

[下载 Jupyter notebook:micro_autotune.ipynb](https://tvm.apache.org/docs/_downloads/f83ba3df2d52f9b54cf141114359481a/micro_autotune.ipynb)
[下载 Jupyter notebook:micro_autotune.ipynb](https://tvm.apache.org/docs/v0.13.0/_downloads/f83ba3df2d52f9b54cf141114359481a/micro_autotune.ipynb)
17 changes: 8 additions & 9 deletions docs/how_to/microtvm/03-tvm_arm.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: 在支持 CMSIS-NN 的 Arm(R) Cortex(R)-M55 CPU 和 Ethos(TM)-U55 NPU 裸机上运行 TVM
title: 7. 在支持 CMSIS-NN 的 Arm(R) Cortex(R)-M55 CPU 和 Ethos(TM)-U55 NPU 裸机上运行 TVM
---

# 在支持 CMSIS-NN 的 Arm(R) Cortex(R)-M55 CPU 和 Ethos(TM)-U55 NPU 裸机上运行 TVM
# 7. 在支持 CMSIS-NN 的 Arm(R) Cortex(R)-M55 CPU 和 Ethos(TM)-U55 NPU 裸机上运行 TVM

:::note
单击 [此处](https://tvm.apache.org/docs/how_to/work_with_microtvm/micro_ethosu.html#sphx-glr-download-how-to-work-with-microtvm-micro-ethosu-py) 下载完整的示例代码
Expand Down Expand Up @@ -51,15 +51,14 @@ TVMC - TVM driver command-line interface
attrs==21.2.0
cloudpickle==2.0.0
decorator==5.1.0
ethos-u-vela==3.2.0
flatbuffers==1.12
ethos-u-vela==3.8.0
flatbuffers==2.0.7
lxml==4.6.3
nose==1.3.7
numpy==1.19.5
Pillow==8.3.2
psutil==5.8.0
scipy==1.5.4
synr==0.4
tflite==2.4.0
tornado==6.1
```
Expand Down Expand Up @@ -278,7 +277,7 @@ demo.c[¶](#demo-c)
#include <tvm_runtime.h>

#include "ethosu_mod.h"
#include "uart.h"
#include "uart_stdout.h"

// convert_image.py 和 convert_labels.py 生成的头文件
#include "inputs.h"
Expand All @@ -288,7 +287,7 @@ demo.c[¶](#demo-c)
int abs(int v) { return v * ((v > 0) - (v < 0)); }

int main(int argc, char** argv) {
uart_init();
UartStdOutInit();
printf("Starting Demo\n");
EthosuInit();

Expand Down Expand Up @@ -444,6 +443,6 @@ Info: /OSCI/SystemC: Simulation stopped by user.

可以看到,输出的最后部分显示图像已被正确分类为「tabby」。

[下载 Python 源代码:micro_ethosu.py](https://tvm.apache.org/docs/_downloads/ab2eef18d10188532645b1d60fc7dd68/micro_ethosu.py)
[下载 Python 源代码:micro_ethosu.py](https://tvm.apache.org/docs/v0.13.0/_downloads/ab2eef18d10188532645b1d60fc7dd68/micro_ethosu.py)

[下载 Jupyter Notebook:micro_ethosu.ipynb](https://tvm.apache.org/docs/_downloads/55a9eff88b1303e525d53269eeb16897/micro_ethosu.ipynb)
[下载 Jupyter Notebook:micro_ethosu.ipynb](https://tvm.apache.org/docs/v0.13.0/_downloads/55a9eff88b1303e525d53269eeb16897/micro_ethosu.ipynb)
Loading

0 comments on commit 345abf6

Please sign in to comment.