blob: b61633dc30fe3b14c091e3bf126afe3955a688a6 [file] [log] [blame]
Michalis Spyroua6825a42018-09-13 12:24:03 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000040namespace arm_compute
41{
Michalis Spyroua6825a42018-09-13 12:24:03 +010042namespace
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}
Michalis Spyroua6825a42018-09-13 12:24:03 +010065} // namespace
66
67NEYOLOLayerKernel::NEYOLOLayerKernel()
68 : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes()
69{
70}
71
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000072template <typename T, int S>
73void NEYOLOLayerKernel::yolo_layer_nchw(const Window &window)
Michalis Spyroua6825a42018-09-13 12:24:03 +010074{
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000075 const auto window_start_x = static_cast<int>(window.x().start());
76 const auto window_end_x = static_cast<int>(window.x().end());
77 const int window_step_x = S;
Michalis Spyroua6825a42018-09-13 12:24:03 +010078
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000079 Window win{ window };
80 win.set(Window::DimX, Window::Dimension(0, 1, 1));
81 Iterator input(_input, win);
82 Iterator output(_output, win);
83
84 execute_window_loop(win, [&](const Coordinates & id)
Michalis Spyroua6825a42018-09-13 12:24:03 +010085 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000086 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
87 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
88 int x = window_start_x;
89 const int box_ch_id = id.z() % (_num_classes + 5);
90 const bool activate = box_ch_id != 2 && box_ch_id != 3;
Michalis Spyroua6825a42018-09-13 12:24:03 +010091
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000092 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michalis Spyroua6825a42018-09-13 12:24:03 +010093 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000094 auto res = wrapper::vloadq(input_ptr + x);
95
96 // Perform activation
97 if(activate)
98 {
99 auto activation = detail::logistic<T, S>(_act_info);
100 activation(res);
101 }
102
103 // Store results
104 wrapper::vstore(output_ptr + x, res);
Michalis Spyroua6825a42018-09-13 12:24:03 +0100105 }
106
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000107 // Compute left-over elements
108 for(; x < window_end_x; ++x)
109 {
110 auto res = *(input_ptr + x);
111
112 // Perform activation
113 if(activate)
114 {
115 res = 1.f / (1.f + std::exp(-res));
116 }
117
118 *(output_ptr + x) = res;
119 }
Michalis Spyroua6825a42018-09-13 12:24:03 +0100120 },
121 input, output);
122}
123
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000124template <typename T>
125void NEYOLOLayerKernel::yolo_layer_nhwc(const Window &window)
Michalis Spyroua6825a42018-09-13 12:24:03 +0100126{
127 Iterator input(_input, window);
128 Iterator output(_output, window);
129
130 execute_window_loop(window, [&](const Coordinates & id)
131 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000132 auto res = *(reinterpret_cast<T *>(input.ptr()));
Michalis Spyroua6825a42018-09-13 12:24:03 +0100133
134 const int box_ch_id = id.x() % (_num_classes + 5);
135 const bool activate = box_ch_id != 2 && box_ch_id != 3;
136
137 // Perform activation
138 if(activate)
139 {
140 res = 1.f / (1.f + std::exp(-res));
141 }
142
143 // Store result
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000144 *(reinterpret_cast<T *>(output.ptr())) = res;
Michalis Spyroua6825a42018-09-13 12:24:03 +0100145 },
146 input, output);
147}
148
Michalis Spyroua6825a42018-09-13 12:24:03 +0100149void NEYOLOLayerKernel::configure(ITensor *input, ITensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
150{
151 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
152 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes));
153
154 _input = input;
155 _output = output;
156 _act_info = act_info;
157 _num_classes = num_classes;
158
159 switch(_input->info()->data_type())
160 {
161#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
162 case DataType::F16:
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000163 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_nhwc<float16_t> : &NEYOLOLayerKernel::yolo_layer_nchw<float16_t, 8>;
Michalis Spyroua6825a42018-09-13 12:24:03 +0100164 break;
165#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
166 case DataType::F32:
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000167 _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_nhwc<float> : &NEYOLOLayerKernel::yolo_layer_nchw<float, 4>;
Michalis Spyroua6825a42018-09-13 12:24:03 +0100168 break;
169 default:
170 ARM_COMPUTE_ERROR("Element size not supported");
171 break;
172 }
173
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000174 Window win = calculate_max_window(*input->info(), Steps());
175
Michalis Spyroua6825a42018-09-13 12:24:03 +0100176 // Configure kernel window
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000177 if(output != nullptr)
178 {
179 // Output auto inizialitation if not yet initialized
180 auto_init_if_empty(*output->info(), *input->info());
181
182 Coordinates coord;
183 coord.set_num_dimensions(output->info()->num_dimensions());
184
185 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
186 }
187
188 ICPPKernel::configure(win);
Michalis Spyroua6825a42018-09-13 12:24:03 +0100189}
190
191Status NEYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
192{
193 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes));
Michalis Spyroua6825a42018-09-13 12:24:03 +0100194
195 return Status{};
196}
197
198void NEYOLOLayerKernel::run(const Window &window, const ThreadInfo &info)
199{
200 ARM_COMPUTE_UNUSED(info);
201 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
202 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
203 ARM_COMPUTE_ERROR_ON(_func == nullptr);
204
205 (this->*_func)(window);
206}
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000207} // namespace arm_compute