| 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 | # Vector scale |
| 8 | |
| 9 | This document describes why the KleidiAI API uses `vs` (_vector scale_, `vscale`) |
| 10 | instead of `vl` (_vector length_) for new micro-kernel names and packing formats. |
| 11 | |
| 12 | ## What is vscale? |
| 13 | |
| 14 | `vs` or `vscale` is a datatype-independent scale factor based on a 128-bit vector length. |
| 15 | `1vs` (i.e. `1 * vscale`) refers to 1 element when the vector length is 128-bits. The number of elements scales with the runtime vector length. |
| 16 | |
| 17 | In KleidiAI, one vscale unit is 16 bytes as defined by `KAI_VSCALE_UNIT_BYTES` in |
| 18 | [`kai/kai_common.h`](../kai/kai_common.h). |
| 19 | |
| 20 | The 128-bit unit corresponds to the Advanced SIMD vector length baseline. For scalable-vector technologies such as SVE and SME, the `vs` factor scales with the runtime vector length. For example, with a 512-bit vector length, `1vs` equals 4, and `8vs` equals 32. |
| 21 | |
| 22 | ## Where is vscale applicable? |
| 23 | |
| 24 | `vscale` is applicable to scalable-vector micro-kernels where the runtime |
| 25 | vector length affects the tile shape, packing shape, or scheduling step. This |
| 26 | includes SVE, SME, and SME2 micro-kernels. |
| 27 | |
| 28 | `vscale` is not used for Advanced SIMD or NEON micro-kernels because their |
| 29 | vector length is fixed and does not vary at runtime. |
| 30 | |
| 31 | ## Why use vscale instead of vector length? |
| 32 | |
| 33 | `vl` refer to the number of elements of a given type that fit into one vector. |
| 34 | That means `vl` changes depending on the element type. For example, assuming a 512-bit |
| 35 | vector, `1vl` has the following element counts: |
| 36 | |
| 37 | | Element Datatype | Elements in `1vl` | |
| 38 | |------------------|-------------------| |
| 39 | | 8-bit | 64 | |
| 40 | | 16-bit | 32 | |
| 41 | | 32-bit | 16 | |
| 42 | |
| 43 | This makes `vl` useful when a format is explicitly tied to a specific element |
| 44 | type, but it can make API names harder to compare across data types. It can |
| 45 | also hide extra scaling factors that are part of the micro-kernel layout. |
| 46 | |
| 47 | ### Example of `vl` Confusion |
| 48 | |
| 49 | For KleidiAI micro-kernels, packing formats are specified in the micro-kernel name using `<type>p<MR>x<KR>`. |
| 50 | |
| 51 | For example, the format `f16p2vlx2` from |
| 52 | [`kai_matmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa.c`](../kai/ukernels/matmul/matmul_clamp_f16_f16p_f16p/kai_matmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa.c#L34) |
| 53 | means that `f16` (16-bit) elements are packed in `2vl` by 2 blocks. At first glance, this can look like a 64x2 block on a 512-bit configuration, because `2vl` of 16-bit elements is 64. |
| 54 | |
| 55 | However, the implementation derives the `M` and `N` block sizes as `kai_mr * kai_get_sme_vector_length_u16() / kai_kr`. In this case, that is `2 * kai_get_sme_vector_length_u16() / 2` giving 32 elements. The format `f16p2vlx2` thus refers to packing in 32x2 blocks. |
| 56 | The calculation in the code is as follows: |
| 57 | |
| 58 | ```c |
| 59 | enum { |
| 60 | MAX_MR = MR * (KAI_SME_VEC_LENGTH_MAX_BYTES / sizeof(int8_t)) / KR, |
| 61 | } |
| 62 | |
| 63 | size_t kai_get_mr_matmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa(void) { |
| 64 | // Code inlined for brevity |
| 65 | return kai_mr * kai_get_sme_vector_length_u16() / kai_kr; |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | The `vl` spelling therefore does not fully describe the relationship between the accumulation |
| 70 | type and the input type that is encoded by the scale factor `KR`. `KR` is dictated by the |
| 71 | number of accumulation per instruction rather than by the input and accumulation types. |
| 72 | |
| 73 | ## What are the benefits of vscale? |
| 74 | |
| 75 | The benefit of using `vs` is simplicity, as `vs` indicates the vector length in a __datatype-independent__ fashion. |
| 76 | |
| 77 | `vs` uses the smallest possible integer such that any concrete vector length will always be a multiple of `vs`. In SME with 512-bit vector configuration, `1vs` always indicates 4 elements, |
| 78 | `4vs` always 16. There is no extra considerations. |
| 79 | |
| 80 | Using the example `f16p2vlx2` and renaming it with `vs` would give `kai_matmul_clamp_f16_f16p8vsx2_f16p8vsx2b_..._sme_mopa`, thus mapping directly to `8 * vscale = 32` elements. |
| 81 | |
| 82 | This also simplifies the logic inside the kernel itself, as the `mr` function gets simplified. |
| 83 | |
| 84 | ```c |
| 85 | enum { |
| 86 | MAX_MR = MR_VSCALE * KAI_VSCALE_MAX, |
| 87 | } |
| 88 | |
| 89 | size_t kai_get_mr_matmul_clamp_f16_f16p8vsx2_f16p8vsx2b_8vsx8vcs_sme_mopa(void) { |
| 90 | // Code inlined for brevity |
| 91 | return MR_VSCALE * kai_get_sme_vscale(); |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | Existing micro-kernels that use `vl` in their names are not modified. New kernel should be based on `vs` rather than `vl` and use the `vs` logic as it is objectively simpler. |
| 96 | |
| 97 | ## Examples of micro-kernels using vscale |
| 98 | |
| 99 | The following micro-kernels show the preferred `vs` naming style and how the |
| 100 | implementation derives runtime dimensions from `kai_get_sme_vscale()`: |
| 101 | |
| 102 | - [`kai_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa.c`](../kai/ukernels/matmul/matmul_clamp_f32_u8p_u8p/kai_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa.c#L27) |
| 103 | uses `MR_VSCALE`, `NR_VSCALE`, `M_STEP_VSCALE`, and `N_STEP_VSCALE` for an |
| 104 | `8vsx8vs` tile. |
| 105 | |
| 106 | - [`kai_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa.c`](../kai/ukernels/matmul/matmul_i32_u8p_u8p/kai_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa.c#L25) |
| 107 | uses the same `8vsx8vs` style for an integer-accumulation micro-kernel. |
| 108 | |
| 109 | - [`kai_matmul_clamp_qai8_qai8_qsi8cxp4vsx4bi32sf32_1x32vs_sme2_dot.c`](../kai/ukernels/matmul/matmul_clamp_qai8_qai8_qsi8cxp/kai_matmul_clamp_qai8_qai8_qsi8cxp4vsx4bi32sf32_1x32vs_sme2_dot.c#L26) |
| 110 | keeps the M dimension fixed at 1 and derives the N dimension from |
| 111 | `NR_VSCALE * kai_get_sme_vscale()`. |