Elffuss BraidHub

kikocis/kleidiai public

4bba2d545594 · 2.9 KiBRawPermalink
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#pragma once
8
9#include <functional>
10#include <type_traits>
11#include <utility>
12
13#include "kai/kai_common.h"
14#include "test/common/buffer.hpp"
15#include "test/common/memory.hpp"
16namespace kai::test {
17
18class DataFormat;
19
20/// Creates a new matrix filled with random data.
21///
22/// @param[in] height Number of rows.
23/// @param[in] width Number of columns.
24/// @param[in] format Data format.
25/// @param[in] seed Random seed.
26///
27/// @return The data buffer for the matrix.
28Buffer fill_matrix_random(size_t height, size_t width, const DataFormat& format, uint32_t seed);
29
30/// Creates a new data buffer filled with random data.
31///
32/// @tparam Value The data type.
33///
34/// @param[in] length The number of elements.
35/// @param[in] seed The random seed.
36///
37/// @return The data buffer.
38template <typename Value>
39Buffer fill_random(size_t length, uint32_t seed);
40
41/// Creates a new matrix filled with data produced by a generator function.
42///
43/// @tparam T Element type.
44/// @tparam Generator Callable returning values convertible to `T`.
45///
46/// @param[in] height Number of rows.
47/// @param[in] width Number of columns.
48/// @param[in] gen Generator function or functor.
49///
50/// @return The data buffer for the matrix.
51template <typename T, typename Generator>
52Buffer fill_matrix_raw(size_t height, size_t width, Generator&& gen) {
53 KAI_ASSUME_ALWAYS(width * size_in_bits<T> % 8 == 0);
54 const auto row_bytes = width * size_in_bits<T> / 8;
55
56 Buffer data(height * row_bytes);
57 auto* ptr = reinterpret_cast<T*>(data.data());
58 auto&& generator = std::forward<Generator>(gen);
59
60 for (size_t y = 0; y < height; ++y) {
61 for (size_t x = 0; x < width; ++x) {
62 write_array<T>(ptr, y * width + x, generator(y, x));
63 }
64 }
65
66 return data;
67}
68
69/// Convenience overload to maintain the legacy std::function signature.
70template <typename T>
71Buffer fill_matrix_raw(size_t height, size_t width, std::function<T(size_t, size_t)> gen) {
72 // Wrap std::function into a generic generator to prevent self-recursion.
73 return fill_matrix_raw<T>(height, width, [&gen](size_t y, size_t x) { return gen(y, x); });
74}
75
76/// Creates a new matrix using a generator that returns a Buffer.
77///
78/// @tparam T Element type.
79/// @tparam Generator Callable returning `Buffer`.
80///
81/// @param[in] height Number of rows.
82/// @param[in] width Number of columns.
83/// @param[in] generator Generator instance.
84///
85/// @return The data buffer for the matrix.
86template <typename Generator>
87Buffer fill_matrix_generate(size_t height, size_t width, const Generator& generator) {
88 auto buffer = generator(height, width);
89 static_assert(std::is_same_v<std::decay_t<decltype(buffer)>, Buffer>, "Generator must return Buffer");
90
91 return buffer;
92}
93
94} // namespace kai::test