Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEYOLOLayerKernel.h" |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 25 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/NEAsymm.h" |
| 34 | #include "src/core/NEON/NEFixedPoint.h" |
| 35 | #include "src/core/NEON/NEMath.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 38 | |
| 39 | #include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h" |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 40 | |
| 41 | #include <arm_neon.h> |
| 42 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 43 | namespace arm_compute |
| 44 | { |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 45 | namespace |
| 46 | { |
| 47 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes) |
| 48 | { |
| 49 | ARM_COMPUTE_UNUSED(act_info); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationFunction::LOGISTIC); |
| 54 | |
| 55 | const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(num_classes <= 0); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(channel_idx) % (num_classes + 5)) != 0); |
| 58 | |
| 59 | // Checks performed when output is configured |
| 60 | if((output != nullptr) && (output->total_size() != 0)) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 64 | } |
| 65 | |
| 66 | return Status{}; |
| 67 | } |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | NEYOLOLayerKernel::NEYOLOLayerKernel() |
| 71 | : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes() |
| 72 | { |
| 73 | } |
| 74 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 75 | template <typename T, int S> |
| 76 | void NEYOLOLayerKernel::yolo_layer_nchw(const Window &window) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 77 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 78 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 79 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 80 | const int window_step_x = S; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 81 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 82 | Window win{ window }; |
| 83 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 84 | Iterator input(_input, win); |
| 85 | Iterator output(_output, win); |
| 86 | |
| 87 | execute_window_loop(win, [&](const Coordinates & id) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 88 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 89 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 90 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
| 91 | int x = window_start_x; |
| 92 | const int box_ch_id = id.z() % (_num_classes + 5); |
| 93 | const bool activate = box_ch_id != 2 && box_ch_id != 3; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 94 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 95 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 96 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 97 | auto res = wrapper::vloadq(input_ptr + x); |
| 98 | |
| 99 | // Perform activation |
| 100 | if(activate) |
| 101 | { |
| 102 | auto activation = detail::logistic<T, S>(_act_info); |
| 103 | activation(res); |
| 104 | } |
| 105 | |
| 106 | // Store results |
| 107 | wrapper::vstore(output_ptr + x, res); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 110 | // Compute left-over elements |
| 111 | for(; x < window_end_x; ++x) |
| 112 | { |
| 113 | auto res = *(input_ptr + x); |
| 114 | |
| 115 | // Perform activation |
| 116 | if(activate) |
| 117 | { |
| 118 | res = 1.f / (1.f + std::exp(-res)); |
| 119 | } |
| 120 | |
| 121 | *(output_ptr + x) = res; |
| 122 | } |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 123 | }, |
| 124 | input, output); |
| 125 | } |
| 126 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 127 | template <typename T> |
| 128 | void NEYOLOLayerKernel::yolo_layer_nhwc(const Window &window) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 129 | { |
| 130 | Iterator input(_input, window); |
| 131 | Iterator output(_output, window); |
| 132 | |
| 133 | execute_window_loop(window, [&](const Coordinates & id) |
| 134 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 135 | auto res = *(reinterpret_cast<T *>(input.ptr())); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 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 |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 147 | *(reinterpret_cast<T *>(output.ptr())) = res; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 148 | }, |
| 149 | input, output); |
| 150 | } |
| 151 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 152 | void NEYOLOLayerKernel::configure(ITensor *input, ITensor *output, const ActivationLayerInfo &act_info, int32_t num_classes) |
| 153 | { |
| 154 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 155 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info, num_classes)); |
| 156 | |
| 157 | _input = input; |
| 158 | _output = output; |
| 159 | _act_info = act_info; |
| 160 | _num_classes = num_classes; |
| 161 | |
| 162 | switch(_input->info()->data_type()) |
| 163 | { |
| 164 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 165 | case DataType::F16: |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 166 | _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_nhwc<float16_t> : &NEYOLOLayerKernel::yolo_layer_nchw<float16_t, 8>; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 167 | break; |
| 168 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 169 | case DataType::F32: |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 170 | _func = (_input->info()->data_layout() == DataLayout::NHWC) ? &NEYOLOLayerKernel::yolo_layer_nhwc<float> : &NEYOLOLayerKernel::yolo_layer_nchw<float, 4>; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 171 | break; |
| 172 | default: |
| 173 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 174 | break; |
| 175 | } |
| 176 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 177 | Window win = calculate_max_window(*input->info(), Steps()); |
| 178 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 179 | // Configure kernel window |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 180 | if(output != nullptr) |
| 181 | { |
| 182 | // Output auto inizialitation if not yet initialized |
| 183 | auto_init_if_empty(*output->info(), *input->info()); |
| 184 | |
| 185 | Coordinates coord; |
| 186 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 187 | |
| 188 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 189 | } |
| 190 | |
| 191 | ICPPKernel::configure(win); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Status NEYOLOLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes) |
| 195 | { |
| 196 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info, num_classes)); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 197 | |
| 198 | return Status{}; |
| 199 | } |
| 200 | |
| 201 | void NEYOLOLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 202 | { |
| 203 | ARM_COMPUTE_UNUSED(info); |
| 204 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 205 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 206 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 207 | |
| 208 | (this->*_func)(window); |
| 209 | } |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 210 | } // namespace arm_compute |