| 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 | #include "test/reference/matmul.hpp" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <array> |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <type_traits> |
| 14 | |
| 15 | #include "kai/kai_common.h" |
| 16 | #include "test/common/buffer.hpp" |
| 17 | #include "test/common/data_format.hpp" |
| 18 | #include "test/common/data_type.hpp" |
| 19 | #include "test/common/float16.hpp" |
| 20 | #include "test/common/int2.hpp" |
| 21 | #include "test/common/int4.hpp" |
| 22 | #include "test/common/memory.hpp" |
| 23 | #include "test/common/round.hpp" |
| 24 | #include "test/reference/binary_elementwise.hpp" |
| 25 | #include "test/reference/pack.hpp" |
| 26 | #include "test/reference/reduce.hpp" |
| 27 | #include "test/reference/transpose.hpp" |
| 28 | |
| 29 | namespace kai::test { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | struct Stride { |
| 34 | size_t row_stride; |
| 35 | size_t col_stride; |
| 36 | |
| 37 | [[nodiscard]] size_t operator()(size_t row, size_t col) const { |
| 38 | return row * row_stride + col * col_stride; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | /// Matrix multiplication. |
| 43 | /// |
| 44 | /// @tparam In LHS and RHS data type. |
| 45 | /// @tparam Acc Accumulator data type. |
| 46 | /// @tparam Out Output data type. |
| 47 | /// @tparam Bias Bias data type. |
| 48 | /// |
| 49 | /// @param[in] lhs LHS operand data buffer. |
| 50 | /// @param[in] rhs RHS operand data buffer. |
| 51 | /// @param[in] bias Bias data buffer. |
| 52 | /// @param[in] m Output height. |
| 53 | /// @param[in] n Output width. |
| 54 | /// @param[in] k Non-transposed LHS width and non-transposed RHS height. |
| 55 | /// @param[in] lhs_stride Stride for accessing the LHS matrix. |
| 56 | /// @param[in] rhs_stride Stride for accessing the RHS matrix. |
| 57 | /// |
| 58 | /// @return The result data buffer. |
| 59 | template <typename In, typename Acc = In, typename Out = In, typename Bias = Acc> |
| 60 | Buffer matmul_any_type( |
| 61 | const void* lhs, const void* rhs, const void* bias, // Input buffers |
| 62 | size_t m, size_t n, size_t k, // Problem shape |
| 63 | const Stride& lhs_stride, const Stride& rhs_stride // Input buffer traversal |
| 64 | ) { |
| 65 | Buffer dst(m * n * size_in_bits<Out> / 8); |
| 66 | KAI_ASSUME_ALWAYS(n * size_in_bits<Out> % 8 == 0); |
| 67 | |
| 68 | for (size_t im = 0; im < m; ++im) { |
| 69 | for (size_t in = 0; in < n; ++in) { |
| 70 | Acc acc = bias != nullptr ? static_cast<Acc>(read_array<Bias>(bias, in)) : Acc(0); |
| 71 | |
| 72 | for (size_t ik = 0; ik < k; ++ik) { |
| 73 | const auto lhs_value = read_array<In>(lhs, lhs_stride(im, ik)); |
| 74 | const auto rhs_value = read_array<In>(rhs, rhs_stride(in, ik)); |
| 75 | acc += static_cast<Acc>(lhs_value) * static_cast<Acc>(rhs_value); |
| 76 | } |
| 77 | |
| 78 | write_array<Out>(dst.data(), im * n + in, static_cast<Out>(acc)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return dst; |
| 83 | } |
| 84 | |
| 85 | /// Function declaration helper. Template arguments does not affect type signature |
| 86 | using MatMulAnyFn = decltype(&matmul_any_type<float>); |
| 87 | |
| 88 | /// Mapping object that maps `DataType` to template instantiation |
| 89 | struct MatMulEntry { |
| 90 | DataType lhs_dt; |
| 91 | DataType rhs_dt; |
| 92 | DataType bias_dt; |
| 93 | DataType acc_dt; |
| 94 | DataType dst_dt; |
| 95 | MatMulAnyFn fn; |
| 96 | }; |
| 97 | |
| 98 | template <DataType DType, typename Type> |
| 99 | struct TypedData { |
| 100 | static constexpr DataType dtype = DType; |
| 101 | using type = Type; |
| 102 | }; |
| 103 | |
| 104 | using FP32 = TypedData<DataType::FP32, float>; |
| 105 | using FP16 = TypedData<DataType::FP16, Float16>; |
| 106 | |
| 107 | template <typename Lhs, typename Rhs, typename Bias, typename Acc, typename Dst> |
| 108 | constexpr MatMulEntry make_matmul_entry() { |
| 109 | return { |
| 110 | Lhs::dtype, Rhs::dtype, |
| 111 | Bias::dtype, Acc::dtype, |
| 112 | Dst::dtype, &matmul_any_type<typename Lhs::type, typename Acc::type, typename Dst::type, typename Bias::type>, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | } // namespace |
| 117 | |
| 118 | Buffer matmul_pack_rhs( |
| 119 | const void* data, const void* scales, const void* zero_points, const DataFormat& src_format, |
| 120 | const DataFormat& dst_format, size_t n, size_t k, bool transposing) { |
| 121 | const auto src_dt = src_format.data_type(); |
| 122 | const auto src_pf = src_format.pack_format(); |
| 123 | |
| 124 | const auto dst_dt = dst_format.data_type(); |
| 125 | const auto dst_pf = dst_format.pack_format(); |
| 126 | |
| 127 | Buffer tmp_data; |
| 128 | Buffer tmp_scales; |
| 129 | Buffer tmp_zero_points; |
| 130 | |
| 131 | if (transposing) { |
| 132 | tmp_data = transpose(data, src_dt, k, n); |
| 133 | data = tmp_data.data(); |
| 134 | } |
| 135 | |
| 136 | if (src_dt == DataType::QSU4 && src_pf == DataFormat::PackFormat::NONE && // |
| 137 | dst_dt == DataType::QSI4 && dst_pf == DataFormat::PackFormat::QUANTIZE_PER_ROW) { |
| 138 | // For this specific RHS format conversion: |
| 139 | // |
| 140 | // * 4-bit data is added by 8. |
| 141 | // * Scale is divided by 16. |
| 142 | // * Zero point is accumulation of all values in the same row. |
| 143 | |
| 144 | KAI_ASSUME_ALWAYS(zero_points == nullptr); |
| 145 | const int32_t zero_point = 8; |
| 146 | const uint8_t zero_point_i4 = UInt4::pack_u8(UInt4(zero_point), UInt4(zero_point)); |
| 147 | const int32_t row_zero_point = zero_point * static_cast<int32_t>(k); |
| 148 | |
| 149 | KAI_ASSUME_ALWAYS(dst_format.subblock_width() > 0); |
| 150 | const auto subblock_width_i32 = static_cast<int32_t>(dst_format.subblock_width()); |
| 151 | const auto subblock_width_f = static_cast<float>(dst_format.subblock_width()); |
| 152 | |
| 153 | tmp_zero_points = reduce_add(data, src_format, n, k, DataFormat(DataType::I32), 0); |
| 154 | tmp_zero_points = sub(tmp_zero_points.data(), DataType::I32, n, 1, &row_zero_point, DataType::I32, 1, 1); |
| 155 | tmp_zero_points = mul(tmp_zero_points.data(), DataType::I32, n, 1, &subblock_width_i32, DataType::I32, 1, 1); |
| 156 | zero_points = tmp_zero_points.data(); |
| 157 | |
| 158 | tmp_data = add(data, DataType::QSU4, n, k, &zero_point_i4, DataType::QSU4, 1, 1); |
| 159 | data = tmp_data.data(); |
| 160 | |
| 161 | tmp_scales = div(scales, DataType::FP32, n, 1, &subblock_width_f, DataType::FP32, 1, 1); |
| 162 | scales = tmp_scales.data(); |
| 163 | } |
| 164 | |
| 165 | return pack(dst_format, data, scales, zero_points, src_format, n, k); |
| 166 | } |
| 167 | |
| 168 | Buffer matmul( |
| 169 | const void* lhs, [[maybe_unused]] const void* lhs_scales, [[maybe_unused]] const void* lhs_zero_points, |
| 170 | DataType lhs_dt, // |
| 171 | const void* rhs, [[maybe_unused]] const void* rhs_scales, [[maybe_unused]] const void* rhs_zero_points, |
| 172 | DataType rhs_dt, // |
| 173 | const void* bias, const void* bias_scales, const void* bias_zero_points, DataType bias_dt, // |
| 174 | DataType dst_dt, // |
| 175 | size_t m, size_t n, size_t k, // |
| 176 | bool lhs_transposed, bool rhs_transposed, DataType accumulator_dt) { |
| 177 | KAI_ASSUME_ALWAYS(lhs_scales == nullptr); |
| 178 | KAI_ASSUME_ALWAYS(lhs_zero_points == nullptr); |
| 179 | KAI_ASSUME_ALWAYS(rhs_scales == nullptr); |
| 180 | KAI_ASSUME_ALWAYS(rhs_zero_points == nullptr); |
| 181 | KAI_ASSUME_ALWAYS(bias_scales == nullptr); |
| 182 | KAI_ASSUME_ALWAYS(bias_zero_points == nullptr); |
| 183 | KAI_ASSUME_ALWAYS(bias_dt == DataType::UNKNOWN || !data_type_is_quantized(bias_dt)); |
| 184 | |
| 185 | const auto matmul_acc_dt = data_type_default(accumulator_dt, DataType::FP32); |
| 186 | const auto matmul_bias_dt = data_type_default(bias_dt, dst_dt); |
| 187 | |
| 188 | const Stride lhs_stride{lhs_transposed ? 1 : k, lhs_transposed ? m : 1}; |
| 189 | const Stride rhs_stride{rhs_transposed ? k : 1, rhs_transposed ? 1 : n}; |
| 190 | |
| 191 | static constexpr std::array matmul_entries = { |
| 192 | make_matmul_entry<FP32, FP32, FP32, FP32, FP32>(), |
| 193 | make_matmul_entry<FP16, FP16, FP16, FP32, FP16>(), |
| 194 | make_matmul_entry<FP16, FP16, FP32, FP32, FP16>(), |
| 195 | }; |
| 196 | |
| 197 | for (const auto& [entry_lhs_dt, entry_rhs_dt, entry_bias_dt, entry_acc_dt, entry_dst_dt, matmul_fn] : |
| 198 | matmul_entries) { |
| 199 | if (lhs_dt == entry_lhs_dt && rhs_dt == entry_rhs_dt && matmul_bias_dt == entry_bias_dt && |
| 200 | matmul_acc_dt == entry_acc_dt && dst_dt == entry_dst_dt) { |
| 201 | return matmul_fn(lhs, rhs, bias, m, n, k, lhs_stride, rhs_stride); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | KAI_ERROR("Matmul data type combination is not implemented!"); |
| 206 | } |
| 207 | |
| 208 | Buffer indirect_matmul( |
| 209 | const void* const* lhs_idata, uintptr_t lhs_offset, const void* lhs_padding_ptr, const void* lhs_scales, |
| 210 | const void* lhs_zero_points, |
| 211 | DataType lhs_dt, // |
| 212 | const void* rhs, const void* rhs_scales, const void* rhs_zero_points, |
| 213 | DataType rhs_dt, // |
| 214 | const void* bias, const void* bias_scales, const void* bias_zero_points, DataType bias_dt, // |
| 215 | DataType dst_dt, // |
| 216 | size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length) { |
| 217 | // This is inefficient, but allows code-reuse |
| 218 | const size_t chunk_bytes = k_chunk_length * round_up_division(data_type_size_in_bits(lhs_dt), 8); |
| 219 | const size_t n_chunks = m * k_chunk_count; |
| 220 | Buffer lhs(n_chunks * chunk_bytes); |
| 221 | |
| 222 | const uintptr_t lhs_padding_ptr_uint = reinterpret_cast<uintptr_t>(lhs_padding_ptr); |
| 223 | |
| 224 | // Copy all chunks to the created matrix |
| 225 | for (size_t i = 0; i < n_chunks; i += 1) { |
| 226 | uintptr_t src_pointer = reinterpret_cast<uintptr_t>(lhs_idata[i]); |
| 227 | if (src_pointer != lhs_padding_ptr_uint) { |
| 228 | src_pointer += lhs_offset; |
| 229 | } |
| 230 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 231 | memcpy(lhs.data() + i * chunk_bytes, reinterpret_cast<const void*>(src_pointer), chunk_bytes); |
| 232 | } |
| 233 | |
| 234 | return matmul( |
| 235 | lhs.data(), lhs_scales, lhs_zero_points, lhs_dt, // |
| 236 | rhs, rhs_scales, rhs_zero_points, rhs_dt, // |
| 237 | bias, bias_scales, bias_zero_points, bias_dt, // |
| 238 | dst_dt, m, n, k_chunk_count * k_chunk_length, false, false); |
| 239 | } |
| 240 | |
| 241 | template < |
| 242 | typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale, |
| 243 | typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData> |
| 244 | Buffer indirect_matmul_nt_t_quantized( |
| 245 | size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, // |
| 246 | const void* const* lhs_ptrs, uintptr_t lhs_offset, const void* lhs_padding_ptr, const void* lhs_scales, |
| 247 | const void* lhs_zero_points, size_t lhs_quant_height, |
| 248 | size_t lhs_quant_width, // |
| 249 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 250 | size_t rhs_quant_width, // |
| 251 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width) { |
| 252 | KAI_ASSUME_ALWAYS(lhs_quant_width != 0); |
| 253 | KAI_ASSUME_ALWAYS(rhs_quant_width != 0); |
| 254 | KAI_ASSUME_ALWAYS(lhs_quant_height != 0); |
| 255 | KAI_ASSUME_ALWAYS(rhs_quant_height != 0); |
| 256 | KAI_ASSUME_ALWAYS(bias_quant_width != 0); |
| 257 | const auto lhs_num_quant_per_row = round_up_division(k_chunk_count * k_chunk_length, lhs_quant_width); |
| 258 | const auto rhs_num_quant_per_row = round_up_division(k_chunk_count * k_chunk_length, rhs_quant_width); |
| 259 | |
| 260 | Buffer dst(m * n * sizeof(DstData)); |
| 261 | |
| 262 | for (size_t i_m = 0; i_m < m; ++i_m) { |
| 263 | for (size_t i_n = 0; i_n < n; ++i_n) { |
| 264 | DstData acc = 0; |
| 265 | |
| 266 | for (size_t i_k_chunk = 0; i_k_chunk < k_chunk_count; ++i_k_chunk) { |
| 267 | // Calculate the K chunk pointer. Apply offset if this is not padding |
| 268 | const size_t k_chunk_idx = i_m * k_chunk_count + i_k_chunk; |
| 269 | const void* k_chunk_ptr = lhs_ptrs[k_chunk_idx]; |
| 270 | if (k_chunk_ptr != lhs_padding_ptr) { |
| 271 | k_chunk_ptr = reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(k_chunk_ptr) + lhs_offset); |
| 272 | } |
| 273 | |
| 274 | for (size_t i_k_chunk_len = 0; i_k_chunk_len < k_chunk_length; ++i_k_chunk_len) { |
| 275 | const size_t i = i_k_chunk * k_chunk_length + i_k_chunk_len; |
| 276 | |
| 277 | const auto lhs_data_index = i_k_chunk_len; |
| 278 | const auto lhs_quant_index = (i_m / lhs_quant_height) * lhs_num_quant_per_row + i / lhs_quant_width; |
| 279 | const auto lhs_value = read_array<LhsData>(k_chunk_ptr, lhs_data_index); |
| 280 | const auto lhs_scale = lhs_scales != nullptr ? read_array<LhsScale>(lhs_scales, lhs_quant_index) |
| 281 | : static_cast<LhsScale>(1); |
| 282 | const auto lhs_zero_point = lhs_zero_points != nullptr |
| 283 | ? read_array<LhsZeroPoint>(lhs_zero_points, lhs_quant_index) |
| 284 | : static_cast<LhsZeroPoint>(0); |
| 285 | |
| 286 | const auto rhs_data_index = i_n * (k_chunk_count * k_chunk_length) + i; |
| 287 | const auto rhs_quant_index = (i_n / rhs_quant_height) * rhs_num_quant_per_row + i / rhs_quant_width; |
| 288 | const auto rhs_value = read_array<RhsData>(rhs_data, rhs_data_index); |
| 289 | const auto rhs_scale = rhs_scales != nullptr ? read_array<RhsScale>(rhs_scales, rhs_quant_index) |
| 290 | : static_cast<RhsScale>(1); |
| 291 | const auto rhs_zero_point = rhs_zero_points != nullptr |
| 292 | ? read_array<RhsZeroPoint>(rhs_zero_points, rhs_quant_index) |
| 293 | : static_cast<RhsZeroPoint>(0); |
| 294 | |
| 295 | acc += (static_cast<DstData>(lhs_value) - static_cast<DstData>(lhs_zero_point)) * |
| 296 | static_cast<DstData>(lhs_scale) * |
| 297 | (static_cast<DstData>(rhs_value) - static_cast<DstData>(rhs_zero_point)) * |
| 298 | static_cast<DstData>(rhs_scale); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (bias_data != nullptr) { |
| 303 | const auto bias_value = read_array<BiasData>(bias_data, i_n); |
| 304 | const auto bias_scale = bias_scales != nullptr |
| 305 | ? read_array<BiasScale>(bias_scales, i_n / bias_quant_width) |
| 306 | : static_cast<BiasScale>(1); |
| 307 | const auto bias_zero_point = bias_zero_points != nullptr |
| 308 | ? read_array<BiasZeroPoint>(bias_zero_points, i_n / bias_quant_width) |
| 309 | : static_cast<BiasZeroPoint>(0); |
| 310 | |
| 311 | acc += (static_cast<DstData>(bias_value) - static_cast<DstData>(bias_zero_point)) * |
| 312 | static_cast<DstData>(bias_scale); |
| 313 | } |
| 314 | |
| 315 | write_array<DstData>(dst.data(), i_m * n + i_n, acc); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return dst; |
| 320 | } |
| 321 | |
| 322 | template < |
| 323 | typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale, |
| 324 | typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData> |
| 325 | Buffer matmul_nt_t_quantized( |
| 326 | size_t m, size_t n, size_t k, // |
| 327 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, // |
| 328 | size_t lhs_quant_height, size_t lhs_quant_width, // |
| 329 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, // |
| 330 | size_t rhs_quant_height, size_t rhs_quant_width, // |
| 331 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, // |
| 332 | size_t bias_quant_width) { |
| 333 | KAI_ASSUME_ALWAYS(lhs_quant_width != 0); |
| 334 | KAI_ASSUME_ALWAYS(rhs_quant_width != 0); |
| 335 | KAI_ASSUME_ALWAYS(lhs_quant_height != 0); |
| 336 | KAI_ASSUME_ALWAYS(rhs_quant_height != 0); |
| 337 | KAI_ASSUME_ALWAYS(bias_quant_width != 0); |
| 338 | |
| 339 | const auto lhs_num_quant_per_row = round_up_division(k, lhs_quant_width); |
| 340 | const auto rhs_num_quant_per_row = round_up_division(k, rhs_quant_width); |
| 341 | |
| 342 | Buffer dst(m * n * sizeof(DstData)); |
| 343 | |
| 344 | for (size_t row = 0; row < m; ++row) { |
| 345 | for (size_t col = 0; col < n; ++col) { |
| 346 | DstData acc = 0; |
| 347 | |
| 348 | for (size_t i = 0; i < k; ++i) { |
| 349 | const auto lhs_data_index = row * k + i; |
| 350 | const auto lhs_quant_index = (row / lhs_quant_height) * lhs_num_quant_per_row + i / lhs_quant_width; |
| 351 | const auto lhs_value = read_array<LhsData>(lhs_data, lhs_data_index); |
| 352 | const auto lhs_scale = lhs_scales != nullptr ? read_array<LhsScale>(lhs_scales, lhs_quant_index) |
| 353 | : static_cast<LhsScale>(1); |
| 354 | const auto lhs_zero_point = lhs_zero_points != nullptr |
| 355 | ? read_array<LhsZeroPoint>(lhs_zero_points, lhs_quant_index) |
| 356 | : static_cast<LhsZeroPoint>(0); |
| 357 | |
| 358 | const auto rhs_data_index = col * k + i; |
| 359 | const auto rhs_quant_index = (col / rhs_quant_height) * rhs_num_quant_per_row + i / rhs_quant_width; |
| 360 | const auto rhs_value = read_array<RhsData>(rhs_data, rhs_data_index); |
| 361 | const auto rhs_scale = rhs_scales != nullptr ? read_array<RhsScale>(rhs_scales, rhs_quant_index) |
| 362 | : static_cast<RhsScale>(1); |
| 363 | const auto rhs_zero_point = rhs_zero_points != nullptr |
| 364 | ? read_array<RhsZeroPoint>(rhs_zero_points, rhs_quant_index) |
| 365 | : static_cast<RhsZeroPoint>(0); |
| 366 | |
| 367 | acc += (static_cast<DstData>(lhs_value) - static_cast<DstData>(lhs_zero_point)) * |
| 368 | static_cast<DstData>(lhs_scale) * |
| 369 | (static_cast<DstData>(rhs_value) - static_cast<DstData>(rhs_zero_point)) * |
| 370 | static_cast<DstData>(rhs_scale); |
| 371 | } |
| 372 | |
| 373 | if (bias_data != nullptr) { |
| 374 | const auto bias_value = read_array<BiasData>(bias_data, col); |
| 375 | const auto bias_scale = bias_scales != nullptr |
| 376 | ? read_array<BiasScale>(bias_scales, col / bias_quant_width) |
| 377 | : static_cast<BiasScale>(1); |
| 378 | const auto bias_zero_point = bias_zero_points != nullptr |
| 379 | ? read_array<BiasZeroPoint>(bias_zero_points, col / bias_quant_width) |
| 380 | : static_cast<BiasZeroPoint>(0); |
| 381 | |
| 382 | acc += (static_cast<DstData>(bias_value) - static_cast<DstData>(bias_zero_point)) * |
| 383 | static_cast<DstData>(bias_scale); |
| 384 | } |
| 385 | |
| 386 | write_array<DstData>(dst.data(), row * n + col, acc); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return dst; |
| 391 | } |
| 392 | |
| 393 | template Buffer matmul_nt_t_quantized<int8_t, float, int32_t, int8_t, float, int32_t, int32_t, float, int32_t, float>( |
| 394 | size_t m, size_t n, size_t k, // |
| 395 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 396 | size_t lhs_quant_width, // |
| 397 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 398 | size_t rhs_quant_width, // |
| 399 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 400 | |
| 401 | template Buffer matmul_nt_t_quantized<int8_t, float, int32_t, Int4, float, int32_t, float, float, int32_t, float>( |
| 402 | size_t m, size_t n, size_t k, // |
| 403 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 404 | size_t lhs_quant_width, // |
| 405 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 406 | size_t rhs_quant_width, // |
| 407 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 408 | |
| 409 | template Buffer matmul_nt_t_quantized<int8_t, float, int32_t, Int2, float, int32_t, float, float, int32_t, float>( |
| 410 | size_t m, size_t n, size_t k, // |
| 411 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 412 | size_t lhs_quant_width, // |
| 413 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 414 | size_t rhs_quant_width, // |
| 415 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 416 | |
| 417 | template Buffer matmul_nt_t_quantized<int8_t, float, int32_t, int8_t, float, int32_t, float, float, int32_t, float>( |
| 418 | size_t m, size_t n, size_t k, // |
| 419 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 420 | size_t lhs_quant_width, // |
| 421 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 422 | size_t rhs_quant_width, // |
| 423 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 424 | |
| 425 | template Buffer |
| 426 | matmul_nt_t_quantized<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 427 | size_t m, size_t n, size_t k, // |
| 428 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 429 | size_t lhs_quant_width, // |
| 430 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 431 | size_t rhs_quant_width, // |
| 432 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 433 | |
| 434 | template Buffer matmul_nt_t_quantized<Float16, float, int32_t, Int4, Float16, int32_t, float, float, int32_t, float>( |
| 435 | size_t m, size_t n, size_t k, // |
| 436 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 437 | size_t lhs_quant_width, // |
| 438 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 439 | size_t rhs_quant_width, // |
| 440 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 441 | |
| 442 | template < |
| 443 | typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale, |
| 444 | typename RhsZeroPoint, typename BiasData, typename BiasScale, typename BiasZeroPoint, typename DstData> |
| 445 | Buffer matmul_nt_nt_quantized( |
| 446 | size_t m, size_t n, size_t k, // |
| 447 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, // |
| 448 | size_t lhs_quant_height, size_t lhs_quant_width, // |
| 449 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, // |
| 450 | size_t rhs_quant_height, size_t rhs_quant_width, // |
| 451 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, // |
| 452 | size_t bias_quant_width) { |
| 453 | KAI_ASSUME_ALWAYS(lhs_quant_width != 0); |
| 454 | KAI_ASSUME_ALWAYS(rhs_quant_width != 0); |
| 455 | KAI_ASSUME_ALWAYS(lhs_quant_height != 0); |
| 456 | KAI_ASSUME_ALWAYS(rhs_quant_height != 0); |
| 457 | KAI_ASSUME_ALWAYS(bias_quant_width != 0); |
| 458 | |
| 459 | const auto lhs_num_quant_per_row = round_up_division(k, lhs_quant_width); |
| 460 | const auto rhs_num_quant_per_row = round_up_division(k, rhs_quant_width); |
| 461 | |
| 462 | Buffer dst(m * n * sizeof(DstData)); |
| 463 | |
| 464 | for (size_t row = 0; row < m; ++row) { |
| 465 | for (size_t col = 0; col < n; ++col) { |
| 466 | DstData acc = 0; |
| 467 | |
| 468 | for (size_t i = 0; i < k; ++i) { |
| 469 | const auto lhs_data_index = row * k + i; |
| 470 | const auto lhs_quant_index = (row / lhs_quant_height) * lhs_num_quant_per_row + i / lhs_quant_width; |
| 471 | const auto lhs_value = read_array<LhsData>(lhs_data, lhs_data_index); |
| 472 | const auto lhs_scale = lhs_scales != nullptr ? read_array<LhsScale>(lhs_scales, lhs_quant_index) |
| 473 | : static_cast<LhsScale>(1); |
| 474 | const auto lhs_zero_point = lhs_zero_points != nullptr |
| 475 | ? read_array<LhsZeroPoint>(lhs_zero_points, lhs_quant_index) |
| 476 | : static_cast<LhsZeroPoint>(0); |
| 477 | |
| 478 | const auto rhs_data_index = col + i * n; |
| 479 | const auto rhs_quant_index = (col / rhs_quant_height) * rhs_num_quant_per_row + i / rhs_quant_width; |
| 480 | const auto rhs_value = read_array<RhsData>(rhs_data, rhs_data_index); |
| 481 | const auto rhs_scale = rhs_scales != nullptr ? read_array<RhsScale>(rhs_scales, rhs_quant_index) |
| 482 | : static_cast<RhsScale>(1); |
| 483 | const auto rhs_zero_point = rhs_zero_points != nullptr |
| 484 | ? read_array<RhsZeroPoint>(rhs_zero_points, rhs_quant_index) |
| 485 | : static_cast<RhsZeroPoint>(0); |
| 486 | |
| 487 | acc += (static_cast<DstData>(lhs_value) - static_cast<DstData>(lhs_zero_point)) * |
| 488 | static_cast<DstData>(lhs_scale) * |
| 489 | (static_cast<DstData>(rhs_value) - static_cast<DstData>(rhs_zero_point)) * |
| 490 | static_cast<DstData>(rhs_scale); |
| 491 | } |
| 492 | |
| 493 | if (bias_data != nullptr) { |
| 494 | const auto bias_value = read_array<BiasData>(bias_data, col); |
| 495 | const auto bias_scale = bias_scales != nullptr |
| 496 | ? read_array<BiasScale>(bias_scales, col / bias_quant_width) |
| 497 | : static_cast<BiasScale>(1); |
| 498 | const auto bias_zero_point = bias_zero_points != nullptr |
| 499 | ? read_array<BiasZeroPoint>(bias_zero_points, col / bias_quant_width) |
| 500 | : static_cast<BiasZeroPoint>(0); |
| 501 | |
| 502 | acc += (static_cast<DstData>(bias_value) - static_cast<DstData>(bias_zero_point)) * |
| 503 | static_cast<DstData>(bias_scale); |
| 504 | } |
| 505 | |
| 506 | write_array<DstData>(dst.data(), row * n + col, acc); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return dst; |
| 511 | } |
| 512 | |
| 513 | template Buffer |
| 514 | matmul_nt_nt_quantized<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 515 | size_t m, size_t n, size_t k, // |
| 516 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 517 | size_t lhs_quant_width, // |
| 518 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 519 | size_t rhs_quant_width, // |
| 520 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 521 | |
| 522 | template Buffer matmul_nt_nt_quantized<BFloat16<>, float, float, BFloat16<>, float, float, float, float, float, float>( |
| 523 | size_t m, size_t n, size_t k, // |
| 524 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_height, |
| 525 | size_t lhs_quant_width, // |
| 526 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 527 | size_t rhs_quant_width, // |
| 528 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 529 | |
| 530 | template Buffer |
| 531 | indirect_matmul_nt_t_quantized<int8_t, float, int32_t, int8_t, float, int32_t, int32_t, float, int32_t, float>( |
| 532 | size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, // |
| 533 | const void* const* lhs_ptrs, uintptr_t lhs_offset, const void* lhs_padding, const void* lhs_scales, |
| 534 | const void* lhs_zero_points, size_t lhs_quant_height, |
| 535 | size_t lhs_quant_width, // |
| 536 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_height, |
| 537 | size_t rhs_quant_width, // |
| 538 | const void* bias_data, const void* bias_scales, const void* bias_zero_points, size_t bias_quant_width); |
| 539 | |
| 540 | template < |
| 541 | typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale, |
| 542 | typename RhsZeroPoint, typename Bias, typename IntAcc, typename DstData> |
| 543 | Buffer matmul_clamp_nt_t( |
| 544 | size_t m, size_t n, size_t k, // |
| 545 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 546 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 547 | const void* biases, // |
| 548 | DstData min_value, DstData max_value) { |
| 549 | KAI_ASSUME_ALWAYS(lhs_quant_width != 0); |
| 550 | KAI_ASSUME_ALWAYS(rhs_quant_width != 0); |
| 551 | const auto lhs_num_quant_per_row = round_up_division(k, lhs_quant_width); |
| 552 | const auto rhs_num_quant_per_row = round_up_division(k, rhs_quant_width); |
| 553 | |
| 554 | Buffer dst(m * n * sizeof(DstData)); |
| 555 | |
| 556 | const auto* lhs_scales_ptr = reinterpret_cast<const LhsScale*>(lhs_scales); |
| 557 | const auto* rhs_scales_ptr = reinterpret_cast<const RhsScale*>(rhs_scales); |
| 558 | const auto* lhs_zero_points_ptr = reinterpret_cast<const LhsZeroPoint*>(lhs_zero_points); |
| 559 | const auto* rhs_zero_points_ptr = reinterpret_cast<const RhsZeroPoint*>(rhs_zero_points); |
| 560 | const auto* biases_ptr = reinterpret_cast<const Bias*>(biases); |
| 561 | auto* dst_ptr = reinterpret_cast<DstData*>(dst.data()); |
| 562 | |
| 563 | for (size_t y = 0; y < m; ++y) { |
| 564 | for (size_t x = 0; x < n; ++x) { |
| 565 | DstData acc = 0; |
| 566 | |
| 567 | for (size_t i = 0; i < k; ++i) { |
| 568 | const auto lhs_value = read_array<LhsData>(lhs_data, y * k + i); |
| 569 | const auto lhs_scale = lhs_scales_ptr[y * lhs_num_quant_per_row + i / lhs_quant_width]; |
| 570 | const auto lhs_zero_point = lhs_zero_points_ptr != nullptr |
| 571 | ? lhs_zero_points_ptr[y * lhs_num_quant_per_row + i / lhs_quant_width] |
| 572 | : 0; |
| 573 | |
| 574 | const auto rhs_value = read_array<RhsData>(rhs_data, x * k + i); |
| 575 | const auto rhs_scale = rhs_scales_ptr[x * rhs_num_quant_per_row + i / rhs_quant_width]; |
| 576 | const auto rhs_zero_point = rhs_zero_points_ptr != nullptr |
| 577 | ? rhs_zero_points_ptr[y * rhs_num_quant_per_row + i / rhs_quant_width] |
| 578 | : 0; |
| 579 | |
| 580 | acc += static_cast<DstData>( |
| 581 | (static_cast<IntAcc>(lhs_value) - static_cast<IntAcc>(lhs_zero_point)) * |
| 582 | (static_cast<IntAcc>(rhs_value) - static_cast<IntAcc>(rhs_zero_point))) * |
| 583 | static_cast<DstData>(lhs_scale) * static_cast<DstData>(rhs_scale); |
| 584 | } |
| 585 | |
| 586 | if (biases_ptr != nullptr) { |
| 587 | acc += static_cast<DstData>(biases_ptr[x]); |
| 588 | } |
| 589 | |
| 590 | acc = std::clamp(acc, min_value, max_value); |
| 591 | dst_ptr[y * n + x] = acc; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return dst; |
| 596 | } |
| 597 | |
| 598 | template Buffer matmul_clamp_nt_t<int8_t, float, int32_t, Int4, float, int32_t, float, int32_t, float>( |
| 599 | size_t m, size_t n, size_t k, // |
| 600 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 601 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 602 | const void* biases, // |
| 603 | float min_value, float max_value); |
| 604 | |
| 605 | template Buffer matmul_clamp_nt_t<int8_t, float, int32_t, Int2, float, int32_t, float, int32_t, float>( |
| 606 | size_t m, size_t n, size_t k, // |
| 607 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 608 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 609 | const void* biases, // |
| 610 | float min_value, float max_value); |
| 611 | |
| 612 | template Buffer matmul_clamp_nt_t<int8_t, Float16, int32_t, Int4, Float16, int32_t, float, int32_t, float>( |
| 613 | size_t m, size_t n, size_t k, // |
| 614 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 615 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 616 | const void* biases, // |
| 617 | float min_value, float max_value); |
| 618 | |
| 619 | template Buffer matmul_clamp_nt_t<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, int32_t, float>( |
| 620 | size_t m, size_t n, size_t k, // |
| 621 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 622 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 623 | const void* biases, // |
| 624 | float min_value, float max_value); |
| 625 | |
| 626 | template Buffer matmul_clamp_nt_t<int8_t, float, int32_t, int8_t, float, int32_t, float, int32_t, float>( |
| 627 | size_t m, size_t n, size_t k, // |
| 628 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 629 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 630 | const void* biases, // |
| 631 | float min_value, float max_value); |
| 632 | |
| 633 | template < |
| 634 | typename LhsData, typename LhsScale, typename LhsZeroPoint, typename RhsData, typename RhsScale, |
| 635 | typename RhsZeroPoint, typename Bias, typename IntAcc, typename DstData> |
| 636 | Buffer matmul_clamp_nt_nt( |
| 637 | size_t m, size_t n, size_t k, // |
| 638 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 639 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 640 | const void* biases, // |
| 641 | DstData min_value, DstData max_value) { |
| 642 | KAI_ASSUME_ALWAYS(lhs_quant_width != 0); |
| 643 | KAI_ASSUME_ALWAYS(rhs_quant_width != 0); |
| 644 | const auto lhs_num_quant_per_row = round_up_division(k, lhs_quant_width); |
| 645 | const auto rhs_num_quant_per_row = round_up_division(k, rhs_quant_width); |
| 646 | |
| 647 | Buffer dst(m * n * sizeof(DstData)); |
| 648 | |
| 649 | const auto* lhs_scales_ptr = reinterpret_cast<const LhsScale*>(lhs_scales); |
| 650 | const auto* rhs_scales_ptr = reinterpret_cast<const RhsScale*>(rhs_scales); |
| 651 | const auto* lhs_zero_points_ptr = reinterpret_cast<const LhsZeroPoint*>(lhs_zero_points); |
| 652 | const auto* rhs_zero_points_ptr = reinterpret_cast<const RhsZeroPoint*>(rhs_zero_points); |
| 653 | const auto* biases_ptr = reinterpret_cast<const Bias*>(biases); |
| 654 | auto* dst_ptr = reinterpret_cast<DstData*>(dst.data()); |
| 655 | |
| 656 | for (size_t y = 0; y < m; ++y) { |
| 657 | for (size_t x = 0; x < n; ++x) { |
| 658 | DstData acc = 0; |
| 659 | |
| 660 | for (size_t i = 0; i < k; ++i) { |
| 661 | const auto lhs_value = read_array<LhsData>(lhs_data, y * k + i); |
| 662 | const auto lhs_scale = lhs_scales_ptr[y * lhs_num_quant_per_row + i / lhs_quant_width]; |
| 663 | const auto lhs_zero_point = lhs_zero_points_ptr != nullptr |
| 664 | ? lhs_zero_points_ptr[y * lhs_num_quant_per_row + i / lhs_quant_width] |
| 665 | : 0; |
| 666 | |
| 667 | const auto rhs_value = read_array<RhsData>(rhs_data, x + i * n); |
| 668 | const auto rhs_scale = rhs_scales_ptr[x * rhs_num_quant_per_row + i / rhs_quant_width]; |
| 669 | const auto rhs_zero_point = rhs_zero_points_ptr != nullptr |
| 670 | ? rhs_zero_points_ptr[y * rhs_num_quant_per_row + i / rhs_quant_width] |
| 671 | : 0; |
| 672 | |
| 673 | acc += static_cast<DstData>( |
| 674 | (static_cast<IntAcc>(lhs_value) - static_cast<IntAcc>(lhs_zero_point)) * |
| 675 | (static_cast<IntAcc>(rhs_value) - static_cast<IntAcc>(rhs_zero_point))) * |
| 676 | static_cast<DstData>(lhs_scale) * static_cast<DstData>(rhs_scale); |
| 677 | } |
| 678 | |
| 679 | if (biases_ptr != nullptr) { |
| 680 | acc += static_cast<DstData>(biases_ptr[x]); |
| 681 | } |
| 682 | |
| 683 | acc = std::clamp(acc, min_value, max_value); |
| 684 | dst_ptr[y * n + x] = acc; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return dst; |
| 689 | } |
| 690 | |
| 691 | template Buffer matmul_clamp_nt_nt<int8_t, float, int32_t, int8_t, float, int32_t, float, int32_t, float>( |
| 692 | size_t m, size_t n, size_t k, // |
| 693 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 694 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 695 | const void* biases, // |
| 696 | float min_value, float max_value); |
| 697 | template Buffer matmul_clamp_nt_nt<int8_t, float, int32_t, Int4, float, int32_t, float, int32_t, float>( |
| 698 | size_t m, size_t n, size_t k, // |
| 699 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 700 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 701 | const void* biases, // |
| 702 | float min_value, float max_value); |
| 703 | |
| 704 | template Buffer matmul_clamp_nt_nt<int8_t, Float16, int32_t, Int4, Float16, int32_t, float, int32_t, float>( |
| 705 | size_t m, size_t n, size_t k, // |
| 706 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 707 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 708 | const void* biases, // |
| 709 | float min_value, float max_value); |
| 710 | |
| 711 | template Buffer matmul_clamp_nt_nt<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, int32_t, float>( |
| 712 | size_t m, size_t n, size_t k, // |
| 713 | const void* lhs_data, const void* lhs_scales, const void* lhs_zero_points, size_t lhs_quant_width, // |
| 714 | const void* rhs_data, const void* rhs_scales, const void* rhs_zero_points, size_t rhs_quant_width, // |
| 715 | const void* biases, // |
| 716 | float min_value, float max_value); |
| 717 | |
| 718 | } // namespace kai::test |