| 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 <gtest/gtest.h> |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <cstdlib> |
| 13 | #include <limits> |
| 14 | #include <string> |
| 15 | #include <tuple> |
| 16 | |
| 17 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h" |
| 18 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme_mopa.h" |
| 19 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot.h" |
| 20 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme_dot.h" |
| 21 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod.h" |
| 22 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h" |
| 23 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod.h" |
| 24 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h" |
| 25 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi8cxp/kai_matmul_clamp_f32_qai8dxp_qsi8cxp_interface.h" |
| 26 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_f32.h" |
| 27 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_qsi8cxp_qsi8cx_neon.h" |
| 28 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h" |
| 29 | #include "test/common/abi_checker.hpp" |
| 30 | #include "test/common/buffer.hpp" |
| 31 | #include "test/common/cache.hpp" |
| 32 | #include "test/common/compare.hpp" |
| 33 | #include "test/common/cpu_info.hpp" |
| 34 | #include "test/common/matmul_test_common.hpp" |
| 35 | #include "test/common/matrix_portion.hpp" |
| 36 | #include "test/common/memory.hpp" |
| 37 | #include "test/common/printer.hpp" |
| 38 | #include "test/common/seed.hpp" |
| 39 | #include "test/common/test_suite.hpp" |
| 40 | #include "test/reference/clamp.hpp" |
| 41 | #include "test/reference/fill.hpp" |
| 42 | #include "test/reference/matmul.hpp" |
| 43 | #include "test/reference/quantize.hpp" |
| 44 | #include "test/reference/transpose.hpp" |
| 45 | |
| 46 | namespace kai::test { |
| 47 | |
| 48 | using F32Qai8Qsi8CacheDataId = std::tuple< |
| 49 | MatMulShape, // |
| 50 | DataFormat, // lhs format |
| 51 | DataFormat, // rhs format |
| 52 | DataFormat, // bias format |
| 53 | std::optional<float>>; |
| 54 | |
| 55 | struct F32Qai8Qsi8CacheData { |
| 56 | Buffer ref_dst_nt_t; |
| 57 | Buffer ref_dst_nt_nt; |
| 58 | Buffer ref_rhs_qsi8_nt_t; |
| 59 | Buffer ref_rhs_qsi8_nt_nt; |
| 60 | Buffer ref_rhs_scales; |
| 61 | Buffer ref_lhs; |
| 62 | Buffer ref_bias; |
| 63 | Range<float> clamp_range; |
| 64 | }; |
| 65 | |
| 66 | template <> |
| 67 | F32Qai8Qsi8CacheData ReferenceGenerator<F32Qai8Qsi8CacheDataId, F32Qai8Qsi8CacheData>::generate_reference( |
| 68 | const F32Qai8Qsi8CacheDataId& data_id) { |
| 69 | auto [shape, lhs_format, rhs_format, bias_format, clamp_keep_ratio] = data_id; |
| 70 | |
| 71 | const size_t M = shape.m; |
| 72 | const size_t N = shape.n; |
| 73 | const size_t K = shape.k; |
| 74 | |
| 75 | // Seed the random generator. |
| 76 | const auto key = std::string("F32Qai8Qsi8_cache:") + std::to_string(M) + "x" + std::to_string(N) + "x" + |
| 77 | std::to_string(K) + ":" + std::to_string(static_cast<uint32_t>(lhs_format.data_type())) + ":" + |
| 78 | std::to_string(static_cast<uint32_t>(rhs_format.data_type())) + ":" + |
| 79 | std::to_string(static_cast<uint32_t>(bias_format.data_type())) + ":" + |
| 80 | (clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp"); |
| 81 | auto& feed = seed_stream(key); |
| 82 | |
| 83 | Buffer lhs = fill_matrix_random(shape.m, shape.k, lhs_format, feed()); |
| 84 | Buffer rhs = fill_matrix_random(shape.k, shape.n, rhs_format, feed()); |
| 85 | Buffer bias = fill_matrix_random(1, shape.n, bias_format, feed()); |
| 86 | |
| 87 | QuantizationInfo lhs_qinfo{}; |
| 88 | lhs_qinfo.quant_width = K; |
| 89 | lhs_qinfo.dst_type = DataType::QAI8; |
| 90 | lhs_qinfo.scale_type = DataType::FP32; |
| 91 | lhs_qinfo.zero_point_type = DataType::I32; |
| 92 | const auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(lhs.data(), DataType::FP32, M, K, lhs_qinfo); |
| 93 | |
| 94 | QuantizationInfo rhs_qinfo{}; |
| 95 | rhs_qinfo.quant_width = K; |
| 96 | rhs_qinfo.dst_type = DataType::QSI8; |
| 97 | rhs_qinfo.scale_type = DataType::FP32; |
| 98 | auto [ref_rhs_quant_t, rhs_qoutputs] = quantize_dynamic(rhs.data(), DataType::FP32, N, K, rhs_qinfo); |
| 99 | |
| 100 | F32Qai8Qsi8CacheData out; |
| 101 | |
| 102 | // Transposed RHS path. |
| 103 | const size_t ref_rhs_qsi8_nxk_stride = K; |
| 104 | const size_t ref_rhs_qsi8_kxn_stride = N; |
| 105 | const size_t ref_rhs_qsi8_kxn_size_bytes = K * ref_rhs_qsi8_kxn_stride; |
| 106 | |
| 107 | // Non-Transposed(kxn) RHS dimensions |
| 108 | auto ref_rhs_qsi8 = transpose_with_padding<int8_t>( |
| 109 | ref_rhs_quant_t.data(), N, K, ref_rhs_qsi8_nxk_stride, ref_rhs_qsi8_kxn_stride, ref_rhs_qsi8_kxn_size_bytes); |
| 110 | |
| 111 | auto ref_dst_nt_nt = matmul_clamp_nt_nt<int8_t, float, int32_t, int8_t, float, int32_t, float, int32_t, float>( |
| 112 | M, N, K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), K, |
| 113 | ref_rhs_qsi8.data(), rhs_qoutputs.scales.data(), nullptr, K, bias.data(), std::numeric_limits<float>::lowest(), |
| 114 | std::numeric_limits<float>::max()); |
| 115 | |
| 116 | // Non-transposed RHS path. |
| 117 | auto ref_dst_nt_t = matmul_clamp_nt_t<int8_t, float, int32_t, int8_t, float, int32_t, float, int32_t, float>( |
| 118 | M, N, K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), K, |
| 119 | ref_rhs_quant_t.data(), rhs_qoutputs.scales.data(), nullptr, K, bias.data(), |
| 120 | std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max()); |
| 121 | |
| 122 | // Only need to calculate range once for both, apply clamping |
| 123 | const auto [clamp_min, clamp_max] = find_clamp_range(DataType::FP32, ref_dst_nt_t.data(), M * N, clamp_keep_ratio); |
| 124 | auto ref_clamped_nt_t = clamp(DataType::FP32, ref_dst_nt_t.data(), M * N, clamp_min, clamp_max); |
| 125 | auto ref_clamped_nt_nt = clamp(DataType::FP32, ref_dst_nt_nt.data(), M * N, clamp_min, clamp_max); |
| 126 | |
| 127 | out.ref_rhs_qsi8_nt_nt = std::move(ref_rhs_qsi8); |
| 128 | out.ref_rhs_qsi8_nt_t = std::move(ref_rhs_quant_t); |
| 129 | out.ref_dst_nt_nt = std::move(ref_clamped_nt_nt); |
| 130 | out.ref_dst_nt_t = std::move(ref_clamped_nt_t); |
| 131 | out.ref_lhs = std::move(lhs); |
| 132 | out.ref_bias = std::move(bias); |
| 133 | out.ref_rhs_scales = std::move(rhs_qoutputs.scales); |
| 134 | out.clamp_range = {clamp_min, clamp_max}; |
| 135 | |
| 136 | return out; |
| 137 | } |
| 138 | |
| 139 | static const std::array<UkernelVariant<kai_matmul_clamp_f32_qai8dxp_qsi8cxp_ukernel>, 8> |
| 140 | variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp = {{ |
| 141 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod), |
| 142 | "kai_matmul_clamp_f32_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod", cpu_has_dotprod}, |
| 143 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod), |
| 144 | "kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod", cpu_has_dotprod}, |
| 145 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod), |
| 146 | "kai_matmul_clamp_f32_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod", cpu_has_dotprod}, |
| 147 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm), |
| 148 | "kai_matmul_clamp_f32_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm", cpu_has_i8mm}, |
| 149 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme_dot), |
| 150 | "kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme_dot", cpu_has_sme}, |
| 151 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme_mopa), |
| 152 | "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme_mopa", cpu_has_sme}, |
| 153 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot), |
| 154 | "kai_matmul_clamp_f32_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot", cpu_has_sme2}, |
| 155 | {UKERNEL_MATMUL_VARIANT(clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa), |
| 156 | "kai_matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa", cpu_has_sme2}, |
| 157 | }}; |
| 158 | |
| 159 | using MatMulClampTestPortionedParams = std::tuple<size_t, MatMulShape, MatrixPortion, std::optional<float>>; |
| 160 | |
| 161 | class MatMulTest_f32_qai8dxp_qsi8cxp : public ::testing::TestWithParam<MatMulClampTestPortionedParams> {}; |
| 162 | |
| 163 | TEST_P(MatMulTest_f32_qai8dxp_qsi8cxp, Offset_RHS) { |
| 164 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio] = GetParam(); |
| 165 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.at(variant_index); |
| 166 | |
| 167 | if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { |
| 168 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 169 | } |
| 170 | |
| 171 | const size_t K = matmul_shape.k; |
| 172 | const auto nr = ukernel_variant.interface.get_nr(); |
| 173 | const auto kr = ukernel_variant.interface.get_kr(); |
| 174 | const auto sr = ukernel_variant.interface.get_sr(); |
| 175 | |
| 176 | const auto n_idx = nr; |
| 177 | |
| 178 | auto rhs_packed_offset_kxn = kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(n_idx, K, nr, kr, sr); |
| 179 | auto rhs_packed_offset_nxk = kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(n_idx, K, nr, kr, sr); |
| 180 | |
| 181 | ASSERT_EQ(rhs_packed_offset_kxn, rhs_packed_offset_nxk); |
| 182 | |
| 183 | auto rhs_matmul_offset = ukernel_variant.interface.get_rhs_packed_offset(n_idx, K); |
| 184 | ASSERT_EQ(rhs_packed_offset_kxn, rhs_matmul_offset); |
| 185 | } |
| 186 | |
| 187 | TEST_P(MatMulTest_f32_qai8dxp_qsi8cxp, Offset_LHS) { |
| 188 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio] = GetParam(); |
| 189 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.at(variant_index); |
| 190 | |
| 191 | if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { |
| 192 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 193 | } |
| 194 | |
| 195 | const size_t K = matmul_shape.k; |
| 196 | const auto mr = ukernel_variant.interface.get_mr(); |
| 197 | const auto kr = ukernel_variant.interface.get_kr(); |
| 198 | const auto sr = ukernel_variant.interface.get_sr(); |
| 199 | |
| 200 | const auto m_idx = mr; |
| 201 | |
| 202 | auto lhs_packed_offset = kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(m_idx, K, mr, kr, sr); |
| 203 | auto lhs_matmul_offset = ukernel_variant.interface.get_lhs_packed_offset(m_idx, K); |
| 204 | |
| 205 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 206 | } |
| 207 | |
| 208 | TEST_P(MatMulTest_f32_qai8dxp_qsi8cxp, EndToEnd_RHS_nxk_qsi8cx) { |
| 209 | auto& [variant_index, matmul_shape, portion, clamp_keep_ratio] = GetParam(); |
| 210 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.at(variant_index); |
| 211 | |
| 212 | if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { |
| 213 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 214 | } |
| 215 | |
| 216 | const size_t M = matmul_shape.m; |
| 217 | const size_t N = matmul_shape.n; |
| 218 | const size_t K = matmul_shape.k; |
| 219 | |
| 220 | const auto mr = ukernel_variant.interface.get_mr(); |
| 221 | const auto nr = ukernel_variant.interface.get_nr(); |
| 222 | const auto kr = ukernel_variant.interface.get_kr(); |
| 223 | const auto sr = ukernel_variant.interface.get_sr(); |
| 224 | |
| 225 | const F32Qai8Qsi8CacheDataId testdata_id = { |
| 226 | matmul_shape, // |
| 227 | DataFormat(DataType::FP32), // |
| 228 | DataFormat(DataType::FP32), // |
| 229 | DataFormat(DataType::FP32), clamp_keep_ratio}; |
| 230 | const F32Qai8Qsi8CacheData& testdata = getV<F32Qai8Qsi8CacheDataId, F32Qai8Qsi8CacheData>(testdata_id); |
| 231 | |
| 232 | const auto& ref_rhs_qsi8 = testdata.ref_rhs_qsi8_nt_t; |
| 233 | const auto& ref_rhs_scales = testdata.ref_rhs_scales; |
| 234 | const auto& ref_dst = testdata.ref_dst_nt_t; |
| 235 | const auto& ref_bias = testdata.ref_bias; |
| 236 | const auto& ref_lhs = testdata.ref_lhs; |
| 237 | auto m_step = ukernel_variant.interface.get_m_step(); |
| 238 | ASSERT_TRUE(m_step % mr == 0); |
| 239 | |
| 240 | auto n_step = ukernel_variant.interface.get_n_step(); |
| 241 | ASSERT_TRUE(n_step % nr == 0); |
| 242 | |
| 243 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 244 | if (rect.height() == 0 || rect.width() == 0) { |
| 245 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 246 | } |
| 247 | |
| 248 | // Runs the LHS packing micro-kernel. |
| 249 | const auto imp_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(M, K, mr, kr, sr); |
| 250 | Buffer imp_packed_lhs(imp_packed_lhs_size); |
| 251 | |
| 252 | const auto lhs_start_row = rect.start_row(); |
| 253 | size_t lhs_stride = K * sizeof(float); |
| 254 | |
| 255 | auto lhs_offset = kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(lhs_start_row, lhs_stride); |
| 256 | auto lhs_packed_offset = kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(lhs_start_row, K, mr, kr, sr); |
| 257 | |
| 258 | kai_run_lhs_quant_pack_qai8dxp_f32( |
| 259 | rect.height(), K, mr, kr, sr, 0, reinterpret_cast<const float*>(ref_lhs.data() + lhs_offset), lhs_stride, |
| 260 | imp_packed_lhs.data() + lhs_packed_offset); |
| 261 | |
| 262 | // Runs the RHS packing micro-kernel. |
| 263 | // * Generates the 8-bit signed symmetric quantized input for the micro-kernel. |
| 264 | // * Packs the RHS matrix. |
| 265 | const auto imp_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(N, K, nr, kr, sr); |
| 266 | |
| 267 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 268 | const kai_rhs_pack_qsi8cx_params params{.lhs_zero_point = 1, .scale_multiplier = 1.0f}; |
| 269 | kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon( |
| 270 | 1, N, K, nr, kr, sr, reinterpret_cast<const int8_t*>(ref_rhs_qsi8.data()), |
| 271 | reinterpret_cast<const float*>(ref_bias.data()), reinterpret_cast<const float*>(ref_rhs_scales.data()), |
| 272 | imp_packed_rhs.data(), 0, ¶ms); |
| 273 | |
| 274 | const auto packed_rhs_start_row = rect.start_col(); |
| 275 | auto rhs_packed_offset = |
| 276 | kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(packed_rhs_start_row, K, nr, kr, sr); |
| 277 | |
| 278 | const auto dst_stride = N * sizeof(float); |
| 279 | const auto dst_offset = ukernel_variant.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride); |
| 280 | const auto ref_dst_offset = rect.start_row() * dst_stride + rect.start_col() * sizeof(float); |
| 281 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 282 | |
| 283 | const auto matmul_lhs_packed_offset = ukernel_variant.interface.get_lhs_packed_offset(rect.start_row(), K); |
| 284 | ASSERT_EQ(lhs_packed_offset, matmul_lhs_packed_offset); |
| 285 | const auto matmul_rhs_packed_offset = ukernel_variant.interface.get_rhs_packed_offset(rect.start_col(), K); |
| 286 | ASSERT_EQ(rhs_packed_offset, matmul_rhs_packed_offset); |
| 287 | |
| 288 | // Runs the GEMM micro-kernel. |
| 289 | const auto imp_dst_size = ukernel_variant.interface.get_dst_size(M, N); |
| 290 | ASSERT_EQ(imp_dst_size, ref_dst.size()); |
| 291 | Buffer imp_dst(imp_dst_size); |
| 292 | abi_check( |
| 293 | ukernel_variant.interface.run_matmul, rect.height(), rect.width(), K, |
| 294 | imp_packed_lhs.data() + matmul_lhs_packed_offset, imp_packed_rhs.data() + matmul_rhs_packed_offset, |
| 295 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), N * sizeof(float), sizeof(float), |
| 296 | testdata.clamp_range.min, testdata.clamp_range.max); |
| 297 | |
| 298 | // Compares the output of the micro-kernels against the output of the reference implementation. |
| 299 | DefaultMismatchHandler handler(0, 0.02, 0, 0.05); |
| 300 | DataFormat dst_format = DataFormat(DataType::FP32); |
| 301 | const auto success = compare(imp_dst.data(), ref_dst.data(), dst_format, M, N, rect, handler); |
| 302 | ASSERT_TRUE(success); |
| 303 | } |
| 304 | |
| 305 | TEST_P(MatMulTest_f32_qai8dxp_qsi8cxp, EndToEnd_RHS_kxn_qsi8cx) { |
| 306 | auto& [variant_index, matmul_shape, portion, clamp_keep_ratio] = GetParam(); |
| 307 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.at(variant_index); |
| 308 | |
| 309 | if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { |
| 310 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 311 | } |
| 312 | |
| 313 | const size_t M = matmul_shape.m; |
| 314 | const size_t N = matmul_shape.n; |
| 315 | const size_t K = matmul_shape.k; |
| 316 | |
| 317 | const auto mr = ukernel_variant.interface.get_mr(); |
| 318 | const auto nr = ukernel_variant.interface.get_nr(); |
| 319 | const auto kr = ukernel_variant.interface.get_kr(); |
| 320 | const auto sr = ukernel_variant.interface.get_sr(); |
| 321 | |
| 322 | const F32Qai8Qsi8CacheDataId testdata_id = { |
| 323 | matmul_shape, // |
| 324 | DataFormat(DataType::FP32), // |
| 325 | DataFormat(DataType::FP32), // |
| 326 | DataFormat(DataType::FP32), clamp_keep_ratio}; |
| 327 | const F32Qai8Qsi8CacheData& testdata = getV<F32Qai8Qsi8CacheDataId, F32Qai8Qsi8CacheData>(testdata_id); |
| 328 | const auto& ref_rhs_qsi8 = testdata.ref_rhs_qsi8_nt_nt; |
| 329 | const auto& ref_rhs_scales = testdata.ref_rhs_scales; |
| 330 | const auto& ref_dst = testdata.ref_dst_nt_nt; |
| 331 | const auto& ref_bias = testdata.ref_bias; |
| 332 | const auto& ref_lhs = testdata.ref_lhs; |
| 333 | |
| 334 | auto m_step = ukernel_variant.interface.get_m_step(); |
| 335 | ASSERT_TRUE(m_step % mr == 0); |
| 336 | |
| 337 | auto n_step = ukernel_variant.interface.get_n_step(); |
| 338 | ASSERT_TRUE(n_step % nr == 0); |
| 339 | |
| 340 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 341 | if (rect.height() == 0 || rect.width() == 0) { |
| 342 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 343 | } |
| 344 | |
| 345 | const auto lhs_start_row = rect.start_row(); |
| 346 | size_t const lhs_stride = K * sizeof(float); |
| 347 | |
| 348 | auto lhs_offset = kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32(lhs_start_row, lhs_stride); |
| 349 | auto lhs_packed_offset = kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(lhs_start_row, K, mr, kr, sr); |
| 350 | |
| 351 | // Runs the LHS packing micro-kernel. |
| 352 | const auto imp_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(M, K, mr, kr, sr); |
| 353 | Buffer imp_packed_lhs(imp_packed_lhs_size); |
| 354 | kai_run_lhs_quant_pack_qai8dxp_f32( |
| 355 | rect.height(), K, mr, kr, sr, 0, reinterpret_cast<const float*>(ref_lhs.data() + lhs_offset), K * sizeof(float), |
| 356 | imp_packed_lhs.data() + lhs_packed_offset); |
| 357 | |
| 358 | // Runs the RHS packing micro-kernel. |
| 359 | // * Generates the 8-bit signed symmetric quantized input for the micro-kernel. |
| 360 | // * Packs the RHS matrix. |
| 361 | const auto imp_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(N, K, nr, kr, sr); |
| 362 | |
| 363 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 364 | const kai_rhs_pack_qsi8cx_params params{.lhs_zero_point = 1, .scale_multiplier = 1.0f}; |
| 365 | kai_run_rhs_pack_kxn_qsi8cxp_qsi8cx_neon( |
| 366 | 1, N, K, nr, kr, sr, reinterpret_cast<const int8_t*>(ref_rhs_qsi8.data()), |
| 367 | reinterpret_cast<const float*>(ref_bias.data()), reinterpret_cast<const float*>(ref_rhs_scales.data()), |
| 368 | imp_packed_rhs.data(), 0, ¶ms); |
| 369 | |
| 370 | const auto packed_rhs_start_row = rect.start_col(); |
| 371 | auto rhs_packed_offset = |
| 372 | kai_get_rhs_packed_offset_rhs_pack_kxn_qsi8cxp_qsi8cx_neon(packed_rhs_start_row, K, nr, kr, sr); |
| 373 | |
| 374 | const auto dst_stride = N * sizeof(float); |
| 375 | const auto dst_offset = ukernel_variant.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride); |
| 376 | const auto ref_dst_offset = rect.start_row() * dst_stride + rect.start_col() * sizeof(float); |
| 377 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 378 | |
| 379 | const auto matmul_lhs_packed_offset = ukernel_variant.interface.get_lhs_packed_offset(rect.start_row(), K); |
| 380 | ASSERT_EQ(lhs_packed_offset, matmul_lhs_packed_offset); |
| 381 | const auto matmul_rhs_packed_offset = ukernel_variant.interface.get_rhs_packed_offset(rect.start_col(), K); |
| 382 | ASSERT_EQ(rhs_packed_offset, matmul_rhs_packed_offset); |
| 383 | |
| 384 | // Runs the GEMM micro-kernel. |
| 385 | const auto imp_dst_size = ukernel_variant.interface.get_dst_size(M, N); |
| 386 | ASSERT_EQ(imp_dst_size, ref_dst.size()); |
| 387 | Buffer imp_dst(imp_dst_size); |
| 388 | abi_check( |
| 389 | ukernel_variant.interface.run_matmul, rect.height(), rect.width(), K, |
| 390 | imp_packed_lhs.data() + matmul_lhs_packed_offset, imp_packed_rhs.data() + matmul_rhs_packed_offset, |
| 391 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), N * sizeof(float), sizeof(float), |
| 392 | testdata.clamp_range.min, testdata.clamp_range.max); |
| 393 | |
| 394 | // Compares the output of the micro-kernels against the output of the reference implementation. |
| 395 | DefaultMismatchHandler handler(0, 0.02, 0, 0.05); |
| 396 | DataFormat dst_format = DataFormat(DataType::FP32); |
| 397 | const auto success = compare(imp_dst.data(), ref_dst.data(), dst_format, M, N, rect, handler); |
| 398 | ASSERT_TRUE(success); |
| 399 | } |
| 400 | |
| 401 | INSTANTIATE_TEST_SUITE_P( |
| 402 | MatMul, MatMulTest_f32_qai8dxp_qsi8cxp, |
| 403 | testing::Combine( |
| 404 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.size()), |
| 405 | testing::Values( |
| 406 | MatMulShape{17, 33, 67}, // |
| 407 | MatMulShape{19, 35, 63}, // |
| 408 | MatMulShape{1, 27, 31}, // |
| 409 | MatMulShape{1, 65, 35}, // |
| 410 | MatMulShape{1, 64, 65}, // |
| 411 | MatMulShape{1, 63, 15}, // |
| 412 | MatMulShape{1, 71, 32}, // |
| 413 | MatMulShape{1, 130, 15}, // |
| 414 | MatMulShape{15, 65, 35}, // |
| 415 | MatMulShape{16, 64, 65}, // |
| 416 | MatMulShape{17, 63, 15}, // |
| 417 | MatMulShape{20, 130, 15}), |
| 418 | testing::Values( |
| 419 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 420 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 421 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 422 | MatrixPortion(0, 0.5, 1, 0.8), // Somewhere Middle |
| 423 | MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner. |
| 424 | MatrixPortion(0.75, 0, 1, 1), // Partial rows |
| 425 | MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle |
| 426 | ), |
| 427 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 428 | std::nullopt, // Disable clamping |
| 429 | 1.0f, // Clamp to full range |
| 430 | 0.9f, // Clamp to 90% range |
| 431 | 0.5f})), // Clamp to 50% range |
| 432 | [](const auto& info) { |
| 433 | const auto variant_idx = std::get<0>(info.param); |
| 434 | const std::string name{variants_kai_matmul_clamp_f32_qai8dxp_qsi8cxp.at(variant_idx).name}; |
| 435 | const auto shape = std::get<MatMulShape>(info.param); |
| 436 | const auto portion = std::get<MatrixPortion>(info.param); |
| 437 | const auto clamp_keep_ratio = std::get<std::optional<float>>(info.param); |
| 438 | |
| 439 | return test_description(name, shape, portion, true, clamp_keep_ratio); |
| 440 | }); |
| 441 | |
| 442 | } // namespace kai::test |