Elffuss BraidHub

kikocis/kleidiai public

main · 8.0 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-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 <cstddef>
10#include <cstring>
11#include <type_traits>
12
13#include "kai/kai_common.h"
14#include "test/common/assert.hpp"
15#include "test/common/bfloat16.hpp"
16#include "test/common/data_type.hpp"
17#include "test/common/int2.hpp"
18#include "test/common/int4.hpp"
19#include "test/common/round.hpp"
20#include "test/common/span.hpp"
21
22namespace kai::test {
23
24/// The size in bits of type `T`.
25template <typename T>
26inline constexpr size_t size_in_bits = sizeof(T) * 8;
27
28/// The size in bits of type `T`.
29template <>
30inline constexpr size_t size_in_bits<UInt4> = 4;
31
32/// The size in bits of type `T`.
33template <>
34inline constexpr size_t size_in_bits<Int4> = 4;
35
36/// The size in bits of type `T`.
37template <>
38inline constexpr size_t size_in_bits<UInt2> = 2;
39
40/// The size in bits of type `T`.
41template <>
42inline constexpr size_t size_in_bits<Int2> = 2;
43
44/// Reads the array at the specified index.
45///
46/// @param[in] array Data buffer.
47/// @param[in] index Array index.
48///
49/// @return The array value at the specified index.
50template <typename T>
51T read_array(const void* array, size_t index) {
52 if constexpr (std::is_same_v<T, UInt4>) {
53 const auto [lo, hi] = UInt4::unpack_u8(reinterpret_cast<const uint8_t*>(array)[index / 2]);
54 return index % 2 == 0 ? lo : hi;
55 } else if constexpr (std::is_same_v<T, Int4>) {
56 const auto [lo, hi] = Int4::unpack_u8(reinterpret_cast<const uint8_t*>(array)[index / 2]);
57 return index % 2 == 0 ? lo : hi;
58 } else if constexpr (std::is_same_v<T, UInt2>) {
59 const auto [v1, v2, v3, v4] = UInt2::unpack_u8(reinterpret_cast<const uint8_t*>(array)[index / 4]);
60 if ((index % 4) == 0) {
61 return v1;
62 } else if ((index % 4) == 1) {
63 return v2;
64 } else if ((index % 4) == 2) {
65 return v3;
66 }
67 return v4;
68 } else if constexpr (std::is_same_v<T, Int2>) {
69 const auto [v1, v2, v3, v4] = Int2::unpack_u8(reinterpret_cast<const uint8_t*>(array)[index / 4]);
70 if ((index % 4) == 0) {
71 return v1;
72 } else if ((index % 4) == 1) {
73 return v2;
74 } else if ((index % 4) == 2) {
75 return v3;
76 }
77 return v4;
78 } else if constexpr (std::is_same_v<T, BFloat16<false>>) {
79 uint16_t raw_value = reinterpret_cast<const uint16_t*>(array)[index];
80 return BFloat16<false>(kai_cast_f32_bf16(raw_value));
81 } else if constexpr (std::is_same_v<T, BFloat16<true>>) {
82 uint16_t raw_value = reinterpret_cast<const uint16_t*>(array)[index];
83 return BFloat16<true>(kai_cast_f32_bf16(raw_value));
84 } else {
85 return reinterpret_cast<const T*>(array)[index];
86 }
87}
88
89/// Reads the array at the specified index.
90///
91/// @param[in] array Data buffer.
92/// @param[in] index Array index.
93///
94/// @return The array value at the specified index.
95template <typename T>
96T read_array(Span<const std::byte> array, size_t index) {
97 const size_t min_size = round_up_division((index + 1) * size_in_bits<T>, 8);
98 KAI_TEST_ASSERT_MSG(array.size() >= min_size, "The read access is out-of-bound!");
99 return read_array<T>(array.data(), index);
100}
101
102/// Reads the 2D array at the specified coordinates.
103///
104/// @param[in] data The data buffer.
105/// @param[in] width The array width.
106/// @param[in] row The row index.
107/// @param[in] col The column index.
108///
109/// @return The array value at the specified coordinates.
110template <typename T>
111T read_2d(Span<const std::byte> data, size_t width, size_t row, size_t col) {
112 const size_t stride = round_up_division(width * size_in_bits<T>, 8);
113 return read_array<T>(data.subspan(row * stride, stride), col);
114}
115
116/// Reads the array at the specified index
117///
118/// @param[in] type Array element data type
119/// @param[in] array Data buffer.
120/// @param[in] index Array index.
121///
122/// @return Value at specified index
123double read_array(DataType type, const void* array, size_t index);
124
125/// Reads the 2D array at the specified coordinates.
126///
127/// @param[in] type Array element data type.
128/// @param[in] data The data buffer.
129/// @param[in] width The array width.
130/// @param[in] row The row index.
131/// @param[in] col The column index.
132///
133/// @return The array value at the specified coordinates.
134inline double read_2d(DataType type, Span<const std::byte> data, size_t width, size_t row, size_t col) {
135 KAI_TEST_ASSERT_MSG(type != DataType::UNKNOWN, "Unknown data type for read_2d.");
136 const size_t stride = round_up_division(width * data_type_size_in_bits(type), 8);
137 return read_array(type, data.subspan(row * stride, stride).data(), col);
138}
139
140/// Writes the specified value to the array.
141///
142/// @param[in] array Data buffer.
143/// @param[in] index Array index.
144/// @param[in] value Value to be stored.
145template <typename T>
146void write_array(void* array, size_t index, T value) {
147 if constexpr (std::is_same_v<T, UInt4>) {
148 auto* arr_value = reinterpret_cast<uint8_t*>(array) + index / 2;
149 const auto [lo, hi] = UInt4::unpack_u8(*arr_value);
150
151 if (index % 2 == 0) {
152 *arr_value = UInt4::pack_u8(value, hi);
153 } else {
154 *arr_value = UInt4::pack_u8(lo, value);
155 }
156 } else if constexpr (std::is_same_v<T, Int4>) {
157 auto* arr_value = reinterpret_cast<uint8_t*>(array) + index / 2;
158 const auto [lo, hi] = Int4::unpack_u8(*arr_value);
159
160 if (index % 2 == 0) {
161 *arr_value = Int4::pack_u8(value, hi);
162 } else {
163 *arr_value = Int4::pack_u8(lo, value);
164 }
165 } else if constexpr (std::is_same_v<T, UInt2>) {
166 auto* arr_value = reinterpret_cast<uint8_t*>(array) + index / 4;
167 const auto [v1, v2, v3, v4] = UInt2::unpack_u8(*arr_value);
168
169 if ((index % 4) == 0) {
170 *arr_value = UInt2::pack_u8(value, v2, v3, v4);
171 } else if ((index % 4) == 1) {
172 *arr_value = UInt2::pack_u8(v1, value, v3, v4);
173 } else if ((index % 4) == 2) {
174 *arr_value = UInt2::pack_u8(v1, v2, value, v4);
175 } else {
176 *arr_value = UInt2::pack_u8(v1, v2, v3, value);
177 }
178 } else if constexpr (std::is_same_v<T, Int2>) {
179 auto* arr_value = reinterpret_cast<uint8_t*>(array) + index / 4;
180 const auto [v1, v2, v3, v4] = Int2::unpack_u8(*arr_value);
181
182 if ((index % 4) == 0) {
183 *arr_value = Int2::pack_u8(value, v2, v3, v4);
184 } else if ((index % 4) == 1) {
185 *arr_value = Int2::pack_u8(v1, value, v3, v4);
186 } else if ((index % 4) == 2) {
187 *arr_value = Int2::pack_u8(v1, v2, value, v4);
188 } else {
189 *arr_value = Int2::pack_u8(v1, v2, v3, value);
190 }
191 } else {
192 reinterpret_cast<T*>(array)[index] = value;
193 }
194}
195
196/// Writes the specified value to the array.
197///
198/// @param[in] array Data buffer.
199/// @param[in] index Array index.
200/// @param[in] value Value to be stored.
201template <typename T>
202void write_array(Span<std::byte> array, size_t index, T value) {
203 const size_t min_size = round_up_division((index + 1) * size_in_bits<T>, 8);
204 KAI_TEST_ASSERT_MSG(array.size() >= min_size, "The write access is out-of-bound!");
205 write_array<T>(array.data(), index, value);
206}
207
208/// Writes the specified value to the 2D array at the specified coordinates.
209///
210/// @param[out] data The data buffer.
211/// @param[in] width The array width.
212/// @param[in] row The row index.
213/// @param[in] col The column index.
214/// @param[in] value The value to be stored.
215template <typename T>
216void write_2d(Span<std::byte> data, size_t width, size_t row, size_t col, T value) {
217 const size_t stride = round_up_division(width * size_in_bits<T>, 8);
218 write_array<T>(data.subspan(row * stride, stride), col, value);
219}
220
221/// Writes the specified value to the array.
222///
223/// @param[in] type Array element type.
224/// @param[in] array Data buffer.
225/// @param[in] index Array index.
226/// @param[in] value Value to be stored.
227void write_array(DataType type, void* array, size_t index, double value);
228
229} // namespace kai::test