Elffuss BraidHub

kikocis/kleidiai public

main · 6.5 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/common/data_format.hpp"
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12
13#include "kai/kai_common.h"
14#include "test/common/data_type.hpp"
15#include "test/common/round.hpp"
16
17namespace kai::test {
18
19DataFormat::DataFormat(
20 DataType data_type, size_t block_height, size_t block_width, PackFormat pack_format, DataType zero_point_dt,
21 DataType scale_dt, size_t subblock_height, size_t subblock_width) noexcept :
22 _data_type(data_type),
23 _pack_format(pack_format),
24 _scale_dt(scale_dt),
25 _zero_point_dt(zero_point_dt),
26 _block_height(block_height),
27 _block_width(block_width),
28 _subblock_height(subblock_height),
29 _subblock_width(subblock_width) {
30}
31
32bool DataFormat::operator==(const DataFormat& rhs) const {
33 return _data_type == rhs._data_type && _pack_format == rhs._pack_format && _scale_dt == rhs._scale_dt &&
34 _zero_point_dt == rhs._zero_point_dt && _block_height == rhs._block_height && _block_width == rhs._block_width;
35}
36
37bool DataFormat::operator!=(const DataFormat& rhs) const {
38 return !(*this == rhs);
39}
40
41DataType DataFormat::data_type() const {
42 return _data_type;
43}
44
45DataFormat::PackFormat DataFormat::pack_format() const {
46 return _pack_format;
47}
48
49DataType DataFormat::scale_data_type() const {
50 return _scale_dt;
51}
52
53DataType DataFormat::zero_point_data_type() const {
54 return _zero_point_dt;
55}
56
57bool DataFormat::is_raw() const {
58 return _pack_format == PackFormat::NONE && //
59 _block_height == 0 && _block_width == 0 && _subblock_height == 0 && _subblock_width == 0;
60}
61
62size_t DataFormat::block_height() const {
63 return _block_height;
64}
65
66size_t DataFormat::block_width() const {
67 return _block_width;
68}
69
70size_t DataFormat::subblock_height() const {
71 return _subblock_height;
72}
73
74size_t DataFormat::subblock_width() const {
75 return _subblock_width;
76}
77
78size_t DataFormat::actual_block_height(size_t full_height) const {
79 return _block_height > 0 ? _block_height
80 : round_up_multiple(full_height, _subblock_height > 0 ? _subblock_height : 1);
81}
82
83size_t DataFormat::actual_block_width(size_t full_width) const {
84 return _block_width > 0 ? _block_width : round_up_multiple(full_width, _subblock_width > 0 ? _subblock_width : 1);
85}
86
87size_t DataFormat::actual_subblock_height(size_t full_height) const {
88 return _subblock_height > 0 ? _subblock_height : actual_block_height(full_height);
89}
90
91size_t DataFormat::actual_subblock_width(size_t full_width) const {
92 return _subblock_width > 0 ? _subblock_width : actual_block_width(full_width);
93}
94
95size_t DataFormat::scheduler_block_height([[maybe_unused]] size_t full_height) const {
96 const auto padded_block_height = round_up_multiple(_block_height, _subblock_height > 0 ? _subblock_height : 1);
97
98 switch (_pack_format) {
99 case PackFormat::NONE:
100 return _block_height > 0 ? padded_block_height : 1;
101
102 case PackFormat::BIAS_PER_ROW:
103 case PackFormat::QUANTIZE_PER_ROW:
104 KAI_ASSUME_ALWAYS(_block_height > 0);
105 return padded_block_height;
106
107 default:
108 KAI_ERROR("Unsupported packing format!");
109 }
110}
111
112size_t DataFormat::scheduler_block_width(size_t full_width) const {
113 const auto padded_block_width = round_up_multiple(_block_width, _subblock_width > 0 ? _subblock_width : 1);
114
115 switch (_pack_format) {
116 case PackFormat::NONE:
117 return _block_width > 0 ? padded_block_width : 1;
118
119 case PackFormat::BIAS_PER_ROW:
120 case PackFormat::QUANTIZE_PER_ROW:
121 return full_width;
122
123 default:
124 KAI_ERROR("Unsupported packing format!");
125 }
126}
127
128uintptr_t DataFormat::default_row_stride(size_t width) const {
129 const auto padded_width = round_up_multiple(width, actual_block_width(width));
130
131 switch (_pack_format) {
132 case PackFormat::NONE:
133 return (_block_height > 0 ? _block_height : 1) * padded_width * data_type_size_in_bits(_data_type) / 8;
134
135 case PackFormat::BIAS_PER_ROW:
136 KAI_ASSUME_ALWAYS(_block_height > 0);
137 return _block_height * data_type_size_in_bits(_zero_point_dt) / 8 + //
138 _block_height * padded_width * data_type_size_in_bits(_data_type) / 8;
139
140 case PackFormat::QUANTIZE_PER_ROW:
141 KAI_ASSUME_ALWAYS(_block_height > 0);
142 return _block_height * data_type_size_in_bits(_zero_point_dt) / 8 + //
143 _block_height * padded_width * data_type_size_in_bits(_data_type) / 8 + //
144 _block_height * data_type_size_in_bits(_scale_dt) / 8;
145
146 default:
147 KAI_ERROR("Unsupported packing format!");
148 }
149}
150
151uintptr_t DataFormat::default_offset_in_bytes(size_t row, size_t col, size_t width) const {
152 const auto row_stride = default_row_stride(width);
153 const auto block_width = scheduler_block_width(width);
154
155 KAI_ASSERT_ALWAYS(col % block_width == 0);
156
157 switch (_pack_format) {
158 case PackFormat::NONE:
159 return row * row_stride / (_block_height > 0 ? _block_height : 1) +
160 col * data_type_size_in_bits(_data_type) / 8;
161
162 case PackFormat::BIAS_PER_ROW:
163 case PackFormat::QUANTIZE_PER_ROW:
164 KAI_ASSUME_ALWAYS(row % _block_height == 0);
165 KAI_ASSUME_ALWAYS(col == 0);
166 return (row / _block_height) * row_stride;
167
168 default:
169 KAI_ERROR("Unsupported packing format!");
170 }
171}
172
173size_t DataFormat::default_size_in_bytes(size_t height, size_t width) const {
174 const auto num_rows = _block_height > 0 ? (height + _block_height - 1) / _block_height : height;
175 const auto block_stride = default_row_stride(width);
176 return num_rows * block_stride;
177}
178
179size_t DataFormat::Hash::operator()(const DataFormat& format) const {
180 return //
181 (std::hash<DataType>{}(format._data_type) << 0) ^ //
182 (std::hash<DataFormat::PackFormat>{}(format._pack_format) << 1) ^ //
183 (std::hash<DataType>{}(format._scale_dt) << 2) ^ //
184 (std::hash<DataType>{}(format._zero_point_dt) << 3) ^ //
185 (std::hash<size_t>{}(format._block_height) << 4) ^ //
186 (std::hash<size_t>{}(format._block_width) << 5) ^ //
187 (std::hash<size_t>{}(format._subblock_height) << 6) ^ //
188 (std::hash<size_t>{}(format._subblock_width) << 7); //
189}
190
191} // namespace kai::test