| 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 <cstddef> |
| 10 | #include <functional> |
| 11 | #include <random> |
| 12 | #include <type_traits> |
| 13 | |
| 14 | #include "kai/kai_common.h" |
| 15 | #include "test/common/bfloat16.hpp" |
| 16 | #include "test/common/buffer.hpp" |
| 17 | #include "test/common/data_format.hpp" |
| 18 | #include "test/common/data_type.hpp" |
| 19 | #include "test/common/float16.hpp" |
| 20 | #include "test/common/int2.hpp" |
| 21 | #include "test/common/int4.hpp" |
| 22 | |
| 23 | namespace kai::test { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | template <typename T> |
| 28 | Buffer fill_matrix_random_raw(size_t height, size_t width, uint32_t seed) { |
| 29 | using TDist = std::conditional_t< |
| 30 | std::is_floating_point_v<T>, std::uniform_real_distribution<float>, std::uniform_int_distribution<T>>; |
| 31 | |
| 32 | std::mt19937 rnd(seed); |
| 33 | TDist dist; |
| 34 | |
| 35 | if (std::is_floating_point_v<T>) { |
| 36 | dist.param(std::uniform_real_distribution<float>::param_type(-2.0f, 1.0f)); |
| 37 | } |
| 38 | |
| 39 | return fill_matrix_raw<T>(height, width, [&](size_t, size_t) { return dist(rnd); }); |
| 40 | } |
| 41 | |
| 42 | template <> |
| 43 | Buffer fill_matrix_random_raw<Float16>(size_t height, size_t width, uint32_t seed) { |
| 44 | std::mt19937 rnd(seed); |
| 45 | std::uniform_real_distribution<float> dist; |
| 46 | |
| 47 | return fill_matrix_raw<Float16>(height, width, [&](size_t, size_t) { return static_cast<Float16>(dist(rnd)); }); |
| 48 | } |
| 49 | |
| 50 | template <> |
| 51 | Buffer fill_matrix_random_raw<BFloat16<>>(size_t height, size_t width, uint32_t seed) { |
| 52 | std::mt19937 rnd(seed); |
| 53 | std::uniform_real_distribution<float> dist; |
| 54 | |
| 55 | return fill_matrix_raw<BFloat16<>>( |
| 56 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<>>(dist(rnd)); }); |
| 57 | } |
| 58 | |
| 59 | template <> |
| 60 | Buffer fill_matrix_random_raw<BFloat16<false>>(size_t height, size_t width, uint32_t seed) { |
| 61 | std::mt19937 rnd(seed); |
| 62 | std::uniform_real_distribution<float> dist; |
| 63 | |
| 64 | return fill_matrix_raw<BFloat16<false>>( |
| 65 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<false>>(dist(rnd)); }); |
| 66 | } |
| 67 | |
| 68 | template <> |
| 69 | Buffer fill_matrix_random_raw<Int4>(size_t height, size_t width, uint32_t seed) { |
| 70 | std::mt19937 rnd(seed); |
| 71 | std::uniform_int_distribution<int16_t> dist(-8, 7); |
| 72 | |
| 73 | return fill_matrix_raw<Int4>(height, width, [&](size_t, size_t) { return Int4(static_cast<int8_t>(dist(rnd))); }); |
| 74 | } |
| 75 | |
| 76 | template <> |
| 77 | Buffer fill_matrix_random_raw<UInt4>(size_t height, size_t width, uint32_t seed) { |
| 78 | std::mt19937 rnd(seed); |
| 79 | std::uniform_int_distribution<int16_t> dist(0, 15); |
| 80 | |
| 81 | return fill_matrix_raw<UInt4>(height, width, [&](size_t, size_t) { return UInt4(static_cast<int8_t>(dist(rnd))); }); |
| 82 | } |
| 83 | |
| 84 | template <> |
| 85 | Buffer fill_matrix_random_raw<Int2>(size_t height, size_t width, uint32_t seed) { |
| 86 | std::mt19937 rnd(seed); |
| 87 | std::uniform_int_distribution<int16_t> dist(-2, 1); |
| 88 | |
| 89 | return fill_matrix_raw<Int2>(height, width, [&](size_t, size_t) { return Int2(static_cast<int8_t>(dist(rnd))); }); |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | |
| 94 | Buffer fill_matrix_random(size_t height, size_t width, const DataFormat& format, uint32_t seed) { |
| 95 | switch (format.pack_format()) { |
| 96 | case DataFormat::PackFormat::NONE: |
| 97 | switch (format.data_type()) { |
| 98 | case DataType::FP32: |
| 99 | return fill_matrix_random_raw<float>(height, width, seed); |
| 100 | |
| 101 | case DataType::FP16: |
| 102 | return fill_matrix_random_raw<Float16>(height, width, seed); |
| 103 | |
| 104 | case DataType::BF16: |
| 105 | return fill_matrix_random_raw<BFloat16<>>(height, width, seed); |
| 106 | |
| 107 | case DataType::QSU4: |
| 108 | return fill_matrix_random_raw<UInt4>(height, width, seed); |
| 109 | |
| 110 | case DataType::QAI4: |
| 111 | case DataType::QSI4: |
| 112 | return fill_matrix_random_raw<Int4>(height, width, seed); |
| 113 | |
| 114 | case DataType::QSI2: |
| 115 | return fill_matrix_random_raw<Int2>(height, width, seed); |
| 116 | |
| 117 | default: |
| 118 | KAI_ERROR("Unsupported data type!"); |
| 119 | } |
| 120 | |
| 121 | break; |
| 122 | |
| 123 | default: |
| 124 | KAI_ERROR("Unsupported data format!"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | template <typename Value> |
| 129 | Buffer fill_random(size_t length, uint32_t seed) { |
| 130 | return fill_matrix_random_raw<Value>(1, length, seed); |
| 131 | } |
| 132 | |
| 133 | template Buffer fill_random<float>(size_t length, uint32_t seed); |
| 134 | template Buffer fill_random<Float16>(size_t length, uint32_t seed); |
| 135 | template Buffer fill_matrix_raw<float>(size_t height, size_t width, std::function<float(size_t, size_t)> gen); |
| 136 | template Buffer fill_matrix_raw<Float16>(size_t height, size_t width, std::function<Float16(size_t, size_t)> gen); |
| 137 | template Buffer fill_random<BFloat16<false>>(size_t length, uint32_t seed); |
| 138 | |
| 139 | } // namespace kai::test |