blob: 821464e3b36f898df03362cfab67d4fff74d1674 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco20d78482018-01-11 15:10:58 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/runtime/CL/functions/CLGEMM.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
Gian Marco Iodice750641d2018-05-08 12:01:57 +010028#include "arm_compute/core/GPUTarget.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010032#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Validate.h"
Gian Marco Iodice750641d2018-05-08 12:01:57 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/runtime/CL/CLScheduler.h"
36#include "arm_compute/runtime/ITensorAllocator.h"
37
38using namespace arm_compute;
Gian Marco Iodice750641d2018-05-08 12:01:57 +010039using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Gian Marco36a0a462018-01-12 10:21:40 +000041namespace
42{
43inline bool is_interleaved_transposed(int m, int n, int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
44{
45 bool flag = true;
46
Georgios Pinitasa34286e2018-09-04 12:18:50 +010047 if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76))
Gian Marco36a0a462018-01-12 10:21:40 +000048 {
49 // COMPMID-852
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010050 if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run)
Gian Marco36a0a462018-01-12 10:21:40 +000051 {
Gian Marco Iodice513fe2e2018-06-04 18:08:48 +010052 constexpr float alpha = 3.2f;
53 constexpr float fact0 = 1.51f;
54 constexpr float fact1 = 1.66f;
55 constexpr float ops = 12.0f;
56 const float scale = k > 1024 ? 1.07f : 1.0f;
57 flag = alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops);
Gian Marco36a0a462018-01-12 10:21:40 +000058 }
59 else
60 {
61 flag = false;
62 }
63 }
Gian Marco Iodicecda0c382018-04-23 16:16:22 +010064 else
65 {
66 // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once
67 flag = m != 1 && reshape_b_only_on_first_run;
68 }
Gian Marco36a0a462018-01-12 10:21:40 +000069
70 return flag;
71}
72} // namespace
73
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010074CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010075 : _memory_group(std::move(memory_manager)),
76 _interleave_kernel(),
77 _transpose_kernel(),
78 _mm_kernel(),
79 _ma_kernel(),
80 _tmp_a(),
81 _tmp_b(),
82 _original_b(nullptr),
83 _is_interleaved_transposed(false),
84 _run_addition(false),
85 _reshape_b_only_on_first_run(false),
86 _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087{
88}
89
Gian Marco1d25ed52017-12-16 19:33:50 +000090void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091{
Georgios Pinitas78c00902018-01-09 17:33:11 +000092 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
Georgios Pinitas78c00902018-01-09 17:33:11 +000094 // Perform validation step
Gian Marco Iodice750641d2018-05-08 12:01:57 +010095 ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
Gian Marco1d25ed52017-12-16 19:33:50 +000097 // Check if we need to reshape the matrix B only on the first run
98 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +010099 _is_prepared = gemm_info.retain_internal_weights();
Georgios Pinitas72219332018-06-05 14:56:06 +0100100 _original_b = b;
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100101
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100102 const ICLTensor *matrix_a = a;
103 const ICLTensor *matrix_b = b;
104
Gian Marco36a0a462018-01-12 10:21:40 +0000105 // Get the GPU target
106 const GPUTarget gpu_target = CLScheduler::get().target();
107
108 // Set the target for the kernels
109 _interleave_kernel.set_target(gpu_target);
110 _mm_kernel.set_target(gpu_target);
111
112 // Arguments used by GEMMReshapeInfo
113 // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo
114 // in order to know how the matrices have been reshaped
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100115 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
116 const int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
Gian Marco36a0a462018-01-12 10:21:40 +0000117 const int n = b->info()->dimension(0);
118 const int k = a->info()->dimension(0);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000119 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco36a0a462018-01-12 10:21:40 +0000120 int mult_transpose1xW_width = 1;
121 int mult_interleave4x4_height = 1;
122
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100123 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
Gian Marco36a0a462018-01-12 10:21:40 +0000124 {
125 mult_transpose1xW_width = 4;
126 mult_interleave4x4_height = 2;
127 }
128
129 // Check if we need to reshape the matrix A and matrix B
130 _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target);
Gian Marcob5311a62017-12-13 12:48:03 +0000131
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100132 // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
133 if(_is_interleaved_transposed)
134 {
135 reinterpret_input_as_3d = false;
136 }
137
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100138 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100140 matrix_a = &_tmp_a;
141 matrix_b = &_tmp_b;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
Gian Marco19835e52018-01-30 13:35:54 +0000143 // Manage intermediate buffers
144 _memory_group.manage(&_tmp_a);
Georgios Pinitasae4ce7b2018-03-19 17:50:45 +0000145 if(!_reshape_b_only_on_first_run)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000146 {
147 _memory_group.manage(&_tmp_b);
148 }
Gian Marco20d78482018-01-11 15:10:58 +0000149 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100150
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 // Configure interleave kernel
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100152 _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154 // Configure transpose kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000155 _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100156 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100158 // Configure and tune matrix multiply kernel
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100159 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k,
160 mult_transpose1xW_width, mult_interleave4x4_height,
161 depth_output_gemm3d, reinterpret_input_as_3d));
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100162 CLScheduler::get().tune_kernel_static(_mm_kernel);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100163
164 if(_is_interleaved_transposed)
165 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 // Allocate intermediate tensors
167 _tmp_a.allocator()->allocate();
Georgios Pinitase0437672018-05-02 14:07:55 +0100168 if(!_reshape_b_only_on_first_run)
169 {
170 _tmp_b.allocator()->allocate();
171 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
174 // Configure matrix addition kernel
175 if(beta != 0 && c != nullptr)
176 {
177 _ma_kernel.configure(c, output, beta);
178 _run_addition = true;
179 }
180}
181
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100182Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000183{
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100184 ARM_COMPUTE_UNUSED(alpha);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100185 ARM_COMPUTE_UNUSED(output);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100186
187 // Check if we need to reshape the matrix B only on the first run
188 const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
189
190 const ITensorInfo *matrix_a_info = a;
191 const ITensorInfo *matrix_b_info = b;
192
193 TensorInfo tmp_a_info{};
194 TensorInfo tmp_b_info{};
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100195
196 // Get the GPU target
197 const GPUTarget gpu_target = CLScheduler::get().target();
198
199 // Arguments used by GEMMReshapeInfo
200 // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo
201 // in order to know how the matrices have been reshaped
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100202 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
203 const int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100204 const int n = b->dimension(0);
205 const int k = a->dimension(0);
206 int mult_transpose1xW_width = 1;
207 int mult_interleave4x4_height = 1;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100208 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100209
210 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
211 {
212 mult_transpose1xW_width = 4;
213 mult_interleave4x4_height = 2;
214 }
215
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100216 // Check if we need to reshape the matrix A and matrix B
217 const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target);
218
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100219 // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
220 if(run_interleave_transpose)
221 {
222 reinterpret_input_as_3d = false;
223 }
224
225 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d);
226
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100227 if(run_interleave_transpose)
228 {
229 matrix_a_info = &tmp_a_info;
230 matrix_b_info = &tmp_b_info;
231
232 // Validate interleave kernel
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100233 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d())));
234 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &tmp_a_info, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d()));
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100235
236 // Validate transpose kernel
237 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
238 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &tmp_b_info, mult_transpose1xW_width));
239 }
240
241 // Validate matrix multiply
Giorgio Arena0f170392018-07-18 16:13:12 +0100242 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, alpha, run_interleave_transpose, reshape_info, gpu_target));
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100243
244 if(beta != 0 && c != nullptr)
245 {
246 // Validate matrix addition kernel
Giorgio Arena0f170392018-07-18 16:13:12 +0100247 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100248 }
249
Georgios Pinitas78c00902018-01-09 17:33:11 +0000250 return Status{};
251}
252
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253void CLGEMM::run()
254{
Georgios Pinitase0437672018-05-02 14:07:55 +0100255 prepare();
256
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100257 _memory_group.acquire();
258
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100259 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100260 {
261 // Run interleave kernel
262 CLScheduler::get().enqueue(_interleave_kernel, false);
263
Georgios Pinitase0437672018-05-02 14:07:55 +0100264 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000265 {
266 // Run transpose kernel
267 CLScheduler::get().enqueue(_transpose_kernel, false);
268 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269 }
270
271 // Run matrix multiply kernel
272 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
273
274 // Run matrix addition kernel
275 if(_run_addition)
276 {
277 CLScheduler::get().enqueue(_ma_kernel);
278 }
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100279
280 _memory_group.release();
Georgios Pinitase0437672018-05-02 14:07:55 +0100281}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100282
Georgios Pinitase0437672018-05-02 14:07:55 +0100283void CLGEMM::prepare()
284{
285 if(!_is_prepared)
286 {
287 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
288 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100289 // Run transpose kernel and mark original weights tensor as unused
Georgios Pinitase0437672018-05-02 14:07:55 +0100290 _tmp_b.allocator()->allocate();
291 CLScheduler::get().enqueue(_transpose_kernel, false);
292 _original_b->mark_as_unused();
293 }
294 CLScheduler::get().queue().finish();
295 _is_prepared = true;
296 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297}