blob: 383c2b8b99a5b45d122f2ceba1894513b59553f1 [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"
Michalis Spyroua6825a42018-09-13 12:24:03 +010033#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <arm_neon.h>
39
40using namespace arm_compute;
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
44{
45 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000046 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michalis Spyroua6825a42018-09-13 12:24:03 +010047 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);
49 ARM_COMPUTE_RETURN_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC);
50
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
98NEYOLOLayerKernel::NEYOLOLayerKernel()
99 : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes()
100{
101}
102
103void NEYOLOLayerKernel::yolo_layer_fp32_nchw(const Window &window)
104{
105 Iterator input(_input, window);
106 Iterator output(_output, window);
107
108 execute_window_loop(window, [&](const Coordinates & id)
109 {
110 float32x4_t res = vld1q_f32(reinterpret_cast<float *>(input.ptr()));
111
112 const int box_ch_id = id.z() % (_num_classes + 5);
113 const bool activate = box_ch_id != 2 && box_ch_id != 3;
114
115 // Perform activation
116 if(activate)
117 {
118 auto activation = ::detail::logistic<float, 4>(_act_info);
119 activation(res);
120 }
121
122 // Store results
123 vst1q_f32(reinterpret_cast<float *>(output.ptr()), res);
124 },
125 input, output);
126}
127
128void NEYOLOLayerKernel::yolo_layer_fp32_nhwc(const Window &window)
129{
130 Iterator input(_input, window);
131 Iterator output(_output, window);
132
133 execute_window_loop(window, [&](const Coordinates & id)
134 {
135 float res = *(reinterpret_cast<float *>(input.ptr()));
136
137 const int box_ch_id = id.x() % (_num_classes + 5);
138 const bool activate = box_ch_id != 2 && box_ch_id != 3;
139
140 // Perform activation
141 if(activate)
142 {
143 res = 1.f / (1.f + std::exp(-res));
144 }
145
146 // Store result
147 *(reinterpret_cast<float *>(output.ptr())) = res;
148 },
149 input, output);
150}
151
152#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
153void NEYOLOLayerKernel::yolo_layer_fp16_nchw(const Window &window)
154{
155 Iterator input(_input, window);
156 Iterator output(_output, window);
157
158 execute_window_loop(window, [&](const Coordinates & id)
159 {
160 float16x8_t res = vld1q_f16(reinterpret_cast<float16_t *>(input.ptr()));
161
162 const int box_ch_id = id.z() % (_num_classes + 5);
163 const bool activate = box_ch_id != 2 && box_ch_id != 3;
164
165 // Perform activation
166 if(activate)
167 {
168 auto activation = ::detail::logistic<float16_t, 8>(_act_info);
169 activation(res);
170 }
171
172 // Store results
173 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), res);
174 },
175 input, output);
176}
177
178void NEYOLOLayerKernel::yolo_layer_fp16_nhwc(const Window &window)
179{
180 Iterator input(_input, window);
181 Iterator output(_output, window);
182
183 execute_window_loop(window, [&](const Coordinates & id)
184 {
185 float16_t res = *(reinterpret_cast<float16_t *>(input.ptr()));
186
187 const int box_ch_id = id.x() % (_num_classes + 5);
188 const bool activate = box_ch_id != 2 && box_ch_id != 3;
189
190 // Perform activation
191 if(activate)
192 {
193 res = 1.f / (1.f + std::exp(-res));
194 }
195
196 // Store result
197 *(reinterpret_cast<float16_t *>(output.ptr())) = res;
198 },
199 input, output);
200}
201#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
202
203void NEYOLOLayerKernel::configure(ITensor *input, ITensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
204{
205 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
206 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes));
207
208 _input = input;
209 _output = output;
210 _act_info = act_info;
211 _num_classes = num_classes;
212
213 switch(_input->info()->data_type())
214 {
215#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
216 case DataType::F16:
217 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_fp16_nhwc : &NEYOLOLayerKernel::yolo_layer_fp16_nchw;
218 break;
219#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
220 case DataType::F32:
221 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_fp32_nhwc : &NEYOLOLayerKernel::yolo_layer_fp32_nchw;
222 break;
223 default:
224 ARM_COMPUTE_ERROR("Element size not supported");
225 break;
226 }
227
228 // Configure kernel window
229 auto win_config = validate_and_configure_window(input->info(), (output == nullptr) ? nullptr : output->info());
230 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
231 ICPPKernel::configure(win_config.second);
232}
233
234Status NEYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
235{
236 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes));
237 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output == nullptr) ? nullptr : output->clone().get()).first);
238
239 return Status{};
240}
241
242void NEYOLOLayerKernel::run(const Window &window, const ThreadInfo &info)
243{
244 ARM_COMPUTE_UNUSED(info);
245 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
246 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
247 ARM_COMPUTE_ERROR_ON(_func == nullptr);
248
249 (this->*_func)(window);
250}