Compute Library
 21.11
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"
30 #include "arm_compute/core/Types.h"
32 #include "src/core/CPP/Validate.h"
36 
37 #include <arm_neon.h>
38 #include <map>
39 #include <string>
40 
41 namespace arm_compute
42 {
43 namespace
44 {
45 template <typename ScalarType, typename VectorType>
46 void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window,
47  const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *))
48 {
49  Window win = window;
50  win.set(Window::DimX, Window::Dimension(0, 1, 1));
51 
52  Iterator condition(cond, win);
53  Iterator input1(in1, win);
54  Iterator input2(in2, win);
55  Iterator output(out, win);
56 
57  execute_window_loop(win, [&](const Coordinates &)
58  {
59  auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
60  const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr());
61  const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
62  const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
63 
64  int x = window_start_x;
65  for(; x <= limit; x += window_step_x)
66  {
67  const auto c = (*condition_conversion)(condition_ptr + x);
68  const auto a = wrapper::vloadq(input1_ptr + x);
69  const auto b = wrapper::vloadq(input2_ptr + x);
70  wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b));
71  }
72  for(; x < window_end_x; ++x)
73  {
74  const auto c = *(condition_ptr + x);
75  const auto a = *(input1_ptr + x);
76  const auto b = *(input2_ptr + x);
77  *(output_ptr + x) = static_cast<bool>(c) ? a : b;
78  }
79  },
80  condition, input1, input2, output);
81 }
82 
83 template <typename ScalarType, typename VectorType>
84 void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
85 {
86  const auto window_step_x = 16 / sizeof(ScalarType);
87  const auto window_start_x = static_cast<int>(window.x().start());
88  const auto window_end_x = static_cast<int>(window.x().end());
89 
90  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
91  {
92  static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag());
93  return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero);
94  });
95 }
96 
97 template <typename ScalarType, typename VectorType>
98 void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
99 {
100  const auto window_step_x = 16 / sizeof(ScalarType);
101  const auto window_start_x = static_cast<int>(window.x().start());
102  const auto window_end_x = static_cast<int>(window.x().end());
103 
104  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
105  {
106  static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag());
107  return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero);
108  });
109 }
110 
111 template <typename ScalarType, typename VectorType>
112 void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
113 {
114  const auto window_step_x = 16 / sizeof(ScalarType);
115  const auto window_start_x = static_cast<int>(window.x().start());
116  const auto window_end_x = static_cast<int>(window.x().end());
117 
118  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
119  {
120  static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag());
122  });
123 }
124 
125 template <typename ScalarType>
126 void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
127 {
128  ARM_COMPUTE_UNUSED(window);
129 
130  auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer());
131  const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer());
132  const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer());
133  const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer());
134 
135  const int outer_size = cond->info()->total_size() / cond->info()->element_size();
136  const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size;
137  int offset = 0;
138  const int step = 16 / in1->info()->element_size();
139 
140  for(int i = 0; i < outer_size; ++i)
141  {
142  int x = offset;
143  const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr;
144  for(; x <= offset + inner_size - step; x += step)
145  {
146  wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x));
147  }
148  if(x <= offset + inner_size - (step / 2))
149  {
150  wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x));
151  x += step / 2;
152  }
153  for(; x < offset + inner_size; ++x)
154  {
155  *(output_ptr + x) = *(input_ptr + x);
156  }
157  offset += inner_size;
158  }
159 }
160 } // namespace
161 
163  : _function(nullptr), _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
164 {
165 }
166 
167 void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output)
168 {
169  ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
170 
171  // Auto initialize output if not initialized
172  auto_init_if_empty(*output->info(), x->info()->tensor_shape(), 1, x->info()->data_type());
173  ARM_COMPUTE_ERROR_THROW_ON(validate(c->info(), x->info(), y->info(), output->info()));
174 
175  _c = c;
176  _x = x;
177  _y = y;
178  _output = output;
179  _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
180 
181  std::string function_to_call("op_");
182  function_to_call += string_from_data_type(x->info()->data_type());
183 
184  static std::map<std::string, SelectFunction *> map_function;
185 
186  if(_has_same_rank)
187  {
188  map_function =
189  {
190  { "op_S8", &select_op_8<int8_t, uint8x16_t> },
191  { "op_S16", &select_op_16<int16_t, uint16x8_t> },
192  { "op_S32", &select_op_32<int32_t, uint32x4_t> },
193  { "op_U8", &select_op_8<uint8_t, uint8x16_t> },
194  { "op_U16", &select_op_16<uint16_t, uint16x8_t> },
195  { "op_U32", &select_op_32<uint32_t, uint32x4_t> },
196  { "op_F32", &select_op_32<float, uint32x4_t> }
197  };
198 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
199  map_function["op_F16"] = &select_op_16<float16_t, uint16x8_t>;
200 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
201  }
202  else
203  {
204  map_function =
205  {
206  { "op_S8", &select_op_not_same_rank<int8_t> },
207  { "op_S16", &select_op_not_same_rank<int16_t> },
208  { "op_S32", &select_op_not_same_rank<int32_t> },
209  { "op_U8", &select_op_not_same_rank<uint8_t> },
210  { "op_U16", &select_op_not_same_rank<uint16_t> },
211  { "op_U32", &select_op_not_same_rank<uint32_t> },
212  { "op_F32", &select_op_not_same_rank<float> }
213  };
214 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
215  map_function["op_F16"] = &select_op_not_same_rank<float16_t>;
216 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
217  }
218 
219  auto it = map_function.find(function_to_call);
220 
221  if(it != map_function.end())
222  {
223  _function = it->second;
224  }
225 
226  Window win = calculate_max_window(*x->info());
227  INEKernel::configure(win);
228 }
229 
230 Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
231 {
238 
239  const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
240  ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape()));
241  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])));
242 
243  if(output != nullptr && output->total_size() != 0)
244  {
247  }
248 
249  return Status{};
250 }
251 
252 void NESelectKernel::run(const Window &window, const ThreadInfo &info)
253 {
254  ARM_COMPUTE_UNUSED(info);
257  ARM_COMPUTE_ERROR_ON(_function == nullptr);
258  _function(_c, _x, _y, _output, window);
259 }
260 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1069
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:115
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 CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
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:915
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:158
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:439
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:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
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:157
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:201