blob: 2bd769cac46f07b9428af3f80b64ea41f9f79019 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgio164b65d2018-04-13 14:28:08 +01002 * Copyright (c) 2017-2018 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/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/AccessWindowTranspose.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
30#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
31#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
32#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
33#include "arm_compute/core/Helpers.h"
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010034#include "arm_compute/core/TensorInfo.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010035#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010039#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010040
41#include <set>
42#include <string>
43
44using namespace arm_compute;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010045using namespace arm_compute::misc::shape_calculator;
46
47namespace
48{
49using ElementsProcessed = Steps;
50
51inline Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
52{
53 ARM_COMPUTE_UNUSED(reshape_info);
54 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F16, DataType::F32);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
57
58 if(!is_interleaved_transposed)
59 {
60 ARM_COMPUTE_ERROR_ON(input0->dimension(0) != input1->dimension(1));
61
62 if(output->total_size() != 0)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != output->dimension(0));
65 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != output->dimension(1));
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
67 }
68 }
69 else
70 {
71 const int m = reshape_info.m();
72 const int n = reshape_info.n();
73 const int k = reshape_info.k();
74 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
75 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
76
77 TensorShape tensor_shape0{ input0->tensor_shape() };
78 tensor_shape0.set(0, k);
79 tensor_shape0.set(1, m);
80
81 TensorShape tensor_shape1{ input1->tensor_shape() };
82 tensor_shape1.set(0, n);
83 tensor_shape1.set(1, k);
84
85 const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0);
86 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
87
88 const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
89 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width));
90
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0);
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
93
94 if(output->total_size() != 0)
95 {
96 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != static_cast<size_t>(n));
97 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != static_cast<size_t>(m));
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, output);
100 }
101 }
102
103 return Status{};
104}
105
106inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output,
107 bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info,
108 GPUTarget gpu_target, ElementsProcessed &num_elements_processed)
109{
110 ARM_COMPUTE_UNUSED(gpu_target);
111
112 // Output tensor auto inizialitation if not yet initialized
113 TensorShape tensor_shape{ input0->tensor_shape() };
114 tensor_shape.set(0, is_interleaved_transposed ? reshape_info.n() : input1->dimension(0));
115 tensor_shape.set(1, is_interleaved_transposed ? reshape_info.m() : input0->dimension(1));
116
117 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(tensor_shape));
118
119 bool window_changed = false;
120 Window win{};
121
122 const DataType data_type = input0->data_type();
123 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
124 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
125
126 if(is_interleaved_transposed)
127 {
128 // Configure window kernel
129 num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(data_type);
130 num_elems_processed_per_iteration_y = 4;
131
132 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
133
134 AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
135 AccessWindowTranspose input1_access(input1, 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f);
136 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
137
138 update_window_and_padding(win, input0_access, input1_access, output_access);
139
140 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
141 }
142 else // The input tensors have not been reshaped
143 {
144 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor
145
146 switch(data_type)
147 {
148 case DataType::F16:
149 num_elems_processed_per_iteration_x = 4;
150 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4);
151 break;
152
153 case DataType::F32:
154 num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(data_type);
155 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4);
156 break;
157
158 default:
159 ARM_COMPUTE_ERROR("Current data type is not supported");
160 break;
161 }
162
163 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
164
165 AccessWindowStatic input0_access(input0, 0, 0, ceil_to_multiple(input0->dimension(0), 8), ceil_to_multiple(input0->dimension(1), num_elems_processed_per_iteration_y));
166 AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1));
167 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
168
169 update_window_and_padding(win, input0_access, input1_access, output_access);
170
171 Coordinates coord;
172 coord.set_num_dimensions(output->num_dimensions());
173 output_access.set_valid_region(win, ValidRegion(coord, output->tensor_shape()));
174 }
175
176 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
177 return std::make_pair(err, win);
178}
179} // namespace
Anthony Barbier7068f992017-10-26 15:23:08 +0100180
181GCGEMMMatrixMultiplyKernel::GCGEMMMatrixMultiplyKernel()
182 : _input0(nullptr), _input1(nullptr), _output(nullptr)
183{
184}
185
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100186void GCGEMMMatrixMultiplyKernel::configure(const IGCTensor *input0, const IGCTensor *input1, IGCTensor *output, float alpha, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Anthony Barbier7068f992017-10-26 15:23:08 +0100187{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100188 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Anthony Barbier7068f992017-10-26 15:23:08 +0100189
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100190 // Perform validate step
191 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info));
Anthony Barbier7068f992017-10-26 15:23:08 +0100192
193 _input0 = input0;
194 _input1 = input1;
195 _output = output;
196
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100197 // Get target architecture
198 GPUTarget gpu_target = get_target();
199
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100200 ElementsProcessed num_elements_processed{};
201
202 // Configure kernel window
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100203 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info, gpu_target, num_elements_processed);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100204 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
205 IGCKernel::configure(win_config.second);
206
207 // Create build options
Anthony Barbier7068f992017-10-26 15:23:08 +0100208 std::set<std::string> build_opts;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100209 std::string kernel_name;
Anthony Barbier7068f992017-10-26 15:23:08 +0100210 Window win;
211
212 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
213 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
214 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
215 build_opts.emplace("#define COLS_A " + support::cpp11::to_string(input0->info()->dimension(0)));
216 build_opts.emplace("#define COLS_B " + support::cpp11::to_string(input1->info()->dimension(0)));
217 build_opts.emplace("#define ALPHA " + float_to_string_with_full_precision(alpha));
218
219 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
220 if(is_interleaved_transposed)
221 {
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100222 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
223 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
224
225 build_opts.emplace("#define MULT_TRANSPOSE1XW_WIDTH " + support::cpp11::to_string(mult_transpose1xW_width));
226 build_opts.emplace("#define MULT_INTERLEAVE4X4_HEIGHT " + support::cpp11::to_string(mult_interleave4x4_height));
227
Anthony Barbier7068f992017-10-26 15:23:08 +0100228 switch(input0->info()->data_type())
229 {
230 case DataType::F16:
231 build_opts.emplace("#define DATA_TYPE_FP16");
232 break;
233
234 case DataType::F32:
235 build_opts.emplace("#define DATA_TYPE_FP32");
236 break;
237
238 default:
239 ARM_COMPUTE_ERROR("Current data type is not supported");
240 break;
241 }
242
243 build_opts.emplace("#define GEMM_MM_INTERLEAVED_TRANSPOSED");
244
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100245 kernel_name = "gemm_mm_interleaved_transposed";
Anthony Barbier7068f992017-10-26 15:23:08 +0100246 }
247 else
248 {
Anthony Barbier7068f992017-10-26 15:23:08 +0100249 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor
Anthony Barbier7068f992017-10-26 15:23:08 +0100250
251 switch(input0->info()->data_type())
252 {
253 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100254 build_opts.emplace("#define DATA_TYPE_FP16");
Frank Leib9d38ee2017-12-05 10:43:33 +0800255 build_opts.emplace("#define MM_PROCESS_4X_OPTIMIZED");
Anthony Barbier7068f992017-10-26 15:23:08 +0100256 break;
257
258 case DataType::F32:
Anthony Barbier7068f992017-10-26 15:23:08 +0100259 build_opts.emplace("#define DATA_TYPE_FP32");
260 break;
261
262 default:
263 ARM_COMPUTE_ERROR("Current data type is not supported");
264 break;
265 }
266
267 build_opts.emplace("#define GEMM_MM_FLOATING_POINT");
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100268 build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_X " + support::cpp11::to_string(num_elements_processed.x()));
269 build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_Y " + support::cpp11::to_string(num_elements_processed.y()));
Anthony Barbier7068f992017-10-26 15:23:08 +0100270
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100271 kernel_name = "gemm_mm_floating_point";
Anthony Barbier7068f992017-10-26 15:23:08 +0100272 }
273
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100274 // Create kernel
275 _kernel = GCKernelLibrary::get().create_kernel(kernel_name, build_opts);
276}
277
278Status GCGEMMMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved_transposed,
279 const GEMMReshapeInfo &reshape_info, GPUTarget gpu_target)
280{
281 ARM_COMPUTE_UNUSED(alpha);
282 ElementsProcessed num_elements_processed{};
283 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
284 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
285 input1->clone().get(),
286 output->clone().get(),
287 is_interleaved_transposed,
288 reshape_info,
289 gpu_target,
290 num_elements_processed)
291 .first);
292 return Status{};
Anthony Barbier7068f992017-10-26 15:23:08 +0100293}
294
295void GCGEMMMatrixMultiplyKernel::run(const Window &window)
296{
297 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
298 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
299
300 _kernel.use();
301
302 Window slice = window.first_slice_window_2D();
303 Window slice_matrix_b = slice;
304
305 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
306 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
307
308 do
309 {
310 Window slice_b = slice;
311 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
312 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
313 if(_input1->info()->num_dimensions() < 3)
314 {
315 slice_b = slice_matrix_b;
316 }
317
318 unsigned int idx = 0;
Anthony Barbier7068f992017-10-26 15:23:08 +0100319
Joel Liangabd03cf2018-01-08 15:20:48 +0800320 add_2D_tensor_argument(idx, _input0, 1, slice);
321 add_2D_tensor_argument(idx, _input1, 2, slice_b);
322 add_2D_tensor_argument(idx, _output, 3, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100323 _kernel.update_shader_params();
324 enqueue(*this, slice);
325 }
326 while(window.slide_window_slice_2D(slice));
327}