Elffuss BraidHub

kikocis/kleidiai public

main · 3.1 KiBRawPermalink
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
17namespace kai::test {
18
19/// Base data generator class.
20template <typename T>
21class DataGenerator {
22public:
23 virtual ~DataGenerator() = default;
24
25 Buffer operator()(size_t rows, size_t cols) const {
26 return generate(rows, cols);
27 };
28
29protected:
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.
34template <typename T>
35class SequentialGenerator : public DataGenerator<T> {
36public:
37 explicit SequentialGenerator(float start = 0.0, float step = 1.0) : m_start(start), m_step(step) {
38 }
39
40protected:
41 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 return fill_matrix_raw<T>(
44 rows, cols, [value = static_cast<T>(m_start), step = static_cast<T>(m_step)](size_t, size_t) mutable -> T {
45 const T current = value;
46 value += step;
47 return current;
48 });
49 }
50
51private:
52 float m_start;
53 float m_step;
54};
55
56/// Produces a matrix filled with a constant value (defaults to zero).
57template <typename T>
58class ConstantGenerator : public SequentialGenerator<T> {
59public:
60 explicit ConstantGenerator(float value = 0.0) : SequentialGenerator<T>(value, 0.0) {
61 }
62};
63
64/// Generates uniformly distributed floating-point values on [low, high).
65template <typename T>
66class UniformRandomGenerator : public DataGenerator<T> {
67public:
68 static_assert(is_floating_point<T>, "UniformRandomGenerator requires floating-point type");
69
70 explicit UniformRandomGenerator(float low = 0.0, float high = 1.0, const std::uint32_t seed = 0) :
71 m_engine(seed), m_dist(low, high) {
72 KAI_ASSERT_ALWAYS(low < high);
73 }
74
75protected:
76 Buffer generate(size_t rows, size_t cols) const override {
77 return fill_matrix_raw<T>(rows, cols, [&](size_t, size_t) mutable { return static_cast<T>(m_dist(m_engine)); });
78 }
79
80private:
81 mutable std::mt19937 m_engine;
82 mutable std::uniform_real_distribution<float> m_dist;
83};
84
85/// Generates normally distributed floating-point values.
86template <typename T>
87class NormalRandomGenerator : public DataGenerator<T> {
88public:
89 static_assert(is_floating_point<T>, "NormalRandomGenerator requires floating-point type");
90
91 explicit NormalRandomGenerator(float mean = 0.0, float stddev = 1.0, const std::uint32_t seed = 0) :
92 m_engine(seed), m_dist(mean, stddev) {
93 KAI_ASSERT_ALWAYS(stddev > 0.0);
94 }
95
96protected:
97 Buffer generate(size_t rows, size_t cols) const override {
98 return fill_matrix_raw<T>(rows, cols, [&](size_t, size_t) mutable { return static_cast<T>(m_dist(m_engine)); });
99 }
100
101private:
102 mutable std::mt19937 m_engine;
103 mutable std::normal_distribution<float> m_dist;
104};
105
106} // namespace kai::test