Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 2 | * Copyright (c) 2017-2022 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuGemmMatrixMultiplyKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Types.h" |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Validate.h" |
| 31 | |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 32 | #include "src/core/common/Registrars.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 36 | #include "src/cpu/kernels/gemm_matrix_mul/list.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 40 | namespace cpu |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | static const std::vector<CpuGemmMatrixMultiplyKernel::GemmMatrixMulKernel> available_kernels = { |
| 47 | {"neon_fp32_gemm_matrix_mul", [](const DataTypeISASelectorData &data) { return (data.dt == DataType::F32); }, |
| 48 | REGISTER_FP32_NEON(neon_fp32_gemm_matrix_mul)}, |
| 49 | {"neon_fp16_gemm_matrix_mul", |
| 50 | [](const DataTypeISASelectorData &data) { return (data.dt == DataType::F16) && data.isa.fp16; }, |
| 51 | REGISTER_FP16_NEON(neon_fp16_gemm_matrix_mul)}, |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 52 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | inline Status validate_arguments(const ITensorInfo *lhs, |
| 55 | const ITensorInfo *rhs, |
| 56 | const ITensorInfo *dst, |
| 57 | float alpha, |
| 58 | bool is_interleaved, |
| 59 | const GEMMReshapeInfo &reshape_info) |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 60 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_UNUSED(alpha); |
| 62 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(lhs); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F16, DataType::F32); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs, dst); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 66 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | if (!is_interleaved) |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 68 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON(lhs->dimension(0) != rhs->dimension(1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 70 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | if (dst->total_size() != 0) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 72 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON(rhs->dimension(0) != dst->dimension(0)); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON(lhs->dimension(1) != dst->dimension(1)); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | const int m = reshape_info.m(); |
| 81 | const int n = reshape_info.n(); |
| 82 | const int k = reshape_info.k(); |
| 83 | const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width(); |
| 84 | const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height(); |
| 85 | |
| 86 | /* Interleave */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | TensorShape tensor_shape0{lhs->tensor_shape()}; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 88 | tensor_shape0.set(0, k); |
| 89 | tensor_shape0.set(1, m); |
| 90 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 91 | const TensorInfo tensor_info0 = lhs->clone()->set_tensor_shape(tensor_shape0); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 92 | const TensorInfo tensor_info_reshaped0 = lhs->clone()->set_tensor_shape( |
| 93 | misc::shape_calculator::compute_interleaved_shape(tensor_info0, mult_interleave4x4_height)); |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lhs, &tensor_info_reshaped0); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (n != 0) /* Transpose */ |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 97 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | TensorShape tensor_shape1{rhs->tensor_shape()}; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 99 | tensor_shape1.set(0, n); |
| 100 | tensor_shape1.set(1, k); |
| 101 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | const TensorInfo tensor_info1 = rhs->clone()->set_tensor_shape(tensor_shape1); |
| 103 | const TensorInfo tensor_info_reshaped1 = |
| 104 | rhs->clone()->set_tensor_shape(misc::shape_calculator::compute_transpose1xW_with_element_size_shape( |
| 105 | tensor_info1, mult_transpose1xW_width)); |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(rhs, &tensor_info_reshaped1); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | if (dst->total_size() != 0) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 110 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | if (n != 0) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 112 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 113 | ARM_COMPUTE_RETURN_ERROR_ON(dst->dimension(0) != static_cast<size_t>(n)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 114 | } |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_RETURN_ERROR_ON(dst->dimension(1) != static_cast<size_t>(m)); |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 117 | } |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return Status{}; |
| 121 | } |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 122 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | } // namespace |
| 124 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | void CpuGemmMatrixMultiplyKernel::configure(const ITensorInfo *lhs, |
| 126 | const ITensorInfo *rhs, |
| 127 | ITensorInfo *dst, |
| 128 | float alpha, |
| 129 | bool is_interleaved, |
| 130 | const GEMMReshapeInfo &reshape_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 132 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 134 | // dst tensor auto inizialitation if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | TensorShape tensor_shape{lhs->tensor_shape()}; |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 136 | tensor_shape.set(0, is_interleaved ? reshape_info.n() : rhs->dimension(0)); |
| 137 | tensor_shape.set(1, is_interleaved ? reshape_info.m() : lhs->dimension(1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 138 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 139 | auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(tensor_shape)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 140 | |
| 141 | // Perform validate step |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(lhs, rhs, dst, alpha, is_interleaved, reshape_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 144 | _alpha = alpha; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 146 | // Configure kernel window |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 147 | Window win{}; |
| 148 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 149 | // Check if the dst tensor is a vector. If so,the kernel runs the vector-matrix multiplication |
| 150 | const bool is_dst_vector = (dst->dimension(1) == 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | if (is_dst_vector) |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 152 | { |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 153 | const unsigned int num_elems_processed_per_iteration_x = (lhs->data_type() == DataType::F32) ? 16 : 32; |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 154 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 155 | win = calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x)); |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
| 159 | constexpr unsigned int num_elems_processed_per_iteration_x = 8; |
| 160 | constexpr unsigned int num_elems_processed_per_iteration_y = 4; |
| 161 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 162 | win = |
| 163 | calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 166 | const auto uk = CpuGemmMatrixMultiplyKernel::get_implementation( |
| 167 | DataTypeISASelectorData{lhs->data_type(), CPUInfo::get().get_isa()}); |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 168 | ARM_COMPUTE_ERROR_ON_NULLPTR(uk); |
| 169 | _func = uk->ukernel; |
| 170 | |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 171 | ICPPKernel::configure(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 173 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | Status CpuGemmMatrixMultiplyKernel::validate(const ITensorInfo *lhs, |
| 175 | const ITensorInfo *rhs, |
| 176 | const ITensorInfo *dst, |
| 177 | float alpha, |
| 178 | bool is_interleaved, |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 179 | const GEMMReshapeInfo &reshape_info) |
| 180 | { |
| 181 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(lhs, rhs, dst, alpha, is_interleaved, reshape_info)); |
| 182 | |
| 183 | return Status{}; |
| 184 | } |
| 185 | |
| 186 | void CpuGemmMatrixMultiplyKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 187 | { |
| 188 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 189 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 190 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
| 191 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 192 | |
| 193 | const ITensor *lhs = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 194 | const ITensor *rhs = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 195 | ITensor *dst = tensors.get_tensor(TensorType::ACL_DST); |
| 196 | |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 197 | const bool is_dst_vector = (dst->info()->dimension(1) == 1); |
| 198 | (*_func)(lhs, rhs, dst, window, info, _alpha, is_dst_vector); |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | const char *CpuGemmMatrixMultiplyKernel::name() const |
| 202 | { |
| 203 | return "CpuGemmMatrixMultiplyKernel"; |
| 204 | } |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 205 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 206 | const std::vector<CpuGemmMatrixMultiplyKernel::GemmMatrixMulKernel> & |
| 207 | CpuGemmMatrixMultiplyKernel::get_available_kernels() |
Dana Zlotnik | 256ac62 | 2022-02-02 15:06:11 +0200 | [diff] [blame] | 208 | { |
| 209 | return available_kernels; |
| 210 | } |
Michele Di Giorgio | 53832b2 | 2021-06-21 14:45:44 +0100 | [diff] [blame] | 211 | } // namespace kernels |
| 212 | } // namespace cpu |
Michele Di Giorgio | cf9e29e | 2020-10-08 11:54:42 +0100 | [diff] [blame] | 213 | } // namespace arm_compute |