blob: 1ee51a0a487476f0351e84c35f801350361d669b [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"
27#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
28#include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h"
29#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
30#include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010035#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Validate.h"
37#include "arm_compute/runtime/CL/CLScheduler.h"
38#include "arm_compute/runtime/ITensorAllocator.h"
39
40using namespace arm_compute;
41
Gian Marco36a0a462018-01-12 10:21:40 +000042namespace
43{
44inline bool is_interleaved_transposed(int m, int n, int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
45{
46 bool flag = true;
47
Sam Laynton56e8e862018-04-05 13:26:08 +010048 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX))
Gian Marco36a0a462018-01-12 10:21:40 +000049 {
50 // COMPMID-852
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010051 if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run)
Gian Marco36a0a462018-01-12 10:21:40 +000052 {
53 const float scale = k < 1024 ? 2.0f : 2.5f;
Gian Marco19835e52018-01-30 13:35:54 +000054 flag = (scale * n) > ((1.66f * n) + 38.4f);
Gian Marco36a0a462018-01-12 10:21:40 +000055 }
56 else
57 {
58 flag = false;
59 }
60 }
Gian Marco Iodicecda0c382018-04-23 16:16:22 +010061 else
62 {
63 // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once
64 flag = m != 1 && reshape_b_only_on_first_run;
65 }
Gian Marco36a0a462018-01-12 10:21:40 +000066
67 return flag;
68}
Georgios Pinitas78c00902018-01-09 17:33:11 +000069
70Status validate_arguments(const ITensorInfo *a, const ITensorInfo *b, const ICLTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info = GEMMInfo())
71{
72 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
73
74 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Georgios Pinitas78c00902018-01-09 17:33:11 +000076 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
78
79 if(c != nullptr)
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info());
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000082 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The matrix C must have the same number of rows as the matrix A");
83 ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The matrix C must have the same number of columns as the matrix B");
84 }
85
86 if(output->total_size() != 0)
87 {
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(b->dimension(0) != output->dimension(0), "The output matrix must have the same number of columns as the matrix B");
89 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(1) != output->dimension(1), "The output matrix must have the same number of rows as the matrix A");
Georgios Pinitas78c00902018-01-09 17:33:11 +000090 }
91
92 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->dimension(0) != b->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
93
94 ARM_COMPUTE_UNUSED(alpha);
95 ARM_COMPUTE_UNUSED(beta);
96 return Status{};
97}
Gian Marco36a0a462018-01-12 10:21:40 +000098} // namespace
99
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100100CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas82b51482018-04-24 15:14:12 +0100101 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _original_b(nullptr), _is_interleaved_transposed(false),
Georgios Pinitase0437672018-05-02 14:07:55 +0100102 _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
104}
105
Gian Marco1d25ed52017-12-16 19:33:50 +0000106void 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 +0100107{
Georgios Pinitas78c00902018-01-09 17:33:11 +0000108 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
Georgios Pinitas78c00902018-01-09 17:33:11 +0000110 // Perform validation step
111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(a->info(), b->info(), c, output->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Georgios Pinitas82b51482018-04-24 15:14:12 +0100113 // Store original b matrix
114 _original_b = b;
115
Gian Marco1d25ed52017-12-16 19:33:50 +0000116 // Check if we need to reshape the matrix B only on the first run
117 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitase0437672018-05-02 14:07:55 +0100118 _is_prepared = false;
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100119
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100120 const ICLTensor *matrix_a = a;
121 const ICLTensor *matrix_b = b;
122
Gian Marco36a0a462018-01-12 10:21:40 +0000123 // Get the GPU target
124 const GPUTarget gpu_target = CLScheduler::get().target();
125
126 // Set the target for the kernels
127 _interleave_kernel.set_target(gpu_target);
128 _mm_kernel.set_target(gpu_target);
129
130 // Arguments used by GEMMReshapeInfo
131 // 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
132 // in order to know how the matrices have been reshaped
133 const int m = a->info()->dimension(1);
134 const int n = b->info()->dimension(0);
135 const int k = a->info()->dimension(0);
136 int mult_transpose1xW_width = 1;
137 int mult_interleave4x4_height = 1;
138
Sam Laynton56e8e862018-04-05 13:26:08 +0100139 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX))
Gian Marco36a0a462018-01-12 10:21:40 +0000140 {
141 mult_transpose1xW_width = 4;
142 mult_interleave4x4_height = 2;
143 }
144
145 // Check if we need to reshape the matrix A and matrix B
146 _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 +0000147
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100148 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100150 matrix_a = &_tmp_a;
151 matrix_b = &_tmp_b;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
Gian Marco19835e52018-01-30 13:35:54 +0000153 // Manage intermediate buffers
154 _memory_group.manage(&_tmp_a);
Georgios Pinitasae4ce7b2018-03-19 17:50:45 +0000155 if(!_reshape_b_only_on_first_run)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000156 {
157 _memory_group.manage(&_tmp_b);
158 }
Gian Marco20d78482018-01-11 15:10:58 +0000159 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100160
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 // Configure interleave kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000162 _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163
164 // Configure transpose kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000165 _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100166 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
Gian Marco36a0a462018-01-12 10:21:40 +0000168 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100169
170 if(_is_interleaved_transposed)
171 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 // Allocate intermediate tensors
173 _tmp_a.allocator()->allocate();
Georgios Pinitase0437672018-05-02 14:07:55 +0100174 if(!_reshape_b_only_on_first_run)
175 {
176 _tmp_b.allocator()->allocate();
177 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180 // Configure matrix addition kernel
181 if(beta != 0 && c != nullptr)
182 {
183 _ma_kernel.configure(c, output, beta);
184 _run_addition = true;
185 }
186}
187
Georgios Pinitas78c00902018-01-09 17:33:11 +0000188Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ICLTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info)
189{
190 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(a, b, c, output, alpha, beta, gemm_info));
191 return Status{};
192}
193
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194void CLGEMM::run()
195{
Georgios Pinitase0437672018-05-02 14:07:55 +0100196 prepare();
197
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100198 _memory_group.acquire();
199
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100200 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 {
202 // Run interleave kernel
203 CLScheduler::get().enqueue(_interleave_kernel, false);
204
Georgios Pinitase0437672018-05-02 14:07:55 +0100205 if(!_reshape_b_only_on_first_run)
Gian Marco1d25ed52017-12-16 19:33:50 +0000206 {
207 // Run transpose kernel
208 CLScheduler::get().enqueue(_transpose_kernel, false);
209 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 }
211
212 // Run matrix multiply kernel
213 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
214
215 // Run matrix addition kernel
216 if(_run_addition)
217 {
218 CLScheduler::get().enqueue(_ma_kernel);
219 }
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100220
221 _memory_group.release();
Georgios Pinitase0437672018-05-02 14:07:55 +0100222}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100223
Georgios Pinitase0437672018-05-02 14:07:55 +0100224void CLGEMM::prepare()
225{
226 if(!_is_prepared)
227 {
228 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
229 {
230 // Run transpose kernel
231 _tmp_b.allocator()->allocate();
232 CLScheduler::get().enqueue(_transpose_kernel, false);
233 _original_b->mark_as_unused();
234 }
235 CLScheduler::get().queue().finish();
236 _is_prepared = true;
237 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238}