blob: ba475f5819bfde74e539f6acc1d6318f33457783 [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/CLGEMMInterleave4x4Kernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Window.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
Georgios Pinitas358ca202017-12-07 16:47:52 +000039using namespace arm_compute::misc::shape_calculator;
40
41namespace
42{
Gian Marco36a0a462018-01-12 10:21:40 +000043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height)
Georgios Pinitas358ca202017-12-07 16:47:52 +000044{
Gian Marco36a0a462018-01-12 10:21:40 +000045 ARM_COMPUTE_RETURN_ERROR_ON(mult_interleave4x4_height < 1);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010046 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Georgios Pinitas358ca202017-12-07 16:47:52 +000047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::U8, DataType::S8,
48 DataType::QS16, DataType::U16, DataType::S16, DataType::U32, DataType::S32,
49 DataType::F16, DataType::F32);
Georgios Pinitas358ca202017-12-07 16:47:52 +000050
51 if(output->total_size() != 0)
52 {
Gian Marco36a0a462018-01-12 10:21:40 +000053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_interleaved_shape(*input, mult_interleave4x4_height));
Georgios Pinitas358ca202017-12-07 16:47:52 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
56 }
57
58 return Status{};
59}
60
Gian Marco36a0a462018-01-12 10:21:40 +000061std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, int mult_interleave4x4_height)
Georgios Pinitas358ca202017-12-07 16:47:52 +000062{
Gian Marco36a0a462018-01-12 10:21:40 +000063 constexpr unsigned int num_elems_processed_per_iteration_x = 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +000064 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
Gian Marco36a0a462018-01-12 10:21:40 +000065 const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +000066 bool window_changed = false;
67
68 // Configure kernel window
69 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
70 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
71 window_changed = window_changed || update_window_and_padding(win, input_access);
72
Gian Marco Iodice750641d2018-05-08 12:01:57 +010073 // Output auto inizialitation if not yet initialized
74 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_interleaved_shape(*input, mult_interleave4x4_height)));
Gian Marco36a0a462018-01-12 10:21:40 +000075
Gian Marco Iodice750641d2018-05-08 12:01:57 +010076 // Configure window
77 const float scale_x = 4.0f * static_cast<float>(mult_interleave4x4_height);
78 const float scale_y = 1.0f / (scale_x);
79
80 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration, 1, scale_x, scale_y);
81 window_changed = window_changed || update_window_and_padding(win, output_access);
82 output_access.set_valid_region(win, input->valid_region());
Georgios Pinitas358ca202017-12-07 16:47:52 +000083
Gian Marcoae2af742018-02-15 12:35:44 +000084 // Collapse along the Z direction
85 // This collapse needs to be here in order to tune the Z dimension of LWS
86 Window collapsed = win.collapse(win, Window::DimZ);
87
Georgios Pinitas358ca202017-12-07 16:47:52 +000088 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +000089 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +000090}
91} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
93CLGEMMInterleave4x4Kernel::CLGEMMInterleave4x4Kernel()
94 : _input(nullptr), _output(nullptr)
95{
96}
97
Gian Marco36a0a462018-01-12 10:21:40 +000098void CLGEMMInterleave4x4Kernel::configure(const ICLTensor *input, ICLTensor *output, int mult_interleave4x4_height)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100101
102 // Output auto inizialitation if not yet initialized
Gian Marco36a0a462018-01-12 10:21:40 +0000103 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_interleaved_shape(*input->info(), mult_interleave4x4_height)));
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100104
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 // Perform validate step
Gian Marco36a0a462018-01-12 10:21:40 +0000106 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_interleave4x4_height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
108 _input = input;
109 _output = output;
110
Gian Marco36a0a462018-01-12 10:21:40 +0000111 // Create build options
112 CLBuildOptions build_opts;
113 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
114 switch(input->info()->element_size())
115 {
116 case 1:
117 build_opts.add_option("-DDATA_TYPE=uchar");
118 break;
119 case 2:
120 build_opts.add_option("-DDATA_TYPE=ushort");
121 break;
122 case 4:
123 build_opts.add_option("-DDATA_TYPE=uint");
124 break;
125 default:
126 ARM_COMPUTE_ERROR("Data type not supported");
127 }
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000130 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
132 // Configure kernel window
Gian Marco36a0a462018-01-12 10:21:40 +0000133 auto win_config = validate_and_configure_window(input->info(), output->info(), mult_interleave4x4_height);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
135 ICLKernel::configure(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100136
137 // Set config_id for enabling LWS tuning
138 _config_id = "interleave4x4_";
139 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
140 _config_id += "_";
141 _config_id += support::cpp11::to_string(output->info()->dimension(0));
142 _config_id += "_";
143 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Gian Marcoae2af742018-02-15 12:35:44 +0000144 _config_id += "_";
145 _config_id += support::cpp11::to_string(output->info()->dimension(2));
146 _config_id += "_";
147 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148}
149
Gian Marco36a0a462018-01-12 10:21:40 +0000150Status CLGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000151{
Gian Marco36a0a462018-01-12 10:21:40 +0000152 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_interleave4x4_height));
153 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), mult_interleave4x4_height).first);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000154
155 return Status{};
156}
157
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158void CLGEMMInterleave4x4Kernel::run(const Window &window, cl::CommandQueue &queue)
159{
160 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
161 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
162
163 /*
164 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
165 * |a00 a01 a02 a03|
166 * |a10 a11 a12 a13|
167 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
168 * |a30 a31 a32 a33|
169 *
170 * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ]
171 */
Gian Marcoae2af742018-02-15 12:35:44 +0000172 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 do
175 {
176 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000177 add_3D_tensor_argument(idx, _input, slice);
178 add_3D_tensor_argument(idx, _output, slice);
179 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 }
Gian Marcoae2af742018-02-15 12:35:44 +0000181 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182}