blob: 43e7b92c6a79698a953f674d6df707ee08d00ef6 [file] [log] [blame]
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2018-2020 ARM Limited.
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00003 *
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/CLGEMMReshapeRHSMatrixKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/CLValidate.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.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/Utils.h"
37#include "arm_compute/core/Window.h"
38#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000040
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010041namespace arm_compute
42{
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000043using namespace arm_compute::misc::shape_calculator;
44
45namespace
46{
47Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info)
48{
49 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 == 0);
50 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 == 0);
51 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.h0 == 0);
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3), "Only 2,3,4,8,16 are supported for n0");
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && (rhs_info.k0 != 1) && (rhs_info.k0 != 3)), "Only 1,2,3,4,8,16 are supported for k0");
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000054 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 > 16);
55 ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16);
56 ARM_COMPUTE_RETURN_ERROR_ON((rhs_info.k0 == 1) && (rhs_info.transpose));
Gian Marco Iodicea98dee22020-06-02 12:12:35 +010057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image && ((rhs_info.n0 != 4) || input->data_type() != DataType::F32), "Export to cl_image only supported with n0 = 4 and F32 data type");
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image
59 && !image2d_from_buffer_supported(CLKernelLibrary::get().get_device()), "The extension cl_khr_image2d_from_buffer is not supported on the target platform");
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image && (get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0), "Impossible to retrieve the cl_image pitch alignment");
61
62 if(rhs_info.export_to_cl_image)
63 {
64 TensorShape output_shape = compute_rhs_reshaped_shape(*input, rhs_info);
65
66 // Check the width and height of the output tensor.
67 // Since we cannot create a 3d image from a buffer, the third dimension is collapsed with the second dimension
68 size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>();
69 size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>();
70
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_shape[0] > max_image_w * 4, "Not supported width for cl_image");
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_shape[1] * output_shape[2] > max_image_h, "Not supported height for cl_image");
73 }
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000074
75 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000076 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000077
78 if(output->total_size() != 0)
79 {
80 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_rhs_reshaped_shape(*input, rhs_info));
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000083 }
84
85 return Status{};
86}
87
88std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info)
89{
90 const unsigned int num_elems_processed_per_iteration_x = rhs_info.n0;
91 const unsigned int num_elems_processed_per_iteration_y = rhs_info.k0;
92 bool window_changed = false;
93
94 // Output auto initialization if not yet initialized
95 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*input, rhs_info)));
96
97 // Configure window
98 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
99
100 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
101 AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
102
103 window_changed = update_window_and_padding(win, input_access);
104 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
105
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100106 if(rhs_info.export_to_cl_image)
107 {
108 constexpr unsigned int num_floats_per_pixel = 4;
109
110 const unsigned int stride_y_in_elements = output->strides_in_bytes()[1] / output->element_size();
111 const unsigned int pixel_aligment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device());
112 const unsigned int row_pitch_alignment = pixel_aligment * num_floats_per_pixel;
113 const unsigned int round_up_width = ((stride_y_in_elements + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment;
114 const unsigned int padding = round_up_width - stride_y_in_elements;
115
116 output->extend_padding(PaddingSize(0, padding, 0, 0));
117 }
118
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000119 // Collapse along the Z direction
120 // This collapse needs to be here in order to tune the Z dimension of LWS
121 Window collapsed = win.collapse(win, Window::DimZ);
122
123 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
124 return std::make_pair(err, collapsed);
125}
126} // namespace
127
128CLGEMMReshapeRHSMatrixKernel::CLGEMMReshapeRHSMatrixKernel()
129 : _input(nullptr), _output(nullptr)
130{
131}
132
133void CLGEMMReshapeRHSMatrixKernel::configure(const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info)
134{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100135 configure(CLKernelLibrary::get().get_compile_context(), input, output, rhs_info);
136}
137
Manuel Bottini679fc962020-04-21 16:08:53 +0100138void CLGEMMReshapeRHSMatrixKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100139{
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000140 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
141
142 // Perform validate step
143 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), rhs_info));
144
145 _input = input;
146 _output = output;
147
148 // Create build options
149 CLBuildOptions build_opts;
150 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
151 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
152 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
153 build_opts.add_option_if(rhs_info.transpose, "-DTRANSPOSE");
154 build_opts.add_option_if(rhs_info.interleave, "-DINTERLEAVE");
155 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100156 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000157
158 std::string kernel_name("gemm_reshape_rhs_matrix_");
159 kernel_name += rhs_info.transpose ? "t" : "nt";
160
161 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100162 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000163
164 // Configure kernel window
165 auto win_config = validate_and_configure_window(input->info(), output->info(), rhs_info);
166 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
167 ICLKernel::configure_internal(win_config.second);
168}
169
170Status CLGEMMReshapeRHSMatrixKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info)
171{
172 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, rhs_info));
173 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), rhs_info).first);
174
175 return Status{};
176}
177
178void CLGEMMReshapeRHSMatrixKernel::run(const Window &window, cl::CommandQueue &queue)
179{
180 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
181 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
182
183 Window slice = window.first_slice_window_3D();
184
185 do
186 {
187 unsigned int idx = 0;
188 add_3D_tensor_argument(idx, _input, slice);
189 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100190 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000191 }
192 while(window.slide_window_slice_3D(slice));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100193}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000194} // namespace arm_compute