blob: beccd94844f6669e6033b54f829dcbe2f8c91898 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Dana Zlotnik256ac622022-02-02 15:06:11 +02002 * Copyright (c) 2017-2022 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuGemmMatrixMultiplyKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/Validate.h"
31
Dana Zlotnik256ac622022-02-02 15:06:11 +020032#include "src/core/common/Registrars.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Dana Zlotnik256ac622022-02-02 15:06:11 +020036#include "src/cpu/kernels/gemm_matrix_mul/list.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38namespace arm_compute
39{
Michele Di Giorgio53832b22021-06-21 14:45:44 +010040namespace cpu
41{
42namespace kernels
43{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044namespace
45{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046static 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 Zlotnik256ac622022-02-02 15:06:11 +020052};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054inline 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 Arena7c23ad02017-11-30 15:08:38 +000060{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000061 ARM_COMPUTE_UNUSED(alpha);
62
Michele Di Giorgio53832b22021-06-21 14:45:44 +010063 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 Arena7c23ad02017-11-30 15:08:38 +000066
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (!is_interleaved)
Giorgio Arena7c23ad02017-11-30 15:08:38 +000068 {
Michele Di Giorgio53832b22021-06-21 14:45:44 +010069 ARM_COMPUTE_RETURN_ERROR_ON(lhs->dimension(0) != rhs->dimension(1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000070
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (dst->total_size() != 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000072 {
Michele Di Giorgio53832b22021-06-21 14:45:44 +010073 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 Szabob4e3e1c2017-11-30 17:17:17 +000076 }
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 TensorShape tensor_shape0{lhs->tensor_shape()};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000088 tensor_shape0.set(0, k);
89 tensor_shape0.set(1, m);
90
Michele Di Giorgio53832b22021-06-21 14:45:44 +010091 const TensorInfo tensor_info0 = lhs->clone()->set_tensor_shape(tensor_shape0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 const TensorInfo tensor_info_reshaped0 = lhs->clone()->set_tensor_shape(
93 misc::shape_calculator::compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
Michele Di Giorgio53832b22021-06-21 14:45:44 +010094 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lhs, &tensor_info_reshaped0);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000095
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (n != 0) /* Transpose */
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000097 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 TensorShape tensor_shape1{rhs->tensor_shape()};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000099 tensor_shape1.set(0, n);
100 tensor_shape1.set(1, k);
101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 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 Giorgio53832b22021-06-21 14:45:44 +0100106 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(rhs, &tensor_info_reshaped1);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000107 }
108
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 if (dst->total_size() != 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000110 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 if (n != 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000112 {
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100113 ARM_COMPUTE_RETURN_ERROR_ON(dst->dimension(0) != static_cast<size_t>(n));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000114 }
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100115 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 Szabob4e3e1c2017-11-30 17:17:17 +0000117 }
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000118 }
119
120 return Status{};
121}
Dana Zlotnik256ac622022-02-02 15:06:11 +0200122
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123} // namespace
124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125void 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 Barbier6ff3b192017-09-04 18:44:23 +0100131{
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100132 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100134 // dst tensor auto inizialitation if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 TensorShape tensor_shape{lhs->tensor_shape()};
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100136 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 Szabob4e3e1c2017-11-30 17:17:17 +0000138
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100139 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(tensor_shape));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000140
141 // Perform validate step
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100142 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(lhs, rhs, dst, alpha, is_interleaved, reshape_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100144 _alpha = alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000146 // Configure kernel window
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100147 Window win{};
148
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100149 // 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 if (is_dst_vector)
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100152 {
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100153 const unsigned int num_elems_processed_per_iteration_x = (lhs->data_type() == DataType::F32) ? 16 : 32;
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100154
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100155 win = calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x));
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100156 }
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 win =
163 calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100164 }
165
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 const auto uk = CpuGemmMatrixMultiplyKernel::get_implementation(
167 DataTypeISASelectorData{lhs->data_type(), CPUInfo::get().get_isa()});
Dana Zlotnik256ac622022-02-02 15:06:11 +0200168 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
169 _func = uk->ukernel;
170
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100171 ICPPKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172}
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100173
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174Status CpuGemmMatrixMultiplyKernel::validate(const ITensorInfo *lhs,
175 const ITensorInfo *rhs,
176 const ITensorInfo *dst,
177 float alpha,
178 bool is_interleaved,
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100179 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
186void 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 Zlotnik256ac622022-02-02 15:06:11 +0200197 const bool is_dst_vector = (dst->info()->dimension(1) == 1);
198 (*_func)(lhs, rhs, dst, window, info, _alpha, is_dst_vector);
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100199}
200
201const char *CpuGemmMatrixMultiplyKernel::name() const
202{
203 return "CpuGemmMatrixMultiplyKernel";
204}
Dana Zlotnik256ac622022-02-02 15:06:11 +0200205
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206const std::vector<CpuGemmMatrixMultiplyKernel::GemmMatrixMulKernel> &
207CpuGemmMatrixMultiplyKernel::get_available_kernels()
Dana Zlotnik256ac622022-02-02 15:06:11 +0200208{
209 return available_kernels;
210}
Michele Di Giorgio53832b22021-06-21 14:45:44 +0100211} // namespace kernels
212} // namespace cpu
Michele Di Giorgiocf9e29e2020-10-08 11:54:42 +0100213} // namespace arm_compute