Skip to content

Commit 85fca59

Browse files
neuropilot-captainneuropilot-captain
andauthored
Add documentation (#6883)
* Move README under mediatek/ Fix some typo * add tutorial * enable mediatek documents --------- Co-authored-by: neuropilot-captain <[email protected]>
1 parent a0ac820 commit 85fca59

File tree

4 files changed

+124
-18
lines changed

4 files changed

+124
-18
lines changed
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
# Build Instructions
1+
# MediaTek Backend on ExecuTorch
2+
MediaTek backend empowers ExecuTorch to speed up PyTorch models on edge devices that equips with MediaTek Neuron Processing Unit (NPU). This document offers a step-by-step guide to set up the build environment for the MediaTek ExecuTorch libraries.
23

3-
This document provides a step-by-step guide to set up the build environment for the MediaTek ExercuTorch libraries.
4+
## Supported Chips
45

5-
## Prerequisites
6+
The examples provided in this repository are tested and supported on the following MediaTek chip:
7+
8+
- MediaTek Dimensity 9300 (D9300)
9+
10+
## Build Instructions
11+
12+
### Prerequisites
613

714
Before you begin, ensure you have the following prerequisites installed and configured:
815

9-
### 1. Buck2 Build Tool
16+
#### 1. Buck2 Build Tool
1017

1118
- **Download Buck2**: Obtain Buck2 from the official [releases page](https://github.com/facebook/buck2/releases/tag/2024-02-01).
1219
- **Add to PATH**: Extract the downloaded file and add the directory to your system's `$PATH` environment variable.
1320
```bash
1421
export PATH=<path_to_buck>:$PATH
1522
```
1623

17-
### 2. Android NDK
24+
#### 2. Android NDK
1825

1926
- **Download Android NDK**: Acquire the Android NDK version 26.3.11579264 from the [Android developer site](https://developer.android.com/ndk/downloads).
2027
- **Set NDK Path**: Ensure that the `$ANDROID_NDK` environment variable is set to the path where the NDK is located.
2128
```bash
2229
export ANDROID_NDK=<path_to_android_ndk>
2330
```
2431

25-
### 3. MediaTek ExercuTorch Libraries
32+
#### 3. MediaTek ExecuTorch Libraries
2633

2734
Download [NeuroPilot Express SDK](https://neuropilot.mediatek.com/resources/public/npexpress/en/docs/npexpress) from MediaTek's NeuroPilot portal:
2835

@@ -31,11 +38,11 @@ Download [NeuroPilot Express SDK](https://neuropilot.mediatek.com/resources/publ
3138
- `mtk_converter-8.8.0.dev20240723+public.d1467db9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl`: This library preprocess the model into a MediaTek representation.
3239
- `mtk_neuron-8.2.2-py3-none-linux_x86_64.whl`: This library converts the model to binaries.
3340

34-
## Setup
41+
### Setup
3542

3643
Follow the steps below to setup your build environment:
3744

38-
1. **Setup ExercuTorch Environment**: Refer to the [Setting up ExercuTorch](https://pytorch.org/executorch/stable/getting-started-setup) guide for detailed instructions on setting up the ExercuTorch environment.
45+
1. **Setup ExecuTorch Environment**: Refer to the [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) guide for detailed instructions on setting up the ExecuTorch environment.
3946

4047
2. **Setup MediaTek Backend Environment**
4148
- Install the dependent libs. Ensure that you are inside backends/mediatek/ directory
@@ -52,18 +59,21 @@ Follow the steps below to setup your build environment:
5259
export NEURON_BUFFER_ALLOCATOR_LIB=<path_to_buffer_allocator>
5360
```
5461

55-
## Build
62+
### Build
63+
1. Navigate to `scripts/` directory.
5664

57-
1. **Build MediaTek Backend**: Once the prerequisites are in place, run the `mtk_build.sh` script to start the build process, MediaTek backend will be built under `cmake-android-out/backends/` as `libneuron_backend.so`
65+
2. **Build MediaTek Backend**: Once the prerequisites are in place, run the `mtk_build.sh` script to start the build process, MediaTek backend will be built under `cmake-android-out/backends/` as `libneuron_backend.so`
5866

5967
```bash
6068
./mtk_build.sh
6169
```
6270

63-
## Run
71+
### Run
6472

65-
1. **Push MediaTek universal SDK and MediaTek backend to the device**: push `libneuronusdk_adapter.mtk.so` and `libneuron_backend.so` to the phone and export it to the `$LD_LIBRARY_PATH` environment variable before executing ExercuTorch with MediaTek backend.
73+
1. **Push MediaTek universal SDK and MediaTek backend to the device**: push `libneuronusdk_adapter.mtk.so` and `libneuron_backend.so` to the phone and export it to the `$LD_LIBRARY_PATH` environment variable before executing ExecuTorch with MediaTek backend.
6674

6775
```bash
6876
export LD_LIBRARY_PATH=<path_to_usdk>:<path_to_neuron_backend>:$LD_LIBRARY_PATH
6977
```
78+
79+
Please refer to `executorch/examples/mediatek/` for export and execution examples of various of models.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Building and Running ExecuTorch with MediaTek Backend
2+
3+
MediaTek backend empowers ExecuTorch to speed up PyTorch models on edge devices that equips with MediaTek Neuron Processing Unit (NPU). This document offers a step-by-step guide to set up the build environment for the MediaTek ExecuTorch libraries.
4+
5+
::::{grid} 2
6+
:::{grid-item-card} What you will learn in this tutorial:
7+
:class-card: card-prerequisites
8+
* How to export and lower a PyTorch model ahead of time with ExecuTorch for MediaTek devices.
9+
* How to build MediaTek backend and examples.
10+
* How to deploy the exported models on device with ExecuTorch runtime.
11+
:::
12+
:::{grid-item-card} Tutorials we recommend you complete before this:
13+
:class-card: card-prerequisites
14+
* [Introduction to ExecuTorch](intro-how-it-works.md)
15+
* [Setting up ExecuTorch](getting-started-setup.md)
16+
* [Building ExecuTorch with CMake](runtime-build-and-cross-compilation.md)
17+
:::
18+
::::
19+
20+
21+
## Prerequisites (Hardware and Software)
22+
23+
### Host OS
24+
- Linux operating system
25+
26+
### Supported Chips:
27+
- MediaTek Dimensity 9300 (D9300)
28+
29+
### Software:
30+
31+
- [NeuroPilot Express SDK](https://neuropilot.mediatek.com/resources/public/npexpress/en/docs/npexpress) is a lightweight SDK for deploying AI applications on MediaTek SOC devices.
32+
33+
## Setting up your developer environment
34+
35+
Follow the steps below to setup your build environment:
36+
37+
1. **Setup ExecuTorch Environment**: Refer to the [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) guide for detailed instructions on setting up the ExecuTorch environment.
38+
39+
2. **Setup MediaTek Backend Environment**
40+
- Install the dependent libs. Ensure that you are inside `backends/mediatek/` directory
41+
```bash
42+
pip3 install -r requirements.txt
43+
```
44+
- Install the two .whl downloaded from NeuroPilot Portal
45+
```bash
46+
pip3 install mtk_neuron-8.2.13-py3-none-linux_x86_64.whl
47+
pip3 install mtk_converter-8.9.1+public-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
48+
```
49+
- Set evironment variables for building backend
50+
```bash
51+
export NEURON_BUFFER_ALLOCATOR_LIB=<path_to_buffer_allocator.so>
52+
```
53+
54+
## Build
55+
56+
### Ahead of time:
57+
58+
**Exporting a PyTorch Model for MediaTek Backend**:
59+
1. Lower and export the `.pte` file for on-device execution. The export script samples are povided under `example/mediatek/`. For example, the following commnad exports the `.pte` using the scripts provided.
60+
```bash
61+
cd executorch
62+
63+
./examples/mediatek/shell_scripts/export_oss.sh mobilenetv3
64+
```
65+
66+
2. Find the `.pte` files under the directory named as same as the model.
67+
68+
### Runtime:
69+
70+
**Build MediaTek Backend for ExecuTorch Runtime**
71+
1. Navigate to `backends/mediatek/scripts/` directory.
72+
73+
2. **Build MediaTek Backend**: Once the prerequisites are in place, run the `mtk_build.sh` script to start the build process:
74+
```bash
75+
./mtk_build.sh
76+
```
77+
78+
3. MediaTek backend will be built under `cmake-android-out/backends/` as `libneuron_backend.so`.
79+
80+
**Build a runner to execute the model on the device**:
81+
1. Build the runners and the backend by exedcuting the script:
82+
```bash
83+
./mtk_build_examples.sh
84+
```
85+
86+
2. The runners will be built under `cmake-android-out/examples/`
87+
88+
## Deploying and running on a device
89+
90+
1. **Push MediaTek universal SDK and MediaTek backend to the device**: push `libneuronusdk_adapter.mtk.so` and `libneuron_backend.so` to the phone and export it to the `$LD_LIBRARY_PATH` environment variable before executing ExecuTorch with MediaTek backend.
91+
92+
```bash
93+
export LD_LIBRARY_PATH=<path_to_usdk>:<path_to_neuron_backend>:$LD_LIBRARY_PATH
94+
```

docs/source/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Topics in this section will help you get started with ExecuTorch.
111111
customcarditem entries below.
112112
executorch-arm-delegate-tutorial
113113
build-run-coreml
114+
build-run-mediatek-backend
114115
build-run-mps
115116
build-run-qualcomm-ai-engine-direct-backend
116117
build-run-xtensa
@@ -331,6 +332,13 @@ ExecuTorch tutorials.
331332
:link: build-run-coreml.html
332333
:tags: Export,Backend,Delegation,CoreML
333334

335+
.. customcarditem::
336+
:header: Building and Running ExecuTorch with MediaTek Backend
337+
:card_description: A tutorial that walks you through the process of building ExecuTorch with MediaTek Backend
338+
:image: _static/img/generic-pytorch-logo.png
339+
:link: build-run-mediatek-backend.html
340+
:tags: Export,Backend,Delegation,MediaTek
341+
334342
.. customcarditem::
335343
:header: Building and Running ExecuTorch with MPS Backend
336344
:card_description: A tutorial that walks you through the process of building ExecuTorch with MPSGraph Backend

examples/mediatek/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ bash shell_scripts/export_oss.sh <model_name>
7272
- `model_name`: deeplabv3/edsr/inceptionv3/inceptionv4/mobilenetv2/mobilenetv3/resnet18/resnet50
7373

7474
# Runtime
75-
## Supported Chips
76-
77-
The examples provided in this repository are tested and supported on the following MediaTek chip:
78-
79-
- MediaTek Dimensity 9300 (D9300)
80-
8175
## Environment Setup
8276

8377
To set up the build environment for the `mtk_executor_runner`:

0 commit comments

Comments
 (0)