blob: 7891844ef6d95f82676aea0042ee5df075d7f37c [file] [log] [blame]
Gian Marco Iodice477531c2018-08-21 17:53:38 +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/CLReorgLayerKernel.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#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
36#include <string>
37
38namespace arm_compute
39{
40namespace
41{
42Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t stride)
43{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1,
46 DataType::U8, DataType::S8, DataType::QASYMM8,
47 DataType::U16, DataType::S16,
48 DataType::U32, DataType::S32,
49 DataType::F16, DataType::F32);
50 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
51
52 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
53 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
54
55 ARM_COMPUTE_RETURN_ERROR_ON(stride <= 0);
56 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");
57 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");
58
59 // Validate output if initialized
60 if(output->total_size() != 0)
61 {
62 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input, stride));
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
65 }
66
67 return Status{};
68}
69} // namespace
70
71CLReorgLayerKernel::CLReorgLayerKernel()
72 : _input(nullptr), _output(nullptr)
73{
74}
75
76void CLReorgLayerKernel::configure(const ICLTensor *input, ICLTensor *output, int32_t stride)
77{
78 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
79 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), stride));
80
81 _input = input;
82 _output = output;
83
84 std::string kernel_name = std::string("reorg_layer_") + lower_string(string_from_data_layout(input->info()->data_layout()));
85 const size_t idx_channel = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
86
87 // Create kernel
88 CLBuildOptions build_opts;
89 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
90 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(idx_channel)));
91 build_opts.add_option("-DSTRIDE=" + support::cpp11::to_string(stride));
92 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
93
94 // Configure window
95 // auto inizialize the output tensor if not yet initialized
96 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input->info(), stride)));
97
98 Window win = calculate_max_window(*output->info(), Steps());
99
100 // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
101 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
102 ICLKernel::configure_internal(win);
103
104 _config_id = kernel_name;
105 _config_id += "_";
106 _config_id += string_from_data_type(input->info()->data_type());
107 _config_id += "_";
108 _config_id += support::cpp11::to_string(input->info()->dimension(0));
109 _config_id += "_";
110 _config_id += support::cpp11::to_string(input->info()->dimension(1));
111 _config_id += "_";
112 _config_id += support::cpp11::to_string(input->info()->dimension(2));
113 _config_id += "_";
114 _config_id += support::cpp11::to_string(stride);
115}
116
117Status CLReorgLayerKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, int32_t stride)
118{
119 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, stride));
120
121 return Status{};
122}
123
124void CLReorgLayerKernel::run(const Window &window, cl::CommandQueue &queue)
125{
126 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
127 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
128
129 Window slice = window.first_slice_window_3D();
130
131 do
132 {
133 unsigned int idx = 0;
134 add_3D_tensor_argument(idx, _input, slice);
135 add_3D_tensor_argument(idx, _output, slice);
136 enqueue(queue, *this, slice, lws_hint());
137 }
138 while(window.slide_window_slice_3D(slice));
139}
140} // namespace arm_compute