blob: d576c30f809ac5e3a3286664878ea1a157fbfb63 [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);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, output);
101 }
102 }
103
104 return Status{};
105}
106
107inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output,
108 bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info,
109 GPUTarget gpu_target, ElementsProcessed &num_elements_processed)
110{
111 ARM_COMPUTE_UNUSED(gpu_target);
112
113 // Output tensor auto inizialitation if not yet initialized
114 TensorShape tensor_shape{ input0->tensor_shape() };
115 tensor_shape.set(0, is_interleaved_transposed ? reshape_info.n() : input1->dimension(0));
116 tensor_shape.set(1, is_interleaved_transposed ? reshape_info.m() : input0->dimension(1));
117
118 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(tensor_shape));
119
120 bool window_changed = false;
121 Window win{};
122
123 const DataType data_type = input0->data_type();
124 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
125 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
126
127 if(is_interleaved_transposed)
128 {
129 // Configure window kernel
130 num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(data_type);
131 num_elems_processed_per_iteration_y = 4;
132
133 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
134
135 AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
136 AccessWindowTranspose input1_access(input1, 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f);
137 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
138
139 update_window_and_padding(win, input0_access, input1_access, output_access);
140
141 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
142 }
143 else // The input tensors have not been reshaped
144 {
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100145 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor.
146 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100147
148 switch(data_type)
149 {
150 case DataType::F16:
151 num_elems_processed_per_iteration_x = 4;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100152 break;
153
154 case DataType::F32:
155 num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(data_type);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100156 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
211 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
212 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
213 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
214 build_opts.emplace("#define COLS_A " + support::cpp11::to_string(input0->info()->dimension(0)));
215 build_opts.emplace("#define COLS_B " + support::cpp11::to_string(input1->info()->dimension(0)));
216 build_opts.emplace("#define ALPHA " + float_to_string_with_full_precision(alpha));
217
218 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
219 if(is_interleaved_transposed)
220 {
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100221 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
222 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
223
224 build_opts.emplace("#define MULT_TRANSPOSE1XW_WIDTH " + support::cpp11::to_string(mult_transpose1xW_width));
225 build_opts.emplace("#define MULT_INTERLEAVE4X4_HEIGHT " + support::cpp11::to_string(mult_interleave4x4_height));
226
Anthony Barbier7068f992017-10-26 15:23:08 +0100227 switch(input0->info()->data_type())
228 {
229 case DataType::F16:
230 build_opts.emplace("#define DATA_TYPE_FP16");
231 break;
232
233 case DataType::F32:
234 build_opts.emplace("#define DATA_TYPE_FP32");
235 break;
236
237 default:
238 ARM_COMPUTE_ERROR("Current data type is not supported");
239 break;
240 }
241
242 build_opts.emplace("#define GEMM_MM_INTERLEAVED_TRANSPOSED");
243
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100244 kernel_name = "gemm_mm_interleaved_transposed";
Anthony Barbier7068f992017-10-26 15:23:08 +0100245 }
246 else
247 {
Anthony Barbier7068f992017-10-26 15:23:08 +0100248 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor
Anthony Barbier7068f992017-10-26 15:23:08 +0100249
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100250 GPUTarget arch_target = get_arch_from_target(gpu_target);
Anthony Barbier7068f992017-10-26 15:23:08 +0100251 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");
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100256 build_opts.emplace("#define GEMM_MM_FLOATING_POINT");
Anthony Barbier7068f992017-10-26 15:23:08 +0100257 break;
258
259 case DataType::F32:
Anthony Barbier7068f992017-10-26 15:23:08 +0100260 build_opts.emplace("#define DATA_TYPE_FP32");
Michele Di Giorgiof6f08da2018-04-26 10:24:30 +0100261
262 if(arch_target == GPUTarget::BIFROST && input0->info()->num_dimensions() != 1)
263 {
264 build_opts.emplace("#define GEMM_MM_FLOATING_POINT_BIFROST");
265 }
266 else
267 {
268 build_opts.emplace("#define GEMM_MM_FLOATING_POINT");
269 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100270 break;
271
272 default:
273 ARM_COMPUTE_ERROR("Current data type is not supported");
274 break;
275 }
276
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100277 build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_X " + support::cpp11::to_string(num_elements_processed.x()));
278 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 +0100279
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100280 kernel_name = "gemm_mm_floating_point";
Anthony Barbier7068f992017-10-26 15:23:08 +0100281 }
282
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100283 // Create kernel
284 _kernel = GCKernelLibrary::get().create_kernel(kernel_name, build_opts);
285}
286
287Status GCGEMMMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved_transposed,
288 const GEMMReshapeInfo &reshape_info, GPUTarget gpu_target)
289{
290 ARM_COMPUTE_UNUSED(alpha);
291 ElementsProcessed num_elements_processed{};
292 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
293 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
294 input1->clone().get(),
295 output->clone().get(),
296 is_interleaved_transposed,
297 reshape_info,
298 gpu_target,
299 num_elements_processed)
300 .first);
301 return Status{};
Anthony Barbier7068f992017-10-26 15:23:08 +0100302}
303
304void GCGEMMMatrixMultiplyKernel::run(const Window &window)
305{
306 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
307 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
308
309 _kernel.use();
310
311 Window slice = window.first_slice_window_2D();
312 Window slice_matrix_b = slice;
313
314 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
315 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
316
317 do
318 {
319 Window slice_b = slice;
320 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
321 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
322 if(_input1->info()->num_dimensions() < 3)
323 {
324 slice_b = slice_matrix_b;
325 }
326
327 unsigned int idx = 0;
Anthony Barbier7068f992017-10-26 15:23:08 +0100328
Joel Liangabd03cf2018-01-08 15:20:48 +0800329 add_2D_tensor_argument(idx, _input0, 1, slice);
330 add_2D_tensor_argument(idx, _input1, 2, slice_b);
331 add_2D_tensor_argument(idx, _output, 3, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100332 _kernel.update_shader_params();
333 enqueue(*this, slice);
334 }
335 while(window.slide_window_slice_2D(slice));
336}