2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright (c) 2016-2018 ARM Limited.
20 * SPDX-License-Identifier: MIT
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:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
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
41 #include "arm_compute/core/CL/kernels/CLBinaryLogicalOpKernel.h"
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"
48 using namespace arm_compute;
52 constexpr unsigned int num_elems_processed_per_iteration = 16;
54 Status validate_parameters(const ITensorInfo *input1, const ITensorInfo *input2,
55 const ITensorInfo *output)
57 const TensorShape &out_shape =
58 TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
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);
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)
68 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8,
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
71 detail::have_different_dimensions(out_shape, output->tensor_shape(), 0),
72 "Wrong shape for output");
78 CLBinaryLogicalOpKernel::CLBinaryLogicalOpKernel()
79 : _input1(nullptr), _input2(nullptr), _output(nullptr)
83 void CLBinaryLogicalOpKernel::configure(const ICLTensor *input1, const ICLTensor *input2,
84 ICLTensor *output, BinaryLogicalOperation op)
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()));
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())));
102 case BinaryLogicalOperation::AND:
105 case BinaryLogicalOperation::OR:
109 throw std::runtime_error("Operation not supported, yet");
112 build_opts.emplace(("-DOP_CODE=" + support::cpp11::to_string(op_code)));
114 ("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
117 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts));
119 const std::pair<TensorShape, ValidRegion> broadcast_pair =
120 ITensorInfo::broadcast_shape_and_valid_region(*input1->info(), *input2->info());
122 const ValidRegion &valid_region = broadcast_pair.second;
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());
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);
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);
136 output_access.set_valid_region(win, valid_region);
138 ICLKernel::configure_internal(win);
141 void CLBinaryLogicalOpKernel::run(const Window &window, cl::CommandQueue &queue)
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
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();
150 bool can_collapse = true;
151 if (std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
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++)
157 can_collapse = (in_shape1[d] == in_shape2[d]);
161 bool has_collapsed = false;
163 can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed)
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;
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);
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);
182 enqueue(queue, *this, slice);
184 collapsed.slide_window_slice_3D(slice_input1);
185 collapsed.slide_window_slice_3D(slice_input2);
186 } while (collapsed.slide_window_slice_3D(slice));
189 BorderSize CLBinaryLogicalOpKernel::border_size() const
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);