| 1 | <!-- |
| 2 | SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | |
| 4 | SPDX-License-Identifier: Apache-2.0 |
| 5 | --> |
| 6 | |
| 7 | # Micro-kernel API design |
| 8 | |
| 9 | ## Directory structure |
| 10 | |
| 11 | The public API of each set of micro-kernels is organized in 2 header files: |
| 12 | |
| 13 | - `kai/ukernels/<op>/kai_<uker>_types.h` contains the data structures that defines the micro-kernel API. |
| 14 | - `kai/ukernels/<op>/kai_<uker>.h` contains the list of micro-kernel creation functions. |
| 15 | |
| 16 | where: |
| 17 | |
| 18 | - `<op>` - the group of micro-kernels with closely related functionality. E.g. `matmul`, `dwconv`. |
| 19 | - `<uker>` - the micro-kernel with distinct functionality. E.g. `matmul`, `matmul_pack_lhs`, `matmul_pack_rhs`. |
| 20 | |
| 21 | ## API definition |
| 22 | |
| 23 | The micro-kernel API definition is implemented in `kai/ukernels/<op>/kai_<uker>_types.h`. It consists of 3 main components: |
| 24 | |
| 25 | - `kai_<uker>_api` structure containing all the function pointers. |
| 26 | - `kai_<uker>_config` structure containing the set of parameters to configure the functionality of the micro-kernel. All functions in the micro-kernel API take the config as an input. |
| 27 | - `kai_<uker>_args` structure containing the information to run the micro-kernel. It can include parameters that affects the functionality of the micro-kernel, but only for the `run` function. |
| 28 | |
| 29 | ### API structure |
| 30 | |
| 31 | The API structure `kai_<uker>_api` contains the following function pointers: |
| 32 | |
| 33 | | Name | Description | |
| 34 | |-|-| |
| 35 | | `run` | It runs the micro-kernel given the micro-kernel configuration and run arguments. | |
| 36 | | `get_<dim>_step` | These functions are used to get the alignment requirement of a given dimension `<dim>` so that the callers can split the workload into smaller chunks and invoke the micro-kernel in multiple threads. | |
| 37 | | `get_<operand>_stride_<dim>` | These functions calculate the default stride in dimension `<dim>` in bytes of operand `<operand>`. The stride is supplied to the run function so that the micro-kernel can access a portion of the input operands to compute a small portion of the output. | |
| 38 | | `get_<operand>_offset` | These functions calculate the pointer offset in bytes for operand `<operand>` given the start coordinate and the strides so that the micro-kernel can access the right portion of the operands. | |
| 39 | | `get_<operand>_size` | These functions are only provided for output operands so that the user can allocate sufficient memory to store the result. | |
| 40 | |
| 41 | ### Configuration structure |
| 42 | |
| 43 | The configuration structure is organized in multiple level of structures, all of them has the type name with `kai_<uker>_` prefix and `_config` suffix. The specific design of the configuration structure is highly dependent on each micro-kernel family, but the top-level structure `kai_<uker>_config` always consists of the following fields: |
| 44 | |
| 45 | | Name | Type | Description | |
| 46 | |-|-|-| |
| 47 | | `<info>` | `kai_<uker>_<info>_config` | The configuration of a certain aspect (`<info>`) of the micro-kernel. | |
| 48 | |
| 49 | The configuration structure should be used only when there are certain configurable parameters that affect the functionality of the micro-kernel and all its supporting functions. If a parameter only affects the functionality of the `run` function, the run arguments structure is the prefered place to specify such parameter. |
| 50 | |
| 51 | ### Run arguments structure |
| 52 | |
| 53 | The run argument structure is organized in multiple level of structures, all of them has the type name with `kai_<uker>_` prefix and `_args` suffix. The top-level structure `kai_<uker>_args` consists of the following fields: |
| 54 | |
| 55 | | Name | Type | Description | |
| 56 | |-|-|-| |
| 57 | | `flags` | `uint64_t` | The bit flags that can be used to control the functionality of the micro-kernel. If no flag is used, **this field must be set to 0**. The possible flag values are provided in `kai_<uker>_args_flags` enum. | |
| 58 | | `shape` | `kai_<uker>_shape_args` | The problem shape. | |
| 59 | | `operands` | `kai_<uker>_operands_args` | The run-time information of each operand involved in the execution of the micro-kernel. | |
| 60 | |
| 61 | #### Problem shape |
| 62 | |
| 63 | Depending on the specific functionality of the micro-kernel family, the problem shape can be defined differently. For example: |
| 64 | |
| 65 | - Matrix multiplication micro-kernel takes `m`, `n`, and `k` as the problem shape. |
| 66 | - LHS packing micro-kernel for matrix multiplication takes `m` and `k` as the problem shape. |
| 67 | |
| 68 | #### Operands |
| 69 | |
| 70 | The list of operands is highly dependent on the micro-kernel functionality and not all of them are used by a given micro-kernel. To allow future extension of the list of operands, as well as the future extension of the definition of the operand, each operand has its own distict structure. |
| 71 | |
| 72 | The operands structure `kai_<uker>_operands_args` contains a list of fields named `<operand>` of type `kai_<uker>_<operand>_args` containing: |
| 73 | |
| 74 | | Name | Type | Description | |
| 75 | |-|-|-| |
| 76 | | `ptr` | `const void*` or `void*` | The function pointer to the data buffer. Constantness is determined by whether this is input, output or scratch operands. Only `void` pointer is used since the same API is shared by micro-kernels operating on different data type. | |
| 77 | | `stride_<dim>` | `size_t` | The strides in bytes of dimension `<dim>`. Depending on the rank of the operand and the actual data format, different number of strides is needed. | |
| 78 | |
| 79 | ## API creation |
| 80 | |
| 81 | The API structure is the primary way to access each individual function of a given micro-kernel. Each micro-kernel variant provides a function to return the API structure to the caller. The list of all micro-kernel variants of micro-kernel `<uker>` is provided in `kai/ukernels/<op>/kai_<uker>.h`. |
| 82 | |
| 83 | Each micro-kernel variant `<variant>` provides the following functions: |
| 84 | |
| 85 | | Name | Return type | Description | |
| 86 | |-|-|-| |
| 87 | | `kai_<uker>_<variant>` | `kai_<uker>_uker_api` | Returns the API structure containing the function pointers to the micro-kernel variant. | |
| 88 | |
| 89 | Notes: |
| 90 | |
| 91 | - The creation function always return the API structure **by value**. |
| 92 | - The creation function does not take any input. If the functionality of the micro-kernel is configurable, the caller needs to populate necessary information to the configuration structure and/or the run arguments structure. |