blob: 09a4a11b66e9dd312f07c49dec4f65cb48f39af7 [file] [log] [blame]
Michalis Spyroua6825a42018-09-13 12:24:03 +01001/*
Georgios Pinitas8f5802f2019-02-22 11:08:32 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyroua6825a42018-09-13 12:24:03 +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/NEON/kernels/NEYOLOLayerKernel.h"
25
26#include "arm_compute/core/CPP/Validate.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/NEON/NEAsymm.h"
30#include "arm_compute/core/NEON/NEFixedPoint.h"
31#include "arm_compute/core/NEON/NEMath.h"
32#include "arm_compute/core/NEON/kernels/detail/NEActivationFunctionDetail.h"
33#include "arm_compute/core/QAsymm8.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
38
39#include <arm_neon.h>
40
41using namespace arm_compute;
42namespace
43{
44Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
45{
46 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000047 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michalis Spyroua6825a42018-09-13 12:24:03 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
50 ARM_COMPUTE_RETURN_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC);
51
52 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
53 ARM_COMPUTE_RETURN_ERROR_ON(num_classes <= 0);
54 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) % (num_classes + 5)) != 0);
55
56 // Checks performed when output is configured
57 if((output != nullptr) && (output->total_size() != 0))
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
61 }
62
63 return Status{};
64}
65
66std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
67{
68 if(output != nullptr)
69 {
70 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
71
72 // Output auto inizialitation if not yet initialized
73 auto_init_if_empty(*output, *input);
74 }
75
76 const bool is_nchw = input->data_layout() == DataLayout::NCHW;
77 const unsigned int num_elems_processed_per_iteration = is_nchw ? 16 / input->element_size() : 1;
78
79 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
80 bool window_changed = false;
81
82 if(output != nullptr)
83 {
84 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
85 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
86 window_changed = update_window_and_padding(win, input_access, output_access);
87 output_access.set_valid_region(win, input->valid_region());
88 }
89 else
90 {
91 window_changed = update_window_and_padding(win, AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
92 }
93
94 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
95 return std::make_pair(err, win);
96}
97} // namespace
98
99NEYOLOLayerKernel::NEYOLOLayerKernel()
100 : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes()
101{
102}
103
104void NEYOLOLayerKernel::yolo_layer_fp32_nchw(const Window &window)
105{
106 Iterator input(_input, window);
107 Iterator output(_output, window);
108
109 execute_window_loop(window, [&](const Coordinates & id)
110 {
111 float32x4_t res = vld1q_f32(reinterpret_cast<float *>(input.ptr()));
112
113 const int box_ch_id = id.z() % (_num_classes + 5);
114 const bool activate = box_ch_id != 2 && box_ch_id != 3;
115
116 // Perform activation
117 if(activate)
118 {
119 auto activation = ::detail::logistic<float, 4>(_act_info);
120 activation(res);
121 }
122
123 // Store results
124 vst1q_f32(reinterpret_cast<float *>(output.ptr()), res);
125 },
126 input, output);
127}
128
129void NEYOLOLayerKernel::yolo_layer_fp32_nhwc(const Window &window)
130{
131 Iterator input(_input, window);
132 Iterator output(_output, window);
133
134 execute_window_loop(window, [&](const Coordinates & id)
135 {
136 float res = *(reinterpret_cast<float *>(input.ptr()));
137
138 const int box_ch_id = id.x() % (_num_classes + 5);
139 const bool activate = box_ch_id != 2 && box_ch_id != 3;
140
141 // Perform activation
142 if(activate)
143 {
144 res = 1.f / (1.f + std::exp(-res));
145 }
146
147 // Store result
148 *(reinterpret_cast<float *>(output.ptr())) = res;
149 },
150 input, output);
151}
152
153#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
154void NEYOLOLayerKernel::yolo_layer_fp16_nchw(const Window &window)
155{
156 Iterator input(_input, window);
157 Iterator output(_output, window);
158
159 execute_window_loop(window, [&](const Coordinates & id)
160 {
161 float16x8_t res = vld1q_f16(reinterpret_cast<float16_t *>(input.ptr()));
162
163 const int box_ch_id = id.z() % (_num_classes + 5);
164 const bool activate = box_ch_id != 2 && box_ch_id != 3;
165
166 // Perform activation
167 if(activate)
168 {
169 auto activation = ::detail::logistic<float16_t, 8>(_act_info);
170 activation(res);
171 }
172
173 // Store results
174 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), res);
175 },
176 input, output);
177}
178
179void NEYOLOLayerKernel::yolo_layer_fp16_nhwc(const Window &window)
180{
181 Iterator input(_input, window);
182 Iterator output(_output, window);
183
184 execute_window_loop(window, [&](const Coordinates & id)
185 {
186 float16_t res = *(reinterpret_cast<float16_t *>(input.ptr()));
187
188 const int box_ch_id = id.x() % (_num_classes + 5);
189 const bool activate = box_ch_id != 2 && box_ch_id != 3;
190
191 // Perform activation
192 if(activate)
193 {
194 res = 1.f / (1.f + std::exp(-res));
195 }
196
197 // Store result
198 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
199 },
200 input, output);
201}
202#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
203
204void NEYOLOLayerKernel::configure(ITensor *input, ITensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
205{
206 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
207 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes));
208
209 _input = input;
210 _output = output;
211 _act_info = act_info;
212 _num_classes = num_classes;
213
214 switch(_input->info()->data_type())
215 {
216#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
217 case DataType::F16:
218 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_fp16_nhwc : &NEYOLOLayerKernel::yolo_layer_fp16_nchw;
219 break;
220#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221 case DataType::F32:
222 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_fp32_nhwc : &NEYOLOLayerKernel::yolo_layer_fp32_nchw;
223 break;
224 default:
225 ARM_COMPUTE_ERROR("Element size not supported");
226 break;
227 }
228
229 // Configure kernel window
230 auto win_config = validate_and_configure_window(input->info(), (output == nullptr) ? nullptr : output->info());
231 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
232 ICPPKernel::configure(win_config.second);
233}
234
235Status NEYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
236{
237 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes));
238 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output == nullptr) ? nullptr : output->clone().get()).first);
239
240 return Status{};
241}
242
243void NEYOLOLayerKernel::run(const Window &window, const ThreadInfo &info)
244{
245 ARM_COMPUTE_UNUSED(info);
246 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
247 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
248 ARM_COMPUTE_ERROR_ON(_func == nullptr);
249
250 (this->*_func)(window);
251}