1 /*
2 * Copyright (c) 2018-2020 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 "src/core/CL/kernels/CLYOLOLayerKernel.h"
25
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLHelpers.h"
28 #include "arm_compute/core/CL/CLKernelLibrary.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/Types.h"
34 #include "arm_compute/core/Utils.h"
35 #include "arm_compute/core/Window.h"
36 #include "src/core/CL/CLValidate.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
39
40 #include "support/StringSupport.h"
41
42 namespace arm_compute
43 {
44 namespace
45 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & act_info,int32_t num_classes)46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
47 {
48 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);
51 ARM_COMPUTE_RETURN_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC);
52
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
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)67 std::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
CLYOLOLayerKernel()100 CLYOLOLayerKernel::CLYOLOLayerKernel()
101 : _input(nullptr), _output(nullptr), _run_in_place(false)
102 {
103 }
104
configure(ICLTensor * input,ICLTensor * output,const ActivationLayerInfo & act_info,int32_t num_classes)105 void CLYOLOLayerKernel::configure(ICLTensor *input, ICLTensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
106 {
107 configure(CLKernelLibrary::get().get_compile_context(), input, output, act_info, num_classes);
108 }
109
configure(const CLCompileContext & compile_context,ICLTensor * input,ICLTensor * output,const ActivationLayerInfo & act_info,int32_t num_classes)110 void CLYOLOLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
111 {
112 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;
126 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
127 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
128 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()));
136 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
137
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
validate(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & act_info,int32_t num_classes)158 Status 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
run(const Window & window,cl::CommandQueue & queue)167 void 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 }
187 } // namespace arm_compute
188