fbc76f5e1c4d7bad6c59e4b2b20dced7e5de22a7
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLBinaryLogicalOpKernel.cpp
1 /*
2  * Copyright (c) 2018 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) 2016-2018 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/CL/kernels/CLBinaryLogicalOpKernel.h"
42
43 #include "arm_compute/core/CL/CLHelpers.h"
44 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
45 #include "arm_compute/core/CL/ICLTensor.h"
46 #include "support/StringSupport.h"
47
48 using namespace arm_compute;
49
50 namespace
51 {
52 constexpr unsigned int num_elems_processed_per_iteration = 16;
53
54 Status validate_parameters(const ITensorInfo *input1, const ITensorInfo *input2,
55                            const ITensorInfo *output)
56 {
57   const TensorShape &out_shape =
58       TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
59
60   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QASYMM8);
61   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QASYMM8);
62
63   ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0,
64                                   "Inputs are not broadcast compatible");
65   // Validate in case of configured output
66   if (output->total_size() > 0)
67   {
68     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8,
69                                                          DataType::QASYMM8);
70     ARM_COMPUTE_RETURN_ERROR_ON_MSG(
71         detail::have_different_dimensions(out_shape, output->tensor_shape(), 0),
72         "Wrong shape for output");
73   }
74   return Status{};
75 }
76 } // namespace
77
78 CLBinaryLogicalOpKernel::CLBinaryLogicalOpKernel()
79     : _input1(nullptr), _input2(nullptr), _output(nullptr)
80 {
81 }
82
83 void CLBinaryLogicalOpKernel::configure(const ICLTensor *input1, const ICLTensor *input2,
84                                         ICLTensor *output, BinaryLogicalOperation op)
85 {
86   ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2);
87   ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input1, output);
88   ARM_COMPUTE_ERROR_THROW_ON(validate_parameters(input1->info(), input2->info(), output->info()));
89
90   _input1 = input1;
91   _input2 = input2;
92   _output = output;
93
94   // Create kernel
95   std::string kernel_name = "binary_logical_op";
96   std::set<std::string> build_opts;
97   build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input1->info()->data_type())));
98
99   int op_code = 0;
100   switch (op)
101   {
102     case BinaryLogicalOperation::AND:
103       op_code = 1;
104       break;
105     case BinaryLogicalOperation::OR:
106       op_code = 2;
107       break;
108     default:
109       throw std::runtime_error("Operation not supported, yet");
110   }
111
112   build_opts.emplace(("-DOP_CODE=" + support::cpp11::to_string(op_code)));
113   build_opts.emplace(
114       ("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
115
116   _kernel =
117       static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts));
118
119   const std::pair<TensorShape, ValidRegion> broadcast_pair =
120       ITensorInfo::broadcast_shape_and_valid_region(*input1->info(), *input2->info());
121
122   const ValidRegion &valid_region = broadcast_pair.second;
123
124   Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
125   Window win_input1 = win.broadcast_if_dimension_le_one(*input1->info());
126   Window win_input2 = win.broadcast_if_dimension_le_one(*input2->info());
127
128   AccessWindowHorizontal input1_access(input1->info(), 0, num_elems_processed_per_iteration);
129   AccessWindowHorizontal input2_access(input2->info(), 0, num_elems_processed_per_iteration);
130   AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
131
132   update_window_and_padding(win_input1, input1_access) ||
133       update_window_and_padding(win_input2, input2_access) ||
134       update_window_and_padding(win, output_access);
135
136   output_access.set_valid_region(win, valid_region);
137
138   ICLKernel::configure_internal(win);
139 }
140
141 void CLBinaryLogicalOpKernel::run(const Window &window, cl::CommandQueue &queue)
142 {
143   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
145
146   const TensorShape &in_shape1 = _input1->info()->tensor_shape();
147   const TensorShape &in_shape2 = _input2->info()->tensor_shape();
148   const TensorShape &out_shape = _output->info()->tensor_shape();
149
150   bool can_collapse = true;
151   if (std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
152   {
153     can_collapse =
154         (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
155     for (size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
156     {
157       can_collapse = (in_shape1[d] == in_shape2[d]);
158     }
159   }
160
161   bool has_collapsed = false;
162   Window collapsed =
163       can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed)
164                    : window;
165
166   const TensorShape &in_shape1_collapsed =
167       has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
168   const TensorShape &in_shape2_collapsed =
169       has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
170
171   Window slice = collapsed.first_slice_window_3D();
172   Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
173   Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
174
175   do
176   {
177     unsigned int idx = 0;
178     add_3D_tensor_argument(idx, _input1, slice_input1);
179     add_3D_tensor_argument(idx, _input2, slice_input2);
180     add_3D_tensor_argument(idx, _output, slice);
181
182     enqueue(queue, *this, slice);
183
184     collapsed.slide_window_slice_3D(slice_input1);
185     collapsed.slide_window_slice_3D(slice_input2);
186   } while (collapsed.slide_window_slice_3D(slice));
187 }
188
189 BorderSize CLBinaryLogicalOpKernel::border_size() const
190 {
191   const unsigned int replicateSize =
192       _output->info()->dimension(0) -
193       std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
194   const unsigned int border =
195       std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
196   return BorderSize(0, border, 0, 0);
197 }