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