| 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 | #include "test/reference/fill.hpp" |
| 8 | |
| 9 | #include <cmath> |
| 10 | #include <cstddef> |
| 11 | #include <functional> |
| 12 | #include <random> |
| 13 | #include <type_traits> |
| 14 | |
| 15 | #include "kai/kai_common.h" |
| 16 | #include "test/common/bfloat16.hpp" |
| 17 | #include "test/common/buffer.hpp" |
| 18 | #include "test/common/data_format.hpp" |
| 19 | #include "test/common/data_type.hpp" |
| 20 | #include "test/common/float16.hpp" |
| 21 | #include "test/common/int2.hpp" |
| 22 | #include "test/common/int4.hpp" |
| 23 | #include "test/common/numeric_limits.hpp" |
| 24 | |
| 25 | namespace kai::test { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | template <typename T> |
| 30 | bool valid_number(float number) { |
| 31 | if (is_integral<T> && (std::trunc(number) != number)) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | using type_defn = std::conditional_t<is_integral<T>, int32_t, float>; |
| 36 | |
| 37 | const type_defn low = static_cast<type_defn>(numeric_lowest<T>); |
| 38 | const type_defn high = static_cast<type_defn>(numeric_highest<T>); |
| 39 | |
| 40 | return number >= low && number <= high; |
| 41 | } |
| 42 | |
| 43 | template <typename T> |
| 44 | bool valid_range(float range_min, float range_max) { |
| 45 | const bool range_error = (range_min == 0.0F && range_max == 0.0F) || range_min > range_max; |
| 46 | const bool valid = !range_error && valid_number<T>(range_min) && valid_number<T>(range_max); |
| 47 | |
| 48 | return valid; |
| 49 | } |
| 50 | |
| 51 | template <typename T> |
| 52 | Buffer fill_matrix_random_raw(size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 53 | if (!valid_range<T>(range_min, range_max)) { |
| 54 | range_min = -2.0F; |
| 55 | range_max = 1.0F; |
| 56 | } |
| 57 | |
| 58 | using TDist = std::conditional_t< |
| 59 | std::is_floating_point_v<T>, std::uniform_real_distribution<float>, std::uniform_int_distribution<T>>; |
| 60 | |
| 61 | std::mt19937 rnd(seed); |
| 62 | TDist dist; |
| 63 | |
| 64 | if (std::is_floating_point_v<T>) { |
| 65 | dist.param(std::uniform_real_distribution<float>::param_type(range_min, range_max)); |
| 66 | } |
| 67 | |
| 68 | return fill_matrix_raw<T>(height, width, [&](size_t, size_t) { return dist(rnd); }); |
| 69 | } |
| 70 | |
| 71 | template <> |
| 72 | Buffer fill_matrix_random_raw<Float16>(size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 73 | if (!valid_range<Float16>(range_min, range_max)) { |
| 74 | range_min = 0.0F; |
| 75 | range_max = 1.0F; |
| 76 | } |
| 77 | |
| 78 | std::mt19937 rnd(seed); |
| 79 | std::uniform_real_distribution<float> dist(range_min, range_max); |
| 80 | |
| 81 | return fill_matrix_raw<Float16>(height, width, [&](size_t, size_t) { return static_cast<Float16>(dist(rnd)); }); |
| 82 | } |
| 83 | |
| 84 | template <> |
| 85 | Buffer fill_matrix_random_raw<BFloat16<>>( |
| 86 | size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 87 | if (!valid_range<BFloat16<>>(range_min, range_max)) { |
| 88 | range_min = 0.0F; |
| 89 | range_max = 1.0F; |
| 90 | } |
| 91 | |
| 92 | std::mt19937 rnd(seed); |
| 93 | std::uniform_real_distribution<float> dist(range_min, range_max); |
| 94 | |
| 95 | return fill_matrix_raw<BFloat16<>>( |
| 96 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<>>(dist(rnd)); }); |
| 97 | } |
| 98 | |
| 99 | template <> |
| 100 | Buffer fill_matrix_random_raw<BFloat16<false>>( |
| 101 | size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 102 | if (!valid_range<BFloat16<>>(range_min, range_max)) { |
| 103 | range_min = 0.0F; |
| 104 | range_max = 1.0F; |
| 105 | } |
| 106 | |
| 107 | std::mt19937 rnd(seed); |
| 108 | std::uniform_real_distribution<float> dist(range_min, range_max); |
| 109 | |
| 110 | return fill_matrix_raw<BFloat16<false>>( |
| 111 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<false>>(dist(rnd)); }); |
| 112 | } |
| 113 | |
| 114 | template <> |
| 115 | Buffer fill_matrix_random_raw<Int4>(size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 116 | if (!valid_range<Int4>(range_min, range_max)) { |
| 117 | range_min = -8; |
| 118 | range_max = 7; |
| 119 | } |
| 120 | |
| 121 | std::mt19937 rnd(seed); |
| 122 | std::uniform_int_distribution<int16_t> dist(range_min, range_max); |
| 123 | |
| 124 | return fill_matrix_raw<Int4>(height, width, [&](size_t, size_t) { return Int4(static_cast<int8_t>(dist(rnd))); }); |
| 125 | } |
| 126 | |
| 127 | template <> |
| 128 | Buffer fill_matrix_random_raw<UInt4>(size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 129 | if (!valid_range<UInt4>(range_min, range_max)) { |
| 130 | range_min = 0; |
| 131 | range_max = 15; |
| 132 | } |
| 133 | |
| 134 | std::mt19937 rnd(seed); |
| 135 | std::uniform_int_distribution<int16_t> dist(range_min, range_max); |
| 136 | |
| 137 | return fill_matrix_raw<UInt4>(height, width, [&](size_t, size_t) { return UInt4(static_cast<int8_t>(dist(rnd))); }); |
| 138 | } |
| 139 | |
| 140 | template <> |
| 141 | Buffer fill_matrix_random_raw<Int2>(size_t height, size_t width, uint32_t seed, float range_min, float range_max) { |
| 142 | if (!valid_range<Int2>(range_min, range_max)) { |
| 143 | range_min = -2; |
| 144 | range_max = 1; |
| 145 | } |
| 146 | |
| 147 | std::mt19937 rnd(seed); |
| 148 | std::uniform_int_distribution<int16_t> dist(range_min, range_max); |
| 149 | |
| 150 | return fill_matrix_raw<Int2>(height, width, [&](size_t, size_t) { return Int2(static_cast<int8_t>(dist(rnd))); }); |
| 151 | } |
| 152 | |
| 153 | } // namespace |
| 154 | |
| 155 | Buffer fill_matrix_random( |
| 156 | size_t height, size_t width, const DataFormat& format, uint32_t seed, float range_min, float range_max) { |
| 157 | switch (format.pack_format()) { |
| 158 | case DataFormat::PackFormat::NONE: |
| 159 | switch (format.data_type()) { |
| 160 | case DataType::FP32: |
| 161 | return fill_matrix_random_raw<float>(height, width, seed, range_min, range_max); |
| 162 | |
| 163 | case DataType::FP16: |
| 164 | return fill_matrix_random_raw<Float16>(height, width, seed, range_min, range_max); |
| 165 | |
| 166 | case DataType::BF16: |
| 167 | return fill_matrix_random_raw<BFloat16<>>(height, width, seed, range_min, range_max); |
| 168 | |
| 169 | case DataType::QSU4: |
| 170 | return fill_matrix_random_raw<UInt4>(height, width, seed, range_min, range_max); |
| 171 | |
| 172 | case DataType::QAI4: |
| 173 | case DataType::QSI4: |
| 174 | return fill_matrix_random_raw<Int4>(height, width, seed, range_min, range_max); |
| 175 | |
| 176 | case DataType::QSI2: |
| 177 | return fill_matrix_random_raw<Int2>(height, width, seed, range_min, range_max); |
| 178 | |
| 179 | default: |
| 180 | KAI_ERROR("Unsupported data type!"); |
| 181 | } |
| 182 | |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | KAI_ERROR("Unsupported data format!"); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | template <typename Value> |
| 191 | Buffer fill_random(size_t length, uint32_t seed, float range_min, float range_max) { |
| 192 | return fill_matrix_random_raw<Value>(1, length, seed, range_min, range_max); |
| 193 | } |
| 194 | |
| 195 | template Buffer fill_random<float>(size_t length, uint32_t seed, float range_min, float range_max); |
| 196 | template Buffer fill_random<Float16>(size_t length, uint32_t seed, float range_min, float range_max); |
| 197 | template Buffer fill_matrix_raw<float>(size_t height, size_t width, std::function<float(size_t, size_t)> gen); |
| 198 | template Buffer fill_matrix_raw<Float16>(size_t height, size_t width, std::function<Float16(size_t, size_t)> gen); |
| 199 | template Buffer fill_random<BFloat16<false>>(size_t length, uint32_t seed, float range_min, float range_max); |
| 200 | |
| 201 | } // namespace kai::test |