blob: b8adc15e77cf4cb21e54d05c1b8d5ed27c66f9da [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas25ef7212020-06-02 23:00:41 +01002 * Copyright (c) 2016-2020 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/NEThresholdKernel.h"
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"
30
Georgios Pinitas25ef7212020-06-02 23:00:41 +010031#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
Georgios Pinitas25ef7212020-06-02 23:00:41 +010035namespace
36{
37Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ThresholdKernelInfo &info)
38{
39 ARM_COMPUTE_UNUSED(info);
40 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
41 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
42
43 // Checks performed when output is configured
44 if((output != nullptr) && (output->total_size() != 0))
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
48 }
49
50 return Status{};
51}
52
53std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
54{
55 // Configure kernel window
56 Window win = calculate_max_window(*input, Steps());
57
58 // Output auto inizialitation if not yet initialized
59 auto_init_if_empty(*output, *input->clone());
60
61 // NEThresholdKernel doesn't need padding so update_window_and_padding() can be skipped
62 Coordinates coord;
63 coord.set_num_dimensions(output->num_dimensions());
64 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
65
66 return std::make_pair(Status{}, win);
67}
68} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
70NEThresholdKernel::NEThresholdKernel()
Georgios Pinitas25ef7212020-06-02 23:00:41 +010071 : _func(nullptr), _input(nullptr), _output(nullptr), _info()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072{
73}
74
Georgios Pinitas25ef7212020-06-02 23:00:41 +010075void NEThresholdKernel::configure(const ITensor *input, ITensor *output, const ThresholdKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076{
Georgios Pinitas25ef7212020-06-02 23:00:41 +010077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
78 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
Georgios Pinitas25ef7212020-06-02 23:00:41 +010080 _input = input;
81 _output = output;
82 _info = info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
Georgios Pinitas25ef7212020-06-02 23:00:41 +010084 switch(_info.type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 {
86 case ThresholdType::BINARY:
87 _func = &NEThresholdKernel::run_binary;
88 break;
89 case ThresholdType::RANGE:
90 _func = &NEThresholdKernel::run_range;
91 break;
92 default:
93 ARM_COMPUTE_ERROR("Thresholding type not recognized");
94 break;
95 }
96
Georgios Pinitas25ef7212020-06-02 23:00:41 +010097 // Configure kernel window
98 auto win_config = validate_and_configure_window(input->info(), output->info());
99 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
100 ICPPKernel::configure(win_config.second);
101}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100103Status NEThresholdKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ThresholdKernelInfo &info)
104{
105 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
106 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100108 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109}
110
111inline void NEThresholdKernel::run_binary(const Window &window)
112{
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100113 /** NEON vector tag type. */
114 using Type = uint8_t;
115 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<Type, wrapper::traits::BitWidth::W128>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100117 const int window_step_x = 16 / sizeof(Type);
118 const auto window_start_x = static_cast<int>(window.x().start());
119 const auto window_end_x = static_cast<int>(window.x().end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100121 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
122 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
123
124 const uint8_t threshold = _info.threshold;
125 const uint8_t true_value = _info.true_value;
126 const uint8_t false_value = _info.false_value;
127
128 const auto vthreshold = wrapper::vdup_n(threshold, ExactTagType{});
129 const auto vtrue_value = wrapper::vdup_n(true_value, ExactTagType{});
130 const auto vfalse_value = wrapper::vdup_n(false_value, ExactTagType{});
131
132 Iterator input(_input, win_collapsed);
133 Iterator output(_output, win_collapsed);
134
135 execute_window_loop(win_collapsed, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 {
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100137 const auto input_ptr = reinterpret_cast<const Type *>(input.ptr());
138 const auto output_ptr = reinterpret_cast<Type *>(output.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100140 int x = window_start_x;
141 for(; x <= (window_end_x - window_step_x); x += window_step_x)
142 {
143 const auto vdata = wrapper::vloadq(input_ptr + x);
144 const auto vmask = wrapper::vcgt(vdata, vthreshold);
145 wrapper::vstore(output_ptr + x, wrapper::vbsl(vmask, vtrue_value, vfalse_value));
146 }
147
148 for(; x < window_end_x; ++x)
149 {
150 const Type data = *(reinterpret_cast<const Type *>(input_ptr + x));
151 *(output_ptr + x) = (data > threshold) ? true_value : false_value;
152 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 },
154 input, output);
155}
156
157inline void NEThresholdKernel::run_range(const Window &window)
158{
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100159 /** NEON vector tag type. */
160 using Type = uint8_t;
161 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<Type, wrapper::traits::BitWidth::W128>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100163 const int window_step_x = 16 / sizeof(Type);
164 const auto window_start_x = static_cast<int>(window.x().start());
165 const auto window_end_x = static_cast<int>(window.x().end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100167 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
168 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
169
170 const uint8_t lower_threshold = _info.threshold;
171 const uint8_t upper_threshold = _info.upper;
172 const uint8_t true_value = _info.true_value;
173 const uint8_t false_value = _info.false_value;
174
175 const auto vlower_threshold = wrapper::vdup_n(lower_threshold, ExactTagType{});
176 const auto vupper_threshold = wrapper::vdup_n(upper_threshold, ExactTagType{});
177 const auto vtrue_value = wrapper::vdup_n(true_value, ExactTagType{});
178 const auto vfalse_value = wrapper::vdup_n(false_value, ExactTagType{});
179
180 Iterator input(_input, win_collapsed);
181 Iterator output(_output, win_collapsed);
182
183 execute_window_loop(win_collapsed, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 {
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100185 const auto input_ptr = reinterpret_cast<const Type *>(input.ptr());
186 const auto output_ptr = reinterpret_cast<Type *>(output.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100188 int x = window_start_x;
189 for(; x <= (window_end_x - window_step_x); x += window_step_x)
190 {
191 const auto vdata = wrapper::vloadq(input_ptr + x);
192 auto vmask = wrapper::vcle(vdata, vupper_threshold);
193 vmask = wrapper::vand(wrapper::vcge(vdata, vlower_threshold), vmask);
194 wrapper::vstore(output_ptr + x, wrapper::vbsl(vmask, vtrue_value, vfalse_value));
195 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196
Georgios Pinitas25ef7212020-06-02 23:00:41 +0100197 for(; x < window_end_x; ++x)
198 {
199 const Type data = *(reinterpret_cast<const Type *>(input_ptr + x));
200 *(output_ptr + x) = (data <= upper_threshold && data >= lower_threshold) ? true_value : false_value;
201 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 },
203 input, output);
204}
205
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100206void NEThresholdKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100208 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
210 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
211 ARM_COMPUTE_ERROR_ON(_func == nullptr);
212
213 (this->*_func)(window);
214}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100215} // namespace arm_compute