| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-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 <initializer_list> |
| 12 | #include <string_view> |
| 13 | #include <tuple> |
| 14 | #include <unordered_map> |
| 15 | |
| 16 | #include "kai/ukernels/matmul/imatmul_clamp_f16_f16p_f16p/kai_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa.h" |
| 17 | #include "kai/ukernels/matmul/imatmul_clamp_f16_f16p_f16p/kai_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa.h" |
| 18 | #include "kai/ukernels/matmul/imatmul_clamp_f16_f16p_f16p/kai_imatmul_clamp_f16_f16p_f16p_interface.h" |
| 19 | #include "kai/ukernels/matmul/imatmul_clamp_f32_f32_f32p/kai_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla.h" |
| 20 | #include "kai/ukernels/matmul/imatmul_clamp_f32_f32_f32p/kai_imatmul_clamp_f32_f32_f32p_interface.h" |
| 21 | #include "kai/ukernels/matmul/imatmul_clamp_f32_f32p_f32p/kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa.h" |
| 22 | #include "kai/ukernels/matmul/imatmul_clamp_f32_f32p_f32p/kai_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa.h" |
| 23 | #include "kai/ukernels/matmul/imatmul_clamp_f32_f32p_f32p/kai_imatmul_clamp_f32_f32p_f32p_interface.h" |
| 24 | #include "kai/ukernels/matmul/pack/kai_lhs_imatmul_pack_x16p2vlx2_x16p_sme.h" |
| 25 | #include "kai/ukernels/matmul/pack/kai_lhs_imatmul_pack_x32p2vlx1_x32p_sme.h" |
| 26 | #include "kai/ukernels/matmul/pack/kai_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme.h" |
| 27 | #include "kai/ukernels/matmul/pack/kai_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme.h" |
| 28 | #include "kai/ukernels/matmul/pack/kai_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve.h" |
| 29 | #include "test/common/abi_checker.hpp" |
| 30 | #include "test/common/buffer.hpp" |
| 31 | #include "test/common/compare.hpp" |
| 32 | #include "test/common/cpu_info.hpp" |
| 33 | #include "test/common/matmul_test_common.hpp" |
| 34 | #include "test/common/matrix_portion.hpp" |
| 35 | #include "test/common/memory.hpp" |
| 36 | #include "test/common/round.hpp" |
| 37 | #include "test/common/seed.hpp" |
| 38 | #include "test/common/sme.hpp" |
| 39 | #include "test/common/sve.hpp" |
| 40 | #include "test/reference/clamp.hpp" |
| 41 | #include "test/reference/fill.hpp" |
| 42 | #include "test/reference/matmul.hpp" |
| 43 | #include "test/reference/reorder.hpp" |
| 44 | |
| 45 | namespace kai::test { |
| 46 | |
| 47 | // Ensure static linkage for all functionality local to this test file |
| 48 | namespace { |
| 49 | |
| 50 | /// Convenience wrapper for K-chunk handling |
| 51 | struct KChunk { |
| 52 | size_t count; |
| 53 | size_t length; |
| 54 | }; |
| 55 | |
| 56 | /// Interface for indirect matmul LHS packing micro-kernel |
| 57 | struct LhsPackIndirectKernel { |
| 58 | std::function<size_t()> get_m_step; |
| 59 | std::function<size_t(size_t m_idx, size_t k_chunk_count, size_t k_chunk_length)> get_lhs_packed_offset; |
| 60 | std::function<size_t(size_t m, size_t k_chunk_count, size_t k_chunk_length)> get_lhs_packed_size; |
| 61 | std::function<void( |
| 62 | size_t m, size_t k_chunk_count, size_t k_chunk_length, const void* const* lhs_ptrs, size_t lhs_ptr_offset, |
| 63 | const void* zero, void* lhs_packed)> |
| 64 | pack{nullptr}; |
| 65 | }; |
| 66 | |
| 67 | /// Interface for indirect matmul RHS packing micro-kernel |
| 68 | struct RhsPackIndirectKernel { |
| 69 | std::function<size_t()> get_n_step; |
| 70 | std::function<size_t(size_t n_idx)> get_rhs_offset; |
| 71 | std::function<size_t(size_t n_idx)> get_bias_offset; |
| 72 | std::function<size_t(size_t n_idx, size_t k_chunk_count, size_t k_chunk_length)> get_rhs_packed_offset; |
| 73 | std::function<size_t(size_t n, size_t k_chunk_count, size_t k_chunk_length)> get_rhs_packed_size; |
| 74 | std::function<void( |
| 75 | size_t n, size_t k_chunk_count, size_t k_chunk_length, size_t rhs_row_stride, const void* rhs, const void* bias, |
| 76 | void* rhs_packed)> |
| 77 | pack; |
| 78 | }; |
| 79 | |
| 80 | /// Interface for indirect matmul kernel |
| 81 | struct MatMulIndirectKernel { |
| 82 | std::function<size_t(void)> get_m_step; |
| 83 | std::function<size_t(void)> get_n_step; |
| 84 | std::function<size_t(void)> get_mr; |
| 85 | std::function<size_t(void)> get_nr; |
| 86 | std::function<size_t(void)> get_kr; |
| 87 | std::function<size_t(size_t m_idx, size_t k_chunk_count, size_t k_chunk_length)> get_lhs_packed_offset; |
| 88 | std::function<size_t(size_t n_idx, size_t k_chunk_count, size_t k_chunk_length)> get_rhs_packed_offset; |
| 89 | std::function<size_t(size_t m_idx, size_t n_idx, size_t dst_stride_row)> get_dst_offset; |
| 90 | std::function<size_t(size_t m, size_t n)> get_dst_size; |
| 91 | |
| 92 | // Matmul function when using lhs pack |
| 93 | std::function<void( |
| 94 | size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, const void* lhs_packed, const void* rhs_packed, |
| 95 | void* dst, size_t dst_stride_row, float clamp_min, float clamp_max)> |
| 96 | imatmul{nullptr}; |
| 97 | |
| 98 | // Matmul function when using indirection buffer (no lhs pack) |
| 99 | std::function<void( |
| 100 | size_t m, size_t n, size_t k_chunk_count, size_t k_chunk_length, const void* indirection_buffer, |
| 101 | const void* pad_ptr, size_t lhs_ptr_offset, const void* rhs_packed, void* dst, size_t dst_stride_row, |
| 102 | float clamp_min, float clamp_max)> |
| 103 | imatmul_no_lhs_pack{nullptr}; |
| 104 | }; |
| 105 | |
| 106 | /// Description of a Indirect Matmul kernel set |
| 107 | struct IndirectMatMul { |
| 108 | std::string_view name; |
| 109 | std::function<bool(void)> is_supported; |
| 110 | |
| 111 | MatMulShape pack_shape; |
| 112 | struct Format { |
| 113 | DataFormat lhs; |
| 114 | DataFormat rhs; |
| 115 | DataFormat bias; |
| 116 | DataFormat out; |
| 117 | |
| 118 | struct Hash { |
| 119 | size_t operator()(const Format& format) const { |
| 120 | return // |
| 121 | (DataFormat::Hash{}(format.lhs) << 0) ^ // |
| 122 | (DataFormat::Hash{}(format.rhs) << 1) ^ // |
| 123 | (DataFormat::Hash{}(format.bias) << 2) ^ // |
| 124 | (DataFormat::Hash{}(format.out) << 3); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | private: |
| 129 | friend bool operator==(const Format& lhs, const Format& rhs) { |
| 130 | return // |
| 131 | lhs.lhs == rhs.lhs && // |
| 132 | lhs.rhs == rhs.rhs && // |
| 133 | lhs.bias == rhs.bias && // |
| 134 | lhs.out == rhs.out; |
| 135 | } |
| 136 | } format; |
| 137 | |
| 138 | LhsPackIndirectKernel lhs; |
| 139 | RhsPackIndirectKernel rhs; |
| 140 | MatMulIndirectKernel imatmul; |
| 141 | |
| 142 | bool is_lhs_pack_needed() const { |
| 143 | return lhs.pack != nullptr; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | /// Test parameter bundle type |
| 148 | using IndirectMatMulTestParams = std::tuple<IndirectMatMul, MatMulShape, size_t, MatrixPortion, std::optional<float>>; |
| 149 | |
| 150 | /// Test type |
| 151 | using IndirectMatMulTest = testing::TestWithParam<IndirectMatMulTestParams>; |
| 152 | |
| 153 | /// Use interface for matmul kernel |
| 154 | const kai_imatmul_clamp_f16_f16p_f16p_ukernel& get_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa() { |
| 155 | static kai_imatmul_clamp_f16_f16p_f16p_ukernel ukernel; |
| 156 | ukernel.get_m_step = kai_get_m_step_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 157 | ukernel.get_n_step = kai_get_n_step_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 158 | ukernel.get_lhs_packed_offset = kai_get_lhs_packed_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 159 | ukernel.get_rhs_packed_offset = kai_get_rhs_packed_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 160 | ukernel.get_dst_offset = kai_get_dst_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 161 | ukernel.get_dst_size = kai_get_dst_size_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 162 | ukernel.run_imatmul = kai_run_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa; |
| 163 | return ukernel; |
| 164 | } |
| 165 | |
| 166 | const kai_imatmul_clamp_f16_f16p_f16p_ukernel& get_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa() { |
| 167 | static kai_imatmul_clamp_f16_f16p_f16p_ukernel ukernel; |
| 168 | ukernel.get_m_step = kai_get_m_step_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 169 | ukernel.get_n_step = kai_get_n_step_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 170 | ukernel.get_lhs_packed_offset = kai_get_lhs_packed_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 171 | ukernel.get_rhs_packed_offset = kai_get_rhs_packed_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 172 | ukernel.get_dst_offset = kai_get_dst_offset_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 173 | ukernel.get_dst_size = kai_get_dst_size_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 174 | ukernel.run_imatmul = kai_run_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa; |
| 175 | return ukernel; |
| 176 | } |
| 177 | |
| 178 | /// Use interface for matmul kernel |
| 179 | const kai_imatmul_clamp_f32_f32p_f32p_ukernel& get_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa() { |
| 180 | static kai_imatmul_clamp_f32_f32p_f32p_ukernel ukernel; |
| 181 | ukernel.get_m_step = kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 182 | ukernel.get_n_step = kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 183 | ukernel.get_lhs_packed_offset = kai_get_lhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 184 | ukernel.get_rhs_packed_offset = kai_get_rhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 185 | ukernel.get_dst_offset = kai_get_dst_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 186 | ukernel.get_dst_size = kai_get_dst_size_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 187 | ukernel.run_imatmul = kai_run_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa; |
| 188 | return ukernel; |
| 189 | } |
| 190 | |
| 191 | const kai_imatmul_clamp_f32_f32p_f32p_ukernel& get_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa() { |
| 192 | static kai_imatmul_clamp_f32_f32p_f32p_ukernel ukernel; |
| 193 | ukernel.get_m_step = kai_get_m_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 194 | ukernel.get_n_step = kai_get_n_step_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 195 | ukernel.get_lhs_packed_offset = kai_get_lhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 196 | ukernel.get_rhs_packed_offset = kai_get_rhs_packed_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 197 | ukernel.get_dst_offset = kai_get_dst_offset_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 198 | ukernel.get_dst_size = kai_get_dst_size_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 199 | ukernel.run_imatmul = kai_run_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa; |
| 200 | return ukernel; |
| 201 | } |
| 202 | |
| 203 | const kai_imatmul_clamp_f32_f32_f32p_ukernel& get_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla() { |
| 204 | static kai_imatmul_clamp_f32_f32_f32p_ukernel ukernel; |
| 205 | ukernel.get_m_step = kai_get_m_step_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 206 | ukernel.get_n_step = kai_get_n_step_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 207 | ukernel.get_rhs_packed_offset = kai_get_rhs_packed_offset_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 208 | ukernel.get_dst_offset = kai_get_dst_offset_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 209 | ukernel.get_dst_size = kai_get_dst_size_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 210 | ukernel.run_imatmul = kai_run_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla; |
| 211 | return ukernel; |
| 212 | } |
| 213 | |
| 214 | /// Retreive the test list |
| 215 | const auto& get_indirect_matmul_methods() { |
| 216 | static std::array<IndirectMatMul, 5> indirect_matmul_methods{}; |
| 217 | // |
| 218 | // ----------------------------- SME ------------------------------------- |
| 219 | // F16 IMATMUL SME2 /////////////////////////////////////////////////////// |
| 220 | indirect_matmul_methods[0].name = "imatmul_f16_f16p_f16p_2vlx2vl_sme2_mopa"; |
| 221 | indirect_matmul_methods[0].is_supported = cpu_has_sme2; |
| 222 | indirect_matmul_methods[0].pack_shape.m = 2 * get_sme_vector_length<int32_t>(); |
| 223 | indirect_matmul_methods[0].pack_shape.n = 2 * get_sme_vector_length<int32_t>(); |
| 224 | indirect_matmul_methods[0].pack_shape.k = sizeof(int32_t); |
| 225 | indirect_matmul_methods[0].format.lhs = DataFormat(DataType::FP16); |
| 226 | indirect_matmul_methods[0].format.rhs = DataFormat(DataType::FP16); |
| 227 | indirect_matmul_methods[0].format.bias = DataFormat(DataType::FP16); |
| 228 | indirect_matmul_methods[0].format.out = DataFormat(DataType::FP16); |
| 229 | |
| 230 | // LHS |
| 231 | indirect_matmul_methods[0].lhs.get_m_step = kai_get_m_step_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 232 | indirect_matmul_methods[0].lhs.get_lhs_packed_offset = |
| 233 | kai_get_lhs_packed_offset_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 234 | indirect_matmul_methods[0].lhs.get_lhs_packed_size = kai_get_lhs_packed_size_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 235 | indirect_matmul_methods[0].lhs.pack = kai_run_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 236 | |
| 237 | // RHS |
| 238 | indirect_matmul_methods[0].rhs.get_n_step = kai_get_n_step_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 239 | indirect_matmul_methods[0].rhs.get_rhs_offset = kai_get_rhs_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 240 | indirect_matmul_methods[0].rhs.get_bias_offset = kai_get_bias_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 241 | indirect_matmul_methods[0].rhs.get_rhs_packed_offset = |
| 242 | kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 243 | indirect_matmul_methods[0].rhs.get_rhs_packed_size = |
| 244 | kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 245 | indirect_matmul_methods[0].rhs.pack = kai_run_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 246 | |
| 247 | // IMATMUL |
| 248 | const kai_imatmul_clamp_f16_f16p_f16p_ukernel& ukernel_f16_sme2 = |
| 249 | get_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2_2vlx2vl_sme2_mopa(); |
| 250 | indirect_matmul_methods[0].imatmul.get_m_step = ukernel_f16_sme2.get_m_step; |
| 251 | indirect_matmul_methods[0].imatmul.get_n_step = ukernel_f16_sme2.get_n_step; |
| 252 | indirect_matmul_methods[0].imatmul.get_lhs_packed_offset = ukernel_f16_sme2.get_lhs_packed_offset; |
| 253 | indirect_matmul_methods[0].imatmul.get_rhs_packed_offset = ukernel_f16_sme2.get_rhs_packed_offset; |
| 254 | indirect_matmul_methods[0].imatmul.get_dst_offset = ukernel_f16_sme2.get_dst_offset; |
| 255 | indirect_matmul_methods[0].imatmul.get_dst_size = ukernel_f16_sme2.get_dst_size; |
| 256 | indirect_matmul_methods[0].imatmul.imatmul = ukernel_f16_sme2.run_imatmul; |
| 257 | |
| 258 | // F32 IMATMUL SME2 /////////////////////////////////////////////////////// |
| 259 | indirect_matmul_methods[1].name = "imatmul_f32_f32p_f32p_2vlx2vl_sme2_mopa"; |
| 260 | indirect_matmul_methods[1].is_supported = cpu_has_sme2; |
| 261 | indirect_matmul_methods[1].pack_shape.m = 2 * get_sme_vector_length<int32_t>(); |
| 262 | indirect_matmul_methods[1].pack_shape.n = 2 * get_sme_vector_length<int32_t>(); |
| 263 | indirect_matmul_methods[1].pack_shape.k = sizeof(int32_t); |
| 264 | indirect_matmul_methods[1].format.lhs = DataFormat(DataType::FP32); |
| 265 | indirect_matmul_methods[1].format.rhs = DataFormat(DataType::FP32); |
| 266 | indirect_matmul_methods[1].format.bias = DataFormat(DataType::FP32); |
| 267 | indirect_matmul_methods[1].format.out = DataFormat(DataType::FP32); |
| 268 | |
| 269 | // LHS |
| 270 | indirect_matmul_methods[1].lhs.get_m_step = kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 271 | indirect_matmul_methods[1].lhs.get_lhs_packed_offset = |
| 272 | kai_get_lhs_packed_offset_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 273 | indirect_matmul_methods[1].lhs.get_lhs_packed_size = kai_get_lhs_packed_size_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 274 | indirect_matmul_methods[1].lhs.pack = kai_run_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 275 | |
| 276 | // RHS |
| 277 | indirect_matmul_methods[1].rhs.get_n_step = kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 278 | indirect_matmul_methods[1].rhs.get_rhs_offset = kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 279 | indirect_matmul_methods[1].rhs.get_bias_offset = kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 280 | indirect_matmul_methods[1].rhs.get_rhs_packed_offset = |
| 281 | kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 282 | indirect_matmul_methods[1].rhs.get_rhs_packed_size = |
| 283 | kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 284 | indirect_matmul_methods[1].rhs.pack = kai_run_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 285 | |
| 286 | // IMATMUL |
| 287 | const kai_imatmul_clamp_f32_f32p_f32p_ukernel& ukernel_f32_sme2 = |
| 288 | get_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa(); |
| 289 | indirect_matmul_methods[1].imatmul.get_m_step = ukernel_f32_sme2.get_m_step; |
| 290 | indirect_matmul_methods[1].imatmul.get_n_step = ukernel_f32_sme2.get_n_step; |
| 291 | indirect_matmul_methods[1].imatmul.get_lhs_packed_offset = ukernel_f32_sme2.get_lhs_packed_offset; |
| 292 | indirect_matmul_methods[1].imatmul.get_rhs_packed_offset = ukernel_f32_sme2.get_rhs_packed_offset; |
| 293 | indirect_matmul_methods[1].imatmul.get_dst_offset = ukernel_f32_sme2.get_dst_offset; |
| 294 | indirect_matmul_methods[1].imatmul.get_dst_size = ukernel_f32_sme2.get_dst_size; |
| 295 | indirect_matmul_methods[1].imatmul.imatmul = ukernel_f32_sme2.run_imatmul; |
| 296 | |
| 297 | // F16 IMATMUL SME //////////////////////////////////////////////////////// |
| 298 | indirect_matmul_methods[2].name = "imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa"; |
| 299 | indirect_matmul_methods[2].is_supported = cpu_has_sme; |
| 300 | indirect_matmul_methods[2].pack_shape.m = 2 * get_sme_vector_length<int32_t>(); |
| 301 | indirect_matmul_methods[2].pack_shape.n = 2 * get_sme_vector_length<int32_t>(); |
| 302 | indirect_matmul_methods[2].pack_shape.k = sizeof(int32_t); |
| 303 | indirect_matmul_methods[2].format.lhs = DataFormat(DataType::FP16); |
| 304 | indirect_matmul_methods[2].format.rhs = DataFormat(DataType::FP16); |
| 305 | indirect_matmul_methods[2].format.bias = DataFormat(DataType::FP16); |
| 306 | indirect_matmul_methods[2].format.out = DataFormat(DataType::FP16); |
| 307 | |
| 308 | // LHS |
| 309 | indirect_matmul_methods[2].lhs.get_m_step = kai_get_m_step_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 310 | indirect_matmul_methods[2].lhs.get_lhs_packed_offset = |
| 311 | kai_get_lhs_packed_offset_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 312 | indirect_matmul_methods[2].lhs.get_lhs_packed_size = kai_get_lhs_packed_size_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 313 | indirect_matmul_methods[2].lhs.pack = kai_run_lhs_imatmul_pack_x16p2vlx2_x16p_sme; |
| 314 | |
| 315 | // RHS |
| 316 | indirect_matmul_methods[2].rhs.get_n_step = kai_get_n_step_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 317 | indirect_matmul_methods[2].rhs.get_rhs_offset = kai_get_rhs_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 318 | indirect_matmul_methods[2].rhs.get_bias_offset = kai_get_bias_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 319 | indirect_matmul_methods[2].rhs.get_rhs_packed_offset = |
| 320 | kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 321 | indirect_matmul_methods[2].rhs.get_rhs_packed_size = |
| 322 | kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 323 | indirect_matmul_methods[2].rhs.pack = kai_run_rhs_imatmul_pack_kxn_x16p2vlx2b_x16_x16_sme; |
| 324 | |
| 325 | // IMATMUL |
| 326 | const kai_imatmul_clamp_f16_f16p_f16p_ukernel& ukernel_f16_sme = |
| 327 | get_imatmul_clamp_f16_f16p2vlx2_f16p2vlx2b_2vlx2vl_sme_mopa(); |
| 328 | indirect_matmul_methods[2].imatmul.get_m_step = ukernel_f16_sme.get_m_step; |
| 329 | indirect_matmul_methods[2].imatmul.get_n_step = ukernel_f16_sme.get_n_step; |
| 330 | indirect_matmul_methods[2].imatmul.get_lhs_packed_offset = ukernel_f16_sme.get_lhs_packed_offset; |
| 331 | indirect_matmul_methods[2].imatmul.get_rhs_packed_offset = ukernel_f16_sme.get_rhs_packed_offset; |
| 332 | indirect_matmul_methods[2].imatmul.get_dst_offset = ukernel_f16_sme.get_dst_offset; |
| 333 | indirect_matmul_methods[2].imatmul.get_dst_size = ukernel_f16_sme.get_dst_size; |
| 334 | indirect_matmul_methods[2].imatmul.imatmul = ukernel_f16_sme.run_imatmul; |
| 335 | |
| 336 | // F32 IMATMUL SME //////////////////////////////////////////////////////// |
| 337 | indirect_matmul_methods[3].name = "imatmul_f32_f32p_f32p_2vlx2vl_sme_mopa"; |
| 338 | indirect_matmul_methods[3].is_supported = cpu_has_sme; |
| 339 | indirect_matmul_methods[3].pack_shape.m = 2 * get_sme_vector_length<int32_t>(); |
| 340 | indirect_matmul_methods[3].pack_shape.n = 2 * get_sme_vector_length<int32_t>(); |
| 341 | indirect_matmul_methods[3].pack_shape.k = sizeof(int32_t); |
| 342 | indirect_matmul_methods[3].format.lhs = DataFormat(DataType::FP32); |
| 343 | indirect_matmul_methods[3].format.rhs = DataFormat(DataType::FP32); |
| 344 | indirect_matmul_methods[3].format.bias = DataFormat(DataType::FP32); |
| 345 | indirect_matmul_methods[3].format.out = DataFormat(DataType::FP32); |
| 346 | |
| 347 | // LHS |
| 348 | indirect_matmul_methods[3].lhs.get_m_step = kai_get_m_step_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 349 | indirect_matmul_methods[3].lhs.get_lhs_packed_offset = |
| 350 | kai_get_lhs_packed_offset_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 351 | indirect_matmul_methods[3].lhs.get_lhs_packed_size = kai_get_lhs_packed_size_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 352 | indirect_matmul_methods[3].lhs.pack = kai_run_lhs_imatmul_pack_x32p2vlx1_x32p_sme; |
| 353 | |
| 354 | // RHS |
| 355 | indirect_matmul_methods[3].rhs.get_n_step = kai_get_n_step_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 356 | indirect_matmul_methods[3].rhs.get_rhs_offset = kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 357 | indirect_matmul_methods[3].rhs.get_bias_offset = kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 358 | indirect_matmul_methods[3].rhs.get_rhs_packed_offset = |
| 359 | kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 360 | indirect_matmul_methods[3].rhs.get_rhs_packed_size = |
| 361 | kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 362 | indirect_matmul_methods[3].rhs.pack = kai_run_rhs_imatmul_pack_kxn_x32p2vlx1b_x32_x32_sme; |
| 363 | |
| 364 | // IMATMUL |
| 365 | const kai_imatmul_clamp_f32_f32p_f32p_ukernel& ukernel_f32_sme = |
| 366 | get_imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme_mopa(); |
| 367 | indirect_matmul_methods[3].imatmul.get_m_step = ukernel_f32_sme.get_m_step; |
| 368 | indirect_matmul_methods[3].imatmul.get_n_step = ukernel_f32_sme.get_n_step; |
| 369 | indirect_matmul_methods[3].imatmul.get_lhs_packed_offset = ukernel_f32_sme.get_lhs_packed_offset; |
| 370 | indirect_matmul_methods[3].imatmul.get_rhs_packed_offset = ukernel_f32_sme.get_rhs_packed_offset; |
| 371 | indirect_matmul_methods[3].imatmul.get_dst_offset = ukernel_f32_sme.get_dst_offset; |
| 372 | indirect_matmul_methods[3].imatmul.get_dst_size = ukernel_f32_sme.get_dst_size; |
| 373 | indirect_matmul_methods[3].imatmul.imatmul = ukernel_f32_sme.run_imatmul; |
| 374 | |
| 375 | // |
| 376 | // ----------------------------- SVE ------------------------------------- |
| 377 | // FP32 - No LHS Packing involved. |
| 378 | // F32 IMATMUL SVE |
| 379 | indirect_matmul_methods[4].name = "imatmul_f32_f32_f32p_6x4vl_sve_mla"; |
| 380 | indirect_matmul_methods[4].is_supported = cpu_has_sve; |
| 381 | indirect_matmul_methods[4].pack_shape.m = 6; |
| 382 | indirect_matmul_methods[4].pack_shape.n = 4 * get_sve_vector_length<int32_t>(); |
| 383 | indirect_matmul_methods[4].pack_shape.k = sizeof(int32_t); |
| 384 | indirect_matmul_methods[4].format.lhs = DataFormat(DataType::FP32); |
| 385 | indirect_matmul_methods[4].format.rhs = DataFormat(DataType::FP32); |
| 386 | indirect_matmul_methods[4].format.bias = DataFormat(DataType::FP32); |
| 387 | indirect_matmul_methods[4].format.out = DataFormat(DataType::FP32); |
| 388 | |
| 389 | // RHS |
| 390 | indirect_matmul_methods[4].rhs.get_n_step = kai_get_n_step_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 391 | indirect_matmul_methods[4].rhs.get_rhs_offset = kai_get_rhs_offset_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 392 | indirect_matmul_methods[4].rhs.get_bias_offset = kai_get_bias_offset_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 393 | indirect_matmul_methods[4].rhs.get_rhs_packed_offset = |
| 394 | kai_get_rhs_packed_offset_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 395 | indirect_matmul_methods[4].rhs.get_rhs_packed_size = |
| 396 | kai_get_rhs_packed_size_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 397 | indirect_matmul_methods[4].rhs.pack = kai_run_rhs_imatmul_pack_kxn_x32p4vlx1b_x32_x32_sve; |
| 398 | |
| 399 | // IMATMUL |
| 400 | const kai_imatmul_clamp_f32_f32_f32p_ukernel& ukernel_f32_sve = |
| 401 | get_imatmul_clamp_f32_f32_f32p4vlx1b_6x4vl_sve_mla(); |
| 402 | indirect_matmul_methods[4].imatmul.get_m_step = ukernel_f32_sve.get_m_step; |
| 403 | indirect_matmul_methods[4].imatmul.get_n_step = ukernel_f32_sve.get_n_step; |
| 404 | indirect_matmul_methods[4].imatmul.get_rhs_packed_offset = ukernel_f32_sve.get_rhs_packed_offset; |
| 405 | indirect_matmul_methods[4].imatmul.get_dst_offset = ukernel_f32_sve.get_dst_offset; |
| 406 | indirect_matmul_methods[4].imatmul.get_dst_size = ukernel_f32_sve.get_dst_size; |
| 407 | indirect_matmul_methods[4].imatmul.imatmul_no_lhs_pack = ukernel_f32_sve.run_imatmul; |
| 408 | |
| 409 | return indirect_matmul_methods; |
| 410 | } |
| 411 | |
| 412 | /// Test reference identification |
| 413 | struct TestDataId { |
| 414 | MatMulShape shape; |
| 415 | MatMulShape pack_shape; |
| 416 | IndirectMatMul::Format format; |
| 417 | size_t k_chunk_length; |
| 418 | std::optional<float> clamp_keep_ratio; |
| 419 | |
| 420 | struct Hash { |
| 421 | size_t operator()(const TestDataId& test_id) const { |
| 422 | return // |
| 423 | (MatMulShape::Hash{}(test_id.shape) << 0) ^ // |
| 424 | (MatMulShape::Hash{}(test_id.pack_shape) << 1) ^ // |
| 425 | (IndirectMatMul::Format::Hash{}(test_id.format) << 2) ^ // |
| 426 | (std::hash<size_t>{}(test_id.k_chunk_length) << 3) ^ // |
| 427 | (std::hash<float>{}(test_id.clamp_keep_ratio.value_or(0.0)) << 4); // |
| 428 | } |
| 429 | }; |
| 430 | |
| 431 | private: |
| 432 | friend bool operator==(const TestDataId& lhs, const TestDataId& rhs) { |
| 433 | return // |
| 434 | lhs.shape == rhs.shape && // |
| 435 | lhs.pack_shape == rhs.pack_shape && // |
| 436 | lhs.format == rhs.format && // |
| 437 | lhs.k_chunk_length == rhs.k_chunk_length && // |
| 438 | lhs.clamp_keep_ratio == rhs.clamp_keep_ratio; |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | /// Test reference data |
| 443 | struct TestData { |
| 444 | Buffer lhs; ///< LHS input matrix |
| 445 | Buffer rhs; ///< RHS input matrix |
| 446 | Buffer bias; ///< Bias vector |
| 447 | Buffer out; ///< Reference imatmul result |
| 448 | Buffer indirection; ///< LHS indirection buffer |
| 449 | uintptr_t indirection_offset; ///< LHS indirection buffer offset |
| 450 | Buffer padding; ///< Padding buffer |
| 451 | Range<float> clamp_range; ///< Clamp range |
| 452 | }; |
| 453 | |
| 454 | /// Reference data generator |
| 455 | /// |
| 456 | /// Uses test id to generate reference data, and caches it. |
| 457 | struct ReferenceGenerator { |
| 458 | /// Retrieve reference data for the provided test identification |
| 459 | static const TestData& get_test_reference(const TestDataId& test_id) { |
| 460 | static std::unordered_map<TestDataId, TestData, TestDataId::Hash> m_data; |
| 461 | if (const auto itr = m_data.find(test_id); itr != end(m_data)) { |
| 462 | return itr->second; |
| 463 | } |
| 464 | |
| 465 | return m_data[test_id] = generate_reference(test_id); |
| 466 | } |
| 467 | |
| 468 | private: |
| 469 | /// Generate reference data. Not intended to be called |
| 470 | /// directly, as this would bypass caching mechanism. |
| 471 | static TestData generate_reference(const TestDataId& test_id) { |
| 472 | const auto& [chunked_shape, pack_shape, format, k_chunk_length, clamp_keep_ratio] = test_id; |
| 473 | |
| 474 | // The LHS matrix will be split into several chunks in the K dimension |
| 475 | const size_t k_chunk_count = chunked_shape.k; |
| 476 | MatMulShape shape = {chunked_shape.m, chunked_shape.n, k_chunk_count * k_chunk_length}; |
| 477 | |
| 478 | // Stable key derived from the cache identifier. |
| 479 | const auto key_hash = static_cast<std::uint32_t>(TestDataId::Hash{}(test_id)); |
| 480 | const auto key = std::string("imatmul_cache:") + std::to_string(key_hash); |
| 481 | auto& feed = seed_stream(key); |
| 482 | |
| 483 | // Generate random input data |
| 484 | Buffer lhs = fill_matrix_random(shape.m, shape.k, format.lhs, feed()); |
| 485 | Buffer rhs = fill_matrix_random(shape.k, shape.n, format.rhs, feed()); |
| 486 | Buffer bias = fill_matrix_random(1, shape.n, format.bias, feed()); |
| 487 | |
| 488 | // Data types used |
| 489 | const DataType lhs_dt = format.lhs.data_type(); |
| 490 | const DataType rhs_dt = format.rhs.data_type(); |
| 491 | const DataType out_dt = format.out.data_type(); |
| 492 | const DataType bias_dt = format.bias.data_type(); |
| 493 | |
| 494 | // Create a padding chunk |
| 495 | const size_t k_chunk_size = round_up_division(k_chunk_length * data_type_size_in_bits(lhs_dt), 8); |
| 496 | const size_t row_size = k_chunk_count * k_chunk_size; |
| 497 | Buffer lhs_padding(k_chunk_size); |
| 498 | for (size_t i = 0; i < k_chunk_length; i += 1) { |
| 499 | static constexpr double padding_value = 0; |
| 500 | write_array(lhs_dt, lhs_padding.data(), i, padding_value); |
| 501 | } |
| 502 | |
| 503 | // Set up indirection buffer |
| 504 | const uintptr_t indirection_offset = reinterpret_cast<uintptr_t>(lhs.data()); |
| 505 | std::vector<const void*> indirection(chunked_shape.m * chunked_shape.k); |
| 506 | for (size_t i_m = 0; i_m < chunked_shape.m; i_m += 1) { |
| 507 | for (size_t i_k = 0; i_k < chunked_shape.k; i_k += 1) { |
| 508 | const size_t idx = i_m * chunked_shape.k + i_k; |
| 509 | // Test padding pointers using first LHS row for shapes where M > 1 |
| 510 | if (chunked_shape.m > 1 && i_m == 0) { |
| 511 | indirection.at(idx) = lhs_padding.data(); |
| 512 | } else { |
| 513 | uintptr_t offset = i_m * row_size + i_k * k_chunk_size; |
| 514 | indirection.at(idx) = reinterpret_cast<const void*>(offset); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // Pack indirection buffer |
| 520 | Buffer indirection_packed = reorder_block<const void*>( |
| 521 | reinterpret_cast<const void* const*>(indirection.data()), chunked_shape.m, chunked_shape.k, pack_shape.m, |
| 522 | 1); |
| 523 | |
| 524 | Buffer out = indirect_matmul( // |
| 525 | indirection.data(), indirection_offset, lhs_padding.data(), nullptr, nullptr, lhs_dt, // LHS |
| 526 | rhs.data(), nullptr, nullptr, rhs_dt, // RHS |
| 527 | bias.data(), nullptr, nullptr, bias_dt, // Bias |
| 528 | out_dt, // Out |
| 529 | chunked_shape.m, chunked_shape.n, chunked_shape.k, k_chunk_length); |
| 530 | |
| 531 | // Calculate clamping range based on full range of values, and then clamp values |
| 532 | const auto [min, max] = find_clamp_range(out_dt, out.data(), shape.m * shape.n, clamp_keep_ratio); |
| 533 | Buffer out_clamped = clamp(out_dt, out.data(), shape.m * shape.n, min, max); |
| 534 | |
| 535 | // Populate reference data |
| 536 | TestData test_reference; |
| 537 | test_reference.lhs = std::move(lhs); |
| 538 | test_reference.rhs = std::move(rhs); |
| 539 | test_reference.bias = std::move(bias); |
| 540 | test_reference.padding = std::move(lhs_padding); |
| 541 | test_reference.out = std::move(out_clamped); |
| 542 | test_reference.indirection_offset = indirection_offset; |
| 543 | test_reference.indirection = std::move(indirection_packed); |
| 544 | test_reference.clamp_range = {min, max}; |
| 545 | |
| 546 | return test_reference; |
| 547 | }; |
| 548 | }; |
| 549 | |
| 550 | /// Perform LHS packing for indirect matmul |
| 551 | Buffer pack_lhs( |
| 552 | const LhsPackIndirectKernel& kernel, const Rect& portion, const TestData& reference, size_t m, |
| 553 | const KChunk& k_chunk) { |
| 554 | const void* const* indirection_pointer = reinterpret_cast<const void* const*>(reference.indirection.data()); |
| 555 | |
| 556 | // Calculate size, and allocate buffer |
| 557 | const size_t dst_size = kernel.get_lhs_packed_size(m, k_chunk.count, k_chunk.length); |
| 558 | Buffer dst(dst_size); |
| 559 | |
| 560 | // Calculate portion offsets |
| 561 | const size_t input_offset = portion.start_row() * k_chunk.count; |
| 562 | const size_t dst_offset = kernel.get_lhs_packed_offset(portion.start_row(), k_chunk.count, k_chunk.length); |
| 563 | |
| 564 | // Perform packing |
| 565 | abi_check( |
| 566 | kernel.pack, // Kernel |
| 567 | portion.height(), k_chunk.count, k_chunk.length, // Dimensions |
| 568 | indirection_pointer + input_offset, // Indirection input |
| 569 | reference.indirection_offset, // Chunk offset |
| 570 | reference.padding.data(), // Padding pointer |
| 571 | dst.data() + dst_offset); |
| 572 | return dst; |
| 573 | } |
| 574 | |
| 575 | /// Perform RHS packign for indirect matmul |
| 576 | Buffer pack_rhs( |
| 577 | const RhsPackIndirectKernel& kernel, const Rect& portion, const TestData& reference, size_t n, |
| 578 | const KChunk& k_chunk, DataType type) { |
| 579 | // Calculate size, and allocate buffer |
| 580 | const size_t row_stride = round_up_division(n * data_type_size_in_bits(type), 8); |
| 581 | const size_t dst_size = kernel.get_rhs_packed_size(n, k_chunk.count, k_chunk.length); |
| 582 | Buffer dst(dst_size); |
| 583 | |
| 584 | // Calculate offsets |
| 585 | const size_t rhs_offset = kernel.get_rhs_offset(portion.start_col()); |
| 586 | const size_t bias_offset = kernel.get_bias_offset(portion.start_col()); |
| 587 | const size_t dst_offset = kernel.get_rhs_packed_offset(portion.start_col(), k_chunk.count, k_chunk.length); |
| 588 | |
| 589 | // Perform actual packing |
| 590 | abi_check( |
| 591 | kernel.pack, // Kernel |
| 592 | portion.width(), k_chunk.count, k_chunk.length, row_stride, // Dimensions |
| 593 | reference.rhs.data() + rhs_offset, // RHS input |
| 594 | reference.bias.data() + bias_offset, // Bias |
| 595 | dst.data() + dst_offset); // Output |
| 596 | return dst; |
| 597 | } |
| 598 | |
| 599 | /// Perform imatmul using indirection buffer - without lhs pack |
| 600 | /// |
| 601 | /// Note, this should not be aware of reference result, as to make it clear that |
| 602 | /// any produced result is strictly from the code under test |
| 603 | Buffer imatmul_without_lhs_pack( |
| 604 | const MatMulIndirectKernel& kernel, const Rect& portion, const MatMulShape& shape, const KChunk& k_chunk, |
| 605 | const TestData& reference, const Buffer& rhs_packed, DataType type) { |
| 606 | const auto clamp_range = reference.clamp_range; |
| 607 | const void* const* indirection_pointer = reinterpret_cast<const void* const*>(reference.indirection.data()); |
| 608 | |
| 609 | // Calculate size, and allocate buffer |
| 610 | const size_t dst_size = kernel.get_dst_size(shape.m, shape.n); |
| 611 | const size_t row_stride = round_up_division(shape.n * data_type_size_in_bits(type), 8); |
| 612 | Buffer dst(dst_size); |
| 613 | |
| 614 | // Calculate portion offsets |
| 615 | const size_t lhs_offset = portion.start_row() * k_chunk.count; |
| 616 | const size_t rhs_offset = kernel.get_rhs_packed_offset(portion.start_col(), k_chunk.count, k_chunk.length); |
| 617 | const size_t dst_offset = kernel.get_dst_offset(portion.start_row(), portion.start_col(), row_stride); |
| 618 | |
| 619 | // Call matmul kernel |
| 620 | abi_check( |
| 621 | kernel.imatmul_no_lhs_pack, portion.height(), portion.width(), k_chunk.count, k_chunk.length, // Dimensions |
| 622 | indirection_pointer + lhs_offset, // LHS - Indirection buffer |
| 623 | reference.padding.data(), reference.indirection_offset, |
| 624 | rhs_packed.data() + rhs_offset, // RHS |
| 625 | dst.data() + dst_offset, // DST |
| 626 | row_stride, clamp_range.min, clamp_range.max); |
| 627 | |
| 628 | return dst; |
| 629 | } |
| 630 | |
| 631 | /// Perform imatmul without LHS pack using indirection buffer directly. |
| 632 | /// |
| 633 | /// Note, this should not be aware of reference result, as to make it clear that |
| 634 | /// any produced result is strictly from the code under test |
| 635 | Buffer imatmul_with_lhs_pack( |
| 636 | const MatMulIndirectKernel& kernel, const Rect& portion, const MatMulShape& shape, const KChunk& k_chunk, |
| 637 | const Buffer& lhs_packed, const Buffer& rhs_packed, Range<float> clamp_range, DataType type) { |
| 638 | // Calculate size, and allocate buffer |
| 639 | const size_t dst_size = kernel.get_dst_size(shape.m, shape.n); |
| 640 | const size_t row_stride = round_up_division(shape.n * data_type_size_in_bits(type), 8); |
| 641 | Buffer dst(dst_size); |
| 642 | |
| 643 | // Calculate portion offsets |
| 644 | const size_t lhs_offset = kernel.get_lhs_packed_offset(portion.start_row(), k_chunk.count, k_chunk.length); |
| 645 | const size_t rhs_offset = kernel.get_rhs_packed_offset(portion.start_col(), k_chunk.count, k_chunk.length); |
| 646 | const size_t dst_offset = kernel.get_dst_offset(portion.start_row(), portion.start_col(), row_stride); |
| 647 | |
| 648 | // Call matmul kernel |
| 649 | abi_check( |
| 650 | kernel.imatmul, // Kernel |
| 651 | portion.height(), portion.width(), k_chunk.count, k_chunk.length, // Dimensions |
| 652 | lhs_packed.data() + lhs_offset, // LHS |
| 653 | rhs_packed.data() + rhs_offset, // RHS |
| 654 | dst.data() + dst_offset, // DST |
| 655 | row_stride, clamp_range.min, clamp_range.max); |
| 656 | |
| 657 | return dst; |
| 658 | } |
| 659 | |
| 660 | } // namespace |
| 661 | |
| 662 | /// End-to-end test for indirection matmul kernels |
| 663 | TEST_P(IndirectMatMulTest, Output) { |
| 664 | const auto& [method, shape, k_chunk_length, output_portion, clamp_keep_ratio] = GetParam(); |
| 665 | if (not method.is_supported()) { |
| 666 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 667 | } |
| 668 | |
| 669 | const KChunk k_chunk{shape.k, k_chunk_length}; |
| 670 | |
| 671 | // Retrieve reference data |
| 672 | const TestDataId test_id{shape, method.pack_shape, method.format, k_chunk_length, clamp_keep_ratio}; |
| 673 | const TestData& test_data = ReferenceGenerator::get_test_reference(test_id); |
| 674 | const Rect portion = output_portion.compute_portion(shape.m, shape.n, method.pack_shape.m, method.pack_shape.n); |
| 675 | |
| 676 | if (portion.height() == 0 || portion.width() == 0) { |
| 677 | GTEST_SKIP() << "Empty dimension of matrix(" << portion.width() << "," << portion.height() << ")"; |
| 678 | } |
| 679 | |
| 680 | // Call packing micro-kernels, and then imatmul kernel |
| 681 | Buffer rhs_packed = pack_rhs(method.rhs, portion, test_data, shape.n, k_chunk, method.format.rhs.data_type()); |
| 682 | const auto dst_size = method.imatmul.get_dst_size(shape.m, shape.n); |
| 683 | |
| 684 | Buffer out(dst_size, 0); |
| 685 | if (method.is_lhs_pack_needed()) { |
| 686 | Buffer lhs_packed = pack_lhs(method.lhs, portion, test_data, shape.m, k_chunk); |
| 687 | out = imatmul_with_lhs_pack( |
| 688 | method.imatmul, portion, shape, k_chunk, lhs_packed, rhs_packed, test_data.clamp_range, |
| 689 | method.format.out.data_type()); |
| 690 | } else { |
| 691 | out = imatmul_without_lhs_pack( |
| 692 | method.imatmul, portion, shape, k_chunk, test_data, rhs_packed, method.format.out.data_type()); |
| 693 | } |
| 694 | |
| 695 | // Compare the actual result with the reference result |
| 696 | DefaultMismatchHandler handler(0, 0.1, 0, 0.05); |
| 697 | const auto success = |
| 698 | compare(out.data(), test_data.out.data(), method.format.out.data_type(), shape.m, shape.n, portion, handler); |
| 699 | ASSERT_TRUE(success); |
| 700 | } |
| 701 | |
| 702 | /// Name generator for test case |
| 703 | [[maybe_unused]] static void PrintTo(const IndirectMatMulTestParams& param, std::ostream* os) { |
| 704 | const auto& [method, shape, k_chunk_length, portion, clamp_keep_ratio] = param; |
| 705 | *os << method.name << "__"; |
| 706 | PrintTo(shape, os); |
| 707 | *os << "__K_chunk_length_" << k_chunk_length; |
| 708 | *os << "__clamp_keep_ratio_" |
| 709 | << (clamp_keep_ratio.has_value() ? std::to_string(static_cast<int>(clamp_keep_ratio.value() * 100)) : "noclamp") |
| 710 | << "__"; |
| 711 | PrintTo(portion, os); |
| 712 | } |
| 713 | |
| 714 | static auto get_indirect_matmul_shapes() { |
| 715 | static const std::array indirect_matmul_shapes{ |
| 716 | // clang-format off |
| 717 | MatMulShape{ 1, 1, 1}, |
| 718 | MatMulShape{ 1, 17, 4}, |
| 719 | MatMulShape{ 1, 19, 24}, |
| 720 | MatMulShape{ 1, 32, 4}, |
| 721 | MatMulShape{ 1, 32, 32}, |
| 722 | MatMulShape{ 1, 33, 7}, |
| 723 | MatMulShape{ 1, 49, 21}, |
| 724 | MatMulShape{ 1, 64, 4}, |
| 725 | MatMulShape{ 1, 65, 4}, |
| 726 | MatMulShape{ 1, 71, 32}, |
| 727 | MatMulShape{ 3, 6, 6}, |
| 728 | MatMulShape{ 3, 28, 25}, |
| 729 | MatMulShape{ 4, 16, 4}, |
| 730 | MatMulShape{ 4, 16, 27}, |
| 731 | MatMulShape{ 6, 18, 31}, |
| 732 | MatMulShape{ 6, 28, 1}, |
| 733 | MatMulShape{ 6, 29, 24}, |
| 734 | MatMulShape{ 8, 16, 16}, |
| 735 | MatMulShape{ 16, 16, 4}, |
| 736 | MatMulShape{ 16, 16, 16}, |
| 737 | MatMulShape{ 20, 30, 40}, |
| 738 | MatMulShape{ 23, 1, 43}, |
| 739 | MatMulShape{ 32, 14, 1}, |
| 740 | MatMulShape{ 32, 16, 27}, |
| 741 | MatMulShape{ 32, 32, 3}, |
| 742 | MatMulShape{ 32, 32, 4}, |
| 743 | MatMulShape{ 33, 29, 24}, |
| 744 | MatMulShape{ 64, 64, 3}, |
| 745 | MatMulShape{ 64, 64, 4}, |
| 746 | MatMulShape{ 96, 96, 3}, |
| 747 | MatMulShape{ 96, 97, 3}, |
| 748 | MatMulShape{ 97, 96, 3}, |
| 749 | MatMulShape{123, 85, 45}, |
| 750 | MatMulShape{128, 128, 3}, |
| 751 | MatMulShape{130, 130, 6}, |
| 752 | // clang-format on |
| 753 | }; |
| 754 | |
| 755 | return indirect_matmul_shapes; |
| 756 | } |
| 757 | |
| 758 | static auto get_indirect_matmul_portions() { |
| 759 | static const std::array<MatrixPortion, 6> indirect_matmul_portions{ |
| 760 | // (Start row , start col , height , width) |
| 761 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 762 | MatrixPortion(0, 0, 1, 0.5), // Left half |
| 763 | MatrixPortion(0, 0, 0.5, 1), // Upper half |
| 764 | MatrixPortion(0, 0.5, 1, 0.5), // Right half |
| 765 | MatrixPortion(0.5, 0, 0.5, 1), // Bottom half |
| 766 | MatrixPortion(0.4, 0.4, 0.3, 0.3), // Center ninth |
| 767 | }; |
| 768 | |
| 769 | return indirect_matmul_portions; |
| 770 | } |
| 771 | |
| 772 | // Test suite focused on small K chunk |
| 773 | INSTANTIATE_TEST_SUITE_P( |
| 774 | ShapesSmallKC, IndirectMatMulTest, |
| 775 | testing::Combine( |
| 776 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 777 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 778 | testing::ValuesIn(std::initializer_list<size_t>{1, 2, 3, 4, 8, 11, 16}), // |
| 779 | testing::ValuesIn(get_indirect_matmul_portions()), // |
| 780 | testing::Values(std::nullopt)), // |
| 781 | testing::PrintToStringParamName()); |
| 782 | |
| 783 | // Test suite focused on K chunk 31 |
| 784 | INSTANTIATE_TEST_SUITE_P( |
| 785 | ShapesKC31, IndirectMatMulTest, |
| 786 | testing::Combine( |
| 787 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 788 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 789 | testing::Values(static_cast<size_t>(31)), // |
| 790 | testing::ValuesIn(get_indirect_matmul_portions()), // |
| 791 | testing::Values(std::nullopt)), // |
| 792 | testing::PrintToStringParamName()); |
| 793 | |
| 794 | // Test suite focused on K chunk 32 |
| 795 | INSTANTIATE_TEST_SUITE_P( |
| 796 | ShapesKC32, IndirectMatMulTest, |
| 797 | testing::Combine( |
| 798 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 799 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 800 | testing::Values(static_cast<size_t>(32)), // |
| 801 | testing::ValuesIn(get_indirect_matmul_portions()), // |
| 802 | testing::Values(std::nullopt)), // |
| 803 | testing::PrintToStringParamName()); |
| 804 | |
| 805 | // Test suite focused on K chunk 64 |
| 806 | INSTANTIATE_TEST_SUITE_P( |
| 807 | ShapesKC64, IndirectMatMulTest, |
| 808 | testing::Combine( |
| 809 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 810 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 811 | testing::Values(static_cast<size_t>(64)), // |
| 812 | testing::ValuesIn(get_indirect_matmul_portions()), // |
| 813 | testing::Values(std::nullopt)), // |
| 814 | testing::PrintToStringParamName()); |
| 815 | |
| 816 | // Test suite focused on K chunk 65, other parametes are limited |
| 817 | INSTANTIATE_TEST_SUITE_P( |
| 818 | ShapesKC65, IndirectMatMulTest, |
| 819 | testing::Combine( |
| 820 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 821 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 822 | testing::Values(static_cast<size_t>(65)), // |
| 823 | testing::ValuesIn(get_indirect_matmul_portions()), // |
| 824 | testing::Values(std::nullopt)), // |
| 825 | testing::PrintToStringParamName()); |
| 826 | |
| 827 | // Test suite focused on clamping values, other parametes are limited |
| 828 | INSTANTIATE_TEST_SUITE_P( |
| 829 | Clamp, IndirectMatMulTest, |
| 830 | testing::Combine( |
| 831 | testing::ValuesIn(get_indirect_matmul_methods()), // |
| 832 | testing::ValuesIn(get_indirect_matmul_shapes()), // |
| 833 | testing::Values(static_cast<size_t>(3)), // |
| 834 | testing::Values(MatrixPortion(0, 0, 1, 1)), // |
| 835 | testing::Values(std::nullopt, 1.0F, 0.9F, 0.5F)), // |
| 836 | testing::PrintToStringParamName()); |
| 837 | |
| 838 | } // namespace kai::test |