| 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 | #pragma once |
| 7 | |
| 8 | #if defined(__ARM_NEON) |
| 9 | #include <arm_neon.h> |
| 10 | #endif // defined(__ARM_NEON) |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | // NOLINTBEGIN(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c) |
| 23 | // |
| 24 | // * cppcoreguidelines-avoid-do-while: do-while is necessary for macros. |
| 25 | // * cppcoreguidelines-pro-type-vararg: use of variadic arguments in fprintf is expected. |
| 26 | // * cert-err33-c: checking the output of fflush and fprintf is not necessary for error reporting. |
| 27 | |
| 28 | #ifndef KLEIDIAI_ERROR_TRAP |
| 29 | #define KLEIDIAI_ERROR_TRAP 0 |
| 30 | #endif |
| 31 | |
| 32 | #ifndef KLEIDIAI_HAS_BUILTIN_UNREACHABLE |
| 33 | #define KLEIDIAI_HAS_BUILTIN_UNREACHABLE 0 |
| 34 | #endif |
| 35 | |
| 36 | #ifndef KLEIDIAI_HAS_BUILTIN_ASSUME0 |
| 37 | #define KLEIDIAI_HAS_BUILTIN_ASSUME0 0 |
| 38 | #endif |
| 39 | |
| 40 | #if KLEIDIAI_ERROR_TRAP |
| 41 | #define KAI_ABORT() __builtin_trap() |
| 42 | #else |
| 43 | #define KAI_ABORT() abort() |
| 44 | #endif |
| 45 | |
| 46 | #if KLEIDIAI_HAS_BUILTIN_UNREACHABLE |
| 47 | #define KAI_UNREACHABLE() __builtin_unreachable() |
| 48 | #elif KLEIDIAI_HAS_BUILTIN_ASSUME0 |
| 49 | #define KAI_UNREACHABLE() __assume(0); |
| 50 | #else |
| 51 | #define KAI_UNREACHABLE() |
| 52 | #endif |
| 53 | |
| 54 | #ifdef NDEBUG |
| 55 | #define KAI_ERROR(msg) \ |
| 56 | do { \ |
| 57 | KAI_UNUSED(msg); \ |
| 58 | KAI_ABORT(); \ |
| 59 | } while (0) |
| 60 | |
| 61 | #define KAI_ASSERT_MSG(cond, msg) \ |
| 62 | do { \ |
| 63 | KAI_UNUSED(msg); \ |
| 64 | if (!(cond)) { \ |
| 65 | KAI_UNREACHABLE(); \ |
| 66 | } \ |
| 67 | } while (0) |
| 68 | #else |
| 69 | #define KAI_ERROR(msg) \ |
| 70 | do { \ |
| 71 | fflush(stdout); \ |
| 72 | fprintf(stderr, "%s:%d %s", __FILE__, __LINE__, msg); \ |
| 73 | KAI_ABORT(); \ |
| 74 | } while (0) |
| 75 | |
| 76 | #define KAI_ASSERT_MSG(cond, msg) KAI_ASSERT_ALWAYS_MSG(cond, msg) |
| 77 | #endif // NDEBUG |
| 78 | |
| 79 | #define KAI_ASSERT_ALWAYS_MSG(cond, msg) \ |
| 80 | do { \ |
| 81 | if (!(cond)) { \ |
| 82 | KAI_ERROR(msg); \ |
| 83 | } \ |
| 84 | } while (0) |
| 85 | |
| 86 | // NOLINTEND(cppcoreguidelines-avoid-do-while,cppcoreguidelines-pro-type-vararg,cert-err33-c) |
| 87 | |
| 88 | /// KAI_ASSERT* is used for logic sanity checking in the program |
| 89 | /// flow. Checks are optimized away in release builds same as |
| 90 | /// `assert` |
| 91 | #define KAI_ASSERT(cond) KAI_ASSERT_MSG(cond, #cond) |
| 92 | #define KAI_ASSERT_IF_MSG(precond, cond, msg) KAI_ASSERT_MSG(!(precond) || (cond), msg) |
| 93 | #define KAI_ASSERT_IF(precond, cond) KAI_ASSERT_IF_MSG(precond, cond, #precond " |-> " #cond) |
| 94 | |
| 95 | /// `KAI_ASSERT_ALWAYS*` is same as `KAI_ASSERT*`, but doesn't get removed by `NDEBUG` |
| 96 | #define KAI_ASSERT_ALWAYS(cond) KAI_ASSERT_ALWAYS_MSG(cond, #cond) |
| 97 | #define KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, msg) KAI_ASSERT_ALWAYS_MSG(!(precond) || (cond), msg) |
| 98 | #define KAI_ASSERT_ALWAYS_IF(precond, cond) KAI_ASSERT_ALWAYS_IF_MSG(precond, cond, #precond " |-> " #cond) |
| 99 | |
| 100 | /// KAI_ASSUME* is used for function pre-condition checking, similar to `[[assume]]` in C++23. |
| 101 | /// So KAI_ASSUME should be used directly on the function parameters, rather than inside |
| 102 | /// function logic. |
| 103 | #define KAI_ASSUME_MSG KAI_ASSERT_MSG |
| 104 | #define KAI_ASSUME KAI_ASSERT |
| 105 | #define KAI_ASSUME_IF_MSG KAI_ASSERT_IF_MSG |
| 106 | #define KAI_ASSUME_IF KAI_ASSERT_IF |
| 107 | |
| 108 | /// `KAI_ASSUME_ALWAYS*` is same as `KAI_ASSUME*`, but doesn't get removed by `NDEBUG` |
| 109 | #define KAI_ASSUME_ALWAYS_MSG KAI_ASSERT_ALWAYS_MSG |
| 110 | #define KAI_ASSUME_ALWAYS KAI_ASSERT_ALWAYS |
| 111 | #define KAI_ASSUME_ALWAYS_IF_MSG KAI_ASSERT_ALWAYS_IF_MSG |
| 112 | #define KAI_ASSUME_ALWAYS_IF KAI_ASSERT_ALWAYS_IF |
| 113 | |
| 114 | /// Indicate that result of `x` is unused |
| 115 | #define KAI_UNUSED(x) (void)(x) |
| 116 | |
| 117 | /// Return minimum or maximum of `a` and `b` |
| 118 | #define KAI_MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 119 | #define KAI_MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 120 | |
| 121 | /// KleidiAI shared constants |
| 122 | enum { |
| 123 | /// Largest supported SME vector length in bytes |
| 124 | KAI_SME_VEC_LENGTH_MAX_BYTES = 256, |
| 125 | |
| 126 | /// Size of one vscale unit, in bytes |
| 127 | KAI_VSCALE_UNIT_BYTES = 16, |
| 128 | |
| 129 | /// Maximum possible VSCALE |
| 130 | KAI_VSCALE_MAX = KAI_SME_VEC_LENGTH_MAX_BYTES / KAI_VSCALE_UNIT_BYTES, |
| 131 | }; |
| 132 | |
| 133 | /// Gets the version of the project in the Major.Minor.Patch semantic versioning format. |
| 134 | /// |
| 135 | /// @return Project version as a string literal. |
| 136 | inline const char* kai_get_version(void) { |
| 137 | return "1.28.0"; |
| 138 | } |
| 139 | |
| 140 | /// KleidiAI data types |
| 141 | /// Format: <byte 3>(reserved)|<byte 2>(num-bytes)|<byte 1>(type)|<byte 0>(variant-type) |
| 142 | enum kai_datatype { |
| 143 | kai_dt_unknown = 0x0000, |
| 144 | kai_dt_f32 = 0x0411, |
| 145 | kai_dt_f16 = 0x0212, |
| 146 | kai_dt_bf16 = 0x0213, |
| 147 | kai_dt_int32 = 0x0421, |
| 148 | kai_dt_int16 = 0x0222, |
| 149 | kai_dt_int8 = 0x0124, |
| 150 | kai_dt_uint32 = 0x0431, |
| 151 | kai_dt_uint16 = 0x0232, |
| 152 | kai_dt_uint8 = 0x0134, |
| 153 | kai_dt_bool = 0x0441 |
| 154 | }; |
| 155 | |
| 156 | /// Gets number of bytes for a given data type |
| 157 | /// @param[in] dt KleidiAI data type |
| 158 | /// |
| 159 | /// @return the numbers of bytes for the data type |
| 160 | inline static size_t kai_get_datatype_size_in_bytes(enum kai_datatype dt) { |
| 161 | return (size_t)(dt >> 8); |
| 162 | } |
| 163 | |
| 164 | /// Converts a scalar f16 value to f32 |
| 165 | /// @param[in] f16 The f16 value |
| 166 | /// |
| 167 | /// @return the f32 value |
| 168 | #if defined(__ARM_NEON) |
| 169 | inline static float kai_cast_f32_f16(uint16_t f16) { |
| 170 | float16_t f32 = 0; |
| 171 | memcpy(&f32, &f16, sizeof(uint16_t)); |
| 172 | return (float)f32; |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | /// Converts a scalar bf16 value to f32 |
| 177 | /// @param[in] bf16 The f16 value |
| 178 | /// |
| 179 | /// @return the f32 value |
| 180 | inline static float kai_cast_f32_bf16(uint16_t bf16) { |
| 181 | const uint32_t i32 = (bf16 << 16); |
| 182 | float f32 = 0; |
| 183 | memcpy(&f32, &i32, sizeof(i32)); |
| 184 | return f32; |
| 185 | } |
| 186 | |
| 187 | /// Converts a f32 value to bf16 |
| 188 | /// @param[in] f32 The f32 value |
| 189 | /// |
| 190 | /// @return the bf16 value |
| 191 | inline static uint16_t kai_cast_bf16_f32(float f32) { |
| 192 | uint16_t bf16 = 0; |
| 193 | #ifdef __ARM_FEATURE_BF16 |
| 194 | __asm__ __volatile__("bfcvt %h[output], %s[input]" : [output] "=w"(bf16) : [input] "w"(f32)); |
| 195 | #else |
| 196 | const uint32_t* i32 = (uint32_t*)(&f32); |
| 197 | bf16 = (*i32 >> 16); |
| 198 | #endif |
| 199 | return bf16; |
| 200 | } |
| 201 | |
| 202 | /// Converts a scalar f32 value to f16 |
| 203 | /// @param[in] f32 The f32 value |
| 204 | /// |
| 205 | /// @return the f16 value |
| 206 | #if defined(__ARM_NEON) |
| 207 | inline static uint16_t kai_cast_f16_f32(float f32) { |
| 208 | uint16_t f16 = 0; |
| 209 | float16_t tmp = (float16_t)f32; |
| 210 | memcpy(&f16, &tmp, sizeof(uint16_t)); |
| 211 | return f16; |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | /// Divide a value, `a`, with a value `b` and round up the result |
| 216 | inline static size_t kai_div_ceil(size_t a, size_t b) { |
| 217 | return (a + b - 1) / b; |
| 218 | } |
| 219 | |
| 220 | /// Round up a value, `a` to a multiple of `b` |
| 221 | inline static size_t kai_roundup(size_t a, size_t b) { |
| 222 | return kai_div_ceil(a, b) * b; |
| 223 | } |
| 224 | |
| 225 | #if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64) |
| 226 | /// Gets the SME vector length for 8-bit elements. |
| 227 | uint64_t kai_get_sme_vector_length_u8(void); |
| 228 | |
| 229 | /// Gets the SME vector length for 16-bit elements. |
| 230 | inline static uint64_t kai_get_sme_vector_length_u16(void) { |
| 231 | return kai_get_sme_vector_length_u8() / 2; |
| 232 | } |
| 233 | |
| 234 | /// Gets the SME vector length for 32-bit elements. |
| 235 | inline static uint64_t kai_get_sme_vector_length_u32(void) { |
| 236 | return kai_get_sme_vector_length_u8() / 4; |
| 237 | } |
| 238 | |
| 239 | /// Gets the vscale scale factor for SME |
| 240 | inline static uint64_t kai_get_sme_vscale(void) { |
| 241 | return kai_get_sme_vector_length_u8() / KAI_VSCALE_UNIT_BYTES; |
| 242 | } |
| 243 | |
| 244 | /// Commit ZA to lazy save buffer |
| 245 | void kai_commit_za(void); |
| 246 | #endif // defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64) |
| 247 | |
| 248 | /// Gets the SVE vector length for 8-bit elements. |
| 249 | uint64_t kai_get_sve_vector_length_u8(void); |
| 250 | |
| 251 | /// Gets the SVE vector length for 16-bit elements. |
| 252 | inline static uint64_t kai_get_sve_vector_length_u16(void) { |
| 253 | return kai_get_sve_vector_length_u8() / 2; |
| 254 | } |
| 255 | |
| 256 | /// Gets the SVE vector length for 32-bit elements. |
| 257 | inline static uint64_t kai_get_sve_vector_length_u32(void) { |
| 258 | return kai_get_sve_vector_length_u8() / 4; |
| 259 | } |
| 260 | |
| 261 | /// Gets the vscale scale factor for SVE |
| 262 | inline static uint64_t kai_get_sve_vscale(void) { |
| 263 | return kai_get_sve_vector_length_u8() / KAI_VSCALE_UNIT_BYTES; |
| 264 | } |
| 265 | |
| 266 | /// Extends the sign bit of int 4-bit value (stored in int8_t variable) |
| 267 | /// @param[in] value The 4-bit int value |
| 268 | /// |
| 269 | /// @return the int8_t value with sign extended |
| 270 | inline static int8_t kai_ext_sign_i8_i4(int8_t value) { |
| 271 | // Make sure value holds correct int4 value |
| 272 | KAI_ASSUME(value <= 0xF); |
| 273 | |
| 274 | return (value ^ 0x8) - 8; // NOLINT(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) |
| 275 | } |
| 276 | |
| 277 | /// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 8-bit with per-channel quantization) |
| 278 | struct kai_rhs_pack_qsi8cx_params { |
| 279 | int32_t lhs_zero_point; ///< LHS Matrix quantization zero-point |
| 280 | float scale_multiplier; ///< Product of input (refers to lhs and rhs) and output quantization scales. |
| 281 | }; |
| 282 | |
| 283 | /// Parameter struct for RHS matrix packing (Quantized Symmetric Integer 4-bit with per-block quantizatio and s1s0 |
| 284 | /// nibble ordering) |
| 285 | struct kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params { |
| 286 | int8_t lhs_zero_point; |
| 287 | uint8_t rhs_zero_point; |
| 288 | enum kai_datatype scale_dt; |
| 289 | }; |
| 290 | |
| 291 | /// Parameter struct for RHS matrix packing (KxN variant for int4 qsi4c32p_qsu4c32s1s0) |
| 292 | struct kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params { |
| 293 | int8_t lhs_zero_point; |
| 294 | uint8_t rhs_zero_point; |
| 295 | enum kai_datatype scale_dt; |
| 296 | }; |
| 297 | |
| 298 | /// Parameter struct for RHS matrix packing |
| 299 | struct kai_rhs_pack_qs4cxs1s0_param { |
| 300 | int8_t lhs_zero_point; ///< LHS Matrix quantization zero-point |
| 301 | uint8_t rhs_zero_point; ///< RHS Matrix quantization zero-point |
| 302 | }; |
| 303 | |
| 304 | /// Requantization and clamp parameters for GEMM/GEMV output stage. |
| 305 | struct kai_matmul_requantize32_params { |
| 306 | int32_t min_value; ///< Minimum output value. |
| 307 | int32_t max_value; ///< Maximum output value. |
| 308 | int32_t output_zero_point; ///< Output quantization zero point. |
| 309 | }; |
| 310 | |
| 311 | #ifdef __cplusplus |
| 312 | } |
| 313 | #endif |