32d7d6237c88e1ba7e8e18850126f012cf9aecf9
[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]), elementwise_logic_op<op>(a.val[1], b.val[1]),
107       elementwise_logic_op<op>(a.val[2], b.val[2]), elementwise_logic_op<op>(a.val[3], b.val[3]),
108   }};
109   return out;
110 }
111
112 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
113 inline VectorType elementwise_logic_op_broadcast(const VectorType &a,
114                                                  const ScalarType &broadcast_value,
115                                                  const bool reorder)
116 {
117   VectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag());
118   return elementwise_logic_op<op>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector);
119 }
120
121 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
122 inline int elementwise_logic_op_loop(int window_start_x, int window_end_x, int window_step_x,
123                                      const ScalarType *input1_ptr, const ScalarType *input2_ptr,
124                                      ScalarType *output_ptr)
125 {
126   int x = window_start_x;
127   for (; x <= (window_end_x - window_step_x); x += window_step_x)
128   {
129     const auto a = wrapper::vloadq(input1_ptr + x);
130     const auto b = wrapper::vloadq(input2_ptr + x);
131     wrapper::vstore(output_ptr + x, elementwise_logic_op<op>(a, b));
132   }
133   return x;
134 }
135
136 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
137 inline int elementwise_logic_op_broadcast_loop(int window_start_x, int window_end_x,
138                                                int window_step_x,
139                                                const ScalarType *non_broadcast_input_ptr,
140                                                const ScalarType &broadcast_value,
141                                                ScalarType *output_ptr, const bool reorder)
142 {
143   int x = window_start_x;
144   for (; x <= (window_end_x - window_step_x); x += window_step_x)
145   {
146     const auto a = wrapper::vloadq((non_broadcast_input_ptr + x));
147     wrapper::vstore(output_ptr + x,
148                     elementwise_logic_op_broadcast<op>(a, broadcast_value, reorder));
149   }
150   return x;
151 }
152
153 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
154 void elementwise_logic_op(const ITensor *in1, const ITensor *in2, ITensor *out,
155                           const Window &window)
156 {
157   elementwise_op(in1, in2, out, window, &elementwise_logic_op_scalar<op, ScalarType>,
158                  &elementwise_logic_op_broadcast_loop<op, ScalarType, VectorType>,
159                  &elementwise_logic_op_loop<op, ScalarType, VectorType>);
160 }
161
162 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> configure_func(
163     const ITensor *input1, const ITensor *input2, ITensor *output,
164     std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function)
165 {
166   std::string function_to_call("op_");
167   function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
168   function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
169   function_to_call += string_from_data_type(output->info()->data_type());
170
171   auto it = map_function.find(function_to_call);
172
173   if (it != map_function.end())
174   {
175     auto func = it->second;
176     return [func](const ITensor *input1, const ITensor *input2, ITensor *output,
177                   const Window &window) { func(input1, input2, output, window); };
178   }
179   return nullptr;
180 }
181
182 template <BinaryLogicalOperation op>
183 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)>
184 configure_logic_func(const ITensor *input1, const ITensor *input2, ITensor *output)
185 {
186   static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = {
187       {"op_U8_U8_U8", &elementwise_logic_op<op, uint8_t, uint8x16_t>},
188       {"op_QASYMM8_QASYMM8_QASYMM8", &elementwise_logic_op<op, uint8_t, uint8x16_t>}};
189
190   return configure_func(input1, input2, output, map_function);
191 }
192
193 void NEBinaryLogicalOperationKernel::configure(BinaryLogicalOperation op, const ITensor *input1,
194                                                const ITensor *input2, ITensor *output)
195 {
196   ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info()));
197   configure_common(input1, input2, output);
198   switch (op)
199   {
200     case BinaryLogicalOperation::AND:
201       _function = configure_logic_func<BinaryLogicalOperation::AND>(input1, input2, output);
202       break;
203     case BinaryLogicalOperation::OR:
204       _function = configure_logic_func<BinaryLogicalOperation::OR>(input1, input2, output);
205       break;
206     default:
207       ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
208   }
209 }
210
211 Status NEBinaryLogicalOperationKernel::validate_arguments(const ITensorInfo &input1,
212                                                           const ITensorInfo &input2,
213                                                           const ITensorInfo &output)
214 {
215   // Validate in case of configured output
216   if (output.total_size() > 0)
217   {
218     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8,
219                                                          DataType::QASYMM8);
220   }
221   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8);
222   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8);
223   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2);
224
225   const TensorShape out_shape =
226       TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
227
228   ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0,
229                                   "Inputs are not broadcast compatible");
230
231   // Validate in case of configured output
232   if (output.total_size() > 0)
233   {
234     ARM_COMPUTE_RETURN_ERROR_ON_MSG(
235         detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
236         "Wrong shape for output");
237   }
238
239   return Status{};
240 }
241
242 Status NEBinaryLogicalOperationKernel::validate(BinaryLogicalOperation op,
243                                                 const ITensorInfo *input1,
244                                                 const ITensorInfo *input2,
245                                                 const ITensorInfo *output)
246 {
247   ARM_COMPUTE_UNUSED(op);
248   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
249   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output));
250   return Status{};
251 }
252
253 } // namespace arm_compute