Elffuss BraidHub

kikocis/kleidiai public

main · 9.6 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2026 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 <algorithm>
10#include <cmath>
11#include <cstddef>
12#include <cstdint>
13#include <random>
14
15#include "test/common/assert.hpp"
16#include "test/common/data_type.hpp"
17#include "test/common/memory.hpp"
18#include "test/common/range.hpp"
19#include "test/common/span.hpp"
20#include "test/nextgen/common/random.hpp"
21
22namespace kai::test {
23
24/// Computes the total number of elements in a shape.
25///
26/// @param[in] shape The size of the multidimensional data.
27///
28/// @return The total number of elements.
29inline size_t element_count(Span<const size_t> shape) {
30 if (shape.empty()) {
31 return 0;
32 }
33
34 size_t count = 1;
35 for (size_t dim : shape) {
36 count *= dim;
37 }
38
39 return count;
40}
41
42/// Computes the integer range for the provided data type.
43inline Range<double> integer_range_for_dtype(DataType dtype) {
44 const size_t bits = data_type_size_in_bits(dtype);
45 KAI_TEST_ASSERT_MSG(bits <= 32, "Data type size must be at most 32 bits.");
46
47 const bool is_signed = data_type_is_signed(dtype);
48 const int64_t max_unsigned = static_cast<int64_t>((1ULL << bits) - 1);
49 const int64_t max_signed = static_cast<int64_t>((1ULL << (bits - 1)) - 1);
50 const int64_t min_signed = -static_cast<int64_t>(1ULL << (bits - 1));
51
52 return is_signed ? Range<double>{static_cast<double>(min_signed), static_cast<double>(max_signed)}
53 : Range<double>{0.0, static_cast<double>(max_unsigned)};
54}
55
56/// Fill an output buffer with random integer values from the requested range using the provided seed.
57///
58/// The requested range is clamped to the integer range representable by the
59/// data type, which allows callers to use one generation policy across
60/// different integer storage types.
61///
62/// @param[in] shape The size of the multidimensional data.
63/// @param[in] dtype The data type of the elements.
64/// @param[out] output The output buffer to populate.
65/// @param[in] seed Seed for the local RNG.
66/// @param[in] range The requested inclusive value range.
67inline void fill_random_with_seed(
68 Span<const size_t> shape, DataType dtype, Span<std::byte> output, uint32_t seed, Range<double> range) {
69 KAI_TEST_ASSERT_MSG(dtype != DataType::UNKNOWN, "Unknown data type for generator.");
70
71 const size_t count = element_count(shape);
72 if (count == 0) {
73 return;
74 }
75
76 const size_t size_needed = data_type_array_size_in_bytes(dtype, count);
77 KAI_TEST_ASSERT_MSG(output.size() >= size_needed, "Output buffer is too small for requested shape.");
78
79 KAI_TEST_ASSERT_MSG(range.is_valid(), "Range minimum must not exceed the maximum.");
80
81 std::mt19937 local_rng(seed);
82 if (data_type_is_integral(dtype)) {
83 const Range<double> dtype_range = integer_range_for_dtype(dtype);
84 const double min_bound = std::max(dtype_range.min, range.min);
85 const double max_bound = std::min(dtype_range.max, range.max);
86 KAI_TEST_ASSERT_MSG(min_bound <= max_bound, "Requested range must overlap the data type range.");
87
88 const int64_t min_value = static_cast<int64_t>(std::ceil(min_bound));
89 const int64_t max_value = static_cast<int64_t>(std::floor(max_bound));
90 KAI_TEST_ASSERT_MSG(min_value <= max_value, "Requested range contains no representable integer values.");
91
92 std::uniform_int_distribution<int64_t> dist(min_value, max_value);
93 for (size_t i = 0; i < count; ++i) {
94 write_array(dtype, output.data(), i, static_cast<double>(dist(local_rng)));
95 }
96 return;
97 }
98
99 std::uniform_real_distribution<double> dist(range.min, range.max);
100 for (size_t i = 0; i < count; ++i) {
101 write_array(dtype, output.data(), i, dist(local_rng));
102 }
103}
104
105/// Fill an output buffer with random values using the provided seed.
106///
107/// @param[in] shape The size of the multidimensional data.
108/// @param[in] dtype The data type of the elements.
109/// @param[out] output The output buffer to populate.
110/// @param[in] seed Seed for the local RNG.
111inline void fill_random_with_seed(Span<const size_t> shape, DataType dtype, Span<std::byte> output, uint32_t seed) {
112 KAI_TEST_ASSERT_MSG(dtype != DataType::UNKNOWN, "Unknown data type for generator.");
113
114 if (data_type_is_integral(dtype)) {
115 fill_random_with_seed(shape, dtype, output, seed, integer_range_for_dtype(dtype));
116 return;
117 }
118
119 const size_t count = element_count(shape);
120 if (count == 0) {
121 return;
122 }
123
124 const size_t size_needed = data_type_array_size_in_bytes(dtype, count);
125 KAI_TEST_ASSERT_MSG(output.size() >= size_needed, "Output buffer is too small for requested shape.");
126
127 std::mt19937 local_rng(seed);
128 std::uniform_real_distribution<float> dist;
129 for (size_t i = 0; i < count; ++i) {
130 write_array(dtype, output.data(), i, static_cast<double>(dist(local_rng)));
131 }
132}
133
134/// Fill an output buffer with random values using the provided RNG.
135///
136/// @param[in] shape The size of the multidimensional data.
137/// @param[in] dtype The data type of the elements.
138/// @param[out] output The output buffer to populate.
139/// @param[in, out] rng Random number generator.
140inline void fill_random(Span<const size_t> shape, DataType dtype, Span<std::byte> output, Rng& rng) {
141 const uint32_t seed = std::uniform_int_distribution<uint32_t>()(rng);
142 fill_random_with_seed(shape, dtype, output, seed);
143}
144
145/// Fill an output buffer with random integer values from the requested range using the provided RNG.
146///
147/// @param[in] shape The size of the multidimensional data.
148/// @param[in] dtype The data type of the elements.
149/// @param[out] output The output buffer to populate.
150/// @param[in, out] rng Random number generator.
151/// @param[in] range The requested inclusive value range.
152inline void fill_random(
153 Span<const size_t> shape, DataType dtype, Span<std::byte> output, Rng& rng, Range<double> range) {
154 const uint32_t seed = std::uniform_int_distribution<uint32_t>()(rng);
155 fill_random_with_seed(shape, dtype, output, seed, range);
156}
157
158/// Fill an output buffer with random scale values whose magnitude is away from zero.
159///
160/// @param[in] shape The size of the multidimensional data.
161/// @param[in] dtype The data type of the elements.
162/// @param[out] output The output buffer to populate.
163/// @param[in, out] rng Random number generator.
164inline void fill_random_scale(Span<const size_t> shape, DataType dtype, Span<std::byte> output, Rng& rng) {
165 KAI_TEST_ASSERT_MSG(dtype != DataType::UNKNOWN, "Unknown data type for scale generator.");
166
167 const size_t count = element_count(shape);
168 if (count == 0) {
169 return;
170 }
171
172 const size_t size_needed = data_type_array_size_in_bytes(dtype, count);
173 KAI_TEST_ASSERT_MSG(output.size() >= size_needed, "Output buffer is too small for requested scale shape.");
174
175 std::bernoulli_distribution negative_dist(0.5);
176
177 if (data_type_is_float(dtype)) {
178 std::uniform_real_distribution<float> magnitude_dist(0.125F, 2.0F);
179 for (size_t i = 0; i < count; ++i) {
180 const float magnitude = magnitude_dist(rng);
181 const float value = negative_dist(rng) ? -magnitude : magnitude;
182 write_array(dtype, output.data(), i, value);
183 }
184 } else {
185 for (size_t i = 0; i < count; ++i) {
186 const int64_t value = data_type_is_signed(dtype) && negative_dist(rng) ? -1 : 1;
187 write_array(dtype, output.data(), i, static_cast<double>(value));
188 }
189 }
190}
191
192/// Fill an output buffer with sequential pattern values starting at a value.
193///
194/// @param[in] shape The size of the multidimensional data.
195/// @param[in] dtype The data type of the elements.
196/// @param[out] output The output buffer to populate.
197/// @param[in] start The starting value for the sequence.
198/// @param[in] step The step between successive values. Use 0 for constant fill.
199inline void fill_sequential(
200 Span<const size_t> shape, DataType dtype, Span<std::byte> output, int64_t start, int64_t step) {
201 KAI_TEST_ASSERT_MSG(dtype != DataType::UNKNOWN, "Unknown data type for generator.");
202
203 const size_t count = element_count(shape);
204 if (count == 0) {
205 return;
206 }
207
208 const size_t size_needed = data_type_array_size_in_bytes(dtype, count);
209 KAI_TEST_ASSERT_MSG(output.size() >= size_needed, "Output buffer is too small for requested shape.");
210
211 const bool is_float = data_type_is_float(dtype);
212 const bool is_signed = data_type_is_signed(dtype);
213
214 for (size_t i = 0; i < count; ++i) {
215 const int64_t offset = start + (static_cast<int64_t>(i) * step);
216 const int64_t pattern = static_cast<int64_t>(offset % 16) - (is_signed ? 8 : 0);
217 const double value = is_float ? static_cast<double>(pattern) / 8.0 : static_cast<double>(pattern);
218 write_array(dtype, output.data(), i, value);
219 }
220}
221
222/// Fill an output buffer with a constant value.
223///
224/// @param[in] shape The size of the multidimensional data.
225/// @param[in] dtype The data type of the elements.
226/// @param[out] output The output buffer to populate.
227/// @param[in] value The constant value to write.
228inline void fill_constant(Span<const size_t> shape, DataType dtype, Span<std::byte> output, int64_t value) {
229 fill_sequential(shape, dtype, output, value, 0);
230}
231
232/// Fill an output buffer with sequential pattern values.
233///
234/// @param[in] shape The size of the multidimensional data.
235/// @param[in] dtype The data type of the elements.
236/// @param[out] output The output buffer to populate.
237inline void fill_sequential(Span<const size_t> shape, DataType dtype, Span<std::byte> output) {
238 fill_sequential(shape, dtype, output, 0, 1);
239}
240
241} // namespace kai::test