blob: 591aa1e5e60e91f582d53e7c055310852ebfa978 [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"
Michalis Spyroua6825a42018-09-13 12:24:03 +010029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/NEAsymm.h"
34#include "src/core/NEON/NEFixedPoint.h"
35#include "src/core/NEON/NEMath.h"
36
37#include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h"
Michalis Spyroua6825a42018-09-13 12:24:03 +010038
39#include <arm_neon.h>
40
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000041namespace arm_compute
42{
Michalis Spyroua6825a42018-09-13 12:24:03 +010043namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
46{
47 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000048 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michalis Spyroua6825a42018-09-13 12:24:03 +010049 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}
Michalis Spyroua6825a42018-09-13 12:24:03 +010066} // namespace
67
68NEYOLOLayerKernel::NEYOLOLayerKernel()
69 : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes()
70{
71}
72
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000073template <typename T, int S>
74void NEYOLOLayerKernel::yolo_layer_nchw(const Window &window)
Michalis Spyroua6825a42018-09-13 12:24:03 +010075{
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000076 const auto window_start_x = static_cast<int>(window.x().start());
77 const auto window_end_x = static_cast<int>(window.x().end());
78 const int window_step_x = S;
Michalis Spyroua6825a42018-09-13 12:24:03 +010079
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000080 Window win{ window };
81 win.set(Window::DimX, Window::Dimension(0, 1, 1));
82 Iterator input(_input, win);
83 Iterator output(_output, win);
84
85 execute_window_loop(win, [&](const Coordinates & id)
Michalis Spyroua6825a42018-09-13 12:24:03 +010086 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000087 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
88 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
89 int x = window_start_x;
90 const int box_ch_id = id.z() % (_num_classes + 5);
91 const bool activate = box_ch_id != 2 && box_ch_id != 3;
Michalis Spyroua6825a42018-09-13 12:24:03 +010092
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000093 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michalis Spyroua6825a42018-09-13 12:24:03 +010094 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +000095 auto res = wrapper::vloadq(input_ptr + x);
96
97 // Perform activation
98 if(activate)
99 {
100 auto activation = detail::logistic<T, S>(_act_info);
101 activation(res);
102 }
103
104 // Store results
105 wrapper::vstore(output_ptr + x, res);
Michalis Spyroua6825a42018-09-13 12:24:03 +0100106 }
107
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000108 // Compute left-over elements
109 for(; x < window_end_x; ++x)
110 {
111 auto res = *(input_ptr + x);
112
113 // Perform activation
114 if(activate)
115 {
116 res = 1.f / (1.f + std::exp(-res));
117 }
118
119 *(output_ptr + x) = res;
120 }
Michalis Spyroua6825a42018-09-13 12:24:03 +0100121 },
122 input, output);
123}
124
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000125template <typename T>
126void NEYOLOLayerKernel::yolo_layer_nhwc(const Window &window)
Michalis Spyroua6825a42018-09-13 12:24:03 +0100127{
128 Iterator input(_input, window);
129 Iterator output(_output, window);
130
131 execute_window_loop(window, [&](const Coordinates & id)
132 {
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000133 auto res = *(reinterpret_cast<T *>(input.ptr()));
Michalis Spyroua6825a42018-09-13 12:24:03 +0100134
135 const int box_ch_id = id.x() % (_num_classes + 5);
136 const bool activate = box_ch_id != 2 && box_ch_id != 3;
137
138 // Perform activation
139 if(activate)
140 {
141 res = 1.f / (1.f + std::exp(-res));
142 }
143
144 // Store result
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000145 *(reinterpret_cast<T *>(output.ptr())) = res;
Michalis Spyroua6825a42018-09-13 12:24:03 +0100146 },
147 input, output);
148}
149
Michalis Spyroua6825a42018-09-13 12:24:03 +0100150void NEYOLOLayerKernel::configure(ITensor *input, ITensor *output, const ActivationLayerInfo &act_info, int32_t num_classes)
151{
152 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
153 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes));
154
155 _input = input;
156 _output = output;
157 _act_info = act_info;
158 _num_classes = num_classes;
159
160 switch(_input->info()->data_type())
161 {
162#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
163 case DataType::F16:
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000164 _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 +0100165 break;
166#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
167 case DataType::F32:
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000168 _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 +0100169 break;
170 default:
171 ARM_COMPUTE_ERROR("Element size not supported");
172 break;
173 }
174
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000175 Window win = calculate_max_window(*input->info(), Steps());
176
Michalis Spyroua6825a42018-09-13 12:24:03 +0100177 // Configure kernel window
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000178 if(output != nullptr)
179 {
180 // Output auto inizialitation if not yet initialized
181 auto_init_if_empty(*output->info(), *input->info());
182
183 Coordinates coord;
184 coord.set_num_dimensions(output->info()->num_dimensions());
185
186 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
187 }
188
189 ICPPKernel::configure(win);
Michalis Spyroua6825a42018-09-13 12:24:03 +0100190}
191
192Status NEYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes)
193{
194 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes));
Michalis Spyroua6825a42018-09-13 12:24:03 +0100195
196 return Status{};
197}
198
199void NEYOLOLayerKernel::run(const Window &window, const ThreadInfo &info)
200{
201 ARM_COMPUTE_UNUSED(info);
202 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
203 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
204 ARM_COMPUTE_ERROR_ON(_func == nullptr);
205
206 (this->*_func)(window);
207}
Michalis Spyroubb1ad0c2020-02-28 17:31:27 +0000208} // namespace arm_compute