blob: 01853450eea9810ceee4585f8fe91ef67a1dd58f [file] [log] [blame]
Gian Marco Iodice477531c2018-08-21 17:53:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice477531c2018-08-21 17:53:38 +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/CLReorgLayerKernel.h"
Gian Marco Iodice477531c2018-08-21 17:53:38 +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"
Gian Marco Iodice477531c2018-08-21 17:53:38 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Validate.h"
Gian Marco Iodice477531c2018-08-21 17:53:38 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Gian Marco Iodice477531c2018-08-21 17:53:38 +010036
37#include <string>
38
39namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t stride)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco Iodice477531c2018-08-21 17:53:38 +010047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
48
49 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
50 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
51
52 ARM_COMPUTE_RETURN_ERROR_ON(stride <= 0);
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_width] % stride) != 0, "The width of the input tensor must be a multiple of stride");
54 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_height] % stride) != 0, "The height of the input tensor must be a multiple of stride");
55
56 // Validate output if initialized
57 if(output->total_size() != 0)
58 {
59 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input, stride));
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 }
63
64 return Status{};
65}
66} // namespace
67
68CLReorgLayerKernel::CLReorgLayerKernel()
69 : _input(nullptr), _output(nullptr)
70{
71}
72
73void CLReorgLayerKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t stride)
74{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010075 configure(CLKernelLibrary::get().get_compile_context(), input, output, stride);
76}
77
Manuel Bottini2803f702020-04-21 16:20:03 +010078void CLReorgLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, int32_t stride)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079{
Gian Marco Iodice477531c2018-08-21 17:53:38 +010080 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), stride));
Manuel Bottinib6869dd2020-12-16 15:34:25 +000082 auto padding_info = get_padding_info({ input, output });
Gian Marco Iodice477531c2018-08-21 17:53:38 +010083
84 _input = input;
85 _output = output;
86
87 std::string kernel_name = std::string("reorg_layer_") + lower_string(string_from_data_layout(input->info()->data_layout()));
88 const size_t idx_channel = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
89
90 // Create kernel
91 CLBuildOptions build_opts;
92 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
93 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(idx_channel)));
94 build_opts.add_option("-DSTRIDE=" + support::cpp11::to_string(stride));
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice477531c2018-08-21 17:53:38 +010096
97 // Configure window
98 // auto inizialize the output tensor if not yet initialized
99 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input->info(), stride)));
100
101 Window win = calculate_max_window(*output->info(), Steps());
102
103 // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
104 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
105 ICLKernel::configure_internal(win);
106
107 _config_id = kernel_name;
108 _config_id += "_";
109 _config_id += string_from_data_type(input->info()->data_type());
110 _config_id += "_";
111 _config_id += support::cpp11::to_string(input->info()->dimension(0));
112 _config_id += "_";
113 _config_id += support::cpp11::to_string(input->info()->dimension(1));
114 _config_id += "_";
115 _config_id += support::cpp11::to_string(input->info()->dimension(2));
116 _config_id += "_";
117 _config_id += support::cpp11::to_string(stride);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000118 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice477531c2018-08-21 17:53:38 +0100119}
120
121Status CLReorgLayerKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, int32_t stride)
122{
123 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, stride));
124
125 return Status{};
126}
127
128void CLReorgLayerKernel::run(const Window &window, cl::CommandQueue &queue)
129{
130 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
131 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
132
133 Window slice = window.first_slice_window_3D();
134
135 do
136 {
137 unsigned int idx = 0;
138 add_3D_tensor_argument(idx, _input, slice);
139 add_3D_tensor_argument(idx, _output, slice);
140 enqueue(queue, *this, slice, lws_hint());
141 }
142 while(window.slide_window_slice_3D(slice));
143}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000144} // namespace arm_compute