blob: ddfe590ee18260c053bdf275ae243457b7554721 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Georgios Pinitasda953f22019-04-02 17:27:03 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/GLES_COMPUTE/functions/GCGEMM.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
29#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h"
30#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h"
31#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
32#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
38#include "arm_compute/runtime/ITensorAllocator.h"
39
40using namespace arm_compute;
Anthony Barbier7068f992017-10-26 15:23:08 +010041
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010042namespace
43{
44Status validate_arguments(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info = GEMMInfo())
45{
46 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
47
48 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F16, DataType::F32);
49 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
50 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
51 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
52
53 if(c != nullptr)
54 {
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c->info());
56 ARM_COMPUTE_ERROR_ON_MSG(a->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A");
57 ARM_COMPUTE_ERROR_ON_MSG(b->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix B");
58 }
59
60 if(output->total_size() != 0)
61 {
62 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");
63 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");
64 }
65
66 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");
67
68 ARM_COMPUTE_UNUSED(alpha);
69 ARM_COMPUTE_UNUSED(beta);
70 ARM_COMPUTE_UNUSED(gemm_info);
71 return Status{};
72}
73} // namespace
74
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000075GCGEMM::GCGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas72219332018-06-05 14:56:06 +010076 : _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),
77 _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier7068f992017-10-26 15:23:08 +010078{
79}
80
Gian Marco1aede362018-02-05 15:07:36 +000081void GCGEMM::configure(const IGCTensor *a, const IGCTensor *b, const IGCTensor *c, IGCTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier7068f992017-10-26 15:23:08 +010082{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010083 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Anthony Barbier7068f992017-10-26 15:23:08 +010084
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010085 // Perform validation step
86 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(a->info(), b->info(), c, output->info(), alpha, beta, gemm_info));
Anthony Barbier7068f992017-10-26 15:23:08 +010087
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010088 // Check if we need to reshape the matrix B only on the first run
89 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010090 _is_prepared = false;
91 _original_b = b;
Anthony Barbier7068f992017-10-26 15:23:08 +010092
93 const IGCTensor *matrix_a = a;
94 const IGCTensor *matrix_b = b;
95
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010096 // Get the GPU target
97 const GPUTarget gpu_target = GCScheduler::get().get_target();
98
99 // Set the target for the kernels
100 _interleave_kernel.set_target(gpu_target);
101 _mm_kernel.set_target(gpu_target);
102
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100103 // Arguments used by GEMMReshapeInfo
104 // If we pass the matrix A and matrix B reshaped to GCGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to GCGEMMReshapeInfo
105 // in order to know how the matrices have been reshaped
106 const int m = a->info()->dimension(1);
107 const int n = b->info()->dimension(0);
108 const int k = a->info()->dimension(0);
109 int mult_transpose1xW_width = 1;
110 int mult_interleave4x4_height = 1;
111
112 // If the input tensor has less than 16 rows, we run a special version of GEMM without reshaping the input tensors
113 _is_interleaved_transposed = a->info()->dimension(1) > 16;
114
Anthony Barbier7068f992017-10-26 15:23:08 +0100115 if(_is_interleaved_transposed)
116 {
117 matrix_a = &_tmp_a;
118 matrix_b = &_tmp_b;
119
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100120 // Manage intermediate buffers
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000121 _memory_group.manage(&_tmp_a);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100122 if(!_reshape_b_only_on_first_run)
Georgios Pinitasceff0f92018-03-19 19:57:01 +0000123 {
124 _memory_group.manage(&_tmp_b);
125 }
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100126 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
Anthony Barbier7068f992017-10-26 15:23:08 +0100127
128 // Configure interleave kernel
129 _interleave_kernel.configure(a, &_tmp_a);
130
131 // Configure transpose kernel
132 _transpose_kernel.configure(b, &_tmp_b);
133 }
134
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100135 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height));
Anthony Barbier7068f992017-10-26 15:23:08 +0100136
137 if(_is_interleaved_transposed)
138 {
139 // Allocate intermediate tensors
140 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100141 if(!_reshape_b_only_on_first_run)
142 {
143 _tmp_b.allocator()->allocate();
144 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100145 }
146
147 // Configure matrix addition kernel
148 if(beta != 0 && c != nullptr)
149 {
150 _ma_kernel.configure(c, output, beta);
151 _run_addition = true;
152 }
153}
154
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100155Status GCGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const IGCTensor *c, const ITensorInfo *output, const float alpha, const float beta, const GEMMInfo &gemm_info)
156{
157 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(a, b, c, output, alpha, beta, gemm_info));
158 return Status{};
159}
160
Anthony Barbier7068f992017-10-26 15:23:08 +0100161void GCGEMM::run()
162{
Georgios Pinitas72219332018-06-05 14:56:06 +0100163 prepare();
164
Georgios Pinitasda953f22019-04-02 17:27:03 +0100165 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas72219332018-06-05 14:56:06 +0100166
Anthony Barbier7068f992017-10-26 15:23:08 +0100167 if(_is_interleaved_transposed)
168 {
169 // Run interleave kernel
Joel Liangeb8f71e2017-12-27 13:16:00 +0800170 GCScheduler::get().dispatch(_interleave_kernel, false);
Anthony Barbier7068f992017-10-26 15:23:08 +0100171
Georgios Pinitas72219332018-06-05 14:56:06 +0100172 if(!_reshape_b_only_on_first_run)
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100173 {
174 // Run transpose kernel
175 GCScheduler::get().dispatch(_transpose_kernel, false);
176 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100177
Joel Liangeb8f71e2017-12-27 13:16:00 +0800178 GCScheduler::get().memory_barrier();
Anthony Barbier7068f992017-10-26 15:23:08 +0100179 }
180
181 // Run matrix multiply kernel
Joel Liangeb8f71e2017-12-27 13:16:00 +0800182 GCScheduler::get().dispatch(_mm_kernel, !_run_addition);
Anthony Barbier7068f992017-10-26 15:23:08 +0100183
184 // Run matrix addition kernel
185 if(_run_addition)
186 {
Joel Liangeb8f71e2017-12-27 13:16:00 +0800187 GCScheduler::get().memory_barrier();
188 GCScheduler::get().dispatch(_ma_kernel);
Anthony Barbier7068f992017-10-26 15:23:08 +0100189 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100190}
Georgios Pinitas72219332018-06-05 14:56:06 +0100191
192void GCGEMM::prepare()
193{
194 if(!_is_prepared)
195 {
196 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
197 {
198 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
199
200 // Run transpose kernel
201 _tmp_b.allocator()->allocate();
202 GCScheduler::get().dispatch(_transpose_kernel, false);
203 GCScheduler::get().memory_barrier();
204
205 // Mark original weights tensor as unused
206 _original_b->mark_as_unused();
207 }
208
209 _is_prepared = true;
210 }
211}