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