blob: a667119c136db8c89d834900340a9ad520bb17fa [file] [log] [blame]
Michele Di Giorgio72175632018-05-01 16:52:00 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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/CLChannelShuffleLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35namespace arm_compute
36{
37namespace
38{
39Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
40{
41 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8,
42 DataType::U16, DataType::S16, DataType::QS16,
43 DataType::U32, DataType::S32,
44 DataType::F16, DataType::F32);
45 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient");
46
47 const unsigned int channels = input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL));
48
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups == channels, "Channel shuffling with same number of groups as number of channels would be inefficient");
50 // There cannot be more groups than channels
51 ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels);
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, "The number of channels must be a multiple of the number of groups");
53
54 // Checks performed when output is configured
55 if(output->total_size() != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
58 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
69 const unsigned int num_elems_processed_per_iteration = max_cl_vector_width / input->element_size();
70
71 // Configure kernel window
72 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration, num_elems_processed_per_iteration));
73 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration);
74 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration);
75
76 const bool window_changed = update_window_and_padding(win, input_access, output_access);
77 output_access.set_valid_region(win, input->valid_region());
78
79 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
80 return std::make_pair(err, win);
81}
82} // namespace
83
84CLChannelShuffleLayerKernel::CLChannelShuffleLayerKernel()
85 : _input(nullptr), _output(nullptr)
86{
87}
88
89void CLChannelShuffleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int num_groups)
90{
91 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
92
93 _input = input;
94 _output = output;
95
96 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups));
97
98 const unsigned int channels = input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL));
99 const unsigned int block_size = max_cl_vector_width / input->info()->element_size();
100
101 // Set kernel build options
102 CLBuildOptions build_opts;
103 build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
104 build_opts.add_option("-DK=" + support::cpp11::to_string(channels / num_groups));
105 build_opts.add_option("-DBLOCK_SIZE=" + support::cpp11::to_string(block_size));
106 switch(input->info()->element_size())
107 {
108 case 1:
109 build_opts.add_option("-DDATA_TYPE=uchar");
110 break;
111 case 2:
112 build_opts.add_option("-DDATA_TYPE=ushort");
113 break;
114 case 4:
115 build_opts.add_option("-DDATA_TYPE=uint");
116 break;
117 default:
118 ARM_COMPUTE_ERROR("Data type not supported");
119 }
120
121 // Create kernel
122 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("channel_shuffle_nchw", build_opts.options()));
123
124 // Configure kernel window
125 auto win_config = validate_and_configure_window(input->info(), output->info());
126 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
127 ICLKernel::configure(win_config.second);
128}
129
130Status CLChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
131{
132 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups));
133 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
134
135 return Status{};
136}
137
138void CLChannelShuffleLayerKernel::run(const Window &window, cl::CommandQueue &queue)
139{
140 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
142
143 Window slice = window.first_slice_window_3D();
144 do
145 {
146 unsigned int idx = 0;
147 add_3D_tensor_argument(idx, _input, slice);
148 add_3D_tensor_argument(idx, _output, slice);
149 enqueue(queue, *this, slice);
150 }
151 while(window.slide_window_slice_3D(slice));
152}
153} // namespace arm_compute