Elffuss BraidHub

kikocis/kleidiai public

main · 11.7 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 <cstdint>
11
12#include "test/common/buffer.hpp"
13#include "test/common/data_type.hpp"
14
15namespace kai::test {
16
17class DataFormat;
18
19/// Packs the RHS operand of matrix multiplication.
20///
21/// @param[in] data Data buffer.
22/// @param[in] scales (Optional) Quantization scales.
23/// @param[in] zero_points (Optional) Quantization zero points.
24/// @param[in] src_format Data format of the RHS matrix.
25/// @param[in] dst_format Data format of the packed RHS matrix.
26/// @param[in] n Number of non-transposed columns.
27/// @param[in] k Number of non-transposed rows.
28/// @param[in] transposing Perform transpose then pack.
29///
30/// @return The packed RHS matrix.
31Buffer matmul_pack_rhs(
32 const void* data, const void* scales, const void* zero_points, const DataFormat& src_format,
33 const DataFormat& dst_format, size_t n, size_t k, bool transposing);
34
35/// Matrix multiplication.
36///
37/// @param[in] lhs LHS operand data.
38/// @param[in] lhs_scales (Optional) LHS operand quantization scales.
39/// @param[in] lhs_zero_points (Optional) LHS operand quantization zero point.
40/// @param[in] lhs_dt LHS operand data type.
41/// @param[in] rhs RHS operand data.
42/// @param[in] rhs_scales (Optional) RHS operand quantization scales.
43/// @param[in] rhs_zero_points (Optional) RHS operand quantization zero point.
44/// @param[in] rhs_dt RHS operand data type.
45/// @param[in] bias Bias operand data.
46/// @param[in] bias_scales (Optional) Bias operand quantization scales.
47/// @param[in] bias_zero_points (Optional) Bias operand quantization zero point.
48/// @param[in] bias_dt Bias operand data type.
49/// @param[in] dst Output data.
50/// @param[in] dst_scales (Optional) Output quantization scales.
51/// @param[in] dst_zero_points (Optional) Output quantization zero point.
52/// @param[in] dst_dt Output data type.
53/// @param[in] m Output height.
54/// @param[in] n Output width.
55/// @param[in] k Non-transposed LHS width and non-transposed RHS height.
56/// @param[in] lhs_transposed `true` if LHS operand is transposed.
57/// @param[in] rhs_transposed `true` if RHS operand is transposed.
58/// @param[in] accumulator_dt Optional accumulator data type. UNKNOWN uses the default reference arithmetic.
59///
60/// @return The result data buffer.
61Buffer matmul(
62 const void* lhs, const void* lhs_scales, const void* lhs_zero_points, DataType lhs_dt, //
63 const void* rhs, const void* rhs_scales, const void* rhs_zero_points, DataType rhs_dt, //
64 const void* bias, const void* bias_scales, const void* bias_zero_points, DataType bias_dt, //
65 DataType dst_dt, //
66 size_t m, size_t n, size_t k, //
67 bool lhs_transposed, bool rhs_transposed, DataType accumulator_dt = DataType::UNKNOWN);
68
69/// Indirect matrix multiplication.
70///
71/// @param[in] lhs_idata The indirect LHS data matrix.
72/// @param[in] lhs_scales (Optional) LHS operand quantization scales.
73/// @param[in] lhs_offset The indirection LHS data matrix offset, applied to non-padding pointers
74/// @param[in] lhs_padding_ptr The indirection LHS padding chunk pointer
75/// @param[in] lhs_zero_points (Optional) LHS operand quantization zero point.
76/// @param[in] lhs_dt LHS operand data type.
77/// @param[in] rhs RHS operand data.
78/// @param[in] rhs_scales (Optional) RHS operand quantization scales.
79/// @param[in] rhs_zero_points (Optional) RHS operand quantization zero point.
80/// @param[in] rhs_dt RHS operand data type.
81/// @param[in] bias Bias operand data.
82/// @param[in] bias_scales (Optional) Bias operand quantization scales.
83/// @param[in] bias_zero_points (Optional) Bias operand quantization zero point.
84/// @param[in] bias_dt Bias operand data type.
85/// @param[in] dst_dt Output data type.
86/// @param[in] m Output height.
87/// @param[in] n Output width.
88/// @param[in] k_chunk_count Number pointers per row in lhs_idata
89/// @param[in] k_chunk_size Number of elements in each LHS K chunk
90///
91/// @return The result data buffer.
92Buffer indirect_matmul(
93 const void* const* lhs_idata, uintptr_t lhs_offset, const void* lhs_padding_ptr, const void* lhs_scales,
94 const void* lhs_zero_points, DataType lhs_dt, //
95 const void* rhs, const void* rhs_scales, const void* rhs_zero_points, DataType rhs_dt, //
96 const void* bias, const void* bias_scales, const void* bias_zero_points, DataType bias_dt, //
97 DataType dst_dt, //
98 size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length);
99
100/// Matrix multiplication with quantized input and floating-point output.
101///
102/// The LHS matrix is non-transposed and the RHS matrix is transposed.
103///
104/// @tparam LhsData The data type of the LHS matrix.
105/// @tparam LhsScale The data type of the quantization scales of the LHS matrix.
106/// @tparam LhsZeroPoint The data type of the quantization zero points of the LHS matrix.
107/// @tparam Rhsdata The data type of the RHS matrix.
108/// @tparam RhsScale The data type of the quantization scales of the RHS matrix.
109/// @tparam RhsZeroPoint The data type of the quantization zero points of the RHS matrix.
110/// @tparam Bias The data type of the bias vector.
111/// @tparam IntAcc The data type of the intermediate integer accumulator.
112/// @tparam DstData The data type of the floating-point accumulator and the output matrix.
113///
114/// @param[in] m The LHS and output height.
115/// @param[in] n The RHS height and output width.
116/// @param[in] k The LHS and RHS width.
117/// @param[in] lhs_data The LHS data matrix.
118/// @param[in] lhs_scales The LHS quantization scales matrix.
119/// @param[in] lhs_zero_points The LHS quantization zero points matrix.
120/// @param[in] lhs_quant_width The LHS quantization block width.
121/// @param[in] rhs_data The RHS data matrix.
122/// @param[in] rhs_scales The RHS quantization scales matrix.
123/// @param[in] rhs_zero_points The RHS quantization zero points matrix.
124/// @param[in] rhs_quant_width The RHS quantization block width.
125/// @param[in] biases The biases vector.
126/// @param[in] min_value The minimum output value.
127/// @param[in] max_value The maximum output value.
128///
129/// @return The output matrix.
130template <
131 typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale,
132 typename RhsZeroPoint, typename Bias, typename IntAcc, typename DstData>
133Buffer matmul_clamp_nt_t(
134 size_t m, size_t n, size_t k, //
135 const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, //
136 const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, //
137 const void* biases, //
138 DstData min_value, DstData max_value);
139
140/// Matrix multiplication with quantized input and floating-point output.
141///
142/// The LHS matrix is non-transposed and the RHS matrix is non-transposed.
143///
144/// @tparam LhsData The data type of the LHS matrix.
145/// @tparam LhsScale The data type of the quantization scales of the LHS matrix.
146/// @tparam LhsZeroPoint The data type of the quantization zero points of the LHS matrix.
147/// @tparam Rhsdata The data type of the RHS matrix.
148/// @tparam RhsScale The data type of the quantization scales of the RHS matrix.
149/// @tparam RhsZeroPoint The data type of the quantization zero points of the RHS matrix.
150/// @tparam Bias The data type of the bias vector.
151/// @tparam IntAcc The data type of the intermediate integer accumulator.
152/// @tparam DstData The data type of the floating-point accumulator and the output matrix.
153///
154/// @param[in] m The LHS and output height.
155/// @param[in] n The RHS height and output width.
156/// @param[in] k The LHS and RHS width.
157/// @param[in] k_chunk_count Number of K chunk pointers per row in lhs_ptrs matrix
158/// @param[in] k_chunk_length Lenght of each K chunk pointed to in lhs_ptrs matrix
159/// @param[in] lhs_data The LHS data matrix.
160/// @param[in] lhs_ptrs The indirect LHS data matrix.
161/// @param[in] lhs_offset The indirection LHS data matrix offset, applied to non-padding pointers
162/// @param[in] lhs_padding_ptr The indirection LHS padding chunk pointer
163/// @param[in] lhs_scales The LHS quantization scales matrix.
164/// @param[in] lhs_zero_points The LHS quantization zero points matrix.
165/// @param[in] lhs_quant_width The LHS quantization block width.
166/// @param[in] rhs_data The RHS data matrix.
167/// @param[in] rhs_scales The RHS quantization scales matrix.
168/// @param[in] rhs_zero_points The RHS quantization zero points matrix.
169/// @param[in] rhs_quant_width The RHS quantization block width.
170/// @param[in] biases The biases vector.
171/// @param[in] min_value The minimum output value.
172/// @param[in] max_value The maximum output value.
173///
174/// @return The output matrix.
175template <
176 typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale,
177 typename RhsZeroPoint, typename Bias, typename IntAcc, typename DstData>
178Buffer matmul_clamp_nt_nt(
179 size_t m, size_t n, size_t k, //
180 const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, //
181 const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, //
182 const void* biases, //
183 DstData min_value, DstData max_value);
184
185template <
186 typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale,
187 typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData>
188Buffer matmul_nt_t_quantized(
189 size_t m, size_t n, size_t k, //
190 const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height,
191 size_t lhs_quant_width, //
192 const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height,
193 size_t rhs_quant_width, //
194 const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width);
195
196template <
197 typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale,
198 typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData>
199Buffer matmul_nt_nt_quantized(
200 size_t m, size_t n, size_t k, //
201 const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height,
202 size_t lhs_quant_width, //
203 const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height,
204 size_t rhs_quant_width, //
205 const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width);
206
207template <
208 typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale,
209 typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData>
210Buffer indirect_matmul_nt_t_quantized(
211 size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, //
212 const void* const* lhs_ptrs, uintptr_t lhs_offset, const void* lhs_padding_ptr, const void* lhs_scales,
213 const void* lhs_zero_points, size_t lhs_quant_height,
214 size_t lhs_quant_width, //
215 const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height,
216 size_t rhs_quant_width, //
217 const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width);
218
219} // namespace kai::test