xref: /aosp_15_r20/external/executorch/examples/mediatek/README.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# Directory Structure
2*523fa7a6SAndroid Build Coastguard Worker
3*523fa7a6SAndroid Build Coastguard WorkerBelow is the layout of the `examples/mediatek` directory, which includes the necessary files for the example applications:
4*523fa7a6SAndroid Build Coastguard Worker
5*523fa7a6SAndroid Build Coastguard Worker```plaintext
6*523fa7a6SAndroid Build Coastguard Workerexamples/mediatek
7*523fa7a6SAndroid Build Coastguard Worker├── aot_utils                         # Utils for AoT export
8*523fa7a6SAndroid Build Coastguard Worker    ├── llm_utils                     # Utils for LLM models
9*523fa7a6SAndroid Build Coastguard Worker        ├── preformatter_templates    # Model specific prompt preformatter templates
10*523fa7a6SAndroid Build Coastguard Worker        ├── prompts                   # Calibration Prompts
11*523fa7a6SAndroid Build Coastguard Worker        ├── tokenizers_               # Model tokenizer scripts
12*523fa7a6SAndroid Build Coastguard Worker    ├── oss_utils                     # Utils for oss models
13*523fa7a6SAndroid Build Coastguard Worker├── eval_utils                        # Utils for eval oss models
14*523fa7a6SAndroid Build Coastguard Worker├── model_export_scripts              # Model specifc export scripts
15*523fa7a6SAndroid Build Coastguard Worker├── models                            # Model definitions
16*523fa7a6SAndroid Build Coastguard Worker    ├── llm_models                    # LLM model definitions
17*523fa7a6SAndroid Build Coastguard Worker        ├── weights                   # LLM model weights location (Offline) [Ensure that config.json, relevant tokenizer files and .bin or .safetensors weights file(s) are placed here]
18*523fa7a6SAndroid Build Coastguard Worker├── executor_runner                   # Example C++ wrapper for the ExecuTorch runtime
19*523fa7a6SAndroid Build Coastguard Worker├── pte                               # Generated .pte files location
20*523fa7a6SAndroid Build Coastguard Worker├── shell_scripts                     # Shell scripts to quickrun model specific exports
21*523fa7a6SAndroid Build Coastguard Worker├── CMakeLists.txt                    # CMake build configuration file for compiling examples
22*523fa7a6SAndroid Build Coastguard Worker├── requirements.txt                  # MTK and other required packages
23*523fa7a6SAndroid Build Coastguard Worker├── mtk_build_examples.sh             # Script for building MediaTek backend and the examples
24*523fa7a6SAndroid Build Coastguard Worker└── README.md                         # Documentation for the examples (this file)
25*523fa7a6SAndroid Build Coastguard Worker```
26*523fa7a6SAndroid Build Coastguard Worker# Examples Build Instructions
27*523fa7a6SAndroid Build Coastguard Worker
28*523fa7a6SAndroid Build Coastguard Worker## Environment Setup
29*523fa7a6SAndroid Build Coastguard Worker- Follow the instructions of **Prerequisites** and **Setup** in `backends/mediatek/scripts/README.md`.
30*523fa7a6SAndroid Build Coastguard Worker
31*523fa7a6SAndroid Build Coastguard Worker## Build MediaTek Examples
32*523fa7a6SAndroid Build Coastguard Worker1. Build the backend and the examples by exedcuting the script:
33*523fa7a6SAndroid Build Coastguard Worker```bash
34*523fa7a6SAndroid Build Coastguard Worker./mtk_build_examples.sh
35*523fa7a6SAndroid Build Coastguard Worker```
36*523fa7a6SAndroid Build Coastguard Worker
37*523fa7a6SAndroid Build Coastguard Worker## LLaMa Example Instructions
38*523fa7a6SAndroid Build Coastguard Worker##### Note: Verify that localhost connection is available before running AoT Flow
39*523fa7a6SAndroid Build Coastguard Worker1. Exporting Models to `.pte`
40*523fa7a6SAndroid Build Coastguard Worker- In the `examples/mediatek directory`, run:
41*523fa7a6SAndroid Build Coastguard Worker```bash
42*523fa7a6SAndroid Build Coastguard Workersource shell_scripts/export_llama.sh <model_name> <num_chunks> <prompt_num_tokens> <cache_size> <calibration_set_name>
43*523fa7a6SAndroid Build Coastguard Worker```
44*523fa7a6SAndroid Build Coastguard Worker- Defaults:
45*523fa7a6SAndroid Build Coastguard Worker    - `model_name` = llama3
46*523fa7a6SAndroid Build Coastguard Worker    - `num_chunks` = 4
47*523fa7a6SAndroid Build Coastguard Worker    - `prompt_num_tokens` = 128
48*523fa7a6SAndroid Build Coastguard Worker    - `cache_size` = 1024
49*523fa7a6SAndroid Build Coastguard Worker    - `calibration_set_name` = None
50*523fa7a6SAndroid Build Coastguard Worker- Argument Explanations/Options:
51*523fa7a6SAndroid Build Coastguard Worker    - `model_name`: llama2/llama3
52*523fa7a6SAndroid Build Coastguard Worker    <sub>**Note: Currently Only Tested on Llama2 7B Chat and Llama3 8B Instruct.**</sub>
53*523fa7a6SAndroid Build Coastguard Worker    - `num_chunks`: Number of chunks to split the model into. Each chunk contains the same number of decoder layers. Will result in `num_chunks` number of `.pte` files being generated. Typical values are 1, 2 and 4.
54*523fa7a6SAndroid Build Coastguard Worker    - `prompt_num_tokens`: Number of tokens (> 1) consumed each forward pass for the prompt processing stage.
55*523fa7a6SAndroid Build Coastguard Worker    - `cache_size`: Cache Size.
56*523fa7a6SAndroid Build Coastguard Worker    - `calibration_set_name`: Name of calibration dataset with extension that is found inside the `aot_utils/llm_utils/prompts` directory. Example: `alpaca.txt`. If `"None"`, will use dummy data to calibrate.
57*523fa7a6SAndroid Build Coastguard Worker    <sub>**Note: Export script example only tested on `.txt` file.**</sub>
58*523fa7a6SAndroid Build Coastguard Worker
59*523fa7a6SAndroid Build Coastguard Worker2. `.pte` files will be generated in `examples/mediatek/pte`
60*523fa7a6SAndroid Build Coastguard Worker    - Users should expect `num_chunks*2` number of pte files (half of them for prompt and half of them for generation).
61*523fa7a6SAndroid Build Coastguard Worker    - Generation `.pte` files have "`1t`" in their names.
62*523fa7a6SAndroid Build Coastguard Worker    - Additionally, an embedding bin file will be generated in the weights folder where the `config.json` can be found in. [`examples/mediatek/models/llm_models/weights/<model_name>/embedding_<model_config_folder>_fp32.bin`]
63*523fa7a6SAndroid Build Coastguard Worker    - eg. For `llama3-8B-instruct`, embedding bin generated in `examples/mediatek/models/llm_models/weights/llama3-8B-instruct/`
64*523fa7a6SAndroid Build Coastguard Worker    - AoT flow will take roughly 2.5 hours (114GB RAM for `num_chunks=4`) to complete (Results will vary by device/hardware configurations)
65*523fa7a6SAndroid Build Coastguard Worker
66*523fa7a6SAndroid Build Coastguard Worker### oss
67*523fa7a6SAndroid Build Coastguard Worker1. Exporting Model to `.pte`
68*523fa7a6SAndroid Build Coastguard Worker```bash
69*523fa7a6SAndroid Build Coastguard Workerbash shell_scripts/export_oss.sh <model_name>
70*523fa7a6SAndroid Build Coastguard Worker```
71*523fa7a6SAndroid Build Coastguard Worker- Argument Options:
72*523fa7a6SAndroid Build Coastguard Worker    - `model_name`: deeplabv3/edsr/inceptionv3/inceptionv4/mobilenetv2/mobilenetv3/resnet18/resnet50
73*523fa7a6SAndroid Build Coastguard Worker
74*523fa7a6SAndroid Build Coastguard Worker# Runtime
75*523fa7a6SAndroid Build Coastguard Worker## Environment Setup
76*523fa7a6SAndroid Build Coastguard Worker
77*523fa7a6SAndroid Build Coastguard WorkerTo set up the build environment for the `mtk_executor_runner`:
78*523fa7a6SAndroid Build Coastguard Worker
79*523fa7a6SAndroid Build Coastguard Worker1. Navigate to the `backends/mediatek/scripts` directory within the repository.
80*523fa7a6SAndroid Build Coastguard Worker2. Follow the detailed build steps provided in that location.
81*523fa7a6SAndroid Build Coastguard Worker3. Upon successful completion of the build steps, the `mtk_executor_runner` binary will be generated.
82*523fa7a6SAndroid Build Coastguard Worker
83*523fa7a6SAndroid Build Coastguard Worker## Deploying and Running on the Device
84*523fa7a6SAndroid Build Coastguard Worker
85*523fa7a6SAndroid Build Coastguard Worker### Pushing Files to the Device
86*523fa7a6SAndroid Build Coastguard Worker
87*523fa7a6SAndroid Build Coastguard WorkerTransfer the `.pte` model files and the `mtk_executor_runner` binary to your Android device using the following commands:
88*523fa7a6SAndroid Build Coastguard Worker
89*523fa7a6SAndroid Build Coastguard Worker```bash
90*523fa7a6SAndroid Build Coastguard Workeradb push mtk_executor_runner <PHONE_PATH, e.g. /data/local/tmp>
91*523fa7a6SAndroid Build Coastguard Workeradb push <MODEL_NAME>.pte <PHONE_PATH, e.g. /data/local/tmp>
92*523fa7a6SAndroid Build Coastguard Worker```
93*523fa7a6SAndroid Build Coastguard Worker
94*523fa7a6SAndroid Build Coastguard WorkerMake sure to replace `<MODEL_NAME>` with the actual name of your model file. And, replace the `<PHONE_PATH>` with the desired detination on the device.
95*523fa7a6SAndroid Build Coastguard Worker
96*523fa7a6SAndroid Build Coastguard Worker##### Note: For oss models, please push additional files to your Android device
97*523fa7a6SAndroid Build Coastguard Worker```bash
98*523fa7a6SAndroid Build Coastguard Workeradb push mtk_oss_executor_runner <PHONE_PATH, e.g. /data/local/tmp>
99*523fa7a6SAndroid Build Coastguard Workeradb push input_list.txt <PHONE_PATH, e.g. /data/local/tmp>
100*523fa7a6SAndroid Build Coastguard Workerfor i in input*bin; do adb push "$i" <PHONE_PATH, e.g. /data/local/tmp>; done;
101*523fa7a6SAndroid Build Coastguard Worker```
102*523fa7a6SAndroid Build Coastguard Worker
103*523fa7a6SAndroid Build Coastguard Worker### Executing the Model
104*523fa7a6SAndroid Build Coastguard Worker
105*523fa7a6SAndroid Build Coastguard WorkerExecute the model on your Android device by running:
106*523fa7a6SAndroid Build Coastguard Worker
107*523fa7a6SAndroid Build Coastguard Worker```bash
108*523fa7a6SAndroid Build Coastguard Workeradb shell "/data/local/tmp/mtk_executor_runner --model_path /data/local/tmp/<MODEL_NAME>.pte --iteration <ITER_TIMES>"
109*523fa7a6SAndroid Build Coastguard Worker```
110*523fa7a6SAndroid Build Coastguard Worker
111*523fa7a6SAndroid Build Coastguard WorkerIn the command above, replace `<MODEL_NAME>` with the name of your model file and `<ITER_TIMES>` with the desired number of iterations to run the model.
112*523fa7a6SAndroid Build Coastguard Worker
113*523fa7a6SAndroid Build Coastguard Worker##### Note: For llama models, please use `mtk_llama_executor_runner`. Refer to `examples/mediatek/executor_runner/run_llama3_sample.sh` for reference.
114*523fa7a6SAndroid Build Coastguard Worker##### Note: For oss models, please use `mtk_oss_executor_runner`.
115*523fa7a6SAndroid Build Coastguard Worker```bash
116*523fa7a6SAndroid Build Coastguard Workeradb shell "/data/local/tmp/mtk_oss_executor_runner --model_path /data/local/tmp/<MODEL_NAME>.pte --input_list /data/local/tmp/input_list.txt --output_folder /data/local/tmp/output_<MODEL_NAME>"
117*523fa7a6SAndroid Build Coastguard Workeradb pull "/data/local/tmp/output_<MODEL_NAME> ./"
118*523fa7a6SAndroid Build Coastguard Worker```
119*523fa7a6SAndroid Build Coastguard Worker
120*523fa7a6SAndroid Build Coastguard Worker### Check oss result on PC
121*523fa7a6SAndroid Build Coastguard Worker```bash
122*523fa7a6SAndroid Build Coastguard Workerpython3 eval_utils/eval_oss_result.py --eval_type <eval_type> --target_f <golden_folder> --output_f <prediction_folder>
123*523fa7a6SAndroid Build Coastguard Worker```
124*523fa7a6SAndroid Build Coastguard WorkerFor example:
125*523fa7a6SAndroid Build Coastguard Worker```
126*523fa7a6SAndroid Build Coastguard Workerpython3 eval_utils/eval_oss_result.py --eval_type piq --target_f edsr --output_f output_edsr
127*523fa7a6SAndroid Build Coastguard Worker```
128*523fa7a6SAndroid Build Coastguard Worker- Argument Options:
129*523fa7a6SAndroid Build Coastguard Worker    - `eval_type`: topk/piq/segmentation
130*523fa7a6SAndroid Build Coastguard Worker    - `target_f`: folder contain golden data files. file name is `golden_<data_idx>_0.bin`
131*523fa7a6SAndroid Build Coastguard Worker    - `output_f`: folder contain model output data files. file name is `output_<data_idx>_0.bin`
132