Elffuss BraidHub

kikocis/kleidiai public

main · 11.9 KiBRenderedRawPermalink
1<!--
2 SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3
4 SPDX-License-Identifier: Apache-2.0
5-->
6
7# KleidiAI
8
9KleidiAI is an open-source library that provides optimized performance-critical routines, also known as **micro-kernels**, for artificial intelligence (AI) workloads tailored for Arm® CPUs.
10
11These routines are tuned to exploit the capabilities of specific Arm® hardware architectures, aiming to maximize performance.
12
13The KleidiAI library has been designed for ease of adoption into C or C++ machine learning (ML) and AI frameworks. Specifically, developers looking to incorporate specific micro-kernels into their projects can only include the corresponding **.c** and **.h** files associated with those micro-kernels and a common header file.
14
15## Who is this library for?
16
17KleidiAI is a library for AI/ML framework developers interested in accelerating the computation on Arm® CPUs.
18
19## What is a micro-kernel?
20
21A micro-kernel, or **ukernel**, can be defined as a near-minimum amount of software to accelerate a given ML operator with high performance.
22
23Following are examples of a micro-kernel
24
25- Function to perform [packing](kai/ukernels/matmul/pack/README.md)
26- Function to perform matrix multiplication
27
28*However, why are the preceding operations not called kernels or functions instead?*
29
30**This is because the micro-kernels are designed to give the flexibility to process also a portion of the output tensor.**
31
32> ℹ️ The API of the micro-kernel is intended to provide the flexibility to dispatch the operation among different working threads or process only a section of the output tensor. Therefore, the caller can control what to process and how.
33
34A micro-kernel exists for different Arm® architectures, technologies, and computational parameters (for example, different output tile sizes). These implementations are called **micro-kernel variants**. All micro-kernel variants of the same micro-kernel type perform the same operation and return the same output result.
35
36## Key features
37
38Some of the key features of KleidiAI are the following:
39
40- No dependencies on external libraries
41
42- No dynamic memory allocation
43
44- No memory management​
45
46- No scheduling
47
48- Stateless, stable, and consistent API​
49
50- Performance-critical compute-bound and memory-bound micro-kernels
51
52- Specialized micro-kernels utilizing different Arm® CPU architectural features (for example, **FEAT_DotProd** and **FEAT_I8MM**)
53
54- Specialized micro-kernels for different fusion patterns
55
56- Micro-kernel as a standalone library, consisting of only a **.c** and **.h** files
57
58> ℹ️ The micro-kernel API is designed to be as generic as possible for integration into third-party runtimes.
59
60## Supported instructions and extensions
61
62- Advanced SIMD instructions
63- Scalable Vector Extension (SVE)
64- Scalable Matrix Extension (SME)
65- Scalable Matrix Extension 2 (SME2)
66
67The SME and SME2 micro-kernels require compiler support to generate SME ABI-compliant code.
68You can still use the micro-kernels without compiler support, but not within a call chain that already uses ZA register.
69At the moment this is not automatically detected, and you need to build with `KLEIDIAI_INTERNAL_EXTRA_ARCH=+sme` to
70enable this support.
71
72## API Compatibility
73
74In order to cater for any future changes in content and structure, it is advised integrators should use the KleidiAI build system provided.
75
76## Filename convention
77
78The `kai/ukernels` directory is the home for all micro-kernels. The micro-kernels are grouped in separate directories based on the performed operation. For example, all the matrix-multiplication micro-kernels are held in the `matmul/` operator directory.
79
80Inside the operator directory, you can find:
81
82- *The common micro-kernels*, which are helper micro-kernels necessary for the correct functioning of the main ones. For example, some of these may be required for packing the input tensors and held in the `pack` subdirectory.
83- *The micro-kernels* files, which are held in separate sub-directories.
84
85The name of the micro-kernel folder provides the description of the operation performed and the data type of the destination and source tensors. The general syntax for the micro-kernel folder is as follows:
86
87`<op>_<dst-data-type>_<src0-data-type>_<src1-data-type>_...`
88
89All **.c** and **.h** pair files in that folder are micro-kernel variants. The variants are differentiated by specifying the computational paramaters (for example, the block size), the Arm® technology (for example, Arm® Neon™), and Arm® architecture feature exploited (for example, **FEAT_DotProd**). The general syntax for the micro-kernel variant is as follows:
90
91`kai_<micro-kernel-folder>_<compute-params>_<technology>_<arch-feature>.c/.h`
92
93> ℹ️ These files, only depend on the `kai_common.h` file.
94
95All functions defined in the **.h** header file of the micro-kernel variant has the following syntax:
96
97`kai_<op>_<micro-kernel-variant-filename>.c/.h`
98
99## Supported micro-kernels
100
101For a list of supported micro-kernels refer to the [KleidiAI micro-kernel tables](docs/microkernel_tables.md#kleidiai-micro-kernel-tables) and the [source](/kai/ukernels/) directory. The micro-kernels are grouped in separate directories based on the performed operation.
102For example, all the matrix-multiplication micro-kernels are held in the `matmul/` subdirectory. In there, the micro-kernels are grouped into directories whose name syntax describes the micro-kernel from a data type point of view of inputs and outputs.
103
104## How to build
105
106### Prerequisites
107
108KleidiAI requires the following dependencies, obtainable via your preferred package manager, to be installed and available on your system to be able to build the project.
109
110- `build-essential`
111- `cmake >= 3.18`
112
113In addition, you may choose to use the following toolchains:
114
115- (Optional) `Arm GNU toolchain` available to download from the [Arm Developer](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads) website.
116- (Optional) `Android NDK` available to download from the [Android Developer](https://developer.android.com/ndk/downloads/index.html) website.
117
118### Compile natively on an Arm®-based system
119
120You can quickly compile KleidiAI on your system with an Arm® processor by using the following commands:
121
122```shell
123cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/
124cmake --build ./build
125```
126
127### Cross-compile to Android™
128
129Cross-compiling for Android systems requires the Android NDK toolset. The downloaded NDK contains the CMake toolchain file necessary for cross-compiling the project and must be provided to CMake with the `-DCMAKE_TOOLCHAIN_FILE` option.
130
131```shell
132cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -S . -B build/
133cmake --build ./build
134```
135
136### Cross-compile to Linux®
137
138The Arm GNU toolchain can be used to cross-compile to a Linux system with an Arm® processor like a Raspberry Pi from an x86_64 Linux host machine. Ensure the toolchain is available on your PATH and provide to CMake the Arm GNU Toolchain CMakefile found in `cmake/toolchains` directory with the `-DCMAKE_TOOLCHAIN_FILE` option.
139
140```shell
141cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/aarch64-none-linux-gnu.toolchain.cmake -S . -B build/
142cmake --build ./build
143```
144
145## Release
146
147### Cadence
148
149Two releases will be done per month. All releases can be found in the [release](https://gitlab.arm.com/kleidi/kleidiai/-/releases) section.
150
151### Version
152
153The release version conforms to Semantic Versioning.
154
155> ⚠️ Please note that API modifications, including function name changes, and feature enhancements may occur without advance notice.
156
157We recommend that you use release versions of KleidiAI. You can either use `git` to clone the repository or download a source archive. These are available from the [official source](https://gitlab.arm.com/kleidi/kleidiai) or the [GitHub mirror](https://github.com/ARM-software/kleidiai).
158
159## Support
160
161Please raise a [GitLab Issue](https://gitlab.arm.com/kleidi/kleidiai/-/issues/new) for technical support.
162
163## Frequently Asked Questions (FAQ)
164
165### Is x86 supported?
166
167No, KleidiAI is a library of micro-kernels optimized for the Arm® architecture. The micro-kernels use specific Arm® instruction set architecture features which prevent compilation for other architectures like x86. Note that this applies to all micro-kernels including the packing and quantization micro-kernels.
168
169### What ML operators are supported?
170
171KleidiAI does not provide traditional ML operators; it implements a set of optimized micro-kernels which can be used as building blocks to implement ML operators. Please refer to the sections [What is a micro-kernel](#what-is-a-micro-kernel), [Supported micro-kernels](#supported-micro-kernels) and the [docs](/docs/README.md) for more information.
172
173### How do I use the KleidiAI micro-kernels?
174
175Please refer to the [Examples](docs/README.md#examples) section of the documentation and the sample applications in the [examples](/examples) sub-directory. These examples are standalone C++ applications that demonstrate end-to-end use of a selection of the micro-kernels.
176
177### What CPUs does KleidiAI support?
178
179Any CPU that implements a set of the Arm® architecture extensions listed in section [Supported instructions and extensions](#supported-instructions-and-extensions) is supported by KleidiAI. However, every micro-kernel does not exist in versions for all architecture extensions. Each micro-kernel advertises the required architecture extensions through the micro-kernel name. See [Micro-kernel naming](kai/ukernels/matmul/README.md#micro-kernel-naming) for further details.
180
181Most operating systems provide a mechanism to list the architecture extensions supported by the runtime environment, e.g. `cat /proc/cpuinfo` on Linux.
182
183### What is the difference between the Compute Library for the Arm® Architecture (ACL) and KleidiAI?
184
185This question will pop up naturally if you are familiar with the **[ACL](https://github.com/ARM-software/ComputeLibrary)**.
186
187> ACL and KleidiAI differ with respect to the integration point into the AI/ML framework.
188
189ACL provides a complete suite of ML operators for Arm® CPUs and Arm Mali™ GPUs. It also provides a runtime with memory management, thread management, fusion capabilities, etc.
190
191Therefore, **ACL is a library suitable for frameworks that need to delegate the model inference computation entirely**.
192
193KleidiAI offers optimized micro-kernels such as matrix multiplication and depthwise convolution which are used as building blocks in the implementation of performance-critical ML operators. As such, **KleidiAI is designed for frameworks where the runtime, memory manager, thread management, and fusion mechanisms are already available**.
194
195### Can the micro-kernels be multi-threaded?
196
197**Yes, they can**. The micro-kernel can be dispatched among multiple threads using the thread management available in the target AI/ML framework.
198
199*The micro-kernel does not use any internal threading mechanism*. However, the micro-kernel's API is designed to allow the computation to be carried out only on specific areas of the output tensor. Therefore, this mechanism is sufficient to split the workload on parallel threads.
200
201Please refer to [examples/matmul_clamp_f32_qsi8d32p_qsi4c32p](/examples/matmul_clamp_f32_qsi8d32p_qsi4c32p) for an example on how to use micro-kernels in a multithreaded environment.
202
203### What are the recommended download sources for KleidiAI?
204
205Note that dynamically generated source archives can result in changed hash digests. If you want something unchanging, then use released source archives. These are listed under _Packages_ on the [official release page](https://gitlab.arm.com/kleidi/kleidiai/-/releases), or have a hash digest (e.g.,`SHA256`) on GitHub.
206
207## Experimental code
208
209Disclaimer: the `experimental` directory holds experimental features. Do not expect production quality. Note that API might change at any point in time for any file in `experimental`.
210
211## License
212
213KleidiAI is distributed under the software licenses in LICENSES directory.