test/reference/fill.cpp
| 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 | #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/int4.hpp" | ||
| 21 | |||
| 22 | namespace kai::test { | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | template <typename T> | ||
| 27 | 100987 | Buffer fill_matrix_random_raw(size_t height, size_t width, uint32_t seed) { | |
| 28 | using TDist = std::conditional_t< | ||
| 29 | std::is_floating_point_v<T>, std::uniform_real_distribution<float>, std::uniform_int_distribution<T>>; | ||
| 30 | |||
| 31 | 100987 | std::mt19937 rnd(seed); | |
| 32 | 100987 | TDist dist; | |
| 33 | |||
| 34 | 332750452 | return fill_matrix_raw<T>(height, width, [&](size_t, size_t) { return dist(rnd); }); | |
| 35 | 100987 | } | |
| 36 | |||
| 37 | template <> | ||
| 38 | 33720 | Buffer fill_matrix_random_raw<Float16>(size_t height, size_t width, uint32_t seed) { | |
| 39 | 33720 | std::mt19937 rnd(seed); | |
| 40 | 33720 | std::uniform_real_distribution<float> dist; | |
| 41 | |||
| 42 | 34061540 | return fill_matrix_raw<Float16>(height, width, [&](size_t, size_t) { return static_cast<Float16>(dist(rnd)); }); | |
| 43 | 33720 | } | |
| 44 | |||
| 45 | template <> | ||
| 46 | 168 | Buffer fill_matrix_random_raw<BFloat16<>>(size_t height, size_t width, uint32_t seed) { | |
| 47 | 168 | std::mt19937 rnd(seed); | |
| 48 | 168 | std::uniform_real_distribution<float> dist; | |
| 49 | |||
| 50 | 168 | return fill_matrix_raw<BFloat16<>>( | |
| 51 | 203844 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<>>(dist(rnd)); }); | |
| 52 | 168 | } | |
| 53 | |||
| 54 | template <> | ||
| 55 | 192 | Buffer fill_matrix_random_raw<BFloat16<false>>(size_t height, size_t width, uint32_t seed) { | |
| 56 | 192 | std::mt19937 rnd(seed); | |
| 57 | 192 | std::uniform_real_distribution<float> dist; | |
| 58 | |||
| 59 | 192 | return fill_matrix_raw<BFloat16<false>>( | |
| 60 | 2709696 | height, width, [&](size_t, size_t) { return static_cast<BFloat16<false>>(dist(rnd)); }); | |
| 61 | 192 | } | |
| 62 | |||
| 63 | template <> | ||
| 64 | ✗ | Buffer fill_matrix_random_raw<Int4>(size_t height, size_t width, uint32_t seed) { | |
| 65 | ✗ | std::mt19937 rnd(seed); | |
| 66 | ✗ | std::uniform_int_distribution<int16_t> dist(-8, 7); | |
| 67 | |||
| 68 | ✗ | return fill_matrix_raw<Int4>(height, width, [&](size_t, size_t) { return Int4(static_cast<int8_t>(dist(rnd))); }); | |
| 69 | ✗ | } | |
| 70 | |||
| 71 | template <> | ||
| 72 | ✗ | Buffer fill_matrix_random_raw<UInt4>(size_t height, size_t width, uint32_t seed) { | |
| 73 | ✗ | std::mt19937 rnd(seed); | |
| 74 | ✗ | std::uniform_int_distribution<int16_t> dist(0, 15); | |
| 75 | |||
| 76 | ✗ | return fill_matrix_raw<UInt4>(height, width, [&](size_t, size_t) { return UInt4(static_cast<int8_t>(dist(rnd))); }); | |
| 77 | ✗ | } | |
| 78 | |||
| 79 | } // namespace | ||
| 80 | |||
| 81 | 5730 | Buffer fill_matrix_random(size_t height, size_t width, const DataFormat& format, uint32_t seed) { | |
| 82 |
1/2✓ Branch 0 taken 5730 times.
✗ Branch 1 not taken.
|
5730 | switch (format.pack_format()) { |
| 83 | case DataFormat::PackFormat::NONE: | ||
| 84 |
3/6✓ Branch 0 taken 3342 times.
✓ Branch 1 taken 2220 times.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
5730 | switch (format.data_type()) { |
| 85 | case DataType::FP32: | ||
| 86 | 3342 | return fill_matrix_random_raw<float>(height, width, seed); | |
| 87 | |||
| 88 | case DataType::FP16: | ||
| 89 | 2220 | return fill_matrix_random_raw<Float16>(height, width, seed); | |
| 90 | |||
| 91 | case DataType::BF16: | ||
| 92 | 168 | return fill_matrix_random_raw<BFloat16<>>(height, width, seed); | |
| 93 | |||
| 94 | case DataType::QSU4: | ||
| 95 | ✗ | return fill_matrix_random_raw<UInt4>(height, width, seed); | |
| 96 | |||
| 97 | case DataType::QAI4: | ||
| 98 | case DataType::QSI4: | ||
| 99 | ✗ | return fill_matrix_random_raw<Int4>(height, width, seed); | |
| 100 | |||
| 101 | default: | ||
| 102 | − | KAI_ERROR("Unsupported data type!"); | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | ✗ | break; | |
| 106 | |||
| 107 | default: | ||
| 108 | − | KAI_ERROR("Unsupported data format!"); | |
| 109 | ✗ | } | |
| 110 | 5730 | } | |
| 111 | |||
| 112 | template <typename Value> | ||
| 113 | 129337 | Buffer fill_random(size_t length, uint32_t seed) { | |
| 114 | 129337 | return fill_matrix_random_raw<Value>(1, length, seed); | |
| 115 | } | ||
| 116 | |||
| 117 | template Buffer fill_random<float>(size_t length, uint32_t seed); | ||
| 118 | template Buffer fill_random<Float16>(size_t length, uint32_t seed); | ||
| 119 | template Buffer fill_matrix_raw<float>(size_t height, size_t width, std::function<float(size_t, size_t)> gen); | ||
| 120 | template Buffer fill_matrix_raw<Float16>(size_t height, size_t width, std::function<Float16(size_t, size_t)> gen); | ||
| 121 | template Buffer fill_random<BFloat16<false>>(size_t length, uint32_t seed); | ||
| 122 | |||
| 123 | } // namespace kai::test | ||
| 124 |