|
| 1 | +--- |
| 2 | +title: Build ONNX Runtime with KleidiAI and SME2 for Android |
| 3 | +weight: 4 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Build ONNX Runtime and benchmark application with KleidiAI and SME2 support for Android |
| 10 | + |
| 11 | +To run this on an Android device, you must cross-compile ORT using the Android NDK. |
| 12 | +Prerequisites |
| 13 | +- Android NDK: Version r26b or newer (r27+ recommended for latest SME2 toolchain support). |
| 14 | +- CMake & Ninja: Ensure these are in your system PATH. |
| 15 | + |
| 16 | +### Build Command |
| 17 | +Run the following from the root of the ONNX Runtime repository: |
| 18 | +```bash |
| 19 | +./build.sh --android --android_sdk_path $ANDROID_NDK_HOME --android_ndk_path $ANDROID_NDK_HOME --android_abi arm64-v8a --android_api 27 --config RelWithDebInfo --build_shared_lib --cmake_extra_defines onnxruntime_USE_KLEIDIAI=ON --cmake_generator Ninja --parallel |
| 20 | +``` |
| 21 | + |
| 22 | +Note: The flag “onnxruntime_USE_KLEIDIAI=ON” triggers the inclusion of Arm KleidiAI kernels into the MLAS library. |
| 23 | + |
| 24 | +## Profiling Performance with onnxruntime_perf_test |
| 25 | +Once the build is complete, you will find the libonnxruntime.so shared library and onnxruntime_perf_test binary in your build directory. The onnxruntime_perf_test is essential for measuring latency and identifying bottlenecks. |
| 26 | +### Step 1: Push files to Android Device |
| 27 | +```bash |
| 28 | +adb push <build_dir>/Android/RelWithDebInfo/onnxruntime_perf_test /data/local/tmp/ |
| 29 | +adb push <build_dir>/Android/RelWithDebInfo/libonnxruntime.so /data/local/tmp/ |
| 30 | +adb push your_model.onnx /data/local/tmp/ |
| 31 | +``` |
| 32 | +### Step 2: Run the Performance Test |
| 33 | +The perf_test tool allows you to simulate inference and gather statistics. For example, |
| 34 | +```bash |
| 35 | +# Execute on the device |
| 36 | +adb shell "/data/local/tmp/onnxruntime_perf_test -e cpu -m times -r 20 -s -Z -x 1 /data/local/tmp/your_model.onnx" |
| 37 | +``` |
| 38 | +The command example set the arguments of the application as, |
| 39 | +- “-e cpu” specifies the provider as cpu provider |
| 40 | +- “-m times” specifies the test mode as “times” |
| 41 | +- “-r 20” specifies the repeated times as 20 |
| 42 | +- “-Z” disallows thread from spinning during runs to reduce cpu usage |
| 43 | +- “-s” shows statistics result |
| 44 | +- “-x 1” sets the number of threads used to parallelize the execution within nodes as 1 |
| 45 | + |
| 46 | +You can try other arguments setting if you would like to. |
| 47 | + |
| 48 | +### Step 3: Deep Dive into Operator Profiling |
| 49 | +To see exactly how many milliseconds are spent on each operator, use the profiling flag -p. |
| 50 | +```bash |
| 51 | +adb shell "/data/local/tmp/onnxruntime_perf_test -p profile.json -e cpu -m times -r 5 -s -Z -x 1 /data/local/tmp/your_model.onnx" |
| 52 | +adb pull /data/local/tmp/profile.json |
| 53 | +``` |
| 54 | +The argument “-p” enables performance profiling during the benchmark run. When you provide this flag followed by a filename, ONNX Runtime will generate a JSON file containing a detailed trace of the model execution. |
| 55 | +You can view the results by opening [prefetto tool]( https://ui.perfetto.dev/), and loading the generated JSON file. This allows you to see a visual timeline of which operations took the most time. |
| 56 | +You also can convert the JSON file to a CSV sheet by creating a python script. |
0 commit comments