Skip to content

Commit fa18ee5

Browse files
Merge pull request #2762 from madeline-underwood/unleash
Unleash SME_reviewed
2 parents 1ec85bc + 7745852 commit fa18ee5

5 files changed

Lines changed: 157 additions & 114 deletions

File tree

content/learning-paths/mobile-graphics-and-gaming/performance_onnxruntime_kleidiai_sme2/_index.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
---
2-
title: Unleashing SME2 Performance - Profile ONNX models with KleidiAI-Optimized ONNX Runtime
2+
title: Profile ONNX model performance with SME2 using KleidiAI and ONNX Runtime
3+
34

4-
draft: true
5-
cascade:
6-
draft: true
75

86
minutes_to_complete: 40
97

10-
who_is_this_for: This is an advanced topic for software developers, performance engineers, and AI practitioners
8+
who_is_this_for: This is an advanced topic for software developers, performance engineers, and AI practitioners.
119

1210
learning_objectives:
13-
- Build ONNX runtime library with KleidiAI and SME2 support
14-
- Profile performance of ONNX models
15-
- Learn how KleidiAI and SME2 accelerates ONNX operators
11+
- Build ONNX Runtime with KleidiAI and SME2 support for Android
12+
- Profile ONNX model performance using benchmark tools
13+
- Analyze how KleidiAI kernels accelerate ONNX operators with SME2
14+
- Compare performance improvements between standard and SME2-optimized execution
1615

1716
prerequisites:
18-
- Knowledge of KleidiAI and SME2
1917
- An Android device with Arm SME2 support
18+
- Basic understanding of machine learning model inference
19+
- Familiarity with Android NDK and cross-compilation
2020

2121
author: Zenon Zhilong Xiu
2222

2323
### Tags
2424
skilllevels: Advanced
2525
subjects: ML
2626
armips:
27-
- Arm C1 CPU
28-
- Arm SME2 unit
27+
- Cortex
2928
tools_software_languages:
3029
- C++
31-
- ONNX runtime
30+
- ONNX Runtime
3231
operatingsystems:
3332
- Android
3433
- Linux
@@ -37,15 +36,15 @@ operatingsystems:
3736

3837
further_reading:
3938
- resource:
40-
title: part 1 Arm Scalable Matrix Extension Introduction
39+
title: Arm Scalable Matrix Extension Introduction (Part 1)
4140
link: https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/arm-scalable-matrix-extension-introduction
4241
type: blog
4342
- resource:
44-
title: part 2 Arm Scalable Matrix Extension Instructions
43+
title: Arm Scalable Matrix Extension Instructions (Part 2)
4544
link: https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/arm-scalable-matrix-extension-introduction-p2
4645
type: blog
4746
- resource:
48-
title: part4 Arm SME2 Introduction
47+
title: Arm SME2 Introduction (Part 4)
4948
link: https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/part4-arm-sme2-introduction
5049
type: blog
5150

content/learning-paths/mobile-graphics-and-gaming/performance_onnxruntime_kleidiai_sme2/build_ort.md

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,76 @@ weight: 4
66
layout: learningpathall
77
---
88

9-
## Build ONNX Runtime and benchmark application with KleidiAI and SME2 support for Android
9+
## Build ONNX Runtime with KleidiAI and SME2 for Android
1010

11-
To run this on an Android device, you must cross-compile ORT using the Android NDK.
11+
To run this on an Android device, you need to cross-compile ONNX Runtime using the Android NDK.
1212

13-
Prerequisites:
14-
- Android NDK: Version r26b or newer (r27+ recommended for latest SME2 toolchain support).
15-
- CMake & Ninja: Ensure these are in your system PATH.
13+
Before you begin, verify that you have:
14+
- Android NDK version r26b or newer (r27 or later is recommended for the latest SME2 toolchain support)
15+
- CMake and Ninja installed and available in your system PATH
1616

17-
### Build `onnxruntime`
17+
## Build onnxruntime
1818

