Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/NEThresholdKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ThresholdKernelInfo &info) |
| 40 | { |
| 41 | ARM_COMPUTE_UNUSED(info); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 44 | |
| 45 | // Checks performed when output is configured |
| 46 | if((output != nullptr) && (output->total_size() != 0)) |
| 47 | { |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 50 | } |
| 51 | |
| 52 | return Status{}; |
| 53 | } |
| 54 | |
| 55 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 56 | { |
| 57 | // Configure kernel window |
| 58 | Window win = calculate_max_window(*input, Steps()); |
| 59 | |
| 60 | // Output auto inizialitation if not yet initialized |
| 61 | auto_init_if_empty(*output, *input->clone()); |
| 62 | |
| 63 | // NEThresholdKernel doesn't need padding so update_window_and_padding() can be skipped |
| 64 | Coordinates coord; |
| 65 | coord.set_num_dimensions(output->num_dimensions()); |
| 66 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
| 67 | |
| 68 | return std::make_pair(Status{}, win); |
| 69 | } |
| 70 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
| 72 | NEThresholdKernel::NEThresholdKernel() |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 73 | : _func(nullptr), _input(nullptr), _output(nullptr), _info() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 77 | void NEThresholdKernel::configure(const ITensor *input, ITensor *output, const ThresholdKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 80 | ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 82 | _input = input; |
| 83 | _output = output; |
| 84 | _info = info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 86 | switch(_info.type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | { |
| 88 | case ThresholdType::BINARY: |
| 89 | _func = &NEThresholdKernel::run_binary; |
| 90 | break; |
| 91 | case ThresholdType::RANGE: |
| 92 | _func = &NEThresholdKernel::run_range; |
| 93 | break; |
| 94 | default: |
| 95 | ARM_COMPUTE_ERROR("Thresholding type not recognized"); |
| 96 | break; |
| 97 | } |
| 98 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 99 | // Configure kernel window |
| 100 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 101 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 102 | ICPPKernel::configure(win_config.second); |
| 103 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 105 | Status NEThresholdKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ThresholdKernelInfo &info) |
| 106 | { |
| 107 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info)); |
| 108 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 110 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline void NEThresholdKernel::run_binary(const Window &window) |
| 114 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 115 | /** NEON vector tag type. */ |
| 116 | using Type = uint8_t; |
| 117 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<Type, wrapper::traits::BitWidth::W128>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 119 | const int window_step_x = 16 / sizeof(Type); |
| 120 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 121 | const auto window_end_x = static_cast<int>(window.x().end()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 123 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 124 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 125 | |
| 126 | const uint8_t threshold = _info.threshold; |
| 127 | const uint8_t true_value = _info.true_value; |
| 128 | const uint8_t false_value = _info.false_value; |
| 129 | |
| 130 | const auto vthreshold = wrapper::vdup_n(threshold, ExactTagType{}); |
| 131 | const auto vtrue_value = wrapper::vdup_n(true_value, ExactTagType{}); |
| 132 | const auto vfalse_value = wrapper::vdup_n(false_value, ExactTagType{}); |
| 133 | |
| 134 | Iterator input(_input, win_collapsed); |
| 135 | Iterator output(_output, win_collapsed); |
| 136 | |
| 137 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 139 | const auto input_ptr = reinterpret_cast<const Type *>(input.ptr()); |
| 140 | const auto output_ptr = reinterpret_cast<Type *>(output.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 142 | int x = window_start_x; |
| 143 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 144 | { |
| 145 | const auto vdata = wrapper::vloadq(input_ptr + x); |
| 146 | const auto vmask = wrapper::vcgt(vdata, vthreshold); |
| 147 | wrapper::vstore(output_ptr + x, wrapper::vbsl(vmask, vtrue_value, vfalse_value)); |
| 148 | } |
| 149 | |
| 150 | for(; x < window_end_x; ++x) |
| 151 | { |
| 152 | const Type data = *(reinterpret_cast<const Type *>(input_ptr + x)); |
| 153 | *(output_ptr + x) = (data > threshold) ? true_value : false_value; |
| 154 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | }, |
| 156 | input, output); |
| 157 | } |
| 158 | |
| 159 | inline void NEThresholdKernel::run_range(const Window &window) |
| 160 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 161 | /** NEON vector tag type. */ |
| 162 | using Type = uint8_t; |
| 163 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<Type, wrapper::traits::BitWidth::W128>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 165 | const int window_step_x = 16 / sizeof(Type); |
| 166 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 167 | const auto window_end_x = static_cast<int>(window.x().end()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 169 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 170 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 171 | |
| 172 | const uint8_t lower_threshold = _info.threshold; |
| 173 | const uint8_t upper_threshold = _info.upper; |
| 174 | const uint8_t true_value = _info.true_value; |
| 175 | const uint8_t false_value = _info.false_value; |
| 176 | |
| 177 | const auto vlower_threshold = wrapper::vdup_n(lower_threshold, ExactTagType{}); |
| 178 | const auto vupper_threshold = wrapper::vdup_n(upper_threshold, ExactTagType{}); |
| 179 | const auto vtrue_value = wrapper::vdup_n(true_value, ExactTagType{}); |
| 180 | const auto vfalse_value = wrapper::vdup_n(false_value, ExactTagType{}); |
| 181 | |
| 182 | Iterator input(_input, win_collapsed); |
| 183 | Iterator output(_output, win_collapsed); |
| 184 | |
| 185 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | { |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 187 | const auto input_ptr = reinterpret_cast<const Type *>(input.ptr()); |
| 188 | const auto output_ptr = reinterpret_cast<Type *>(output.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 190 | int x = window_start_x; |
| 191 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 192 | { |
| 193 | const auto vdata = wrapper::vloadq(input_ptr + x); |
| 194 | auto vmask = wrapper::vcle(vdata, vupper_threshold); |
| 195 | vmask = wrapper::vand(wrapper::vcge(vdata, vlower_threshold), vmask); |
| 196 | wrapper::vstore(output_ptr + x, wrapper::vbsl(vmask, vtrue_value, vfalse_value)); |
| 197 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | |
Georgios Pinitas | 25ef721 | 2020-06-02 23:00:41 +0100 | [diff] [blame] | 199 | for(; x < window_end_x; ++x) |
| 200 | { |
| 201 | const Type data = *(reinterpret_cast<const Type *>(input_ptr + x)); |
| 202 | *(output_ptr + x) = (data <= upper_threshold && data >= lower_threshold) ? true_value : false_value; |
| 203 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | }, |
| 205 | input, output); |
| 206 | } |
| 207 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 208 | void NEThresholdKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 210 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 212 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 213 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 214 | |
| 215 | (this->*_func)(window); |
| 216 | } |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 217 | } // namespace arm_compute |