| 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 | #pragma once |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <functional> |
| 12 | #include <string> |
| 13 | |
| 14 | namespace kai::test { |
| 15 | |
| 16 | /// Data type. |
| 17 | enum class DataType : uint16_t { |
| 18 | // Encoding: |
| 19 | // |
| 20 | // 15 0 |
| 21 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 22 | // | i | s | q | a | RES0 | bits | |
| 23 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 24 | // |
| 25 | // (RES0: reserved, filled with 0s) |
| 26 | // |
| 27 | // Fields: |
| 28 | // |
| 29 | // * i: integer (1) or floating-point (0). |
| 30 | // * s: signed (1) or unsigned (0). |
| 31 | // * q: |
| 32 | // - Integer (i): quantized (1) or non-quantized (0). |
| 33 | // - Floating-point (!i): brain (1) or binary (0). |
| 34 | // * a: |
| 35 | // - Quantized (i && q): asymmetric (1) or symmetric (0). |
| 36 | // - Otherwise: RES0. |
| 37 | // * bits: size in bits. |
| 38 | |
| 39 | UNKNOWN = 0, ///< No data. |
| 40 | |
| 41 | FP32 = 0b0'1'0'0'0000'00100000, ///< Single-precision floating-point. |
| 42 | FP16 = 0b0'1'0'0'0000'00010000, ///< Half-precision floating-point. |
| 43 | |
| 44 | BF16 = 0b0'1'1'0'0000'00010000, ///< Half-precision brain floating-point. |
| 45 | |
| 46 | I32 = 0b1'1'0'0'0000'00100000, ///< 32-bit signed integer. |
| 47 | I8 = 0b1'1'0'0'0000'00001000, ///< 8-bit signed integer. |
| 48 | I4 = 0b1'1'0'0'0000'00000100, ///< 4-bit signed integer. |
| 49 | |
| 50 | U32 = 0b1'0'0'0'0000'00100000, ///< 32-bit unsigned integer. |
| 51 | U8 = 0b1'0'0'0'0000'00001000, ///< 8-bit unsigned integer. |
| 52 | U4 = 0b1'0'0'0'0000'00000100, ///< 4-bit unsigned integer. |
| 53 | |
| 54 | QAI8 = 0b1'1'1'1'0000'00001000, ///< 8-bit signed asymmetric quantized. |
| 55 | QSI8 = 0b1'1'1'0'0000'00001000, ///< 8-bit signed symmetric quantized. |
| 56 | |
| 57 | QSU4 = 0b1'0'1'0'0000'00000100, ///< 4-bit unsigned symmetric quantized. |
| 58 | QSI4 = 0b1'1'1'0'0000'00000100, ///< 4-bit signed symmetric quantized. |
| 59 | QAI4 = 0b1'1'1'1'0000'00000100, ///< 4-bit signed asymmetric quantized. |
| 60 | |
| 61 | QSI2 = 0b1'1'1'0'0000'00000010, ///< 2-bit signed symmetric quantized. |
| 62 | }; |
| 63 | |
| 64 | /// Gets the data type, or the default data type if unknown. |
| 65 | /// |
| 66 | /// @param[in] dt The data type. |
| 67 | /// @param[in] default_dt The default data type. |
| 68 | /// |
| 69 | /// @return The data type if known, otherwise the default data type. |
| 70 | [[nodiscard]] constexpr DataType data_type_default(DataType dt, DataType default_dt) { |
| 71 | return dt != DataType::UNKNOWN ? dt : default_dt; |
| 72 | } |
| 73 | |
| 74 | /// Gets the name of the specified data type. |
| 75 | /// |
| 76 | /// @param[in] dt The data type. |
| 77 | /// |
| 78 | /// @return The data type name. |
| 79 | [[nodiscard]] const char* to_cstring(DataType dt); |
| 80 | |
| 81 | /// Gets a short unique identifier for the data type (e.g. f32, bf16, i8). |
| 82 | /// |
| 83 | /// @param[in] dt The data type. |
| 84 | /// |
| 85 | /// @return String identifier. |
| 86 | [[nodiscard]] std::string data_type_uid(DataType dt); |
| 87 | |
| 88 | /// Gets the size in bits of the specified data type. |
| 89 | /// |
| 90 | /// @param[in] dt The data type. |
| 91 | /// |
| 92 | /// @return The size in bits. |
| 93 | [[nodiscard]] size_t data_type_size_in_bits(DataType dt); |
| 94 | |
| 95 | /// Gets the size in bytes of an array of the specified data type |
| 96 | /// |
| 97 | /// @param[in] dt The data type. |
| 98 | /// @param[in] len The number of elements in the vector |
| 99 | /// |
| 100 | /// @return size in bytes |
| 101 | [[nodiscard]] size_t data_type_array_size_in_bytes(DataType dt, size_t len); |
| 102 | |
| 103 | /// Gets a value indicating whether the data type is integral. |
| 104 | /// |
| 105 | /// @param[in] dt The data type. |
| 106 | /// |
| 107 | /// @return `true` if the data type is integral. |
| 108 | [[nodiscard]] bool data_type_is_integral(DataType dt); |
| 109 | |
| 110 | /// Gets a value indicating whether the data type is floating-point. |
| 111 | /// |
| 112 | /// @param[in] dt The data type. |
| 113 | /// |
| 114 | /// @return `true` if the data type is floating-point. |
| 115 | [[nodiscard]] bool data_type_is_float(DataType dt); |
| 116 | |
| 117 | /// Gets a value indicating whether the data type is binary floating-point. |
| 118 | /// |
| 119 | /// Binary floating point are `half`, `float`, `double`. |
| 120 | /// |
| 121 | /// @param[in] dt The data type. |
| 122 | /// |
| 123 | /// @return `true` if the data type is binary floating-point. |
| 124 | [[nodiscard]] bool data_type_is_float_fp(DataType dt); |
| 125 | |
| 126 | /// Gets a value indicating whether the data type is brain floating-point. |
| 127 | /// |
| 128 | /// Binary floating point are `bfloat16`. |
| 129 | /// |
| 130 | /// @param[in] dt The data type. |
| 131 | /// |
| 132 | /// @return `true` if the data type is brain floating-point. |
| 133 | [[nodiscard]] bool data_type_is_float_bf(DataType dt); |
| 134 | |
| 135 | /// Gets a value indicating whether the data type is signed. |
| 136 | /// |
| 137 | /// @param[in] dt The data type. |
| 138 | /// |
| 139 | /// @return `true` if the data type is signed. |
| 140 | [[nodiscard]] bool data_type_is_signed(DataType dt); |
| 141 | |
| 142 | /// Gets a value indicating whether the data type is quantized. |
| 143 | /// |
| 144 | /// @param[in] dt The data type. |
| 145 | /// |
| 146 | /// @return `true` if the data type is quantized. |
| 147 | [[nodiscard]] bool data_type_is_quantized(DataType dt); |
| 148 | |
| 149 | /// Gets a value indicating whether the data type is asymmetric quantized. |
| 150 | /// |
| 151 | /// @param[in] dt The data type. |
| 152 | /// |
| 153 | /// @return `true` if the data type is asymmetric quantized. |
| 154 | [[nodiscard]] bool data_type_is_quantized_asymm(DataType dt); |
| 155 | |
| 156 | /// Gets a value indicating whether the data type is quantized int8. |
| 157 | /// |
| 158 | /// @param[in] dt The data type. |
| 159 | /// |
| 160 | /// @return `true` if the data type is quantized int8. |
| 161 | [[nodiscard]] bool data_type_is_quantized_int8(DataType dt); |
| 162 | |
| 163 | /// Gets a value indicating whether the data type is quantized int4. |
| 164 | /// |
| 165 | /// @param[in] dt The data type. |
| 166 | /// |
| 167 | /// @return `true` if the data type is quantized int4. |
| 168 | [[nodiscard]] bool data_type_is_quantized_int4(DataType dt); |
| 169 | |
| 170 | } // namespace kai::test |
| 171 | |
| 172 | template <> |
| 173 | struct std::hash<kai::test::DataType> { |
| 174 | size_t operator()(const kai::test::DataType& dt) const { |
| 175 | using DT = std::underlying_type_t<kai::test::DataType>; |
| 176 | return std::hash<DT>{}(static_cast<DT>(dt)); |
| 177 | } |
| 178 | }; |