blob: 9f3461e912f1f4e222f5f2860bfb0af5cf92a2e3 [file] [log] [blame]
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 Arm Limited.
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +00003 *
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 */
24#include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfigurationValhall.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/gemm/CLGEMMHelpers.h"
29#include "arm_compute/core/GPUTarget.h"
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/TensorShape.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +000033
34#include <map>
35#include <utility>
36
37namespace arm_compute
38{
39namespace cl_gemm
40{
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010041using namespace arm_compute::misc::shape_calculator;
42
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +000043CLGEMMReshapedOnlyRHSKernelConfigurationValhall::CLGEMMReshapedOnlyRHSKernelConfigurationValhall(GPUTarget gpu)
44 : ICLGEMMKernelConfiguration(gpu)
45{
46}
47
48std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure(unsigned int m, unsigned int n, unsigned int k, unsigned int b, DataType data_type)
49{
50 using ConfigurationFunctionExecutorPtr = std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> (CLGEMMReshapedOnlyRHSKernelConfigurationValhall::*)(unsigned int m, unsigned int n, unsigned int k,
51 unsigned int b);
52
53 // Configurations for Mali-G77
54 static std::map<DataType, ConfigurationFunctionExecutorPtr> gemm_configs_G77 =
55 {
56 { DataType::F32, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f32 },
57 { DataType::F16, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f16 },
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +010058 { DataType::QASYMM8, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 },
59 { DataType::QSYMM8, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 },
60 { DataType::QASYMM8_SIGNED, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 },
61 { DataType::QSYMM8_PER_CHANNEL, &CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +000062 };
63
64 switch(_target)
65 {
66 case GPUTarget::G77:
67 default:
68 if(gemm_configs_G77.find(data_type) != gemm_configs_G77.end())
69 {
70 return (this->*gemm_configs_G77[data_type])(m, n, k, b);
71 }
72 else
73 {
74 ARM_COMPUTE_ERROR("Not supported data type");
75 }
76 }
77}
78
79std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f32(unsigned int m, unsigned int n, unsigned int k, unsigned int b)
80{
81 ARM_COMPUTE_UNUSED(k);
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +000082
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010083 GEMMLHSMatrixInfo lhs_info_buf;
84 GEMMRHSMatrixInfo rhs_info_buf;
85 GEMMLHSMatrixInfo lhs_info_img;
86 GEMMRHSMatrixInfo rhs_info_img;
87
88 // Get lhs_info/rhs_info in case of OpenCL buffer
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +000089 if(m == 1)
90 {
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010091 const unsigned int h0 = std::max(n / 4, 1U);
92 std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 1, 4, 4, 1, h0, false, true, false, true);
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +000093 }
94 else
95 {
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010096 if(m > 256)
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +000097 {
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +010098 const int v0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1));
99 std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 4, 4, 4, 1, v0, false, true, false, true);
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000100 }
101 else
102 {
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +0100103 const int v0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1));
104 std::tie(lhs_info_buf, rhs_info_buf) = configure_lhs_rhs_info(m, n, 2, 4, 4, 1, v0, false, true, false, true);
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000105 }
106 }
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +0100107
108 // Get lhs_info/rhs_info in case of OpenCL image
109 if(m == 1)
110 {
111 std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 1, 4, 4, 1, 8, true, true, false, false, true);
112 }
113 else
114 {
115 if((m / 4) * (n / 4) > 4096)
116 {
117 const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1));
118 std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 4, 4, 4, 1, h0, false, true, false, false, true);
119 }
120 else
121 {
122 const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(8)), static_cast<int>(1));
123 std::tie(lhs_info_img, rhs_info_img) = configure_lhs_rhs_info(m, n, 2, 4, 4, 1, h0, false, true, false, false, true);
124 }
125 }
126
127 const TensorInfo tensor_rhs_info(TensorShape(n, k, b), 1, DataType::F32);
128 const TensorShape shape = compute_rhs_reshaped_shape(tensor_rhs_info, rhs_info_img);
129 const TensorInfo tensor_reshaped_info(shape, 1, DataType::F32);
130
131 // In case of small workloads, we use the OpenCL buffer rather than the OpenCL image2d
132 const bool use_cl_image2d = ((m / lhs_info_img.m0) * (n / rhs_info_img.n0)) * b < 1024 ? false : true;
133
134 if(bool(validate_image2d_support_on_rhs(tensor_reshaped_info, rhs_info_img)) && use_cl_image2d)
135 {
136 return std::make_pair(lhs_info_img, rhs_info_img);
137 }
138 else
139 {
140 return std::make_pair(lhs_info_buf, rhs_info_buf);
141 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000142}
143
144std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_f16(unsigned int m, unsigned int n, unsigned int k, unsigned int b)
145{
146 ARM_COMPUTE_UNUSED(k);
147 ARM_COMPUTE_UNUSED(b);
148
149 if(m == 1)
150 {
Gian Marco Iodice2cfd3f72020-05-06 11:27:08 +0100151 if(n > 2048)
152 {
153 const unsigned int h0 = std::max(n / 4, 1U);
154 return configure_lhs_rhs_info(m, n, 1, 4, 4, 1, h0, false, true, false, true);
155 }
156 else
157 {
158 const unsigned int h0 = std::max(n / 2, 1U);
159 return configure_lhs_rhs_info(m, n, 1, 2, 8, 1, h0, false, true, false, true);
160 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000161 }
Gian Marco Iodicec6eaec32020-07-20 13:31:05 +0100162 else if(m < 128)
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000163 {
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000164 const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1));
Gian Marco Iodice2cfd3f72020-05-06 11:27:08 +0100165 if(k >= 512)
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000166 {
167 return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, false);
168 }
169 else
170 {
171 return configure_lhs_rhs_info(m, n, 2, 4, 8, 1, h0, false, true, false, false);
172 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000173 }
Gian Marco Iodice939586e2020-05-05 15:10:21 +0100174 else
175 {
176 const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1));
Gian Marco Iodice2886c752020-05-07 10:26:15 +0100177 if(n >= 64)
178 {
179 return configure_lhs_rhs_info(m, n, 4, 4, 4, 1, h0, false, true, false, false);
180 }
181 else
182 {
183 if(k >= 512)
184 {
185 return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, false);
186 }
187 else
188 {
189 return configure_lhs_rhs_info(m, n, 2, 4, 8, 1, h0, false, true, false, false);
190 }
191 }
Gian Marco Iodice939586e2020-05-05 15:10:21 +0100192 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000193}
194
195std::pair<GEMMLHSMatrixInfo, GEMMRHSMatrixInfo> CLGEMMReshapedOnlyRHSKernelConfigurationValhall::configure_G77_u8(unsigned int m, unsigned int n, unsigned int k, unsigned int b)
196{
197 ARM_COMPUTE_UNUSED(k);
198 ARM_COMPUTE_UNUSED(b);
199
200 if(m == 1)
201 {
202 const unsigned int h0 = std::max(n / 2, 1U);
203 return configure_lhs_rhs_info(m, n, 1, 4, 16, 1, h0, false, true, false, true);
204 }
205 else
206 {
Gian Marco Iodiceeb65f6d2020-04-15 11:42:15 +0100207 const int h0 = std::max(std::min(static_cast<int>(n / 4), static_cast<int>(256)), static_cast<int>(1));
208 if(m >= 28)
209 {
210 return configure_lhs_rhs_info(m, n, 4, 4, 16, 1, h0, false, true, false, true);
211 }
212 else
213 {
214 return configure_lhs_rhs_info(m, n, 2, 4, 16, 1, h0, false, true, false, true);
215 }
Gian Marco Iodice12f2b8c2020-02-13 12:27:37 +0000216 }
217}
218} // namespace cl_gemm
219} // namespace arm_compute