blob: 132d5d1cd07e4aa4c76dc358723205f458d97d80 [file] [log] [blame]
Giorgio Arena73023022018-09-04 14:55:55 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Giorgio Arena73023022018-09-04 14:55:55 +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 */
24#include "arm_compute/core/CL/kernels/CLYOLOLayerKernel.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/IAccessWindow.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Window.h"
35
36#include "arm_compute/core/CL/CLHelpers.h"
37#include "arm_compute/core/Types.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Giorgio Arena73023022018-09-04 14:55:55 +010039
40namespace arm_compute
41{
42namespace
43{
44Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
45{
Giorgio Arena73023022018-09-04 14:55:55 +010046 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Michalis Spyrou80943252019-01-10 17:19:50 +000049 ARM_COMPUTE_RETURN_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC);
Giorgio Arena73023022018-09-04 14:55:55 +010050
51 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
52 ARM_COMPUTE_RETURN_ERROR_ON(num_classes <= 0);
53 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) % (num_classes + 5)) != 0);
54
55 // Checks performed when output is configured
56 if((output != nullptr) && (output->total_size() != 0))
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 }
61
62 return Status{};
63}
64
65std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
66{
67 if(output != nullptr)
68 {
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70
71 // Output auto inizialitation if not yet initialized
72 auto_init_if_empty(*output, *input);
73 }
74
75 const bool is_nchw = input->data_layout() == DataLayout::NCHW;
76 const unsigned int num_elems_processed_per_iteration = is_nchw ? 16 / input->element_size() : 1;
77
78 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
79 bool window_changed = false;
80
81 if(output != nullptr)
82 {
83 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
84 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
85 window_changed = update_window_and_padding(win, input_access, output_access);
86 output_access.set_valid_region(win, input->valid_region());
87 }
88 else
89 {
90 window_changed = update_window_and_padding(win, AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
91 }
92
93 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
94 return std::make_pair(err, win);
95}
96} // namespace
97
98CLYOLOLayerKernel::CLYOLOLayerKernel()
99 : _input(nullptr), _output(nullptr), _run_in_place(false)
100{
101}
102
103void CLYOLOLayerKernel::configure(ICLTensor *input, ICLTensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
104{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100105 configure(CLKernelLibrary::get().get_compile_context(), input, output, act_info, num_classes);
106}
107
Manuel Bottini2803f702020-04-21 16:20:03 +0100108void CLYOLOLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100109{
Giorgio Arena73023022018-09-04 14:55:55 +0100110 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
111
112 _run_in_place = (output == nullptr) || (output == input);
113
114 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes));
115
116 const bool is_nchw = input->info()->data_layout() == DataLayout::NCHW;
117 const unsigned int num_elems_processed_per_iteration = is_nchw ? 16 / input->info()->element_size() : 1;
118 const DataType dt = input->info()->data_type();
119 float a_const = act_info.a();
120 float b_const = act_info.b();
121
122 // Set build options
123 CLBuildOptions build_opts;
Usama Arif6a98a6e2019-05-10 17:07:27 +0100124 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arena73023022018-09-04 14:55:55 +0100125 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Giorgio Arena73023022018-09-04 14:55:55 +0100126 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
127 build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(a_const));
128 build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(b_const));
129 build_opts.add_option("-DNUM_CLASSES=" + support::cpp11::to_string(num_classes));
130 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
131
132 // Create kernel
133 std::string kernel_name = std::string("yolo_layer_") + lower_string(string_from_data_layout(input->info()->data_layout()));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100134 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Giorgio Arena73023022018-09-04 14:55:55 +0100135
136 // Make sure _kernel is initialized before calling the parent's configure
137 _input = input;
138 _output = output;
139
140 // Configure kernel window
141 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info());
142 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
143 ICLKernel::configure_internal(win_config.second);
144
145 // Set config_id for enabling LWS tuning
146 _config_id = "yolo_layer_";
147 _config_id += lower_string(string_from_data_type(dt));
148 _config_id += "_";
149 _config_id += support::cpp11::to_string(input->info()->dimension(0));
150 _config_id += "_";
151 _config_id += support::cpp11::to_string(input->info()->dimension(1));
152 _config_id += "_";
153 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
154}
155
156Status CLYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
157{
158 const bool run_in_place = (output == nullptr) || (output == input);
159 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes));
160 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first);
161
162 return Status{};
163}
164
165void CLYOLOLayerKernel::run(const Window &window, cl::CommandQueue &queue)
166{
167 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
168 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
169
170 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
171 Window slice = collapsed.first_slice_window_3D();
172
173 do
174 {
175 unsigned int idx = 0;
176 add_3D_tensor_argument(idx, _input, slice);
177 if(!_run_in_place)
178 {
179 add_3D_tensor_argument(idx, _output, slice);
180 }
181 enqueue(queue, *this, slice, lws_hint());
182 }
183 while(collapsed.slide_window_slice_3D(slice));
184}
Usama Arif6a98a6e2019-05-10 17:07:27 +0100185} // namespace arm_compute