| 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 <functional> |
| 13 | #include <limits> |
| 14 | #include <map> |
| 15 | #include <string_view> |
| 16 | #include <tuple> |
| 17 | #include <utility> |
| 18 | |
| 19 | #include "kai/kai_common.h" |
| 20 | #include "test/common/abi_checker.hpp" |
| 21 | #include "test/common/buffer.hpp" |
| 22 | #include "test/common/compare.hpp" |
| 23 | #include "test/common/cpu_info.hpp" |
| 24 | #include "test/common/data_format.hpp" |
| 25 | #include "test/common/data_type.hpp" |
| 26 | #include "test/common/matmul_test_common.hpp" |
| 27 | #include "test/common/matrix_portion.hpp" |
| 28 | #include "test/common/printer.hpp" |
| 29 | #include "test/common/seed.hpp" |
| 30 | #include "test/common/sme.hpp" |
| 31 | #include "test/reference/cast.hpp" |
| 32 | #include "test/reference/clamp.hpp" |
| 33 | #include "test/reference/fill.hpp" |
| 34 | #include "test/reference/matmul.hpp" |
| 35 | #include "test/reference/pack.hpp" |
| 36 | |
| 37 | // Advanced SIMD kernels |
| 38 | #include "kai/ukernels/matmul/matmul_clamp_f32_bf16p_bf16p/kai_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot.h" |
| 39 | #include "kai/ukernels/matmul/matmul_clamp_f32_bf16p_bf16p/kai_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla.h" |
| 40 | #include "kai/ukernels/matmul/pack/kai_lhs_pack_bf16p8x4_f16_neon.h" |
| 41 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_bf16p1x4_f32_neon.h" |
| 42 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_bf16p8x4_f32_neon.h" |
| 43 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_bf16p12x4biasf32_f16_neon.h" |
| 44 | #include "kai/ukernels/matmul/pack/kai_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon.h" |
| 45 | |
| 46 | // SME kernels |
| 47 | #include "kai/ukernels/matmul/matmul_clamp_fp32_bf16p_bf16p/kai_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa.h" |
| 48 | #include "kai/ukernels/matmul/matmul_clamp_fp32_bf16p_bf16p/kai_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa.h" |
| 49 | #include "kai/ukernels/matmul/pack/kai_lhs_pack_bf16p2vlx2_f32_sme.h" |
| 50 | #include "kai/ukernels/matmul/pack/kai_lhs_pack_bf16p2vlx2_f32_sme2.h" |
| 51 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme.h" |
| 52 | |
| 53 | namespace kai::test { |
| 54 | |
| 55 | /// List of supported matrix multiplication methods. |
| 56 | namespace { |
| 57 | |
| 58 | static const std::array<MatMulMethod, 6>& get_gemm_methods() { |
| 59 | static std::array<MatMulMethod, 6> gemm_methods{}; |
| 60 | gemm_methods[0].name = "matmul_nt_nt_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa"; |
| 61 | gemm_methods[0].m0 = 2 * get_sme_vector_length<float>(); |
| 62 | gemm_methods[0].n0 = 2 * get_sme_vector_length<float>(); |
| 63 | gemm_methods[0].k0 = 2; |
| 64 | gemm_methods[0].dst_format = DataFormat(DataType::FP32); |
| 65 | gemm_methods[0].lhs_format = DataFormat(DataType::FP32); |
| 66 | gemm_methods[0].packed_lhs_format = DataFormat( |
| 67 | DataType::BF16, 2 * get_sme_vector_length<float>(), 2, DataFormat::PackFormat::NONE, DataType::FP32, |
| 68 | DataType::UNKNOWN, 2 * get_sme_vector_length<float>(), 2); |
| 69 | gemm_methods[0].rhs_format = DataFormat(DataType::FP32); |
| 70 | gemm_methods[0].packed_rhs_format = DataFormat( |
| 71 | DataType::BF16, 2 * get_sme_vector_length<float>(), 2, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, |
| 72 | DataType::UNKNOWN, 2 * get_sme_vector_length<float>(), 2); |
| 73 | gemm_methods[0].bias_format = DataFormat(DataType::FP32); |
| 74 | gemm_methods[0].fn_is_supported = cpu_has_sme2; |
| 75 | gemm_methods[0].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 76 | gemm_methods[0].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 77 | gemm_methods[0].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 78 | gemm_methods[0].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 79 | gemm_methods[0].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 80 | gemm_methods[0].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 81 | gemm_methods[0].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 82 | gemm_methods[0].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p2vlx2_f32_sme2; |
| 83 | gemm_methods[0].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p2vlx2_f32_sme2; |
| 84 | gemm_methods[0].fn_get_packed_lhs_offset = |
| 85 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 86 | gemm_methods[0].fn_pack_lhs = kai_run_lhs_pack_bf16p2vlx2_f32_sme2; |
| 87 | gemm_methods[0].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 88 | gemm_methods[0].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 89 | gemm_methods[0].fn_get_main_packed_rhs_offset = |
| 90 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 91 | gemm_methods[0].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 92 | gemm_methods[0].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 93 | gemm_methods[0].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 94 | gemm_methods[0].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 95 | gemm_methods[0].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme2_mopa; |
| 96 | |
| 97 | gemm_methods[1].name = "matmul_nt_nt_f32_bf16p_bf16p_8x12_neon_mla"; |
| 98 | gemm_methods[1].m0 = 8; |
| 99 | gemm_methods[1].n0 = 12; |
| 100 | gemm_methods[1].k0 = 4; |
| 101 | gemm_methods[1].dst_format = DataFormat(DataType::FP32); |
| 102 | gemm_methods[1].lhs_format = DataFormat(DataType::FP32); |
| 103 | gemm_methods[1].packed_lhs_format = |
| 104 | DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP32, DataType::UNKNOWN, 8, 4); |
| 105 | gemm_methods[1].rhs_format = DataFormat(DataType::FP32); |
| 106 | gemm_methods[1].packed_rhs_format = DataFormat( |
| 107 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 108 | gemm_methods[1].bias_format = DataFormat(DataType::FP32); |
| 109 | gemm_methods[1].fn_is_supported = cpu_has_bf16; |
| 110 | gemm_methods[1].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 111 | gemm_methods[1].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 112 | gemm_methods[1].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 113 | gemm_methods[1].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 114 | gemm_methods[1].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 115 | gemm_methods[1].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 116 | gemm_methods[1].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 117 | gemm_methods[1].fn_get_lhs_offset = kai_get_lhs_offset_lhs_quant_pack_bf16p8x4_f32_neon; |
| 118 | gemm_methods[1].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_bf16p8x4_f32_neon; |
| 119 | gemm_methods[1].fn_get_packed_lhs_offset = |
| 120 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 121 | gemm_methods[1].fn_pack_lhs = kai_run_lhs_quant_pack_bf16p8x4_f32_neon; |
| 122 | gemm_methods[1].fn_get_rhs_offset = kai_get_rhs_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 123 | gemm_methods[1].fn_get_packed_rhs_size_generic_block_size = |
| 124 | kai_get_rhs_packed_size_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 125 | gemm_methods[1].fn_get_main_packed_rhs_offset = |
| 126 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 127 | gemm_methods[1].fn_pack_rhs = kai_run_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 128 | gemm_methods[1].fn_get_bias_offset = kai_get_bias_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 129 | gemm_methods[1].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 130 | gemm_methods[1].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 131 | gemm_methods[1].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 132 | |
| 133 | gemm_methods[2].name = "matmul_nt_nt_f32_bf16p_bf16p_8x12_neon_mla_f16_inputs_f32_bias_and_output"; |
| 134 | gemm_methods[2].m0 = 8; |
| 135 | gemm_methods[2].n0 = 12; |
| 136 | gemm_methods[2].k0 = 4; |
| 137 | gemm_methods[2].dst_format = DataFormat(DataType::FP32); |
| 138 | gemm_methods[2].lhs_format = DataFormat(DataType::FP16); |
| 139 | gemm_methods[2].packed_lhs_format = |
| 140 | DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP16, DataType::UNKNOWN, 8, 4); |
| 141 | gemm_methods[2].rhs_format = DataFormat(DataType::FP16); |
| 142 | gemm_methods[2].packed_rhs_format = DataFormat( |
| 143 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 144 | gemm_methods[2].bias_format = DataFormat(DataType::FP32); |
| 145 | gemm_methods[2].fn_is_supported = cpu_has_bf16; |
| 146 | gemm_methods[2].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 147 | gemm_methods[2].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 148 | gemm_methods[2].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 149 | gemm_methods[2].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 150 | gemm_methods[2].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 151 | gemm_methods[2].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 152 | gemm_methods[2].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 153 | gemm_methods[2].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p8x4_f16_neon; |
| 154 | gemm_methods[2].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p8x4_f16_neon; |
| 155 | gemm_methods[2].fn_get_packed_lhs_offset = |
| 156 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 157 | gemm_methods[2].fn_pack_lhs = kai_run_lhs_pack_bf16p8x4_f16_neon; |
| 158 | gemm_methods[2].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 159 | gemm_methods[2].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 160 | gemm_methods[2].fn_get_main_packed_rhs_offset = |
| 161 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 162 | gemm_methods[2].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 163 | gemm_methods[2].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 164 | gemm_methods[2].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 165 | gemm_methods[2].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 166 | gemm_methods[2].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 167 | |
| 168 | gemm_methods[3].name = "matmul_nt_nt_f32_bf16p_bf16p_8x12_neon_mla_f16_inputs_f32_bias_and_output_opt_bias"; |
| 169 | gemm_methods[3].m0 = 8; |
| 170 | gemm_methods[3].n0 = 12; |
| 171 | gemm_methods[3].k0 = 4; |
| 172 | gemm_methods[3].dst_format = DataFormat(DataType::FP32); |
| 173 | gemm_methods[3].lhs_format = DataFormat(DataType::FP16); |
| 174 | gemm_methods[3].packed_lhs_format = |
| 175 | DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP16, DataType::UNKNOWN, 8, 4); |
| 176 | gemm_methods[3].rhs_format = DataFormat(DataType::FP16); |
| 177 | gemm_methods[3].packed_rhs_format = DataFormat( |
| 178 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 179 | gemm_methods[3].bias_format = DataFormat(DataType::UNKNOWN); |
| 180 | gemm_methods[3].fn_is_supported = cpu_has_bf16; |
| 181 | gemm_methods[3].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 182 | gemm_methods[3].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 183 | gemm_methods[3].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 184 | gemm_methods[3].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 185 | gemm_methods[3].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 186 | gemm_methods[3].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 187 | gemm_methods[3].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 188 | gemm_methods[3].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p8x4_f16_neon; |
| 189 | gemm_methods[3].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p8x4_f16_neon; |
| 190 | gemm_methods[3].fn_get_packed_lhs_offset = |
| 191 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 192 | gemm_methods[3].fn_pack_lhs = kai_run_lhs_pack_bf16p8x4_f16_neon; |
| 193 | gemm_methods[3].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 194 | gemm_methods[3].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 195 | gemm_methods[3].fn_get_main_packed_rhs_offset = |
| 196 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 197 | gemm_methods[3].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 198 | gemm_methods[3].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p12x4biasf32_f16_neon; |
| 199 | gemm_methods[3].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 200 | gemm_methods[3].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 201 | gemm_methods[3].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 202 | |
| 203 | gemm_methods[4].name = "matmul_nt_nt_f32_bf16p_bf16p_8x12_neon_mla_opt_bias"; |
| 204 | gemm_methods[4].m0 = 8; |
| 205 | gemm_methods[4].n0 = 12; |
| 206 | gemm_methods[4].k0 = 4; |
| 207 | gemm_methods[4].dst_format = DataFormat(DataType::FP32); |
| 208 | gemm_methods[4].lhs_format = DataFormat(DataType::FP32); |
| 209 | gemm_methods[4].packed_lhs_format = |
| 210 | DataFormat(DataType::BF16, 8, 4, DataFormat::PackFormat::NONE, DataType::FP32, DataType::UNKNOWN, 8, 4); |
| 211 | gemm_methods[4].rhs_format = DataFormat(DataType::FP32); |
| 212 | gemm_methods[4].packed_rhs_format = DataFormat( |
| 213 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 214 | gemm_methods[4].bias_format = DataFormat(DataType::UNKNOWN); |
| 215 | gemm_methods[4].fn_is_supported = cpu_has_bf16; |
| 216 | gemm_methods[4].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 217 | gemm_methods[4].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 218 | gemm_methods[4].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 219 | gemm_methods[4].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 220 | gemm_methods[4].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 221 | gemm_methods[4].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 222 | gemm_methods[4].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 223 | gemm_methods[4].fn_get_lhs_offset = kai_get_lhs_offset_lhs_quant_pack_bf16p8x4_f32_neon; |
| 224 | gemm_methods[4].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_bf16p8x4_f32_neon; |
| 225 | gemm_methods[4].fn_get_packed_lhs_offset = |
| 226 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 227 | gemm_methods[4].fn_pack_lhs = kai_run_lhs_quant_pack_bf16p8x4_f32_neon; |
| 228 | gemm_methods[4].fn_get_rhs_offset = kai_get_rhs_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 229 | gemm_methods[4].fn_get_packed_rhs_size_generic_block_size = |
| 230 | kai_get_rhs_packed_size_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 231 | gemm_methods[4].fn_get_main_packed_rhs_offset = |
| 232 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 233 | gemm_methods[4].fn_pack_rhs = kai_run_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 234 | gemm_methods[4].fn_get_bias_offset = kai_get_bias_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 235 | gemm_methods[4].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 236 | gemm_methods[4].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 237 | gemm_methods[4].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p8x4_bf16p12x4b_8x12_neon_mmla; |
| 238 | |
| 239 | gemm_methods[5].name = "matmul_nt_nt_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa"; |
| 240 | gemm_methods[5].m0 = 2 * get_sme_vector_length<float>(); |
| 241 | gemm_methods[5].n0 = 2 * get_sme_vector_length<float>(); |
| 242 | gemm_methods[5].k0 = 2; |
| 243 | gemm_methods[5].dst_format = DataFormat(DataType::FP32); |
| 244 | gemm_methods[5].lhs_format = DataFormat(DataType::FP32); |
| 245 | gemm_methods[5].packed_lhs_format = DataFormat( |
| 246 | DataType::BF16, 2 * get_sme_vector_length<float>(), 2, DataFormat::PackFormat::NONE, DataType::FP32, |
| 247 | DataType::UNKNOWN, 2 * get_sme_vector_length<float>(), 2); |
| 248 | gemm_methods[5].rhs_format = DataFormat(DataType::FP32); |
| 249 | gemm_methods[5].packed_rhs_format = DataFormat( |
| 250 | DataType::BF16, 2 * get_sme_vector_length<float>(), 2, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, |
| 251 | DataType::UNKNOWN, 2 * get_sme_vector_length<float>(), 2); |
| 252 | gemm_methods[5].bias_format = DataFormat(DataType::FP32); |
| 253 | gemm_methods[5].fn_is_supported = cpu_has_sme; |
| 254 | gemm_methods[5].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 255 | gemm_methods[5].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 256 | gemm_methods[5].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 257 | gemm_methods[5].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 258 | gemm_methods[5].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 259 | gemm_methods[5].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 260 | gemm_methods[5].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 261 | gemm_methods[5].fn_get_lhs_offset = kai_get_lhs_offset_lhs_pack_bf16p2vlx2_f32_sme; |
| 262 | gemm_methods[5].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_pack_bf16p2vlx2_f32_sme; |
| 263 | gemm_methods[5].fn_get_packed_lhs_offset = |
| 264 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 265 | gemm_methods[5].fn_pack_lhs = kai_run_lhs_pack_bf16p2vlx2_f32_sme; |
| 266 | gemm_methods[5].fn_get_rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 267 | gemm_methods[5].fn_get_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 268 | gemm_methods[5].fn_get_main_packed_rhs_offset = |
| 269 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 270 | gemm_methods[5].fn_pack_rhs = kai_run_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 271 | gemm_methods[5].fn_get_bias_offset = kai_get_bias_offset_rhs_pack_kxn_bf16p2vlx2b_f32_x32_sme; |
| 272 | gemm_methods[5].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 273 | gemm_methods[5].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 274 | gemm_methods[5].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p2vlx2_bf16p2vlx2_2vlx2vl_sme_mopa; |
| 275 | |
| 276 | return gemm_methods; |
| 277 | } |
| 278 | |
| 279 | static const std::array<MatMulMethod, 2>& get_gemv_methods() { |
| 280 | static std::array<MatMulMethod, 2> gemv_methods{}; |
| 281 | gemv_methods[0].name = "matmul_nt_nt_f32_bf16p_bf16p_1x36_neon_dot"; |
| 282 | gemv_methods[0].m0 = 1; |
| 283 | gemv_methods[0].n0 = 12; |
| 284 | gemv_methods[0].k0 = 4; |
| 285 | gemv_methods[0].dst_format = DataFormat(DataType::FP32); |
| 286 | gemv_methods[0].lhs_format = DataFormat(DataType::FP32); |
| 287 | gemv_methods[0].packed_lhs_format = |
| 288 | DataFormat(DataType::BF16, 1, 4, DataFormat::PackFormat::NONE, DataType::FP32, DataType::UNKNOWN, 1, 4); |
| 289 | gemv_methods[0].rhs_format = DataFormat(DataType::FP32); |
| 290 | gemv_methods[0].packed_rhs_format = DataFormat( |
| 291 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 292 | gemv_methods[0].bias_format = DataFormat(DataType::FP32); |
| 293 | gemv_methods[0].fn_is_supported = cpu_has_bf16; |
| 294 | gemv_methods[0].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 295 | gemv_methods[0].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 296 | gemv_methods[0].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 297 | gemv_methods[0].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 298 | gemv_methods[0].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 299 | gemv_methods[0].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 300 | gemv_methods[0].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 301 | gemv_methods[0].fn_get_lhs_offset = kai_get_lhs_offset_lhs_quant_pack_bf16p1x4_f32_neon; |
| 302 | gemv_methods[0].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_bf16p1x4_f32_neon; |
| 303 | gemv_methods[0].fn_get_packed_lhs_offset = |
| 304 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 305 | gemv_methods[0].fn_pack_lhs = kai_run_lhs_quant_pack_bf16p1x4_f32_neon; |
| 306 | gemv_methods[0].fn_get_rhs_offset = kai_get_rhs_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 307 | gemv_methods[0].fn_get_packed_rhs_size_generic_block_size = |
| 308 | kai_get_rhs_packed_size_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 309 | gemv_methods[0].fn_get_main_packed_rhs_offset = |
| 310 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 311 | gemv_methods[0].fn_pack_rhs = kai_run_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 312 | gemv_methods[0].fn_get_bias_offset = kai_get_bias_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 313 | gemv_methods[0].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 314 | gemv_methods[0].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 315 | gemv_methods[0].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 316 | |
| 317 | gemv_methods[1].name = "matmul_nt_nt_f32_bf16p_bf16p_1x36_neon_dot_opt_bias"; |
| 318 | gemv_methods[1].m0 = 1; |
| 319 | gemv_methods[1].n0 = 12; |
| 320 | gemv_methods[1].k0 = 4; |
| 321 | gemv_methods[1].dst_format = DataFormat(DataType::FP32); |
| 322 | gemv_methods[1].lhs_format = DataFormat(DataType::FP32); |
| 323 | gemv_methods[1].packed_lhs_format = |
| 324 | DataFormat(DataType::BF16, 1, 4, DataFormat::PackFormat::NONE, DataType::FP32, DataType::UNKNOWN, 1, 4); |
| 325 | gemv_methods[1].rhs_format = DataFormat(DataType::FP32); |
| 326 | gemv_methods[1].packed_rhs_format = DataFormat( |
| 327 | DataType::BF16, 12, 4, DataFormat::PackFormat::BIAS_PER_ROW, DataType::FP32, DataType::UNKNOWN, 12, 4); |
| 328 | gemv_methods[1].bias_format = DataFormat(DataType::UNKNOWN); |
| 329 | gemv_methods[1].fn_is_supported = cpu_has_bf16; |
| 330 | gemv_methods[1].fn_get_mr = kai_get_mr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 331 | gemv_methods[1].fn_get_nr = kai_get_nr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 332 | gemv_methods[1].fn_get_kr = kai_get_kr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 333 | gemv_methods[1].fn_get_sr = kai_get_sr_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 334 | gemv_methods[1].fn_get_main_m_step = kai_get_m_step_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 335 | gemv_methods[1].fn_get_pack_rhs_n_step = kai_get_n_step_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 336 | gemv_methods[1].fn_get_main_n_step = kai_get_n_step_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 337 | gemv_methods[1].fn_get_lhs_offset = kai_get_lhs_offset_lhs_quant_pack_bf16p1x4_f32_neon; |
| 338 | gemv_methods[1].fn_get_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_bf16p1x4_f32_neon; |
| 339 | gemv_methods[1].fn_get_packed_lhs_offset = |
| 340 | kai_get_lhs_packed_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 341 | gemv_methods[1].fn_pack_lhs = kai_run_lhs_quant_pack_bf16p1x4_f32_neon; |
| 342 | gemv_methods[1].fn_get_rhs_offset = kai_get_rhs_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 343 | gemv_methods[1].fn_get_packed_rhs_size_generic_block_size = |
| 344 | kai_get_rhs_packed_size_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 345 | gemv_methods[1].fn_get_main_packed_rhs_offset = |
| 346 | kai_get_rhs_packed_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 347 | gemv_methods[1].fn_pack_rhs = kai_run_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 348 | gemv_methods[1].fn_get_bias_offset = kai_get_bias_offset_rhs_quant_pack_kxn_bf16p12x4biasf32_f32_neon; |
| 349 | gemv_methods[1].fn_get_dst_offset = kai_get_dst_offset_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 350 | gemv_methods[1].fn_get_dst_size = kai_get_dst_size_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 351 | gemv_methods[1].fn_matmul_f32_bf16p_bf16p = kai_run_matmul_clamp_f32_bf16p1x4_bf16p12x4b_1x36_neon_dot; |
| 352 | |
| 353 | return gemv_methods; |
| 354 | } |
| 355 | |
| 356 | } // namespace |
| 357 | |
| 358 | /// Matrix multiplication test fixture. |
| 359 | class MatMulTestBf16 : public testing::TestWithParam<MatMulClampTestParams> { |
| 360 | private: |
| 361 | /// Unique ID: m, n, k |
| 362 | using TestDataId = std::tuple<size_t, size_t, size_t, std::optional<float>, std::string_view>; |
| 363 | |
| 364 | protected: |
| 365 | /// Cached test data that is shared between multiple test case. |
| 366 | struct TestData { |
| 367 | Buffer lhs{}; ///< LHS operand. |
| 368 | Buffer ref_packed_lhs{}; ///< Reference packed LHS. |
| 369 | Buffer rhs{}; ///< RHS operand. |
| 370 | Buffer rhs_scales{}; ///< RHS per-row quantization scales. |
| 371 | Buffer bias{}; ///< Bias. |
| 372 | Buffer ref_packed_rhs{}; ///< Reference packed RHS. |
| 373 | Buffer ref_dst{}; ///< Reference output. |
| 374 | Buffer ref_clamped{}; ///< Reference clamped. |
| 375 | Range<float> clamp_range; ///< Clamping Range. |
| 376 | }; |
| 377 | |
| 378 | /// Gets the test data for the current test case. |
| 379 | static const TestData& test_data() { |
| 380 | const auto& [method, info, portion, bias_mode, clamp_keep_ratio] = GetParam(); |
| 381 | const TestDataId data_id{info.m, info.n, info.k, clamp_keep_ratio, method.name}; |
| 382 | |
| 383 | // Creates a unique seed for the test data. |
| 384 | const auto key = std::string(method.name) + "_" + std::to_string(info.m) + "x" + std::to_string(info.n) + "x" + |
| 385 | std::to_string(info.k) + "_" + (bias_mode == BiasMode::INTERNAL ? "internal" : "provided") + ":" + |
| 386 | (clamp_keep_ratio ? std::to_string(clamp_keep_ratio.value()) : "noclamp"); |
| 387 | auto& feed = seed_stream(key); |
| 388 | |
| 389 | // If the test data is already available, returns it. |
| 390 | const auto data_it = _data.find(data_id); |
| 391 | |
| 392 | if (data_it != _data.end()) { |
| 393 | return data_it->second; |
| 394 | } |
| 395 | |
| 396 | // Generates the test data. |
| 397 | const auto has_lhs_pack = method.packed_lhs_format.data_type() != DataType::UNKNOWN; |
| 398 | const auto has_rhs_pack = method.packed_rhs_format.data_type() != DataType::UNKNOWN; |
| 399 | const auto has_bias = method.bias_format.data_type() != DataType::UNKNOWN; |
| 400 | |
| 401 | const auto lhs_h = info.m; |
| 402 | const auto lhs_w = info.k; |
| 403 | auto lhs = fill_matrix_random(lhs_h, lhs_w, method.lhs_format, feed()); |
| 404 | Buffer ref_packed_lhs; |
| 405 | |
| 406 | if (has_lhs_pack) { |
| 407 | ref_packed_lhs = |
| 408 | pack(method.packed_lhs_format, lhs.data(), nullptr, nullptr, method.lhs_format, lhs_h, lhs_w); |
| 409 | } |
| 410 | |
| 411 | const auto rhs_h = info.k; |
| 412 | const auto rhs_w = info.n; |
| 413 | auto rhs = fill_matrix_random(rhs_h, rhs_w, method.rhs_format, feed()); |
| 414 | |
| 415 | Buffer rhs_scales; |
| 416 | if (data_type_is_quantized(method.rhs_format.data_type()) && |
| 417 | method.rhs_format.pack_format() == DataFormat::PackFormat::NONE) { |
| 418 | rhs_scales = fill_matrix_random(rhs_h, 1, DataFormat(DataType::FP32), feed()); |
| 419 | } |
| 420 | |
| 421 | const auto bias_h = 1; |
| 422 | const auto bias_w = info.n; |
| 423 | Buffer bias; |
| 424 | |
| 425 | if (has_bias) { |
| 426 | bias = fill_matrix_random(bias_h, bias_w, method.bias_format, feed()); |
| 427 | } |
| 428 | |
| 429 | constexpr size_t nr = 12; |
| 430 | constexpr size_t kr = 4; |
| 431 | |
| 432 | size_t packed_rhs_size = 0; |
| 433 | |
| 434 | if (method.fn_get_packed_rhs_size) { |
| 435 | packed_rhs_size = method.fn_get_packed_rhs_size(rhs_w, rhs_h); |
| 436 | } else if (method.fn_get_packed_rhs_size_generic_block_size) { |
| 437 | packed_rhs_size = method.fn_get_packed_rhs_size_generic_block_size(rhs_w, rhs_h, nr, kr); |
| 438 | } else { |
| 439 | KAI_ERROR("No function to calculate Packed Rhs Matrix Size"); |
| 440 | } |
| 441 | |
| 442 | Buffer packed_rhs(packed_rhs_size); |
| 443 | |
| 444 | if (has_rhs_pack) { |
| 445 | const auto ref_rhs_row_stride = method.rhs_format.default_row_stride(rhs_w); |
| 446 | method.pack_rhs( |
| 447 | info.n, info.k, rhs.data(), ref_rhs_row_stride, has_bias ? bias.data() : nullptr, nullptr, |
| 448 | packed_rhs.data()); |
| 449 | } |
| 450 | |
| 451 | KAI_ASSUME_ALWAYS(method.lhs_format.is_raw()); |
| 452 | KAI_ASSUME_ALWAYS(method.rhs_format.is_raw()); |
| 453 | KAI_ASSUME_ALWAYS(method.dst_format.is_raw()); |
| 454 | |
| 455 | Buffer tmp_lhs; |
| 456 | Buffer tmp_rhs; |
| 457 | const void* p_lhs_buff = lhs.data(); |
| 458 | const void* p_rhs_buff = rhs.data(); |
| 459 | |
| 460 | if (method.lhs_format.data_type() == DataType::FP32 || method.lhs_format.data_type() == DataType::FP16) { |
| 461 | tmp_lhs = cast(p_lhs_buff, method.lhs_format.data_type(), DataType::BF16, lhs_h, lhs_w); |
| 462 | p_lhs_buff = tmp_lhs.data(); |
| 463 | } |
| 464 | if (method.rhs_format.data_type() == DataType::FP32 || method.rhs_format.data_type() == DataType::FP16) { |
| 465 | tmp_rhs = cast(p_rhs_buff, method.rhs_format.data_type(), DataType::BF16, rhs_h, rhs_w); |
| 466 | p_rhs_buff = tmp_rhs.data(); |
| 467 | } |
| 468 | |
| 469 | auto ref_dst = |
| 470 | matmul_nt_nt_quantized<BFloat16<>, float, float, BFloat16<>, float, float, float, float, float, float>( |
| 471 | info.m, info.n, info.k, p_lhs_buff, nullptr, nullptr, 1, info.k, p_rhs_buff, nullptr, nullptr, 1, |
| 472 | info.k, bias.data(), nullptr, nullptr, info.k); |
| 473 | |
| 474 | const auto [min, max] = |
| 475 | find_clamp_range(method.dst_format.data_type(), ref_dst.data(), info.m * info.n, clamp_keep_ratio); |
| 476 | |
| 477 | auto ref_clamped = clamp(DataType::FP32, ref_dst.data(), info.m * info.n, min, max); |
| 478 | |
| 479 | auto& data = _data[data_id] = {}; |
| 480 | data.lhs = std::move(lhs); |
| 481 | data.ref_packed_lhs = std::move(ref_packed_lhs); |
| 482 | data.rhs = std::move(rhs); |
| 483 | data.rhs_scales = std::move(rhs_scales); |
| 484 | data.bias = std::move(bias); |
| 485 | data.ref_packed_rhs = std::move(packed_rhs); |
| 486 | data.ref_dst = std::move(ref_dst); |
| 487 | data.ref_clamped = std::move(ref_clamped); |
| 488 | data.clamp_range = {min, max}; |
| 489 | |
| 490 | return data; |
| 491 | } |
| 492 | |
| 493 | private: |
| 494 | // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) |
| 495 | static std::map<TestDataId, TestData> _data; |
| 496 | // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) |
| 497 | }; |
| 498 | |
| 499 | // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) |
| 500 | std::map<MatMulTestBf16::TestDataId, MatMulTestBf16::TestData> MatMulTestBf16::_data; |
| 501 | // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) |
| 502 | |
| 503 | /// Tests the output. |
| 504 | TEST_P(MatMulTestBf16, Output) { |
| 505 | const auto& [method, info, portion, clamp_keep_ratio, bias_mode] = GetParam(); |
| 506 | |
| 507 | if (method.fn_is_supported && !method.fn_is_supported()) { |
| 508 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 509 | } |
| 510 | |
| 511 | if (!method.has_main_kernel()) { |
| 512 | GTEST_SKIP() << "No main kernel available"; |
| 513 | } |
| 514 | |
| 515 | const auto& data = test_data(); |
| 516 | const auto m_step = method.fn_get_main_m_step(); |
| 517 | ASSERT_TRUE(m_step % method.m0 == 0); |
| 518 | |
| 519 | const auto n_step = method.fn_get_main_n_step(); |
| 520 | ASSERT_TRUE(n_step % method.n0 == 0); |
| 521 | |
| 522 | const auto rect = portion.compute_portion(info.m, info.n, m_step, n_step); |
| 523 | |
| 524 | if (rect.height() == 0 || rect.width() == 0) { |
| 525 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 526 | } |
| 527 | |
| 528 | const size_t lhs_w = info.k; |
| 529 | const size_t rhs_w = rect.width(); |
| 530 | const size_t bias_w = info.n; |
| 531 | const size_t dst_w = info.n; |
| 532 | const bool has_bias = (data.bias.size() > 0); |
| 533 | |
| 534 | const auto lhs_start_row = rect.start_row(); |
| 535 | const auto lhs_stride = method.lhs_format.default_row_stride(lhs_w); |
| 536 | |
| 537 | const size_t lhs_packed_size = method.fn_get_packed_lhs_size(info.m, info.k, method.m0, method.k0, 1 /* sr */); |
| 538 | Buffer lhs_data(lhs_packed_size); |
| 539 | |
| 540 | uintptr_t lhs_offset = method.fn_get_lhs_offset(lhs_start_row, lhs_stride); |
| 541 | uintptr_t lhs_packed_offset = method.fn_get_packed_lhs_offset(lhs_start_row, info.k); |
| 542 | |
| 543 | KAI_UNUSED(lhs_offset); |
| 544 | abi_check( |
| 545 | method.fn_pack_lhs, rect.height(), info.k, method.m0, method.k0, 1 /* sr */, 0 /* m_idx_start */, |
| 546 | data.lhs.data() + lhs_offset, lhs_stride, lhs_data.data() + lhs_packed_offset); |
| 547 | |
| 548 | const auto rhs_stride = method.rhs_format.default_row_stride(info.n); |
| 549 | |
| 550 | size_t rhs_packed_size = 0; |
| 551 | |
| 552 | if (method.fn_get_packed_rhs_size_generic_block_size) { |
| 553 | rhs_packed_size = method.fn_get_packed_rhs_size_generic_block_size(info.n, info.k, method.n0, method.k0); |
| 554 | } else if (method.fn_get_packed_rhs_size) { |
| 555 | rhs_packed_size = method.fn_get_packed_rhs_size(info.n, info.k); |
| 556 | } |
| 557 | |
| 558 | Buffer rhs_data(rhs_packed_size); |
| 559 | |
| 560 | const auto packed_rhs_start_row = rect.start_col(); |
| 561 | const auto packed_rhs_start_col = 0; |
| 562 | |
| 563 | uintptr_t rhs_offset = method.fn_get_rhs_offset(rect.start_col()); |
| 564 | uintptr_t rhs_packed_offset = method.fn_get_main_packed_rhs_offset(packed_rhs_start_row, info.k); |
| 565 | const auto ref_rhs_packed_offset = |
| 566 | method.packed_rhs_format.default_offset_in_bytes(packed_rhs_start_row, packed_rhs_start_col, info.k); |
| 567 | |
| 568 | ASSERT_EQ(rhs_packed_offset, ref_rhs_packed_offset); |
| 569 | |
| 570 | uintptr_t bias_offset = sizeof(float) * rect.start_col(); |
| 571 | |
| 572 | abi_check( |
| 573 | method.fn_pack_rhs, |
| 574 | 1, // num_groups |
| 575 | rhs_w, info.k, method.n0, method.k0, |
| 576 | 1, // sr |
| 577 | rhs_stride, data.rhs.data() + rhs_offset, has_bias ? data.bias.data() + bias_offset : nullptr, |
| 578 | nullptr, // Scale |
| 579 | rhs_data.data() + rhs_packed_offset, 0, nullptr); |
| 580 | |
| 581 | if (has_bias) { |
| 582 | const auto ref_bias_offset = method.bias_format.default_offset_in_bytes(0, rect.start_col(), bias_w); |
| 583 | ASSERT_EQ(ref_bias_offset, bias_offset); |
| 584 | } |
| 585 | |
| 586 | const auto dst_stride = method.dst_format.default_row_stride(dst_w); |
| 587 | const auto dst_offset = method.fn_get_dst_offset(rect.start_row(), rect.start_col(), dst_stride); |
| 588 | const auto ref_dst_offset = method.dst_format.default_offset_in_bytes(rect.start_row(), rect.start_col(), dst_w); |
| 589 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 590 | |
| 591 | const auto dst_size = method.fn_get_dst_size(info.m, info.n); |
| 592 | const auto ref_dst_size = method.dst_format.default_size_in_bytes(info.m, info.n); |
| 593 | ASSERT_EQ(dst_size, ref_dst_size); |
| 594 | |
| 595 | Buffer dst(dst_size); |
| 596 | abi_check( |
| 597 | &MatMulMethod::main_kernel, method, rect.height(), rect.width(), info.k, lhs_data.data() + lhs_packed_offset, |
| 598 | rhs_data.data() + rhs_packed_offset, nullptr, dst.data() + dst_offset, lhs_stride, rhs_stride, dst_stride, |
| 599 | data.clamp_range.min, data.clamp_range.max); |
| 600 | |
| 601 | DefaultMismatchHandler handler(0, 0.02, 0, 0.05); |
| 602 | const auto success = compare(dst.data(), data.ref_clamped.data(), method.dst_format, info.m, info.n, rect, handler); |
| 603 | |
| 604 | ASSERT_TRUE(success); |
| 605 | } |
| 606 | |
| 607 | INSTANTIATE_TEST_SUITE_P( |
| 608 | MatMulGemm, MatMulTestBf16, |
| 609 | testing::Combine( |
| 610 | testing::ValuesIn(get_gemm_methods()), |
| 611 | testing::Values( |
| 612 | MatMulShape{1, 1, 1}, // Smallest Possible Shape |
| 613 | MatMulShape{3, 7, 3}, // Smaller than block size |
| 614 | MatMulShape{12, 8, 4}, // Same block size |
| 615 | MatMulShape{1, 1, 1023}, // Long K |
| 616 | MatMulShape{1013, 1, 5}, // Long M |
| 617 | MatMulShape{2, 1013, 6}, // Long N |
| 618 | MatMulShape{13, 33, 23}, // |
| 619 | MatMulShape{93, 57, 89}, // |
| 620 | MatMulShape{256, 256, 256}, // Nice shapes |
| 621 | MatMulShape{257, 113, 373} // Prime numbers |
| 622 | ), |
| 623 | testing::Values( |
| 624 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 625 | MatrixPortion(0, 0, 0.25, 0.25), // Top-left corner. |
| 626 | MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner. |
| 627 | MatrixPortion(0.75, 0, 1, 1), // Partial rows |
| 628 | MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle |
| 629 | ), |
| 630 | testing::Values(BiasMode::PROVIDED), // |
| 631 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 632 | std::nullopt, // Disable clamping |
| 633 | 1.0f, // Clamp to full range |
| 634 | 0.9f, // Clamp to 90% range |
| 635 | 0.5f})), // Clamp to 50% range |
| 636 | testing::PrintToStringParamName()); |
| 637 | |
| 638 | INSTANTIATE_TEST_SUITE_P( |
| 639 | MatMulGemv, MatMulTestBf16, |
| 640 | testing::Combine( |
| 641 | testing::ValuesIn(get_gemv_methods()), |
| 642 | testing::Values( |
| 643 | MatMulShape{1, 1, 1}, // Smallest Possible Shape |
| 644 | MatMulShape{1, 1, 1023}, // Long K |
| 645 | MatMulShape{1, 1023, 1}, // Long N |
| 646 | MatMulShape{1, 1013, 1023}, // Large Rhs |
| 647 | MatMulShape{1, 37, 23}, // |
| 648 | MatMulShape{1, 57, 89}, // |
| 649 | MatMulShape{1, 36, 89}, // |
| 650 | MatMulShape{1, 71, 32}, // |
| 651 | MatMulShape{1, 98, 23}, // |
| 652 | MatMulShape{1, 64, 1024}, // Nice shapes - Long Rhs Rect |
| 653 | MatMulShape{1, 1024, 64}, // Nice shapes - Wide Rhs Rect |
| 654 | MatMulShape{1, 256, 256}, // Nice shapes - Square |
| 655 | MatMulShape{1, 113, 373} // Prime numbers |
| 656 | ), |
| 657 | testing::Values( |
| 658 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 659 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 660 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 661 | MatrixPortion(0, 0.5, 1, 0.8) // Somewhere Middle |
| 662 | ), |
| 663 | testing::Values(BiasMode::PROVIDED), // |
| 664 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 665 | std::nullopt, // Disable clamping |
| 666 | 1.0f, // Clamp to full range |
| 667 | 0.9f, // Clamp to 90% range |
| 668 | 0.5f})), // Clamp to 50% range |
| 669 | testing::PrintToStringParamName()); |
| 670 | } // namespace kai::test |