test/reference/generators.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 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 <random> | ||
| 12 | #include <type_traits> | ||
| 13 | |||
| 14 | #include "kai/kai_common.h" | ||
| 15 | #include "test/reference/fill.hpp" | ||
| 16 | |||
| 17 | namespace kai::test { | ||
| 18 | |||
| 19 | /// Base data generator class. | ||
| 20 | template <typename T> | ||
| 21 | class DataGenerator { | ||
| 22 | public: | ||
| 23 | 1989 | virtual ~DataGenerator() = default; | |
| 24 | |||
| 25 | 1989 | Buffer operator()(size_t rows, size_t cols) const { | |
| 26 | 1989 | return generate(rows, cols); | |
| 27 | }; | ||
| 28 | |||
| 29 | protected: | ||
| 30 | virtual Buffer generate(size_t rows, size_t cols) const = 0; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// Fills a matrix with sequentially increasing values in row-major order. | ||
| 34 | template <typename T> | ||
| 35 | class SequentialGenerator : public DataGenerator<T> { | ||
| 36 | public: | ||
| 37 | 144 | explicit SequentialGenerator(float start = 0.0, float step = 1.0) : m_start(start), m_step(step) { | |
| 38 | 144 | } | |
| 39 | |||
| 40 | protected: | ||
| 41 | 144 | Buffer generate(size_t rows, size_t cols) const override { | |
| 42 | // Emit the initial value before advancing so sequences start exactly at m_start. | ||
| 43 | 144 | return fill_matrix_raw<T>( | |
| 44 | 4728 | rows, cols, [value = static_cast<T>(m_start), step = static_cast<T>(m_step)](size_t, size_t) mutable -> T { | |
| 45 | 4584 | const T current = value; | |
| 46 | 4584 | value += step; | |
| 47 | 6876 | return current; | |
| 48 | 2292 | }); | |
| 49 | } | ||
| 50 | |||
| 51 | private: | ||
| 52 | float m_start; | ||
| 53 | float m_step; | ||
| 54 | }; | ||
| 55 | |||
| 56 | /// Produces a matrix filled with a constant value (defaults to zero). | ||
| 57 | template <typename T> | ||
| 58 | class ConstantGenerator : public SequentialGenerator<T> { | ||
| 59 | public: | ||
| 60 | 216 | explicit ConstantGenerator(float value = 0.0) : SequentialGenerator<T>(value, 0.0) { | |
| 61 | 216 | } | |
| 62 | }; | ||
| 63 | |||
| 64 | /// Generates uniformly distributed floating-point values on [low, high). | ||
| 65 | template <typename T> | ||
| 66 | class UniformRandomGenerator : public DataGenerator<T> { | ||
| 67 | public: | ||
| 68 | static_assert(is_floating_point<T>, "UniformRandomGenerator requires floating-point type"); | ||
| 69 | |||
| 70 | 2952 | explicit UniformRandomGenerator(float low = 0.0, float high = 1.0, const std::uint32_t seed = 0) : | |
| 71 |
4/8✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 702 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1035 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1035 times.
✗ Branch 7 not taken.
|
2952 | m_engine(seed), m_dist(low, high) { |
| 72 | − | KAI_ASSERT_ALWAYS(low < high); | |
| 73 | 2952 | } | |
| 74 | |||
| 75 | protected: | ||
| 76 | 1737 | Buffer generate(size_t rows, size_t cols) const override { | |
| 77 | 3385494 | return fill_matrix_raw<T>(rows, cols, [&](size_t, size_t) mutable { return static_cast<T>(m_dist(m_engine)); }); | |
| 78 | } | ||
| 79 | |||
| 80 | private: | ||
| 81 | mutable std::mt19937 m_engine; | ||
| 82 | mutable std::uniform_real_distribution<float> m_dist; | ||
| 83 | }; | ||
| 84 | |||
| 85 | /// Generates normally distributed floating-point values. | ||
| 86 | template <typename T> | ||
| 87 | class NormalRandomGenerator : public DataGenerator<T> { | ||
| 88 | public: | ||
| 89 | static_assert(is_floating_point<T>, "NormalRandomGenerator requires floating-point type"); | ||
| 90 | |||
| 91 | 162 | explicit NormalRandomGenerator(float mean = 0.0, float stddev = 1.0, const std::uint32_t seed = 0) : | |
| 92 |
2/4✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
|
162 | m_engine(seed), m_dist(mean, stddev) { |
| 93 | − | KAI_ASSERT_ALWAYS(stddev > 0.0); | |
| 94 | 162 | } | |
| 95 | |||
| 96 | protected: | ||
| 97 | 108 | Buffer generate(size_t rows, size_t cols) const override { | |
| 98 | 80946 | return fill_matrix_raw<T>(rows, cols, [&](size_t, size_t) mutable { return static_cast<T>(m_dist(m_engine)); }); | |
| 99 | } | ||
| 100 | |||
| 101 | private: | ||
| 102 | mutable std::mt19937 m_engine; | ||
| 103 | mutable std::normal_distribution<float> m_dist; | ||
| 104 | }; | ||
| 105 | |||
| 106 | } // namespace kai::test | ||
| 107 |