Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / NEON / kernels / NEBinaryLogicalOperationKernel.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2018-2019 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40
41 #include "arm_compute/core/NEON/kernels/NEBinaryLogicalOperationKernel.h"
42
43 #include "arm_compute/core/Error.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/core/ITensor.h"
46 #include "arm_compute/core/NEON/wrapper/wrapper.h"
47 #include "arm_compute/core/NEON/NEElementwiseOperationFuncs.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Validate.h"
50
51 #include <algorithm>
52 #include <arm_neon.h>
53 #include <map>
54 #include <string>
55
56 namespace arm_compute
57 {
58 class Coordinates;
59 } // namespace arm_compute
60
61 namespace arm_compute
62 {
63
64 template <BinaryLogicalOperation op, typename ScalarType>
65 inline ScalarType elementwise_logic_op_scalar(const ScalarType &a, const ScalarType &b)
66 {
67   auto res = ScalarType(0);
68
69   switch (op)
70   {
71     case BinaryLogicalOperation::AND:
72       res = a & b;
73       break;
74     case BinaryLogicalOperation::OR:
75       res = a | b;
76       break;
77     default:
78       ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
79   }
80   return res;
81 }
82
83 template <BinaryLogicalOperation op, typename VectorType>
84 inline VectorType elementwise_logic_op(const VectorType &a, const VectorType &b)
85 {
86   VectorType res = {0, 0, 0, 0};
87
88   switch (op)
89   {
90     case BinaryLogicalOperation::AND:
91       res = wrapper::vand(a, b);
92       break;
93     case BinaryLogicalOperation::OR:
94       res = wrapper::vorr(a, b);
95       break;
96     default:
97       ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
98   }
99   return res;
100 }
101
102 template <BinaryLogicalOperation op>
103 inline uint8x16x4_t elementwise_logic_op(const uint8x16x4_t &a, const uint8x16x4_t &b)
104 {
105   uint8x16x4_t out = {{
106     elementwise_logic_op<op>(a.val[0], b.val[0]),
107     elementwise_logic_op<op>(a.val[1], b.val[1]),
108     elementwise_logic_op<op>(a.val[2], b.val[2]),
109     elementwise_logic_op<op>(a.val[3], b.val[3]),
110   }};
111   return out;
112 }
113
114 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
115 inline VectorType elementwise_logic_op_broadcast(const VectorType &a,
116                                                  const ScalarType &broadcast_value,
117                                                  const bool reorder)
118 {
119   VectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag());
120   return elementwise_logic_op<op>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector);
121 }
122
123 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
124 inline int elementwise_logic_op_loop(int window_start_x, int window_end_x, int window_step_x,
125                                      const ScalarType *input1_ptr, const ScalarType *input2_ptr,
126                                      ScalarType *output_ptr)
127 {
128   int x = window_start_x;
129   for (; x <= (window_end_x - window_step_x); x += window_step_x)
130   {
131     const auto a = wrapper::vloadq(input1_ptr + x);
132     const auto b = wrapper::vloadq(input2_ptr + x);
133     wrapper::vstore(output_ptr + x, elementwise_logic_op<op>(a, b));
134   }
135   return x;
136 }
137
138 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
139 inline int elementwise_logic_op_broadcast_loop(int window_start_x, int window_end_x,
140                                                int window_step_x,
141                                                const ScalarType *non_broadcast_input_ptr,
142                                                const ScalarType &broadcast_value,
143                                                ScalarType *output_ptr, const bool reorder)
144 {
145   int x = window_start_x;
146   for (; x <= (window_end_x - window_step_x); x += window_step_x)
147   {
148     const auto a = wrapper::vloadq((non_broadcast_input_ptr + x));
149     wrapper::vstore(output_ptr + x,
150                     elementwise_logic_op_broadcast<op>(a, broadcast_value, reorder));
151   }
152   return x;
153 }
154
155 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
156 void elementwise_logic_op(const ITensor *in1, const ITensor *in2, ITensor *out,
157                           const Window &window)
158 {
159   elementwise_op(in1, in2, out, window, &elementwise_logic_op_scalar<op, ScalarType>,
160                  &elementwise_logic_op_broadcast_loop<op, ScalarType, VectorType>,
161                  &elementwise_logic_op_loop<op, ScalarType, VectorType>);
162 }
163
164 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> configure_func(
165   const ITensor *input1, const ITensor *input2, ITensor *output,
166   std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function)
167 {
168   std::string function_to_call("op_");
169   function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
170   function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
171   function_to_call += string_from_data_type(output->info()->data_type());
172
173   auto it = map_function.find(function_to_call);
174
175   if (it != map_function.end())
176   {
177     auto func = it->second;
178     return [func](const ITensor *input1, const ITensor *input2, ITensor *output,
179                   const Window &window) { func(input1, input2, output, window); };
180   }
181   return nullptr;
182 }
183
184 template <BinaryLogicalOperation op>
185 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)>
186 configure_logic_func(const ITensor *input1, const ITensor *input2, ITensor *output)
187 {
188   static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = {
189     {"op_U8_U8_U8", &elementwise_logic_op<op, uint8_t, uint8x16_t>},
190     {"op_QASYMM8_QASYMM8_QASYMM8", &elementwise_logic_op<op, uint8_t, uint8x16_t>}};
191
192   return configure_func(input1, input2, output, map_function);
193 }
194
195 void NEBinaryLogicalOperationKernel::configure(BinaryLogicalOperation op, const ITensor *input1,
196                                                const ITensor *input2, ITensor *output)
197 {
198   ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info()));
199   configure_common(input1, input2, output);
200   switch (op)
201   {
202     case BinaryLogicalOperation::AND:
203       _function = configure_logic_func<BinaryLogicalOperation::AND>(input1, input2, output);
204       break;
205     case BinaryLogicalOperation::OR:
206       _function = configure_logic_func<BinaryLogicalOperation::OR>(input1, input2, output);
207       break;
208     default:
209       ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
210   }
211 }
212
213 Status NEBinaryLogicalOperationKernel::validate_arguments(const ITensorInfo &input1,
214                                                           const ITensorInfo &input2,
215                                                           const ITensorInfo &output)
216 {
217   // Validate in case of configured output
218   if (output.total_size() > 0)
219   {
220     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8,
221                                                          DataType::QASYMM8);
222   }
223   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8);
224   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8);
225   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2);
226
227   const TensorShape out_shape =
228     TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
229
230   ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0,
231                                   "Inputs are not broadcast compatible");
232
233   // Validate in case of configured output
234   if (output.total_size() > 0)
235   {
236     ARM_COMPUTE_RETURN_ERROR_ON_MSG(
237       detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
238       "Wrong shape for output");
239   }
240
241   return Status{};
242 }
243
244 Status NEBinaryLogicalOperationKernel::validate(BinaryLogicalOperation op,
245                                                 const ITensorInfo *input1,
246                                                 const ITensorInfo *input2,
247                                                 const ITensorInfo *output)
248 {
249   ARM_COMPUTE_UNUSED(op);
250   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
251   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output));
252   return Status{};
253 }
254
255 } // namespace arm_compute