Compute Library
 21.02
NESelectKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 Arm Limited.
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  */
25 
26 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Types.h"
33 #include "src/core/CPP/Validate.h"
37 
38 #include <arm_neon.h>
39 #include <map>
40 #include <string>
41 
42 namespace arm_compute
43 {
44 namespace
45 {
46 template <typename ScalarType, typename VectorType>
47 void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window,
48  const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *))
49 {
50  Window win = window;
51  win.set(Window::DimX, Window::Dimension(0, 1, 1));
52 
53  Iterator condition(cond, win);
54  Iterator input1(in1, win);
55  Iterator input2(in2, win);
56  Iterator output(out, win);
57 
58  execute_window_loop(win, [&](const Coordinates &)
59  {
60  auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
61  const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr());
62  const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
63  const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
64 
65  int x = window_start_x;
66  for(; x <= limit; x += window_step_x)
67  {
68  const auto c = (*condition_conversion)(condition_ptr + x);
69  const auto a = wrapper::vloadq(input1_ptr + x);
70  const auto b = wrapper::vloadq(input2_ptr + x);
71  wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b));
72  }
73  for(; x < window_end_x; ++x)
74  {
75  const auto c = *(condition_ptr + x);
76  const auto a = *(input1_ptr + x);
77  const auto b = *(input2_ptr + x);
78  *(output_ptr + x) = static_cast<bool>(c) ? a : b;
79  }
80  },
81  condition, input1, input2, output);
82 }
83 
84 template <typename ScalarType, typename VectorType>
85 void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
86 {
87  const auto window_step_x = 16 / sizeof(ScalarType);
88  const auto window_start_x = static_cast<int>(window.x().start());
89  const auto window_end_x = static_cast<int>(window.x().end());
90 
91  select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
92  {
93  static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag());
94  return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero);
95  });
96 }
97 
98 template <typename ScalarType, typename VectorType>
99 void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
100 {
101  const auto window_step_x = 16 / sizeof(ScalarType);
102  const auto window_start_x = static_cast<int>(window.x().start());
103  const auto window_end_x = static_cast<int>(window.x().end());
104 
105  select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
106  {
107  static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag());
108  return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero);
109  });
110 }
111 
112 template <typename ScalarType, typename VectorType>
113 void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
114 {
115  const auto window_step_x = 16 / sizeof(ScalarType);
116  const auto window_start_x = static_cast<int>(window.x().start());
117  const auto window_end_x = static_cast<int>(window.x().end());
118 
119  select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
120  {
121  static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag());
123  });
124 }
125 
126 template <typename ScalarType>
127 void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
128 {
129  ARM_COMPUTE_UNUSED(window);
130 
131  auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer());
132  const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer());
133  const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer());
134  const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer());
135 
136  const int outer_size = cond->info()->total_size() / cond->info()->element_size();
137  const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size;
138  int offset = 0;
139  const int step = 16 / in1->info()->element_size();
140 
141  for(int i = 0; i < outer_size; ++i)
142  {
143  int x = offset;
144  const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr;
145  for(; x <= offset + inner_size - step; x += step)
146  {
147  wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x));
148  }
149  if(x <= offset + inner_size - (step / 2))
150  {
151  wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x));
152  x += step / 2;
153  }
154  for(; x < offset + inner_size; ++x)
155  {
156  *(output_ptr + x) = *(input_ptr + x);
157  }
158  offset += inner_size;
159  }
160 }
161 } // namespace
162 
164  : _function(nullptr), _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
165 {
166 }
167 
168 void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output)
169 {
170  ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
171 
172  // Auto initialize output if not initialized
173  auto_init_if_empty(*output->info(), x->info()->tensor_shape(), 1, x->info()->data_type());
174  ARM_COMPUTE_ERROR_THROW_ON(validate(c->info(), x->info(), y->info(), output->info()));
175 
176  _c = c;
177  _x = x;
178  _y = y;
179  _output = output;
180  _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
181 
182  std::string function_to_call("op_");
183  function_to_call += string_from_data_type(x->info()->data_type());
184 
185  static std::map<std::string, SelectFunction *> map_function;
186 
187  if(_has_same_rank)
188  {
189  map_function =
190  {
191  { "op_S8", &select_op_8<int8_t, uint8x16_t> },
192  { "op_S16", &select_op_16<int16_t, uint16x8_t> },
193  { "op_S32", &select_op_32<int32_t, uint32x4_t> },
194  { "op_U8", &select_op_8<uint8_t, uint8x16_t> },
195  { "op_U16", &select_op_16<uint16_t, uint16x8_t> },
196  { "op_U32", &select_op_32<uint32_t, uint32x4_t> },
197  { "op_F32", &select_op_32<float, uint32x4_t> }
198  };
199 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
200  map_function["op_F16"] = &select_op_16<float16_t, uint16x8_t>;
201 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
202  }
203  else
204  {
205  map_function =
206  {
207  { "op_S8", &select_op_not_same_rank<int8_t> },
208  { "op_S16", &select_op_not_same_rank<int16_t> },
209  { "op_S32", &select_op_not_same_rank<int32_t> },
210  { "op_U8", &select_op_not_same_rank<uint8_t> },
211  { "op_U16", &select_op_not_same_rank<uint16_t> },
212  { "op_U32", &select_op_not_same_rank<uint32_t> },
213  { "op_F32", &select_op_not_same_rank<float> }
214  };
215 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
216  map_function["op_F16"] = &select_op_not_same_rank<float16_t>;
217 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
218  }
219 
220  auto it = map_function.find(function_to_call);
221 
222  if(it != map_function.end())
223  {
224  _function = it->second;
225  }
226 
228  INEKernel::configure(win);
229 }
230 
231 Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
232 {
239 
240  const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
241  ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape()));
242  ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && ((c->tensor_shape().num_dimensions() > 1) || (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1])));
243 
244  if(output != nullptr && output->total_size() != 0)
245  {
248  }
249 
250  return Status{};
251 }
252 
253 void NESelectKernel::run(const Window &window, const ThreadInfo &info)
254 {
255  ARM_COMPUTE_UNUSED(info);
258  ARM_COMPUTE_ERROR_ON(_function == nullptr);
259  _function(_c, _x, _y, _output, window);
260 }
261 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:108
NESelectKernel()
Default constructor.
Condition condition(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:697
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
SimpleTensor< float > b
Definition: DFT.cpp:157
1 channel, 1 U8 per channel
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
uint8x8_t vcgt(const uint8x8_t &a, const uint8x8_t &b)
Definition: cgt.h:39
uint8x8_t vbsl(const uint8x8_t &a, const uint8x8_t &b, const uint8x8_t &c)
Definition: bsl.h:39
Information about executing thread and CPU.
Definition: CPPTypes.h:235
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
constexpr int step
Definition: fp32.cpp:35
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
uint8x8_t vload(const uint8_t *ptr)
Definition: load.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Includes all wrapper headers at once.
static Status validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
Validate the argument passed to the kernel.
uint16x8_t vmovl(const uint8x8_t &a)
Definition: movl.h:39
void configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output)
Common signature for all the specialised elementwise functions.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205