blob: c76d8395516f15f7de86b29ec61e1ec37652f3cb [file] [log] [blame]
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +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/CLPriorBoxLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
36#include "support/ToolchainSupport.h"
37
38using namespace arm_compute::misc::shape_calculator;
39
40namespace arm_compute
41{
42namespace
43{
44Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, input2);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2);
50
51 // Check variances
52 const int var_size = info.variances().size();
53 if(var_size > 1)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values");
56 for(int i = 0; i < var_size; ++i)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0");
59 }
60 }
61 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0");
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0");
63
64 if(!info.max_sizes().empty())
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), "Max and min sizes dimensions should match");
67 }
68
69 for(unsigned int i = 0; i < info.max_sizes().size(); ++i)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], "Max size should be greater than min size");
72 }
73
74 if(output != nullptr && output->total_size() != 0)
75 {
Michalis Spyrou3112e332018-11-21 15:44:55 +000076 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != 2);
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +010077 }
78
79 return Status{};
80}
81
82std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, const PriorBoxLayerInfo &info, int num_priors)
83{
84 ARM_COMPUTE_UNUSED(input2);
85 // Output tensor auto initialization if not yet initialized
86 TensorShape output_shape = compute_prior_box_shape(*input1, info);
87 auto_init_if_empty(*output, output_shape, 1, input1->data_type());
88
Michalis Spyrou3112e332018-11-21 15:44:55 +000089 const unsigned int num_elems_processed_per_iteration = 4 * num_priors;
90 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
91 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
92 bool window_changed = update_window_and_padding(win, output_access);
93 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +010094 return std::make_pair(err, win);
95}
96} // namespace
97
98CLPriorBoxLayerKernel::CLPriorBoxLayerKernel()
99 : _input1(nullptr), _input2(nullptr), _output(nullptr), _info(), _num_priors(), _min(), _max(), _aspect_ratios()
100{
101}
102
103void CLPriorBoxLayerKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const PriorBoxLayerInfo &info, cl::Buffer *min, cl::Buffer *max, cl::Buffer *aspect_ratios)
104{
105 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
106
107 _input1 = input1;
108 _input2 = input2;
109 _output = output;
110 _info = info;
111 _min = min;
112 _max = max;
113 _aspect_ratios = aspect_ratios;
114
115 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info));
116
117 // Calculate number of aspect ratios
118 _num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
119
120 const DataLayout data_layout = input1->info()->data_layout();
121
122 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
123 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
124
125 const int layer_width = input1->info()->dimension(width_idx);
126 const int layer_height = input1->info()->dimension(height_idx);
127
128 int img_width = info.img_size().x;
129 int img_height = info.img_size().y;
130 if(img_width == 0 || img_height == 0)
131 {
132 img_width = input2->info()->dimension(width_idx);
133 img_height = input2->info()->dimension(height_idx);
134 }
135
136 float step_x = info.steps()[0];
137 float step_y = info.steps()[0];
138 if(step_x == 0.f || step_y == 0.f)
139 {
140 step_x = static_cast<float>(img_width) / layer_width;
141 step_y = static_cast<float>(img_height) / layer_height;
142 }
143
144 // Set build options
145 CLBuildOptions build_opts;
146 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input1->info()->data_type()));
147 build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(img_width));
148 build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(img_height));
149 build_opts.add_option("-DLAYER_WIDTH=" + support::cpp11::to_string(layer_width));
150 build_opts.add_option("-DLAYER_HEIGHT=" + support::cpp11::to_string(layer_height));
151 build_opts.add_option("-DSTEP_X=" + support::cpp11::to_string(step_x));
152 build_opts.add_option("-DSTEP_Y=" + support::cpp11::to_string(step_y));
153 build_opts.add_option("-DNUM_PRIORS=" + support::cpp11::to_string(_num_priors));
154 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(info.offset()));
155 build_opts.add_option_if(info.clip(), "-DIN_PLACE");
156
157 if(info.variances().size() > 1)
158 {
159 for(unsigned int i = 0; i < info.variances().size(); ++i)
160 {
161 build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(info.variances().at(i)));
162 }
163 }
164 else
165 {
166 for(unsigned int i = 0; i < 4; ++i)
167 {
168 build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + support::cpp11::to_string(info.variances().at(0)));
169 }
170 }
171
Michalis Spyrou3112e332018-11-21 15:44:55 +0000172 unsigned int idx = num_arguments_per_2D_tensor();
173 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("prior_box_layer_nchw", build_opts.options()));
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100174
175 _kernel.setArg(idx++, *_min);
176 _kernel.setArg(idx++, *_max);
177 _kernel.setArg(idx++, *_aspect_ratios);
178 _kernel.setArg<unsigned int>(idx++, info.min_sizes().size());
179 _kernel.setArg<unsigned int>(idx++, info.max_sizes().size());
180 _kernel.setArg<unsigned int>(idx++, info.aspect_ratios().size());
181
182 // Configure kernel window
183 auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info, _num_priors);
184
185 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
186 ICLKernel::configure_internal(win_config.second);
187}
188
189Status CLPriorBoxLayerKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info)
190{
191 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
192 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info));
193 const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
194 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get(), info, num_priors)
195 .first);
196
197 return Status{};
198}
199
200void CLPriorBoxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
201{
202 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
203 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
204
205 queue.enqueueWriteBuffer(*_min, CL_TRUE, 0, _info.min_sizes().size() * sizeof(float), _info.min_sizes().data());
206 queue.enqueueWriteBuffer(*_aspect_ratios, CL_TRUE, 0, _info.aspect_ratios().size() * sizeof(float), _info.aspect_ratios().data());
207 if(!_info.max_sizes().empty())
208 {
209 queue.enqueueWriteBuffer(*_max, CL_TRUE, 0, _info.max_sizes().size() * sizeof(float), _info.max_sizes().data());
210 }
211
Michalis Spyrou3112e332018-11-21 15:44:55 +0000212 Window slice = window.first_slice_window_2D();
213 slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2));
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100214
Michalis Spyrou3112e332018-11-21 15:44:55 +0000215 unsigned int idx = 0;
216 add_2D_tensor_argument(idx, _output, slice);
217 enqueue(queue, *this, slice);
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100218}
219} // namespace arm_compute