xref: /aosp_15_r20/external/executorch/backends/mediatek/scripts/README.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# Build Instructions
2*523fa7a6SAndroid Build Coastguard Worker
3*523fa7a6SAndroid Build Coastguard WorkerThis document provides a step-by-step guide to set up the build environment for the MediaTek ExercuTorch libraries.
4*523fa7a6SAndroid Build Coastguard Worker
5*523fa7a6SAndroid Build Coastguard Worker## Prerequisites
6*523fa7a6SAndroid Build Coastguard Worker
7*523fa7a6SAndroid Build Coastguard WorkerBefore you begin, ensure you have the following prerequisites installed and configured:
8*523fa7a6SAndroid Build Coastguard Worker
9*523fa7a6SAndroid Build Coastguard Worker### 1. Buck2 Build Tool
10*523fa7a6SAndroid Build Coastguard Worker
11*523fa7a6SAndroid Build Coastguard Worker- **Download Buck2**: Obtain Buck2 from the official [releases page](https://github.com/facebook/buck2/releases/tag/2024-02-01).
12*523fa7a6SAndroid Build Coastguard Worker- **Add to PATH**: Extract the downloaded file and add the directory to your system's `$PATH` environment variable.
13*523fa7a6SAndroid Build Coastguard Worker   ```bash
14*523fa7a6SAndroid Build Coastguard Worker   export PATH=<path_to_buck>:$PATH
15*523fa7a6SAndroid Build Coastguard Worker   ```
16*523fa7a6SAndroid Build Coastguard Worker
17*523fa7a6SAndroid Build Coastguard Worker### 2. Android NDK
18*523fa7a6SAndroid Build Coastguard Worker
19*523fa7a6SAndroid Build Coastguard Worker- **Download Android NDK**: Acquire the Android NDK version 26.3.11579264 from the [Android developer site](https://developer.android.com/ndk/downloads).
20*523fa7a6SAndroid Build Coastguard Worker- **Set NDK Path**: Ensure that the `$ANDROID_NDK` environment variable is set to the path where the NDK is located.
21*523fa7a6SAndroid Build Coastguard Worker   ```bash
22*523fa7a6SAndroid Build Coastguard Worker   export ANDROID_NDK=<path_to_android_ndk>
23*523fa7a6SAndroid Build Coastguard Worker   ```
24*523fa7a6SAndroid Build Coastguard Worker
25*523fa7a6SAndroid Build Coastguard Worker### 3. MediaTek ExercuTorch Libraries
26*523fa7a6SAndroid Build Coastguard Worker
27*523fa7a6SAndroid Build Coastguard WorkerDownload [NeuroPilot Express SDK](https://neuropilot.mediatek.com/resources/public/npexpress/en/docs/npexpress) from MediaTek's NeuroPilot portal:
28*523fa7a6SAndroid Build Coastguard Worker
29*523fa7a6SAndroid Build Coastguard Worker- `libneuronusdk_adapter.mtk.so`: This universal SDK contains the implementation required for executing target-dependent code on the MediaTek chip.
30*523fa7a6SAndroid Build Coastguard Worker- `libneuron_buffer_allocator.so`: This utility library is designed for allocating DMA buffers necessary for model inference.
31*523fa7a6SAndroid Build Coastguard Worker- `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.
32*523fa7a6SAndroid Build Coastguard Worker- `mtk_neuron-8.2.2-py3-none-linux_x86_64.whl`: This library converts the model to binaries.
33*523fa7a6SAndroid Build Coastguard Worker
34*523fa7a6SAndroid Build Coastguard Worker## Setup
35*523fa7a6SAndroid Build Coastguard Worker
36*523fa7a6SAndroid Build Coastguard WorkerFollow the steps below to setup your build environment:
37*523fa7a6SAndroid Build Coastguard Worker
38*523fa7a6SAndroid Build Coastguard Worker1. **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.
39*523fa7a6SAndroid Build Coastguard Worker
40*523fa7a6SAndroid Build Coastguard Worker2. **Setup MediaTek Backend Environment**
41*523fa7a6SAndroid Build Coastguard Worker- Install the dependent libs. Ensure that you are inside backends/mediatek/ directory
42*523fa7a6SAndroid Build Coastguard Worker   ```bash
43*523fa7a6SAndroid Build Coastguard Worker   pip3 install -r requirements.txt
44*523fa7a6SAndroid Build Coastguard Worker   ```
45*523fa7a6SAndroid Build Coastguard Worker- Install the two .whl downloaded from NeuroPilot Portal
46*523fa7a6SAndroid Build Coastguard Worker   ```bash
47*523fa7a6SAndroid Build Coastguard Worker   pip3 install mtk_neuron-8.2.2-py3-none-linux_x86_64.whl
48*523fa7a6SAndroid Build Coastguard Worker   pip3 install mtk_converter-8.8.0.dev20240723+public.d1467db9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
49*523fa7a6SAndroid Build Coastguard Worker   ```
50*523fa7a6SAndroid Build Coastguard Worker- Set evironment variables for building backend
51*523fa7a6SAndroid Build Coastguard Worker   ```bash
52*523fa7a6SAndroid Build Coastguard Worker   export NEURON_BUFFER_ALLOCATOR_LIB=<path_to_buffer_allocator>
53*523fa7a6SAndroid Build Coastguard Worker   ```
54*523fa7a6SAndroid Build Coastguard Worker
55*523fa7a6SAndroid Build Coastguard Worker## Build
56*523fa7a6SAndroid Build Coastguard Worker
57*523fa7a6SAndroid Build Coastguard Worker1. **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`
58*523fa7a6SAndroid Build Coastguard Worker
59*523fa7a6SAndroid Build Coastguard Worker   ```bash
60*523fa7a6SAndroid Build Coastguard Worker   ./mtk_build.sh
61*523fa7a6SAndroid Build Coastguard Worker   ```
62*523fa7a6SAndroid Build Coastguard Worker
63*523fa7a6SAndroid Build Coastguard Worker## Run
64*523fa7a6SAndroid Build Coastguard Worker
65*523fa7a6SAndroid Build Coastguard Worker1. **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.
66*523fa7a6SAndroid Build Coastguard Worker
67*523fa7a6SAndroid Build Coastguard Worker   ```bash
68*523fa7a6SAndroid Build Coastguard Worker   export LD_LIBRARY_PATH=<path_to_usdk>:<path_to_neuron_backend>:$LD_LIBRARY_PATH
69*523fa7a6SAndroid Build Coastguard Worker   ```
70