Elffuss BraidHub

kikocis/kleidiai public

main · 7.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#pragma once
8
9#include <cstddef>
10
11#include "test/common/buffer.hpp"
12
13namespace kai::test {
14
15class DataFormat;
16
17/// Packs the matrix.
18///
19/// @param[in] dst_format Data format of the destination matrix.
20/// @param[in] src Data buffer of the source matrix.
21/// @param[in] src_format Data format of the source matrix.
22/// @param[in] height Number of rows of the source matrix.
23/// @param[in] width Number of columns of the source matrix.
24Buffer pack(
25 const DataFormat& dst_format, const void* src, const void* scales, const void* bias, const DataFormat& src_format,
26 size_t height, size_t width);
27
28/// Packs the quantized data and the quantization scale into a single buffer.
29///
30/// ```
31/// Quantized data matrix:
32///
33/// --->|-----------------|<--- Quantization block width
34/// | |
35/// +-----------------+-----------------+----- ...
36/// | q00 q01 q02 q03 | q04 q05 q06 q07 | ........
37/// | q10 q11 q12 q13 | q14 q15 q16 q17 | ........
38/// | q20 q21 q22 q23 | q24 q25 q26 q27 | ........
39/// | q30 q31 q32 q33 | q34 q35 q36 q37 | ........
40/// | ............... | ............... | ........
41/// : ............... : ............... : ........
42///
43/// Quantization scale matrix:
44///
45/// +-----+-----+-- ...
46/// | s00 | s01 | .....
47/// | s10 | s11 | .....
48/// | s20 | s21 | .....
49/// | s30 | s31 | .....
50/// | ... | ... | .....
51/// : ... : ... : .....
52/// ```
53///
54/// The packed data has each quantization scale followed by the quantized block row.
55///
56/// ```
57/// Packed data:
58///
59/// +-----+-----------------+-----+-----------------+----- ...
60/// | s00 | q00 q01 q02 q03 | s01 | q04 q05 q06 q07 | ........
61/// | s10 | q10 q11 q12 q13 | s11 | q14 q15 q16 q17 | ........
62/// | s20 | q20 q21 q22 q23 | s21 | q24 q25 q26 q27 | ........
63/// | s30 | q30 q31 q32 q33 | s31 | q34 q35 q36 q37 | ........
64/// | ... | ............... | ... | ............... | ........
65/// : ... : ............... : ... : ............... : ........
66/// ```
67///
68/// @tparam Data The data type of the quantized value.
69/// @tparam Scale The data type of the quantization scale.
70///
71/// @param[in] data The quantized data.
72/// @param[in] scales The quantization scales.
73/// @param[in] height The number of rows.
74/// @param[in] width The number of columns.
75/// @param[in] quant_width The number of columns of the quantization block.
76///
77/// @return The packed data buffer.
78template <typename Data, typename Scale>
79Buffer pack_data_scales(const void* data, const void* scales, size_t height, size_t width, size_t quant_width);
80
81/// Packs the zero point, data and scale into a single buffer.
82///
83/// ```
84/// Data matrix:
85///
86/// +-----------------+
87/// | q00 q01 q02 q03 |
88/// | q10 q11 q12 q13 |
89/// | q20 q21 q22 q23 |
90/// | q30 q31 q32 q33 |
91/// | ............... |
92/// : ............... :
93///
94/// Scales for each row:
95///
96/// +----+
97/// | s0 |
98/// | s1 |
99/// | s2 |
100/// | s3 |
101/// | .. |
102/// : .. :
103///
104/// Zero points for each row:
105///
106/// +----+
107/// | z0 |
108/// | z1 |
109/// | z2 |
110/// | z3 |
111/// | .. |
112/// : .. :
113/// ```
114///
115/// The packed data has each zero point followed by the data row followed by the scale.
116///
117/// ```
118/// Packed data:
119///
120/// +----+-----------------+----+
121/// | z0 | q00 q01 q02 q03 | s0 |
122/// | z1 | q10 q11 q12 q13 | s1 |
123/// | z2 | q20 q21 q22 q23 | s2 |
124/// | z3 | q30 q31 q32 q33 | s3 |
125/// | .. | ............... | .. |
126/// : .. : ............... : .. :
127/// ```
128///
129/// @tparam Data The data type of the data.
130/// @tparam Scale The data type of the scale.
131/// @tparam ZeroPoint The data type of the zero point.
132///
133/// @param[in] data The data buffer.
134/// @param[in] scales The scales buffer.
135/// @param[in] zero_points The zero points buffer.
136/// @param[in] height The number of rows.
137/// @param[in] width The number of columns.
138///
139/// @return The packed data buffer.
140template <typename ZeroPoint, typename Data, typename Scale>
141Buffer pack_zero_points_data_scales_per_block(
142 const void* zero_points, const void* data, const void* scales, size_t num_blocks, size_t block_num_zero_points,
143 size_t block_num_data, size_t block_num_scales);
144
145/// Packs the quantized data and the quantization scale into a single buffer.
146///
147/// ```
148/// Quantized data matrix:
149///
150/// --->|-----------------|<--- Quantization block width
151/// | |
152/// +-----------------+-----------------+----- ...
153/// | q00 q01 q02 q03 | q04 q05 q06 q07 | ........
154/// | q10 q11 q12 q13 | q14 q15 q16 q17 | ........
155/// | q20 q21 q22 q23 | q24 q25 q26 q27 | ........
156/// | q30 q31 q32 q33 | q34 q35 q36 q37 | ........
157/// | ............... | ............... | ........
158/// : ............... : ............... : ........
159///
160/// Quantization scale matrix:
161///
162/// +-----+-----+-- ...
163/// | s00 | s01 | .....
164/// | s10 | s11 | .....
165/// | s20 | s21 | .....
166/// | s30 | s31 | .....
167/// | ... | ... | .....
168/// : ... : ... : .....
169/// ```
170///
171/// The packed data has each quantization scale followed by the quantized block row.
172///
173/// This function is different from @ref pack_data_scales that in this packing method
174/// the quantized data row is splitted into two halves and they are interleaved together.
175///
176/// ```
177/// Packed data:
178///
179/// +-----+-----------------+-----+-----------------+----- ...
180/// | s00 | q00 q02 q01 q03 | s01 | q04 q06 q05 q07 | ........
181/// | s10 | q10 q12 q11 q13 | s11 | q14 q16 q15 q17 | ........
182/// | s20 | q20 q22 q21 q23 | s21 | q24 q26 q25 q27 | ........
183/// | s30 | q30 q32 q31 q33 | s31 | q34 q36 q35 q37 | ........
184/// | ... | ............... | ... | ............... | ........
185/// : ... : ............... : ... : ............... : ........
186/// ```
187///
188/// @tparam Data The data type of the quantized value.
189/// @tparam Scale The data type of the quantization scale.
190///
191/// @param[in] data The quantized data.
192/// @param[in] scales The quantization scales.
193/// @param[in] height The number of rows.
194/// @param[in] width The number of columns.
195/// @param[in] quant_width The number of columns of the quantization block.
196///
197/// @return The packed data buffer.
198template <typename Data, typename Scale>
199Buffer pack_data_scales_interleave_block(
200 const void* data, const void* scales, size_t height, size_t width, size_t quant_width);
201
202/// Packs the quantized data with two halves of a block interleaved.
203///
204/// ```
205/// Quantized data matrix:
206///
207/// --->|-----------------|<--- Block width
208/// | |
209/// +-----------------+-----------------+----- ...
210/// | q00 q01 q02 q03 | q04 q05 q06 q07 | ........
211/// | q10 q11 q12 q13 | q14 q15 q16 q17 | ........
212/// | q20 q21 q22 q23 | q24 q25 q26 q27 | ........
213/// | q30 q31 q32 q33 | q34 q35 q36 q37 | ........
214/// | ............... | ............... | ........
215/// : ............... : ............... : ........
216///
217/// Packed data:
218///
219/// +-----------------+-----------------+----- ...
220/// | q00 q02 q01 q03 | q04 q06 q05 q07 | ........
221/// | q10 q12 q11 q13 | q14 q16 q15 q17 | ........
222/// | q20 q22 q21 q23 | q24 q26 q25 q27 | ........
223/// | q30 q32 q31 q33 | q34 q36 q35 q37 | ........
224/// | ............... | ............... | ........
225/// : ............... : ............... : ........
226/// ```
227///
228/// @tparam Data The data type of the quantized value.
229///
230/// @param[in] data The quantized data.
231/// @param[in] height The number of rows.
232/// @param[in] width The number of columns.
233/// @param[in] block_width The number of columns in a block.
234///
235/// @return The packed data buffer.
236template <typename Data>
237Buffer pack_data_interleave_block(const void* data, size_t height, size_t width, size_t block_width) {
238 return pack_data_scales_interleave_block<Data, std::nullptr_t>(data, nullptr, height, width, block_width);
239}
240
241} // namespace kai::test