test/common/data_type.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2025 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 | |||
| 13 | namespace kai::test { | ||
| 14 | |||
| 15 | /// Data type. | ||
| 16 | enum class DataType : uint16_t { | ||
| 17 | // Encoding: | ||
| 18 | // | ||
| 19 | // 15 0 | ||
| 20 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | ||
| 21 | // | i | s | q | a | RES0 | bits | | ||
| 22 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | ||
| 23 | // | ||
| 24 | // (RES0: reserved, filled with 0s) | ||
| 25 | // | ||
| 26 | // Fields: | ||
| 27 | // | ||
| 28 | // * i: integer (1) or floating-point (0). | ||
| 29 | // * s: signed (1) or unsigned (0). | ||
| 30 | // * q: | ||
| 31 | // - Integer (i): quantized (1) or non-quantized (0). | ||
| 32 | // - Floating-point (!i): brain (1) or binary (0). | ||
| 33 | // * a: | ||
| 34 | // - Quantized (i && q): asymmetric (1) or symmetric (0). | ||
| 35 | // - Otherwise: RES0. | ||
| 36 | // * bits: size in bits. | ||
| 37 | |||
| 38 | UNKNOWN = 0, ///< No data. | ||
| 39 | |||
| 40 | FP32 = 0b0'1'0'0'0000'00100000, ///< Single-precision floating-point. | ||
| 41 | FP16 = 0b0'1'0'0'0000'00010000, ///< Half-precision floating-point. | ||
| 42 | |||
| 43 | BF16 = 0b0'1'1'0'0000'00010000, ///< Half-precision brain floating-point. | ||
| 44 | |||
| 45 | I32 = 0b1'1'0'0'0000'00100000, ///< 32-bit signed integer. | ||
| 46 | I8 = 0b1'1'0'0'0000'00001000, ///< 8-bit signed integer. | ||
| 47 | I4 = 0b1'1'0'0'0000'00000100, ///< 4-bit signed integer. | ||
| 48 | |||
| 49 | U32 = 0b1'0'0'0'0000'00100000, ///< 32-bit unsigned integer. | ||
| 50 | U8 = 0b1'0'0'0'0000'00001000, ///< 8-bit unsigned integer. | ||
| 51 | U4 = 0b1'0'0'0'0000'00000100, ///< 4-bit unsigned integer. | ||
| 52 | |||
| 53 | QAI8 = 0b1'1'1'1'0000'00001000, ///< 8-bit signed asymmetric quantized. | ||
| 54 | QSI8 = 0b1'1'1'0'0000'00001000, ///< 8-bit signed symmetric quantized. | ||
| 55 | |||
| 56 | QSU4 = 0b1'0'1'0'0000'00000100, ///< 4-bit unsigned symmetric quantized. | ||
| 57 | QSI4 = 0b1'1'1'0'0000'00000100, ///< 4-bit signed symmetric quantized. | ||
| 58 | QAI4 = 0b1'1'1'1'0000'00000100, ///< 4-bit signed asymmetric quantized. | ||
| 59 | }; | ||
| 60 | |||
| 61 | /// Gets the size in bits of the specified data type. | ||
| 62 | /// | ||
| 63 | /// @param[in] dt The data type. | ||
| 64 | /// | ||
| 65 | /// @return The size in bits. | ||
| 66 | [[nodiscard]] size_t data_type_size_in_bits(DataType dt); | ||
| 67 | |||
| 68 | /// Gets a value indicating whether the data type is integral. | ||
| 69 | /// | ||
| 70 | /// @param[in] dt The data type. | ||
| 71 | /// | ||
| 72 | /// @return `true` if the data type is integral. | ||
| 73 | [[nodiscard]] bool data_type_is_integral(DataType dt); | ||
| 74 | |||
| 75 | /// Gets a value indicating whether the data type is floating-point. | ||
| 76 | /// | ||
| 77 | /// @param[in] dt The data type. | ||
| 78 | /// | ||
| 79 | /// @return `true` if the data type is floating-point. | ||
| 80 | [[nodiscard]] bool data_type_is_float(DataType dt); | ||
| 81 | |||
| 82 | /// Gets a value indicating whether the data type is binary floating-point. | ||
| 83 | /// | ||
| 84 | /// Binary floating point are `half`, `float`, `double`. | ||
| 85 | /// | ||
| 86 | /// @param[in] dt The data type. | ||
| 87 | /// | ||
| 88 | /// @return `true` if the data type is binary floating-point. | ||
| 89 | [[nodiscard]] bool data_type_is_float_fp(DataType dt); | ||
| 90 | |||
| 91 | /// Gets a value indicating whether the data type is brain floating-point. | ||
| 92 | /// | ||
| 93 | /// Binary floating point are `bfloat16`. | ||
| 94 | /// | ||
| 95 | /// @param[in] dt The data type. | ||
| 96 | /// | ||
| 97 | /// @return `true` if the data type is brain floating-point. | ||
| 98 | [[nodiscard]] bool data_type_is_float_bf(DataType dt); | ||
| 99 | |||
| 100 | /// Gets a value indicating whether the data type is signed. | ||
| 101 | /// | ||
| 102 | /// @param[in] dt The data type. | ||
| 103 | /// | ||
| 104 | /// @return `true` if the data type is signed. | ||
| 105 | [[nodiscard]] bool data_type_is_signed(DataType dt); | ||
| 106 | |||
| 107 | /// Gets a value indicating whether the data type is quantized. | ||
| 108 | /// | ||
| 109 | /// @param[in] dt The data type. | ||
| 110 | /// | ||
| 111 | /// @return `true` if the data type is quantized. | ||
| 112 | [[nodiscard]] bool data_type_is_quantized(DataType dt); | ||
| 113 | |||
| 114 | /// Gets a value indicating whether the data type is asymmetric quantized. | ||
| 115 | /// | ||
| 116 | /// @param[in] dt The data type. | ||
| 117 | /// | ||
| 118 | /// @return `true` if the data type is asymmetric quantized. | ||
| 119 | [[nodiscard]] bool data_type_is_quantized_asymm(DataType dt); | ||
| 120 | |||
| 121 | /// Gets a value indicating whether the data type is quantized int8. | ||
| 122 | /// | ||
| 123 | /// @param[in] dt The data type. | ||
| 124 | /// | ||
| 125 | /// @return `true` if the data type is quantized int8. | ||
| 126 | [[nodiscard]] bool data_type_is_quantized_int8(DataType dt); | ||
| 127 | |||
| 128 | /// Gets a value indicating whether the data type is quantized int4. | ||
| 129 | /// | ||
| 130 | /// @param[in] dt The data type. | ||
| 131 | /// | ||
| 132 | /// @return `true` if the data type is quantized int4. | ||
| 133 | [[nodiscard]] bool data_type_is_quantized_int4(DataType dt); | ||
| 134 | |||
| 135 | } // namespace kai::test | ||
| 136 | |||
| 137 | template <> | ||
| 138 | struct std::hash<kai::test::DataType> { | ||
| 139 | 330303 | size_t operator()(const kai::test::DataType& dt) const { | |
| 140 | using DT = std::underlying_type_t<kai::test::DataType>; | ||
| 141 | 330303 | return std::hash<DT>{}(static_cast<DT>(dt)); | |
| 142 | } | ||
| 143 | }; | ||
| 144 |