19-
First, clone the [ONNX Runtime](https://github.com/microsoft/onnxruntime). This learning path uses version `v1.23.2`:
19+
First, clone the [ONNX Runtime](https://github.com/microsoft/onnxruntime). This Learning Path uses version `v1.23.2`:
2020
```bash
2121
git clone https://github.com/microsoft/onnxruntime.git onnxruntime.git
2222
cd onnxruntime.git/
2323
git checkout v1.23.2
2424
```
2525

26-
Then run the following from the root of the ONNX Runtime repository (the `onnxruntime.git/` directory from the previous command):
26+
Build ONNX Runtime with KleidiAI support enabled. The build script configures cross-compilation for Android arm64-v8a, enables shared library output, and activates KleidiAI integration through the `onnxruntime_USE_KLEIDIAI=ON` flag. Run the following from the root of the ONNX Runtime repository (the `onnxruntime.git/` directory from the previous command):
2727
```bash
2828
./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
2929
```
3030

31-
Notes:
31+
{{% notice Note %}}
3232
- The flag `onnxruntime_USE_KLEIDIAI=ON` triggers the inclusion of Arm KleidiAI kernels into the MLAS library.
33-
- The build directory is `build/` by default. This can be overriden with the `--build_dir <path_to_your_build_directory>` commande line option to `build.sh`
33+
- The build directory is `build/` by default. This can be overridden with the `--build_dir <path_to_your_build_directory>` command line option to `build.sh`.
34+
{{% /notice %}}
35+
36+
## Profile model performance with onnxruntime_perf_test
3437

35-
## Profiling Performance with onnxruntime_perf_test
3638
Once the build is complete, you will find the `libonnxruntime.so` shared library and `onnxruntime_perf_test` binary in your build directory.
3739

3840
`onnxruntime_perf_test` is essential for measuring latency and identifying bottlenecks of an ONNX model (named `<your_model>.onnx` hereafter). Note that `onnxruntime_perf_test` expects the ONNX model to come with some ancilliary files organized in some directory tree (input data for example).
3941

40-
### Step 1: Push files to Android Device
42+
## Push files to Android device
43+
44+
Transfer the benchmark binary and shared library to your Android device:
45+
4146
```bash
4247
adb push <build_dir>/Android/RelWithDebInfo/onnxruntime_perf_test /data/local/tmp/
4348
adb push <build_dir>/Android/RelWithDebInfo/libonnxruntime.so /data/local/tmp/
4449
```
4550

46-
### Step 2: Run the Performance Test
47-
The `onnxruntime_perf_test` tool allows you to simulate inference and gather statistics. For example,
51+
## Run the performance test
52+
53+
The `onnxruntime_perf_test` tool simulates inference and gathers statistics. Run a benchmark with 20 iterations:
4854
```bash
4955
# Execute on the device
5056
adb shell "/data/local/tmp/onnxruntime_perf_test -e cpu -m times -r 20 -s -Z -x 1 /data/local/tmp/<your_model>/<your_model>.onnx"
5157
```
58+
### Command options explained
5259

53-
The command example set the arguments of the application as,
54-
- `-e cpu` specifies the provider as cpu provider
55-
- `-m times` specifies the test mode as “times”
56-
- `-r 20` specifies the repeated times as 20
57-
- `-Z` disallows thread from spinning during runs to reduce cpu usage
58-
- `-s` shows statistics result
59-
- `-x 1` sets the number of threads used to parallelize the execution within nodes as 1
60+
The benchmark command uses several flags to control execution:
6061

61-
You can try other arguments setting if you would like to.
62+
- `-e cpu`: Use the CPU execution provider
63+
- `-m times`: Run in timing mode to measure latency
64+
- `-r 20`: Repeat the test 20 times for consistent results
65+
- `-Z`: Prevent thread spinning to reduce CPU usage
66+
- `-s`: Display statistics after the run
67+
- `-x 1`: Use a single thread for parallel execution within nodes
6268

63-
### Step 3: Deep Dive into Operator Profiling
64-
To see exactly how many milliseconds are spent on each operator, use the profiling flag `-p`.
69+
You can adjust these settings based on your performance testing needs.
70+
71+
## Deep dive into operator profiling
72+
73+
To see exactly how many milliseconds each operator consumes, use the profiling flag `-p`. This generates a JSON trace file:
6574
```bash
6675
adb shell "/data/local/tmp/onnxruntime_perf_test -p /data/local/tmp/profile.json -e cpu -m times -r 5 -s -Z -x 1 /data/local/tmp/<your model>/<your_model>.onnx"
6776
adb pull /data/local/tmp/profile.json
6877
```
6978

70-
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.
71-
You can view the results by opening [perfetto 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.
72-
You also can convert the JSON file to a CSV sheet by creating a python script.
79+
The `-p` flag enables performance profiling during the benchmark run. When you provide this flag followed by a filename, ONNX Runtime generates a JSON file containing a detailed trace of model execution.
80+
81+
You can view the results by opening [perfetto tool](https://ui.perfetto.dev/) and loading the generated JSON file. This shows a visual timeline of which operations took the most time. You can also convert the JSON file to a CSV sheet by creating a Python script.

content/learning-paths/mobile-graphics-and-gaming/performance_onnxruntime_kleidiai_sme2/kleidiai_integration.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ weight: 3
66
layout: learningpathall
77
---
88

9-
## Integration of KleidiAI to ONNX runtime MLAS
10-
ONNX runtime is built with KleidiAI support:
11-
1. Detection: At runtime, MLAS checks the CPU capabilities for SME2 support.
12-
2. Dispatch: If SME2 is detected, MLAS overrides its default kernels. For example, a Gemm (General Matrix Multiplication) operation that would normally use standard vector instructions (such as NEON) is dispatched to a KleidiAI SME2 micro-kernel.
9+
## Integration of KleidiAI to ONNX Runtime MLAS
10+
ONNX Runtime automatically detects and uses KleidiAI when SME2 support is available:
1311

14-
Currently, KleidiAI in MLAS provides `ArmKleidiAI::MlasConv`, `ArmKleidiAI::MlasGemmBatch` and `ArmKleidiAI::MlasDynamicQGemmBatch` kernels.
12+
- Detection: MLAS checks the CPU capabilities for SME2 support at runtime.
13+
- Dispatch: when SME2 is detected, MLAS replaces its default kernels with KleidiAI micro-kernels. For example, a Gemm operation that normally uses NEON instructions dispatches to a KleidiAI SME2 micro-kernel instead.
14+
15+
Currently, KleidiAI in MLAS provides `ArmKleidiAI::MlasConv`, `ArmKleidiAI::MlasGemmBatch`, and `ArmKleidiAI::MlasDynamicQGemmBatch` kernels.
1516

1617
### The ArmKleidiAI::MlasConv kernel
17-
Usually, 2D fp32 convolution operators with batch_size=1 and multiple filters (filter kernel is equal or greater than (3,3)) are dispatched to the `ArmKleidiAI::MlasConv` kernel.
18+
ORT dispatches 2D fp32 (32-bit floating point) convolution operators with batch_size=1 and multiple filters (filter kernel equal to or greater than (3,3)) to the `ArmKleidiAI::MlasConv` kernel.
1819

1920
For example, the figure below shows a (7,7) Conv node.
2021

21-
![Diagram illustrating an example of 7x7 Conv alt-text#center](images/conv_nodes_7x7.jpg "An example of (7,7) Conv node")
22+
![Diagram showing a 7x7 convolution node with input tensor of size 112x112x64, filter size 7x7x64, and output tensor size 56x56x64 with stride 2#center](images/conv_nodes_7x7.jpg "An example of a (7,7) Conv node")
2223

23-
`ArmKleidiAI::MlasConv` kernel makes use of KleidiAIs indirect matrix multiplication (imatmul) micro kernel to accelerate the convolution.
24+
`ArmKleidiAI::MlasConv` kernel uses KleidiAI's indirect matrix multiplication (imatmul) micro kernel to accelerate the convolution.
2425

2526
The function calls are shown as below.
2627
```text
@@ -46,11 +47,11 @@ onnxruntime::InferenceSession::Run
4647
```
4748

4849
### The ArmKleidiAI::MlasGemmBatch kernel
49-
It performs a batched fp32 matrix multiplication (GEMM or GemV) operation using KleidiAI matmul micro kernels. fp32 Conv operators with (1,1) filter kernels also uses this kernel.
50+
This kernel performs a batched fp32 matrix multiplication (GEMM or GemV) operation using KleidiAI matmul micro kernels. fp32 Conv operators with (1,1) filter kernels also use this kernel.
5051

5152
For example, the figure below shows a (1,1) Conv node.
5253

53-
![Diagram illustrating an example of 1x1 Conv alt-text#center](images/conv_nodes_1x1.jpg "An example of (1,1) FusedConv node")
54+
![Diagram showing a 1x1 convolution node with input tensor of size 56x56x64, filter size 1x1x64, and output tensor size 56x56x256 fused with BatchNormalization and Relu operations alt-txt#center](images/conv_nodes_1x1.jpg "An example of a (1,1) FusedConv node")
5455

5556
The function calls of fp32 Conv operators with (1,1) filter kernels are shown below.
5657

@@ -79,7 +80,7 @@ onnxruntime::InferenceSession::Run
7980

8081
For example, the figure below shows a Gemm node.
8182

82-
![Diagram illustrating an example of Gemm node alt-text#center](images/gemm_node.jpg "An example of Gemm node")
83+
![Diagram showing a Gemm (General Matrix Multiplication) node with input matrices A and B, performing matrix multiplication with optional bias addition alt-txt#center](images/gemm_node.jpg "An example of a Gemm node")
8384

8485
The function calls of fp32 Gemm operators are shown below.
8586
```text
@@ -101,5 +102,11 @@ onnxruntime::InferenceSession::Run
101102
```
102103

103104
### The ArmKleidiAI::MlasDynamicQGemmBatch kernel
104-
This kernel is for matrix multiplication with float output of dynamic quantized A and symmetric quantized B.
105-
It uses KleidiAI `kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa` micro kernel.
105+
This kernel performs matrix multiplication with float output of dynamically quantized A and symmetrically quantized B.
106+
It uses the KleidiAI `kai_kernel_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa` micro kernel.
107+
108+
## What you've accomplished and what's next
109+
110+
You now understand how KleidiAI integrates into ONNX Runtime's MLAS backend and which operators benefit from SME2 acceleration. You've learned about the three main kernel types: `MlasConv` for convolution operations, `MlasGemmBatch` for matrix multiplications and 1x1 convolutions, and `MlasDynamicQGemmBatch` for quantized operations. You've also seen the complete function call stacks showing how ONNX Runtime dispatches to KleidiAI kernels.
111+
112+
Next, you'll build ONNX Runtime with KleidiAI support enabled and prepare the benchmark tools for profiling on Android.

0 commit comments

Comments
 (0)