| 1 | <!-- |
| 2 | SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates |
| 3 | <open-source-office@arm.com> |
| 4 | |
| 5 | SPDX-License-Identifier: Apache-2.0 |
| 6 | --> |
| 7 | |
| 8 | # Testing a new micro-kernel in the test suite |
| 9 | |
| 10 | This guide explains how to add a new micro-kernel to the test suite. The process |
| 11 | is general, but the concrete example assumes an F16 matmul micro-kernel with F16 |
| 12 | input and F16 output, that has a RHS packing kernel, no LHS packing, and per $N$ |
| 13 | bias. Input matrices are assumed to be row major format. Do note that this is |
| 14 | intended to be a guiding example, and there might be aspects of the examples |
| 15 | here that are not yet implemented. |
| 16 | |
| 17 | The test suite is built around a small set of components and entry points: |
| 18 | |
| 19 | - Operator registration and test enumeration in |
| 20 | `test/nextgen/operators/matmul/matmul_operator.cpp` and |
| 21 | `test/nextgen/tests/matmul_test_next.cpp`. |
| 22 | - Wrapper classes (specializations using the `KernelWrapper` interface) in |
| 23 | subdirectories of `test/nextgen/operators/matmul`. The files here adapt the |
| 24 | micro-kernel C API into the common test harness API. Micro-kernel factory |
| 25 | declarations are listed in wrapper interface headers, e.g., |
| 26 | `test/nextgen/operators/matmul/matmul/matmul_wrapper.hpp`. |
| 27 | - A test bench in `test/nextgen/operators/matmul/matmul_tb.cpp` that generates |
| 28 | inputs, runs the implementation, and compares to a reference. |
| 29 | |
| 30 | This is the second version of the testing framework, which is why there are a |
| 31 | lot of references to _NextGen_. This naming might change in the future, once the |
| 32 | transition to _NextGen_ is completely done. |
| 33 | |
| 34 | ## Glossary |
| 35 | |
| 36 | - *LHS* - Left-hand side. The $A$ matrix in $C = A * B + β$. |
| 37 | - *MatMul* - Matrix multiplication. |
| 38 | - *operator* - a set of micro-kernels that together are used to accelerate an |
| 39 | operation. For example, RHS packing and matmul micro-kernels. |
| 40 | - *Packing* - reordering of input matrices to enable efficient memory access |
| 41 | patterns. |
| 42 | - *portion* - Refers to test cases where only partial output is calculated, |
| 43 | as to test tiling functionality. |
| 44 | - *RHS* - Right-hand side. The $B$ matrix in $C = A * B + β$. |
| 45 | - *tensor slot* - a named entry in `MatMulSlot` used by `MatMulTb` tensor |
| 46 | storage to hold configuration/arguments and tensor data (source, derived, packed, and |
| 47 | destination). Wrappers declare required slots via `run_inputs()` and |
| 48 | `ref_inputs()`. |
| 49 | - *Wrapper* - a wrapper around the micro-kernel as to expose a standardized API |
| 50 | to the test framework. |
| 51 | |
| 52 | ## The testing framework API |
| 53 | |
| 54 | Two utility types appear throughout the test harness API: `Span<T>` and |
| 55 | `Poly<Base>`. Both are local backports of standard-library functionality from |
| 56 | C++20 and later, and are used to keep wrapper and format interfaces compact. |
| 57 | |
| 58 | `Span<T>` is a non-owning view over contiguous memory, similar to `std::span`. |
| 59 | The harness uses it for shapes, tile coordinates, and tensor data buffers. |
| 60 | |
| 61 | `Poly<Base>` is an owning, copyable wrapper for an object derived from `Base`, |
| 62 | similar to later standard-library polymorphic value types. In the harness it is |
| 63 | mainly used as `Poly<Format>` so tensors and wrappers can store concrete format |
| 64 | objects by value while using the common `Format` interface. |
| 65 | |
| 66 | ## Test flow overview |
| 67 | |
| 68 | `MatMulNext` is the matmul test suite registered in |
| 69 | `test/nextgen/tests/matmul_test_next.cpp`. |
| 70 | Its flow is: |
| 71 | |
| 72 | 1. Enumerate available operators from `get_available_matmul_operators()`. |
| 73 | 1. Skip operators that current CPU doesn't support. This is checked by calling |
| 74 | `is_cpu_supported()` on operator. |
| 75 | 1. For each operator, sample random shapes and keep only shapes that satisfy |
| 76 | `is_shape_suitable(shape_m, shape_n, shape_k, portion)` on operator. |
| 77 | 1. Register tests for LHS pack, RHS pack, and matmul. |
| 78 | 1. Build a cached `MatMulTb` per fixture, generate test data once, then run |
| 79 | tile-level tests against precomputed references. |
| 80 | |
| 81 | Inside `MatMulTb::generate_test_data()`, the sequence is: |
| 82 | |
| 83 | 1. Populate `CONFIG`, then collect the required tensor slots from each |
| 84 | wrapper's `run_inputs()` and `ref_inputs()`. |
| 85 | 1. Let wrappers populate constant slot metadata and argument payloads such as |
| 86 | `PACK_ARGS`, `MATMUL_ARGS`, and packed tensor formats. |
| 87 | 1. Generate the source tensors (`LHS_DATA`, `RHS_DATA`, `BIAS_DATA`), then |
| 88 | derive any always-needed source variants such as `RHS_T_DATA`. |
| 89 | 1. Apply optional quantization (`lhs_quant`, `rhs_quant`, `bias_quant`). |
| 90 | 1. Compute extra derived tensors required by wrappers (for example |
| 91 | `LHS_QZP_NEG`, `RHS_T_QDATA_SIGN`, `RHS_T_QDATA_SIGN_SUM`). |
| 92 | 1. Compute required packed reference LHS and RHS input, and reference |
| 93 | destination data. |
| 94 | |
| 95 | Conceptually, `MatMulTb` owns a tensor pool indexed by `MatMulSlot`. Each slot |
| 96 | names one piece of state in the test flow: source tensors, derived tensors, |
| 97 | packed tensors, or non-array argument blobs such as `PACK_ARGS` and |
| 98 | `MATMUL_ARGS`. |
| 99 | |
| 100 | Wrappers declare which slots they need by returning slot IDs from `run_inputs()` |
| 101 | for the implementation path and `ref_inputs()` for the reference path. |
| 102 | `determine_required_tensors()` takes the union of those declarations and uses it |
| 103 | to decide which data must exist before any wrapper runs. |
| 104 | |
| 105 | This is how a floating-point matmul wrapper can simply ask for `LHS_PACKED`, |
| 106 | `RHS_PACKED`, and `MATMUL_ARGS`, while an RHS pack wrapper can ask for |
| 107 | `RHS_DATA` or `RHS_T_DATA` depending on layout and still use `RHS_T_DATA` for |
| 108 | reference packing. Optional derived slots such as `LHS_QZP_NEG` or |
| 109 | `RHS_T_QDATA_SIGN_SUM` are only computed when some wrapper has declared that it |
| 110 | needs them. |
| 111 | |
| 112 | ## Adding a new micro-kernel to the test suite |
| 113 | |
| 114 | This section outlines the steps needed to be able to add a new micro-kernel to |
| 115 | the test suite. |
| 116 | |
| 117 | High-level steps: |
| 118 | |
| 119 | - Add wrapper factory functions for any required pack micro-kernels and the |
| 120 | matmul micro-kernel. |
| 121 | - Register a `MatMulOperator` entry pointing at those wrappers and setting |
| 122 | data types, bias modes, quantizers, and shape suitability. |
| 123 | |
| 124 | ### Step 1: Add or extend matmul wrapper factories |
| 125 | |
| 126 | Wrappers adapt the micro-kernel C API to the harness API (`KernelWrapper`). For |
| 127 | matmul, the relevant interfaces are: |
| 128 | |
| 129 | - `test/nextgen/operators/matmul/matmul/matmul_interface.hpp` |
| 130 | - `test/nextgen/operators/matmul/pack_lhs/matmul_pack_lhs_interface.hpp` |
| 131 | - `test/nextgen/operators/matmul/pack_rhs/matmul_pack_rhs_interface.hpp` |
| 132 | |
| 133 | For a simple F16 example, with no LHS packing: |
| 134 | |
| 135 | - Use `MatMulFpWrapper` for the matmul micro-kernel. |
| 136 | - Use `MatMulPackRhsFpNtWrapper` for RHS packing. |
| 137 | |
| 138 | As an example, the matmul factory can be implemented as follows: |
| 139 | |
| 140 | ```cpp |
| 141 | // test/nextgen/operators/matmul/matmul/matmul_wrapper.cpp |
| 142 | std::unique_ptr<KernelWrapper<MatMulShape>> |
| 143 | create_matmul_clamp_f16_f16_f16p_neon() { |
| 144 | return std::make_unique<MatMulFpWrapper>( |
| 145 | "matmul_clamp_f16_f16_f16p_neon", |
| 146 | MatMulFpInterface{ |
| 147 | kai_get_m_step_matmul_clamp_f16_f16_f16p_neon, |
| 148 | ... |
| 149 | kai_get_dst_size_matmul_clamp_f16_f16_f16p_neon, |
| 150 | kai_run_matmul_clamp_f16_f16_f16p_neon, |
| 151 | }, |
| 152 | make_poly<PlainFormat>(DataType::FP16), // LHS input format |
| 153 | make_poly<Block2dRowFormat>( |
| 154 | 16, 1, 1, false, DataType::FP16, |
| 155 | std::array{DataType::FP16}, |
| 156 | std::array<DataType, 0>{}), // Packed RHS input format |
| 157 | make_poly<PlainFormat>(DataType::FP16)); // Output format |
| 158 | } |
| 159 | ``` |
| 160 | |
| 161 | For this example, the matmul wrapper uses unpacked FP16 LHS, packed FP16 RHS |
| 162 | with block size of $16 ⨯ 1$, and a plain FP16 destination format. Use existing |
| 163 | factories in `matmul_wrapper.cpp` and packer wrappers as a model. |
| 164 | |
| 165 | Make sure there is a `create_matmul_...` factory declaration added to |
| 166 | `test/nextgen/operators/matmul/matmul/matmul_wrapper.hpp` for the new micro-kernel. |
| 167 | |
| 168 | ### Step 2: Add packing micro-kernel wrapper factories |
| 169 | |
| 170 | For the F16 example, only RHS packing is needed. Add or reuse the RHS |
| 171 | packer wrapper in: |
| 172 | |
| 173 | - `test/nextgen/operators/matmul/pack_rhs/matmul_pack_rhs_wrapper.cpp` |
| 174 | |
| 175 | If another micro-kernel also packs the LHS, add or reuse a packer wrapper in |
| 176 | `test/nextgen/operators/matmul/pack_lhs/matmul_pack_lhs_wrapper.cpp`. |
| 177 | |
| 178 | Example skeleton for RHS packing: |
| 179 | |
| 180 | ```cpp |
| 181 | // test/nextgen/operators/matmul/pack_rhs/matmul_pack_rhs_wrapper.cpp |
| 182 | std::unique_ptr<KernelWrapper<MatShape>> |
| 183 | create_matmul_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon() { |
| 184 | return std::make_unique<MatMulPackRhsFpNtWrapper>( |
| 185 | "matmul_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon", |
| 186 | MatMulPackRhsFpInterface{ |
| 187 | kai_get_n_step_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 188 | kai_get_rhs_offset_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 189 | kai_get_bias_offset_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 190 | kai_get_rhs_packed_stride_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 191 | kai_get_rhs_packed_offset_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 192 | kai_get_rhs_packed_size_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 193 | kai_run_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon, |
| 194 | }, |
| 195 | make_poly<PlainFormat>(DataType::FP16), // RHS input format |
| 196 | make_poly<PlainFormat>(DataType::FP16), // Bias input format |
| 197 | make_poly<Block2dRowFormat>( |
| 198 | 16, 1, 1, false, DataType::FP16, |
| 199 | std::array{DataType::FP16}, |
| 200 | std::array<DataType, 0>{})); // Packed RHS output format |
| 201 | } |
| 202 | ``` |
| 203 | |
| 204 | In this example the wrapper is for a non-transposed RHS packing micro-kernel, |
| 205 | which is why the RHS input matrix is consumed from `RHS_DATA` in plain row |
| 206 | major $K × N$ layout, while the bias row vector is consumed from `BIAS_DATA` in |
| 207 | plain row major $N$ layout. The packed output is a bit more complex, as it uses |
| 208 | `Block2dRowFormat` to describe a $16 × 1$ packing format with the per-$N$ bias |
| 209 | stored alongside the packed RHS data. |
| 210 | |
| 211 | If the packing micro-kernel instead expects a transposed RHS, the source format |
| 212 | is still `PlainFormat`, but the logical shape changes to plain row major $N × K$ |
| 213 | and the wrapper should consume `RHS_T_DATA` rather than `RHS_DATA`. The test |
| 214 | bench already derives `RHS_T_DATA` by transposing the generated $K × N$ RHS, so |
| 215 | the operator registration does not need any extra transpose step. In practice, |
| 216 | `...kxn...` packers use the non-transposed path shown above, while `...nxk...` |
| 217 | packers should follow the transposed wrapper pattern and read from the |
| 218 | transposed RHS slot. The bias handling, data types, and packed output format |
| 219 | description stay the same; only the selected pack wrapper/factory and matching |
| 220 | shape-suitability helper change. |
| 221 | |
| 222 | ### Step 3: Register the operator |
| 223 | |
| 224 | Register the micro-kernel in `test/nextgen/operators/matmul/matmul_operator.cpp`. Each |
| 225 | operator declares: |
| 226 | |
| 227 | - Micro-kernel availability (`is_cpu_supported`). |
| 228 | - Shape suitability for matrix portions (`is_shape_suitable(...)`). |
| 229 | - Supported bias modes. |
| 230 | - Input/output/accumulator data types. |
| 231 | - Optional quantizers (for quantized micro-kernels). |
| 232 | - Wrapper factories for any required packers and matmul. |
| 233 | |
| 234 | Example skeleton for operator |
| 235 | |
| 236 | ```cpp |
| 237 | // test/nextgen/operators/matmul/matmul_operator.cpp |
| 238 | operators[i].name = "matmul_clamp_f16_f16_f16p_neon"; |
| 239 | |
| 240 | operators[i].is_cpu_supported = []() { return cpu_has_fp16(); }; |
| 241 | operators[i].is_shape_suitable = |
| 242 | [](size_t shape_m, size_t shape_n, size_t shape_k, const MatrixPortion& portion) { |
| 243 | return is_shape_suitable_rhs_f16_example(shape_m, shape_n, shape_k, portion); |
| 244 | }; |
| 245 | |
| 246 | operators[i].supported_bias_modes = { MatMulBiasMode::PER_N, }; |
| 247 | |
| 248 | operators[i].lhs_dtype = DataType::FP16; |
| 249 | operators[i].rhs_dtype = DataType::FP16; |
| 250 | operators[i].bias_dtype = DataType::FP16; |
| 251 | operators[i].acc_dtype = DataType::FP16; |
| 252 | operators[i].dst_dtype = DataType::FP16; |
| 253 | |
| 254 | operators[i].pack_rhs = create_matmul_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon(); |
| 255 | operators[i].matmul = create_matmul_clamp_f16_f16_f16p_neon(); |
| 256 | ``` |
| 257 | |
| 258 | ## Build and run tests |
| 259 | |
| 260 | Tests are compiled via the main test targets. Use a filter to focus on matmul |
| 261 | tests. |
| 262 | |
| 263 | CMake: |
| 264 | |
| 265 | ```bash |
| 266 | cmake -S . -B build |
| 267 | cmake --build build |
| 268 | build/kleidiai_test --gtest_filter='MatMulNext*' |
| 269 | ``` |
| 270 | |
| 271 | Bazel: |
| 272 | |
| 273 | ```bash |
| 274 | bazelisk test //test:kleidiai_test --test_filter='MatMulNext*' |
| 275 | ``` |
| 276 | |
| 277 | `MatMulNext` is deterministic by default because the test harness uses a fixed |
| 278 | seed unless one is explicitly provided. When adding a new micro-kernel, do not |
| 279 | stop at a single default-seed run; it is good practice to rerun the filtered |
| 280 | tests with several explicit seeds to cover more generated shapes and values. |
| 281 | |
| 282 | Examples: |
| 283 | |
| 284 | ```bash |
| 285 | build/kleidiai_test --gtest_filter='MatMulNext*' --gtest_random_seed=<seed> |
| 286 | bazelisk test //test:kleidiai_test --test_filter='MatMulNext*' --test_arg=--gtest_random_seed=<seed> |
| 287 | ``` |