Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [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 | */ |
| 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 33 | #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 Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info, int32_t num_classes) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(act_info); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 48 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | NEYOLOLayerKernel::NEYOLOLayerKernel() |
| 68 | : _func(nullptr), _input(nullptr), _output(nullptr), _act_info(), _num_classes() |
| 69 | { |
| 70 | } |
| 71 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 72 | template <typename T, int S> |
| 73 | void NEYOLOLayerKernel::yolo_layer_nchw(const Window &window) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 74 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 75 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 78 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 79 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 85 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 86 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 91 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 92 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 93 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 94 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 107 | // 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 120 | }, |
| 121 | input, output); |
| 122 | } |
| 123 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 124 | template <typename T> |
| 125 | void NEYOLOLayerKernel::yolo_layer_nhwc(const Window &window) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 126 | { |
| 127 | Iterator input(_input, window); |
| 128 | Iterator output(_output, window); |
| 129 | |
| 130 | execute_window_loop(window, [&](const Coordinates & id) |
| 131 | { |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 132 | auto res = *(reinterpret_cast<T *>(input.ptr())); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 133 | |
| 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 Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 144 | *(reinterpret_cast<T *>(output.ptr())) = res; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 145 | }, |
| 146 | input, output); |
| 147 | } |
| 148 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 149 | void 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 Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 163 | _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] | 164 | break; |
| 165 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 166 | case DataType::F32: |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 167 | _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] | 168 | break; |
| 169 | default: |
| 170 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 171 | break; |
| 172 | } |
| 173 | |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 174 | Window win = calculate_max_window(*input->info(), Steps()); |
| 175 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 176 | // Configure kernel window |
Michalis Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 177 | 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | Status 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 Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 194 | |
| 195 | return Status{}; |
| 196 | } |
| 197 | |
| 198 | void 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 Spyrou | bb1ad0c | 2020-02-28 17:31:27 +0000 | [diff] [blame] | 207 | } // namespace arm_compute |