Compute Library
 22.11
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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  */
24 
27 #include "src/core/NEON/NEAsymm.h"
28 
29 #include <arm_neon.h>
30 #include <map>
31 #include <string>
32 
33 namespace arm_compute
34 {
35 namespace cpu
36 {
37 template <typename ScalarType, typename VectorType>
38 void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window,
39  const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *))
40 {
41  Window win = window;
42  win.set(Window::DimX, Window::Dimension(0, 1, 1));
43 
44  Iterator condition(cond, win);
45  Iterator input1(in1, win);
46  Iterator input2(in2, win);
47  Iterator output(out, win);
48 
49  execute_window_loop(win, [&](const Coordinates &)
50  {
51  auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
52  const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr());
53  const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
54  const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
55 
56  int x = window_start_x;
57  for(; x <= limit; x += window_step_x)
58  {
59  const auto c = (*condition_conversion)(condition_ptr + x);
60  const auto a = wrapper::vloadq(input1_ptr + x);
61  const auto b = wrapper::vloadq(input2_ptr + x);
62  wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b));
63  }
64  for(; x < window_end_x; ++x)
65  {
66  const auto c = *(condition_ptr + x);
67  const auto a = *(input1_ptr + x);
68  const auto b = *(input2_ptr + x);
69  *(output_ptr + x) = static_cast<bool>(c) ? a : b;
70  }
71  },
72  condition, input1, input2, output);
73 }
74 
75 template <typename ScalarType, typename VectorType>
76 void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
77 {
78  const auto window_step_x = 16 / sizeof(ScalarType);
79  const auto window_start_x = static_cast<int>(window.x().start());
80  const auto window_end_x = static_cast<int>(window.x().end());
81 
82  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
83  {
84  static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag());
85  return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero);
86  });
87 }
88 
89 template <typename ScalarType, typename VectorType>
90 void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
91 {
92  const auto window_step_x = 16 / sizeof(ScalarType);
93  const auto window_start_x = static_cast<int>(window.x().start());
94  const auto window_end_x = static_cast<int>(window.x().end());
95 
96  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
97  {
98  static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag());
99  return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero);
100  });
101 }
102 
103 template <typename ScalarType, typename VectorType>
104 void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
105 {
106  const auto window_step_x = 16 / sizeof(ScalarType);
107  const auto window_start_x = static_cast<int>(window.x().start());
108  const auto window_end_x = static_cast<int>(window.x().end());
109 
110  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
111  {
112  static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag());
114  });
115 }
116 
117 template <typename ScalarType>
118 void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
119 {
120  ARM_COMPUTE_UNUSED(window);
121 
122  auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer());
123  const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer());
124  const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer());
125  const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer());
126 
127  const int outer_size = cond->info()->total_size() / cond->info()->element_size();
128  const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size;
129  int offset = 0;
130  const int step = 16 / in1->info()->element_size();
131 
132  for(int i = 0; i < outer_size; ++i)
133  {
134  int x = offset;
135  const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr;
136  for(; x <= offset + inner_size - step; x += step)
137  {
138  wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x));
139  }
140  if(x <= offset + inner_size - (step / 2))
141  {
142  wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x));
143  x += step / 2;
144  }
145  for(; x < offset + inner_size; ++x)
146  {
147  *(output_ptr + x) = *(input_ptr + x);
148  }
149  offset += inner_size;
150  }
151 }
152 
153 template void select_op_32<float, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
154 
155 template void select_op_not_same_rank<float>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
156 
157 template void select_op_8<int8_t, uint8x16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
158 
159 template void select_op_16<int16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
160 
161 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
162 template void select_op_16<float16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
163 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
164 
165 template void select_op_32<int32_t, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
166 
167 template void select_op_not_same_rank<int8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
168 
169 template void select_op_not_same_rank<int16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
170 
171 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
172 template void select_op_not_same_rank<float16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
173 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
174 
175 template void select_op_not_same_rank<int32_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
176 
177 template void select_op_8<uint8_t, uint8x16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
178 
179 template void select_op_16<uint16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
180 
181 template void select_op_32<uint32_t, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
182 
183 template void select_op_not_same_rank<uint8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
184 
185 template void select_op_not_same_rank<uint16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
186 
187 template void select_op_not_same_rank<uint32_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
188 
189 } // namespace cpu
190 
191 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1084
void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Definition: impl.cpp:76
template void select_op_32< float, uint32x4_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Condition condition(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:697
SimpleTensor< float > b
Definition: DFT.cpp:157
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Definition: impl.cpp:104
void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Definition: impl.cpp:90
template void select_op_not_same_rank< uint16_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
template void select_op_not_same_rank< uint8_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
template void select_op_32< int32_t, uint32x4_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
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
template void select_op_16< uint16_t, uint16x8_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
template void select_op_not_same_rank< uint32_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
template void select_op_not_same_rank< int8_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType(*condition_conversion)(const uint8_t *))
Definition: impl.cpp:38
template void select_op_not_same_rank< int32_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
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
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
constexpr int step
Definition: fp32.cpp:35
uint8x8_t vload(const uint8_t *ptr)
Definition: load.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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
template void select_op_8< uint8_t, uint8x16_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
template void select_op_8< int8_t, uint8x16_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
template void select_op_not_same_rank< float >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:102
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
uint16x8_t vmovl(const uint8x8_t &a)
Definition: movl.h:39
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:97
Describe a multidimensional execution window.
Definition: Window.h:39
template void select_op_16< int16_t, uint16x8_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
Definition: impl.cpp:118
template void select_op_32< uint32_t, uint32x4_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:159
template void select_op_not_same_rank< int16_t >(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)