blob: e23feb269a53a3939d5f7f4213ac4b8fcdc55158 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +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/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/AccessWindowTranspose.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/CLHelpers.h"
29#include "arm_compute/core/CL/CLKernelLibrary.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010033#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Helpers.h"
35#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"
Gian Marco36a0a462018-01-12 10:21:40 +000039#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042#include <string>
43
44using namespace arm_compute;
Gian Marco36a0a462018-01-12 10:21:40 +000045using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
Georgios Pinitas358ca202017-12-07 16:47:52 +000047namespace
48{
49using ElementsProcessed = Steps;
50
Gian Marco36a0a462018-01-12 10:21:40 +000051inline Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +000052{
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco36a0a462018-01-12 10:21:40 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, input1);
56
Georgios Pinitas358ca202017-12-07 16:47:52 +000057 if(!is_interleaved_transposed)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1));
Gian Marco36a0a462018-01-12 10:21:40 +000060
61 if(output->total_size() != 0)
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != output->dimension(0));
64 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != output->dimension(1));
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(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 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000101 }
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, GPUTarget gpu_target,
108 ElementsProcessed &num_elements_processed)
109{
110 bool window_changed = false;
111 Window win{};
112
113 const DataType data_type = input0->data_type();
114 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
115 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
116
117 if(is_interleaved_transposed)
118 {
119 // Configure kernel window
120 num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(data_type);
121 num_elems_processed_per_iteration_y = 4;
122
123 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
124
125 AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
126 AccessWindowTranspose input1_access(input1, 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f);
127 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
128
129 window_changed = update_window_and_padding(win, input0_access, input1_access, output_access);
130
131 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
132 }
133 else // The input tensors have not been reshaped
134 {
135 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x is set up for the default case.
136 num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(data_type);
137 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4);
138
139 // Create kernels according to the architecture, data type and input size.
140 if(gpu_target == GPUTarget::BIFROST && data_type == DataType::F32)
141 {
Gian Marco1d25ed52017-12-16 19:33:50 +0000142 num_elems_processed_per_iteration_x = (input1->dimension(0) <= 1000 && input0->num_dimensions() == 1) ? 2 : 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000143 }
144
145 // Configure window
146 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
147
148 AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), ceil_to_multiple(input0->dimension(1), num_elems_processed_per_iteration_y));
149 AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1));
150 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
151
152 window_changed = update_window_and_padding(win, input0_access, input1_access, output_access);
153
154 Coordinates coord;
155 coord.set_num_dimensions(output->num_dimensions());
156 output_access.set_valid_region(win, ValidRegion(coord, output->tensor_shape()));
157 }
158
159 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
160 return std::make_pair(err, win);
161}
162} // namespace
163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164CLGEMMMatrixMultiplyKernel::CLGEMMMatrixMultiplyKernel()
165 : _input0(nullptr), _input1(nullptr), _output(nullptr)
166{
167}
168
Gian Marco36a0a462018-01-12 10:21:40 +0000169void CLGEMMMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, float alpha, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000171 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
172
Gian Marco36a0a462018-01-12 10:21:40 +0000173 // Output tensor auto inizialitation if not yet initialized
174 TensorShape tensor_shape{ input0->info()->tensor_shape() };
175 tensor_shape.set(0, is_interleaved_transposed ? reshape_info.n() : input1->info()->dimension(0));
176 tensor_shape.set(1, is_interleaved_transposed ? reshape_info.m() : input0->info()->dimension(1));
177
178 auto_init_if_empty(*output->info(), input0->info()->clone()->set_tensor_shape(tensor_shape));
179
Georgios Pinitas358ca202017-12-07 16:47:52 +0000180 // Perform validate step
Gian Marco36a0a462018-01-12 10:21:40 +0000181 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
183 _input0 = input0;
184 _input1 = input1;
185 _output = output;
186
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000187 const DataType data_type = input0->info()->data_type();
188 const int fp_pos = input0->info()->fixed_point_position();
189
190 // Get target architecture
191 GPUTarget arch_target = get_arch_from_target(get_target());
192
193 // Configure LWS hint
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000194 if(arch_target == GPUTarget::BIFROST && input1->info()->dimension(1) == 24)
195 {
196 // LWS optimized for the 11x11 AlexNet convolution on Bifrost.
197 _lws_hint = cl::NDRange(2, 2);
198 }
199 else if(output->info()->dimension(1) == 196)
200 {
201 _lws_hint = cl::NDRange(1, 7);
202 }
203 else
204 {
205 _lws_hint = cl::NDRange(8, 8);
206 }
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000207
Georgios Pinitas358ca202017-12-07 16:47:52 +0000208 ElementsProcessed num_elements_processed{};
209
210 // Configure kernel window
211 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, arch_target, num_elements_processed);
212 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
213 ICLKernel::configure(win_config.second);
214
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000215 // Create build options
216 CLBuildOptions build_opts;
217 build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fp_pos));
218
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000219 // Only define ALPHA when alpha is not 1.0f. This avoids performing unnecessary multiplications.
Georgios Pinitas358ca202017-12-07 16:47:52 +0000220 if(std::abs(1.0f - alpha) > 0.00001f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000222 build_opts.add_option_if_else(is_data_type_fixed_point(data_type),
223 "-DALPHA=" + support::cpp11::to_string((data_type == DataType::QS8 ? sqcvt_qs8_f32(alpha, fp_pos) : sqcvt_qs16_f32(alpha, fp_pos))),
224 "-DALPHA=" + float_to_string_with_full_precision(alpha));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 }
226
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000227 std::string kernel_name;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100228 if(is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 {
Gian Marco36a0a462018-01-12 10:21:40 +0000230 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
231 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
232
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000233 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0)));
Gian Marco36a0a462018-01-12 10:21:40 +0000234 build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width));
235 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
236
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000237 if(data_type == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000239 kernel_name = "gemm_mm_interleaved_transposed_f32_" + string_from_target(arch_target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 }
241 else
242 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000243 kernel_name = "gemm_mm_interleaved_transposed_" + lower_string(string_from_data_type(data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 }
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100246 else // The input tensors have not been reshaped
247 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000248 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0)));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100249
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000250 // Create kernels according to the architecture, data type and input size.
251 if(arch_target == GPUTarget::BIFROST && data_type == DataType::F32)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100252 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000253 // The first kernel is optimized for the case of 1000 or less output elements (e.g. FC8 of AlexNet and VGG-16, and
254 // FC1 of Inception v3). The second kernel is optimized for the case of greater than 1000 output elements (e.g.
255 // FC6 and FC7 of AlexNet and VGG-16).
Gian Marco1d25ed52017-12-16 19:33:50 +0000256 kernel_name = (input1->info()->dimension(0) <= 1000 && input0->info()->num_dimensions() == 1) ? "gemm_mm_floating_point_f32_bifrost_1000" : "gemm_mm_floating_point_f32_bifrost";
Georgios Pinitas358ca202017-12-07 16:47:52 +0000257
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000258 // The work-group size equal to the Bifrost quad size has been proved to be optimal for these kernels
259 // via exhaustive autotuning over a range of representative layer configurations.
260 _lws_hint = cl::NDRange(4);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100261 }
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000262 else if(is_data_type_fixed_point(data_type))
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100263 {
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000264 kernel_name = "gemm_mm_" + lower_string(string_from_data_type(data_type));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100265 }
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000266 else // (MIDGARD and F32) or (F16)
267 {
268 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
269 kernel_name = "gemm_mm_floating_point";
270 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000271 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y()));
272 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x()));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100273 }
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000274
275 // Create kernel
276 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
277
278 // Set config_id for enabling LWS tuning
279 _config_id = "gemm_";
280 _config_id += (is_interleaved_transposed ? "reshaped_" : "");
281 _config_id += lower_string(string_from_data_type(input0->info()->data_type()));
282 _config_id += "_";
283 _config_id += support::cpp11::to_string(output->info()->dimension(1));
284 _config_id += "_";
285 _config_id += support::cpp11::to_string(output->info()->dimension(0));
286 _config_id += "_";
287 _config_id += (is_interleaved_transposed ? support::cpp11::to_string(input1->info()->dimension(0)) : support::cpp11::to_string(input1->info()->dimension(1)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288}
289
Gian Marco36a0a462018-01-12 10:21:40 +0000290Status CLGEMMMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved_transposed,
291 const GEMMReshapeInfo &reshape_info, GPUTarget gpu_target)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000292{
Gian Marco36a0a462018-01-12 10:21:40 +0000293 // Note: num_elements_processed will be set in validate_and_configure_window()
Georgios Pinitas358ca202017-12-07 16:47:52 +0000294 ElementsProcessed num_elements_processed{};
295 ARM_COMPUTE_UNUSED(alpha);
Gian Marco36a0a462018-01-12 10:21:40 +0000296 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000297 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
298 input1->clone().get(),
299 output->clone().get(),
300 is_interleaved_transposed,
301 gpu_target,
302 num_elements_processed)
303 .first);
304
305 return Status{};
306}
307
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308void CLGEMMMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue)
309{
310 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
311 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
312
313 Window slice = window.first_slice_window_2D();
314 Window slice_matrix_b = slice;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100315
316 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
317 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318
319 do
320 {
321 Window slice_b = slice;
322 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
323 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
324 if(_input1->info()->num_dimensions() < 3)
325 {
326 slice_b = slice_matrix_b;
327 }
328
329 unsigned int idx = 0;
330 add_2D_tensor_argument(idx, _input0, slice);
331 add_2D_tensor_argument(idx, _input1, slice_b);
332 add_2D_tensor_argument(idx, _output, slice);
333 enqueue(queue, *this, slice, _lws_hint);
334 }
335 while(window.slide_window_slice_2D(slice));
336}