blob: c774e98e4466cde5a58324e3da8a6d6af5cdbdc1 [file] [log] [blame]
Michele Di Giorgio72175632018-05-01 16:52:00 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2018-2021 Arm Limited.
Michele Di Giorgio72175632018-05-01 16:52:00 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLChannelShuffleLayerKernel.h"
Michele Di Giorgio72175632018-05-01 16:52:00 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Michele Di Giorgio72175632018-05-01 16:52:00 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
Michele Di Giorgio72175632018-05-01 16:52:00 +010035
36namespace arm_compute
37{
38namespace
39{
40Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
41{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010042 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000043 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michele Di Giorgio72175632018-05-01 16:52:00 +010044 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient");
45
46 const unsigned int channels = input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL));
47
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups == channels, "Channel shuffling with same number of groups as number of channels would be inefficient");
49 // There cannot be more groups than channels
50 ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, "The number of channels must be a multiple of the number of groups");
52
53 // Checks performed when output is configured
54 if(output->total_size() != 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000057 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgio72175632018-05-01 16:52:00 +010058 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 }
60
61 return Status{};
62}
63
64std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
65{
66 // Output tensor auto initialization if not yet initialized
67 auto_init_if_empty(*output, *input->clone());
68
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +010069 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
70 const unsigned int num_elems_processed_per_iteration_x = is_nhwc ? 4 : max_cl_vector_width / input->element_size();
71 constexpr unsigned int num_elems_processed_per_iteration_y = 2;
Michele Di Giorgio72175632018-05-01 16:52:00 +010072
73 // Configure kernel window
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +010074 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
75 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
76 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
Michele Di Giorgio72175632018-05-01 16:52:00 +010077
78 const bool window_changed = update_window_and_padding(win, input_access, output_access);
Michele Di Giorgio72175632018-05-01 16:52:00 +010079
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +010080 Window win_collapsed = win.collapse(win, Window::DimZ);
81
Michele Di Giorgio72175632018-05-01 16:52:00 +010082 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +010083 return std::make_pair(err, win_collapsed);
Michele Di Giorgio72175632018-05-01 16:52:00 +010084}
85} // namespace
86
87CLChannelShuffleLayerKernel::CLChannelShuffleLayerKernel()
88 : _input(nullptr), _output(nullptr)
89{
90}
91
92void CLChannelShuffleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int num_groups)
93{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010094 configure(CLKernelLibrary::get().get_compile_context(), input, output, num_groups);
95}
96
Manuel Bottini256c0b92020-04-21 13:29:30 +010097void CLChannelShuffleLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, unsigned int num_groups)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010098{
Michele Di Giorgio72175632018-05-01 16:52:00 +010099 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
100
101 _input = input;
102 _output = output;
103
104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups));
105
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +0100106 const DataLayout data_layout = input->info()->data_layout();
107 const bool is_nhwc = data_layout == DataLayout::NHWC;
108 const unsigned int channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
109 const unsigned int vec_size = is_nhwc ? 4 : max_cl_vector_width / input->info()->element_size();
Michele Di Giorgio72175632018-05-01 16:52:00 +0100110
111 // Set kernel build options
112 CLBuildOptions build_opts;
113 build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
114 build_opts.add_option("-DK=" + support::cpp11::to_string(channels / num_groups));
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +0100115 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
116 build_opts.add_option("-DSRC_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
117 build_opts.add_option("-DLAST_ACCESSED=" + support::cpp11::to_string(std::max(static_cast<int>(channels - vec_size), 0)));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100118 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Michele Di Giorgio72175632018-05-01 16:52:00 +0100119
120 // Create kernel
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +0100121 std::string kernel_name = "channel_shuffle_" + lower_string(string_from_data_layout(data_layout));
Giorgio Arena6e9d0e02020-01-03 15:02:04 +0000122
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100123 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Michele Di Giorgio72175632018-05-01 16:52:00 +0100124
125 // Configure kernel window
126 auto win_config = validate_and_configure_window(input->info(), output->info());
127 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100128 ICLKernel::configure_internal(win_config.second);
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +0100129
130 // Set config_id for enabling LWS tuning
131 _config_id = kernel_name;
132 _config_id += "_";
133 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
134 _config_id += "_";
135 _config_id += support::cpp11::to_string(num_groups);
136 _config_id += "_";
137 _config_id += support::cpp11::to_string(input->info()->dimension(0));
138 _config_id += "_";
139 _config_id += support::cpp11::to_string(input->info()->dimension(1));
140 _config_id += "_";
141 _config_id += support::cpp11::to_string(input->info()->dimension(2));
142 _config_id += "_";
143 _config_id += support::cpp11::to_string(output->info()->dimension(0));
144 _config_id += "_";
145 _config_id += support::cpp11::to_string(output->info()->dimension(1));
146 _config_id += "_";
147 _config_id += support::cpp11::to_string(output->info()->dimension(2));
Michele Di Giorgio72175632018-05-01 16:52:00 +0100148}
149
150Status CLChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
151{
152 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups));
153 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
154
155 return Status{};
156}
157
158void CLChannelShuffleLayerKernel::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
Gian Marco Iodice8bab0ee2018-09-13 11:51:56 +0100163 unsigned int idx = 0;
164 add_4D_tensor_argument(idx, _input, window);
165 add_4D_tensor_argument(idx, _output, window);
166 enqueue(queue, *this, window, lws_hint());
Michele Di Giorgio72175632018-05-01 16:52:00 +0100167}
168} // namespace arm_compute