Elffuss BraidHub

kikocis/kleidiai public

main · 19.7 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#include "test/reference/pack.hpp"
8
9#include <arm_neon.h>
10
11#include <algorithm>
12#include <cstddef>
13#include <cstdint>
14#include <cstring>
15
16#include "kai/kai_common.h"
17#include "test/common/bfloat16.hpp"
18#include "test/common/buffer.hpp"
19#include "test/common/data_format.hpp"
20#include "test/common/data_type.hpp"
21#include "test/common/float16.hpp"
22#include "test/common/memory.hpp"
23#include "test/common/round.hpp"
24
25namespace kai::test {
26
27namespace {
28
29BFloat16<> convert(const uint8_t* src_ptr_elm, DataType src_dtype, DataType dst_dtype) {
30 KAI_ASSUME_ALWAYS((src_dtype == DataType::FP32 || src_dtype == DataType::FP16) && dst_dtype == DataType::BF16);
31
32 switch (src_dtype) {
33 case DataType::FP32:
34 return BFloat16<>(*reinterpret_cast<const float*>(src_ptr_elm));
35 case DataType::FP16:
36 return BFloat16<>(static_cast<float>(*reinterpret_cast<const Float16*>(src_ptr_elm)));
37 default:
38 KAI_ERROR("Unsupported Data Type");
39 }
40}
41
42Buffer pack_block(
43 const void* src, DataType src_dtype, DataType dst_dtype, size_t src_esize, size_t dst_esize, size_t full_height,
44 size_t full_width, size_t block_height, size_t block_width, size_t subblock_height, size_t subblock_width) {
45 const auto dst_bytes =
46 round_up_multiple(full_height, block_height) * round_up_multiple(full_width, block_width) * dst_esize;
47
48 Buffer dst(dst_bytes, 0);
49
50 const auto* src_ptr = reinterpret_cast<const uint8_t*>(src);
51 auto* dst_ptr = dst.data();
52
53 for (size_t y_block = 0; y_block < full_height; y_block += block_height) {
54 for (size_t x_block = 0; x_block < full_width; x_block += block_width) {
55 for (size_t y_subblock = 0; y_subblock < block_height; y_subblock += subblock_height) {
56 for (size_t x_subblock = 0; x_subblock < block_width; x_subblock += subblock_width) {
57 for (size_t y_element = 0; y_element < subblock_height; ++y_element) {
58 if (src_dtype == dst_dtype) {
59 const size_t esize = dst_esize;
60
61 if (y_block + y_subblock + y_element < full_height) {
62 const size_t y_offset = (y_block + y_subblock + y_element) * full_width;
63 const size_t x_offset = x_block + x_subblock;
64 const size_t offset = y_offset + x_offset;
65 const auto len = std::min(subblock_width, full_width - x_offset);
66
67 memcpy(dst_ptr, src_ptr + offset * esize, len * esize);
68 }
69
70 dst_ptr += subblock_width * esize;
71 } else if (dst_esize == 2 /* 16 bits */) {
72 for (size_t x_element = 0; x_element < subblock_width; ++x_element) {
73 if (y_block + y_subblock + y_element < full_height) {
74 if (x_block + x_subblock + x_element < full_width) {
75 const uint8_t* src_ptr_elm = src_ptr +
76 ((y_block + y_subblock + y_element) * full_width + x_block + x_subblock +
77 x_element) *
78 src_esize;
79
80 const BFloat16 src_value = convert(src_ptr_elm, src_dtype, dst_dtype);
81 memcpy(dst_ptr, &src_value, dst_esize);
82 }
83 }
84
85 dst_ptr += dst_esize;
86 }
87 }
88 }
89 }
90 }
91 }
92 }
93
94 KAI_ASSERT_ALWAYS(reinterpret_cast<uintptr_t>(dst_ptr) - reinterpret_cast<uintptr_t>(dst.data()) == dst_bytes);
95
96 return dst;
97}
98
99/// Packs the matrix from raw to per-row bias format.
100Buffer pack_bias_per_row(
101 DataType src_dtype, DataType bias_dtype, DataType dst_dtype, size_t src_esize, size_t bias_esize, size_t dst_esize,
102 const void* src, const void* bias, size_t height, size_t width, size_t block_height, size_t block_width,
103 size_t subblock_height, size_t subblock_width) {
104 KAI_ASSUME_ALWAYS(src_dtype == bias_dtype);
105
106 const auto num_groups = (height + block_height - 1) / block_height;
107 const auto group_num_blocks = (width + block_width - 1) / block_width;
108 const auto group_bias_bytes = block_height * bias_esize;
109 const auto block_data_bytes = block_height * block_width * dst_esize;
110 const auto group_bytes = group_bias_bytes + group_num_blocks * block_data_bytes;
111 const auto dst_bytes = num_groups * group_bytes;
112
113 Buffer dst(dst_bytes, 0);
114
115 const auto* src_ptr = reinterpret_cast<const uint8_t*>(src);
116 const auto* bias_ptr = reinterpret_cast<const uint8_t*>(bias);
117 auto* dst_ptr = dst.data();
118
119 for (size_t y_block = 0; y_block < height; y_block += block_height) {
120 // Packs the bias.
121 const auto bias_len = std::min(block_height, height - y_block);
122 memcpy(dst_ptr, bias_ptr, bias_len * bias_esize);
123 bias_ptr += block_height * bias_esize;
124 dst_ptr += block_height * bias_esize;
125
126 for (size_t x_block = 0; x_block < width; x_block += block_width) {
127 for (size_t y_subblock = 0; y_subblock < block_height; y_subblock += subblock_height) {
128 for (size_t x_subblock = 0; x_subblock < block_width; x_subblock += subblock_width) {
129 for (size_t y_element = 0; y_element < subblock_height; ++y_element) {
130 if (src_dtype == dst_dtype) {
131 const size_t esize = dst_esize;
132 if (y_block + y_subblock + y_element < height) {
133 const auto len = std::min(subblock_width, width - x_block - x_subblock);
134
135 memcpy(
136 dst_ptr,
137 src_ptr +
138 ((y_block + y_subblock + y_element) * width + x_block + x_subblock) * esize,
139 len * esize);
140 }
141
142 dst_ptr += subblock_width * esize;
143 } else if (dst_esize == 2 /* 16 bits */) {
144 for (size_t x_element = 0; x_element < subblock_width; ++x_element) {
145 if (y_block + y_subblock + y_element < height) {
146 if (x_block + x_subblock + x_element < width) {
147 const uint8_t* src_ptr_elm = src_ptr +
148 ((y_block + y_subblock + y_element) * width + x_block + x_subblock +
149 x_element) *
150 src_esize;
151
152 const BFloat16 dst_value = convert(src_ptr_elm, src_dtype, dst_dtype);
153 memcpy(dst_ptr, &dst_value, dst_esize);
154 }
155 }
156
157 dst_ptr += dst_esize;
158 }
159 }
160 }
161 }
162 }
163 }
164 }
165
166 KAI_ASSERT_ALWAYS(reinterpret_cast<uintptr_t>(dst_ptr) - reinterpret_cast<uintptr_t>(dst.data()) == dst_bytes);
167
168 return dst;
169}
170
171} // namespace
172
173Buffer pack(
174 const DataFormat& dst_format, const void* src, [[maybe_unused]] const void* scales, const void* bias,
175 const DataFormat& src_format, size_t height, size_t width) {
176 const auto dst_dt = dst_format.data_type();
177 const auto dst_qf = dst_format.pack_format();
178 const auto src_dt = src_format.data_type();
179 const auto src_qf = src_format.pack_format();
180
181 const auto block_height = dst_format.actual_block_height(height);
182 const auto block_width = dst_format.actual_block_width(width);
183 const auto subblock_height = dst_format.actual_subblock_height(height);
184 const auto subblock_width = dst_format.actual_subblock_width(width);
185
186 if (src_qf == DataFormat::PackFormat::NONE && dst_qf == DataFormat::PackFormat::BIAS_PER_ROW) {
187 KAI_ASSUME_ALWAYS(
188 (src_dt == dst_dt) || (src_dt == DataType::FP32 && dst_dt == DataType::BF16) ||
189 (src_dt == DataType::FP16 && dst_dt == DataType::BF16));
190
191 const auto src_esize = data_type_size_in_bits(src_dt);
192 const auto dst_esize = data_type_size_in_bits(dst_dt);
193 const auto bias_esize = data_type_size_in_bits(dst_format.zero_point_data_type());
194 const auto bias_dt = dst_format.zero_point_data_type();
195
196 KAI_ASSUME_ALWAYS(dst_esize % 8 == 0 && bias_esize % 8 == 0 && src_esize % 8 == 0);
197
198 return pack_bias_per_row(
199 src_dt, bias_dt, dst_dt, src_esize / 8, bias_esize / 8, dst_esize / 8, src, bias, height, width,
200 block_height, block_width, subblock_height, subblock_width);
201 }
202
203 if (src_qf == DataFormat::PackFormat::NONE && dst_qf == DataFormat::PackFormat::NONE) {
204 KAI_ASSUME_ALWAYS(
205 (src_dt == dst_dt) || (src_dt == DataType::FP32 && dst_dt == DataType::BF16) ||
206 (src_dt == DataType::FP16 && dst_dt == DataType::BF16));
207
208 const auto dst_esize = data_type_size_in_bits(dst_dt);
209 const auto src_esize = data_type_size_in_bits(src_dt);
210
211 KAI_ASSUME_ALWAYS(src_esize % 8 == 0 && dst_esize % 8 == 0);
212
213 return pack_block(
214 src, src_dt, dst_dt, src_esize / 8, dst_esize / 8, height, width, block_height, block_width,
215 subblock_height, subblock_width);
216 }
217
218 KAI_ERROR("Unsupported operation!");
219}
220
221template <typename Data, typename Scale>
222Buffer pack_data_scales(const void* data, const void* scales, size_t height, size_t width, size_t quant_width) {
223 KAI_ASSUME_ALWAYS_IF(size_in_bits<Data> < 8, quant_width % (8 / size_in_bits<Data>) == 0);
224 KAI_ASSUME_ALWAYS_IF(size_in_bits<Data> < 8, width % (8 / size_in_bits<Data>) == 0);
225
226 const auto num_quant_packets_x = round_up_multiple(width, quant_width) / quant_width;
227
228 const auto data_bytes = height * width * size_in_bits<Data> / 8;
229 const auto scales_bytes = height * num_quant_packets_x * sizeof(Scale);
230
231 Buffer dst(data_bytes + scales_bytes);
232
233 const auto* scales_ptr = reinterpret_cast<const Scale*>(scales);
234 auto* dst_ptr = dst.data();
235
236 for (size_t y = 0; y < height; ++y) {
237 for (size_t x_quant = 0; x_quant < width; x_quant += quant_width) {
238 write_array(dst_ptr, 0, *scales_ptr);
239 dst_ptr += sizeof(Scale);
240 ++scales_ptr;
241
242 const auto len = std::min(x_quant + quant_width, width) - x_quant;
243
244 for (size_t x_element = 0; x_element < len; ++x_element) {
245 const auto x = x_quant + x_element;
246 write_array(dst_ptr, x_element, read_array<Data>(data, y * width + x));
247 }
248
249 dst_ptr += len * size_in_bits<Data> / 8;
250 }
251 }
252
253 KAI_ASSERT_ALWAYS(dst_ptr == dst.data() + dst.size());
254
255 return dst;
256}
257
258template Buffer pack_data_scales<UInt4, Float16>(
259 const void* data, const void* scales, size_t height, size_t width, size_t quant_width);
260
261template <typename ZeroPoint, typename Data, typename Scale>
262Buffer pack_zero_points_data_scales_per_block(
263 const void* zero_points, const void* data, const void* scales, size_t num_blocks, size_t block_num_zero_points,
264 size_t block_num_data, size_t block_num_scales) {
265 // Only data is allowed to be sub-byte.
266 KAI_ASSUME_ALWAYS(size_in_bits<ZeroPoint> % 8 == 0);
267 KAI_ASSUME_ALWAYS(size_in_bits<Scale> % 8 == 0);
268
269 // Checks for memory alignment.
270 KAI_ASSUME_ALWAYS(size_in_bits<ZeroPoint> % size_in_bits<Data> == 0);
271 KAI_ASSUME_ALWAYS(
272 (block_num_zero_points * size_in_bits<ZeroPoint> + block_num_data * size_in_bits<Data>) % size_in_bits<Scale> ==
273 0);
274 KAI_ASSUME_ALWAYS(
275 (block_num_data * size_in_bits<Data> + block_num_scales * size_in_bits<Scale>) % size_in_bits<ZeroPoint> == 0);
276
277 Buffer dst(round_up_division(
278 num_blocks *
279 (block_num_zero_points * size_in_bits<ZeroPoint> + block_num_data * size_in_bits<Data> +
280 block_num_scales * size_in_bits<Scale>),
281 8));
282 auto* dst_ptr = dst.data();
283
284 for (size_t block_no = 0; block_no < num_blocks; ++block_no) {
285 for (size_t i = 0; i < block_num_zero_points; ++i) {
286 write_array<ZeroPoint>(
287 dst_ptr, i, read_array<ZeroPoint>(zero_points, block_no * block_num_zero_points + i));
288 }
289 dst_ptr += block_num_zero_points * sizeof(ZeroPoint);
290
291 for (size_t i = 0; i < block_num_data; ++i) {
292 write_array<Data>(dst_ptr, i, read_array<Data>(data, block_no * block_num_data + i));
293 }
294 dst_ptr += round_up_division(block_num_data * size_in_bits<Data>, 8);
295
296 for (size_t i = 0; i < block_num_scales; ++i) {
297 write_array<Scale>(dst_ptr, i, read_array<Scale>(scales, block_no * block_num_scales + i));
298 }
299 dst_ptr += block_num_scales * sizeof(Scale);
300 }
301
302 KAI_ASSERT_ALWAYS(dst_ptr == dst.data() + dst.size());
303
304 return dst;
305}
306
307template Buffer pack_zero_points_data_scales_per_block<int32_t, int8_t, float>(
308 const void* zero_points, const void* data, const void* scales, size_t num_blocks, size_t block_num_zero_points,
309 size_t block_num_data, size_t block_num_scales);
310
311template <typename Data, typename Scale>
312Buffer pack_data_scales_interleave_block(
313 const void* data, const void* scales, size_t height, size_t width, size_t quant_width) {
314 KAI_ASSUME_ALWAYS_IF(size_in_bits<Data> < 8, quant_width % (8 / size_in_bits<Data>) == 0);
315 KAI_ASSUME_ALWAYS_IF(size_in_bits<Data> < 8, width % (8 / size_in_bits<Data>) == 0);
316 KAI_ASSUME_ALWAYS(width % quant_width == 0);
317 KAI_ASSUME_ALWAYS(quant_width % 2 == 0);
318
319 const auto num_quant_packets_x = round_up_multiple(width, quant_width) / quant_width;
320
321 const auto data_bytes = height * width * size_in_bits<Data> / 8;
322 const auto scales_bytes = scales != nullptr ? height * num_quant_packets_x * sizeof(Scale) : 0;
323
324 Buffer dst(data_bytes + scales_bytes);
325
326 const auto* scales_ptr = reinterpret_cast<const Scale*>(scales);
327 auto* dst_ptr = dst.data();
328
329 for (size_t y = 0; y < height; ++y) {
330 for (size_t x_quant = 0; x_quant < width; x_quant += quant_width) {
331 if (scales_ptr != nullptr) {
332 write_array(dst_ptr, 0, *scales_ptr);
333 dst_ptr += sizeof(Scale);
334 ++scales_ptr;
335 }
336
337 for (size_t x_element = 0; x_element < quant_width; ++x_element) {
338 const auto x = x_quant + x_element / 2 + (x_element % 2 != 0 ? quant_width / 2 : 0);
339 write_array(dst_ptr, x_element, read_array<Data>(data, y * width + x));
340 }
341
342 dst_ptr += quant_width * size_in_bits<Data> / 8;
343 }
344 }
345
346 KAI_ASSERT_ALWAYS(dst_ptr == dst.data() + dst.size());
347
348 return dst;
349}
350
351template Buffer pack_data_scales_interleave_block<UInt4, Float16>(
352 const void* data, const void* scales, size_t height, size_t width, size_t quant_width);
353template Buffer pack_data_scales_interleave_block<UInt4, std::nullptr_t>(
354 const void* data, const void* scales, size_t height, size_t width, size_t quant_width);
355
356template <typename Data, typename ZeroPoint, typename Scale, typename Bias>
357Buffer pack_block_data_zero_points_scale_bias(
358 const void* data, const void* zero_points, const void* scales, const void* biases, size_t height, size_t width,
359 size_t quant_height, size_t quant_width, size_t block_height, size_t block_width, size_t interleave_x_blocks) {
360 if (quant_width == width) {
361 quant_width = round_up_multiple(quant_width, block_width);
362 }
363
364 KAI_ASSERT_ALWAYS(quant_height == block_height);
365 KAI_ASSERT_ALWAYS(quant_width % block_width == 0);
366
367 if (interleave_x_blocks == 0) {
368 interleave_x_blocks = quant_width / block_width;
369 }
370
371 const auto has_zero_points = zero_points != nullptr;
372 const auto has_biases = biases != nullptr;
373
374 const auto num_quant_packets_y = round_up_division(height, quant_height);
375 const auto num_quant_packets_x = round_up_division(width, quant_width);
376
377 const auto quant_packet_data_bytes = quant_height * quant_width * size_in_bits<Data> / 8;
378 const auto quant_packet_zero_points_bytes = has_zero_points ? quant_height * sizeof(ZeroPoint) : 0;
379 const auto quant_packet_scales_bytes = quant_height * sizeof(Scale);
380 const auto quant_packet_bytes =
381 quant_packet_zero_points_bytes + quant_packet_data_bytes + quant_packet_scales_bytes;
382
383 const auto num_quant_packets_per_row = round_up_division(width, quant_width);
384 const auto biases_bytes = has_biases ? height * sizeof(Bias) : 0;
385
386 const auto dst_bytes = num_quant_packets_y * num_quant_packets_x * quant_packet_bytes + biases_bytes;
387 Buffer dst(dst_bytes);
388
389 const auto* zero_points_ptr = reinterpret_cast<const ZeroPoint*>(zero_points);
390 const auto* scales_ptr = reinterpret_cast<const Scale*>(scales);
391 const auto* biases_ptr = reinterpret_cast<const Bias*>(biases);
392 auto* dst_ptr = dst.data();
393
394 for (size_t y_quant = 0; y_quant < height; y_quant += quant_height) {
395 for (size_t x_quant = 0; x_quant < width; x_quant += quant_width) {
396 size_t dst_index = 0;
397
398 // Packs the data.
399 for (size_t y_pack = 0; y_pack < quant_height; y_pack += block_height) {
400 for (size_t x_pack = 0; x_pack < block_width * interleave_x_blocks; x_pack += block_width) {
401 for (size_t y_element = 0; y_element < block_height; ++y_element) {
402 for (size_t x_element = 0; x_element < block_width; ++x_element) {
403 for (size_t x_interleave = 0; x_interleave < quant_width;
404 x_interleave += block_width * interleave_x_blocks) {
405 const auto y = y_quant + y_pack + y_element;
406 const auto x = x_quant + x_pack + x_element + x_interleave;
407
408 if (y < height && x < width) {
409 write_array(dst_ptr, dst_index, read_array<Data>(data, y * width + x));
410 }
411
412 ++dst_index;
413 }
414 }
415 }
416 }
417 }
418
419 dst_ptr += dst_index * size_in_bits<Data> / 8;
420
421 // Packs the zero points.
422 if (has_zero_points) {
423 for (size_t y_element = 0; y_element < quant_height; ++y_element) {
424 const auto y = y_quant + y_element;
425 const auto x = x_quant / quant_width;
426 memcpy(dst_ptr, &zero_points_ptr[y * num_quant_packets_per_row + x], sizeof(ZeroPoint));
427 dst_ptr += sizeof(ZeroPoint);
428 }
429 }
430
431 // Packs the scales.
432 for (size_t y_element = 0; y_element < quant_height; ++y_element) {
433 const auto y = y_quant + y_element;
434 const auto x = x_quant / quant_width;
435 memcpy(dst_ptr, &scales_ptr[y * num_quant_packets_per_row + x], sizeof(Scale));
436 dst_ptr += sizeof(Scale);
437 }
438 }
439
440 // Packs the biases.
441 if (has_biases) {
442 for (size_t y_element = 0; y_element < quant_height; ++y_element) {
443 const auto y = y_quant + y_element;
444 memcpy(dst_ptr, &biases_ptr[y], sizeof(Bias));
445 dst_ptr += sizeof(Bias);
446 }
447 }
448 }
449
450 KAI_ASSERT_ALWAYS(dst_ptr == dst.data() + dst.size());
451
452 return dst;
453}
454
455} // namespace kai::test