blob: 8ead05abfb667f3018479859534ad7b3190b6c3c [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);
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +010055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F16, DataType::F32);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the matrix B must be <= 3");
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010058
59 if(!is_interleaved_transposed)
60 {
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +010061 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1));
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010062
63 if(output->total_size() != 0)
64 {
65 ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != output->dimension(0));
66 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != output->dimension(1));
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
68 }
69 }
70 else
71 {
72 const int m = reshape_info.m();
73 const int n = reshape_info.n();
74 const int k = reshape_info.k();
75 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
76 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
77
78 TensorShape tensor_shape0{ input0->tensor_shape() };
79 tensor_shape0.set(0, k);
80 tensor_shape0.set(1, m);
81
82 TensorShape tensor_shape1{ input1->tensor_shape() };
83 tensor_shape1.set(0, n);
84 tensor_shape1.set(1, k);
85
86 const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0);
87 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
88
89 const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
90 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width));
91
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0);
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
94
95 if(output->total_size() != 0)
96 {
97 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != static_cast<size_t>(n));
98 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != static_cast<size_t>(m));
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100100 }
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 {
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100144 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor.
145 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100146
147 switch(data_type)
148 {
149 case DataType::F16:
150 num_elems_processed_per_iteration_x = 4;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100151 break;
152
153 case DataType::F32:
154 num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(data_type);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100155 break;
156
157 default:
158 ARM_COMPUTE_ERROR("Current data type is not supported");
159 break;
160 }
161
162 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
163
164 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));
165 AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1));
166 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
167
168 update_window_and_padding(win, input0_access, input1_access, output_access);
169
170 Coordinates coord;
171 coord.set_num_dimensions(output->num_dimensions());
172 output_access.set_valid_region(win, ValidRegion(coord, output->tensor_shape()));
173 }
174
175 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
176 return std::make_pair(err, win);
177}
178} // namespace
Anthony Barbier7068f992017-10-26 15:23:08 +0100179
180GCGEMMMatrixMultiplyKernel::GCGEMMMatrixMultiplyKernel()
181 : _input0(nullptr), _input1(nullptr), _output(nullptr)
182{
183}
184
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100185void 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 +0100186{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100187 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Anthony Barbier7068f992017-10-26 15:23:08 +0100188
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100189 // Perform validate step
190 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 +0100191
192 _input0 = input0;
193 _input1 = input1;
194 _output = output;
195
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100196 // Get target architecture
197 GPUTarget gpu_target = get_target();
198
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100199 ElementsProcessed num_elements_processed{};
200
201 // Configure kernel window
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100202 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 +0100203 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
204 IGCKernel::configure(win_config.second);
205
206 // Create build options
Anthony Barbier7068f992017-10-26 15:23:08 +0100207 std::set<std::string> build_opts;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100208 std::string kernel_name;
Anthony Barbier7068f992017-10-26 15:23:08 +0100209
210 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
211 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
212 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
213 build_opts.emplace("#define COLS_A " + support::cpp11::to_string(input0->info()->dimension(0)));
214 build_opts.emplace("#define COLS_B " + support::cpp11::to_string(input1->info()->dimension(0)));
215 build_opts.emplace("#define ALPHA " + float_to_string_with_full_precision(alpha));
216
217 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
218 if(is_interleaved_transposed)
219 {
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100220 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
221 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
222
223 build_opts.emplace("#define MULT_TRANSPOSE1XW_WIDTH " + support::cpp11::to_string(mult_transpose1xW_width));
224 build_opts.emplace("#define MULT_INTERLEAVE4X4_HEIGHT " + support::cpp11::to_string(mult_interleave4x4_height));
225
Anthony Barbier7068f992017-10-26 15:23:08 +0100226 switch(input0->info()->data_type())
227 {
228 case DataType::F16:
229 build_opts.emplace("#define DATA_TYPE_FP16");
230 break;
231
232 case DataType::F32:
233 build_opts.emplace("#define DATA_TYPE_FP32");
234 break;
235
236 default:
237 ARM_COMPUTE_ERROR("Current data type is not supported");
238 break;
239 }
240
241 build_opts.emplace("#define GEMM_MM_INTERLEAVED_TRANSPOSED");
242
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100243 kernel_name = "gemm_mm_interleaved_transposed";
Anthony Barbier7068f992017-10-26 15:23:08 +0100244 }
245 else
246 {
Anthony Barbier7068f992017-10-26 15:23:08 +0100247 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor
Anthony Barbier7068f992017-10-26 15:23:08 +0100248
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100249 GPUTarget arch_target = get_arch_from_target(gpu_target);
Anthony Barbier7068f992017-10-26 15:23:08 +0100250 switch(input0->info()->data_type())
251 {
252 case DataType::F16:
Anthony Barbier7068f992017-10-26 15:23:08 +0100253 build_opts.emplace("#define DATA_TYPE_FP16");
Frank Leib9d38ee2017-12-05 10:43:33 +0800254 build_opts.emplace("#define MM_PROCESS_4X_OPTIMIZED");
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100255 build_opts.emplace("#define GEMM_MM_FLOATING_POINT");
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");
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100260
261 if(arch_target == GPUTarget::BIFROST && input0->info()->num_dimensions() != 1)
262 {
263 build_opts.emplace("#define GEMM_MM_FLOATING_POINT_BIFROST");
264 }
265 else
266 {
267 build_opts.emplace("#define GEMM_MM_FLOATING_POINT");
268 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100269 break;
270
271 default:
272 ARM_COMPUTE_ERROR("Current data type is not supported");
273 break;
274 }
275
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100276 build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_X " + support::cpp11::to_string(num_elements_processed.x()));
277 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 +0100278
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100279 kernel_name = "gemm_mm_floating_point";
Anthony Barbier7068f992017-10-26 15:23:08 +0100280 }
281
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100282 // Create kernel
283 _kernel = GCKernelLibrary::get().create_kernel(kernel_name, build_opts);
284}
285
286Status GCGEMMMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved_transposed,
287 const GEMMReshapeInfo &reshape_info, GPUTarget gpu_target)
288{
289 ARM_COMPUTE_UNUSED(alpha);
290 ElementsProcessed num_elements_processed{};
291 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
292 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
293 input1->clone().get(),
294 output->clone().get(),
295 is_interleaved_transposed,
296 reshape_info,
297 gpu_target,
298 num_elements_processed)
299 .first);
300 return Status{};
Anthony Barbier7068f992017-10-26 15:23:08 +0100301}
302
303void GCGEMMMatrixMultiplyKernel::run(const Window &window)
304{
305 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
306 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
307
308 _kernel.use();
309
310 Window slice = window.first_slice_window_2D();
311 Window slice_matrix_b = slice;
312
313 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
314 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
315
316 do
317 {
318 Window slice_b = slice;
319 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
320 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
321 if(_input1->info()->num_dimensions() < 3)
322 {
323 slice_b = slice_matrix_b;
324 }
325
326 unsigned int idx = 0;
Anthony Barbier7068f992017-10-26 15:23:08 +0100327
Joel Liangabd03cf2018-01-08 15:20:48 +0800328 add_2D_tensor_argument(idx, _input0, 1, slice);
329 add_2D_tensor_argument(idx, _input1, 2, slice_b);
330 add_2D_tensor_argument(idx, _output, 3, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100331 _kernel.update_shader_params();
332 enqueue(*this, slice);
333 }
334 while(window.slide_window_slice_2D(slice));
335}