Elffuss BraidHub

kikocis/kleidiai public

main · 2.3 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#include "test/nextgen/reference/print.hpp"
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <numeric>
13#include <ostream>
14#include <string>
15
16#include "test/common/assert.hpp"
17#include "test/common/data_type.hpp"
18#include "test/common/float16.hpp"
19#include "test/common/int4.hpp"
20#include "test/common/memory.hpp"
21#include "test/common/round.hpp"
22#include "test/common/span.hpp"
23
24namespace kai::test {
25
26namespace {
27
28template <typename T>
29void print_impl(std::ostream& os, Shape shape, Span<const std::byte> data, size_t level = 0) {
30 const std::string indent(level * 2, ' ');
31 const size_t len = shape.at(0);
32
33 if (shape.size() == 1) {
34 os << indent << "[";
35
36 for (size_t i = 0; i < len; ++i) {
37 const T value = read_array<T>(data, i);
38 os << displayable(value) << ", ";
39 }
40
41 os << "]";
42 } else {
43 const size_t row_size = round_up_division(shape.at(shape.size() - 1) * size_in_bits<T>, 8);
44 const size_t num_rows = std::accumulate(shape.begin() + 1, shape.end() - 1, size_t{1}, std::multiplies<>());
45 const size_t stride = num_rows * row_size;
46
47 os << indent << "[\n";
48
49 for (size_t i = 0; i < len; ++i) {
50 print_impl<T>(os, shape.subspan(1), data.subspan(i * stride), level + 1);
51 os << ",\n";
52 }
53
54 os << indent << "]";
55 }
56}
57
58template <typename T>
59void print_array(std::ostream& os, Shape shape, Span<const std::byte> data, size_t level) {
60 print_impl<T>(os, shape, data, level);
61}
62
63} // namespace
64
65PrintFn make_print_array(DataType dtype) {
66 switch (dtype) {
67 case DataType::FP32:
68 return print_array<float>;
69
70 case DataType::FP16:
71 return print_array<Float16>;
72
73 case DataType::I32:
74 return print_array<int32_t>;
75
76 case DataType::I8:
77 return print_array<int8_t>;
78
79 case DataType::U8:
80 return print_array<uint8_t>;
81
82 case DataType::U4:
83 return print_array<UInt4>;
84
85 case DataType::I4:
86 return print_array<Int4>;
87
88 default:
89 KAI_TEST_ERROR("Not supported.");
90 }
91}
92
93} // namespace kai::test