blob: 95d201104d097c8f485a7e5e1fea39edca5833f6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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#include "arm_compute/core/CL/kernels/CLArithmeticSubtractionKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Window.h"
35
36#include <set>
37#include <string>
38
Georgios Pinitascbf39c62018-09-10 15:07:45 +010039namespace arm_compute
40{
Giorgio Arena70623822017-11-27 15:50:10 +000041namespace
42{
Georgios Pinitascbf39c62018-09-10 15:07:45 +010043constexpr unsigned int num_elems_processed_per_iteration = 16;
44
45Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy)
Giorgio Arena70623822017-11-27 15:50:10 +000046{
47 ARM_COMPUTE_UNUSED(policy);
Georgios Pinitascbf39c62018-09-10 15:07:45 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input1);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
50 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input2);
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
52 const bool is_qasymm = is_data_type_quantized_asymmetric(input1.data_type()) || is_data_type_quantized_asymmetric(input2.data_type());
53 if(is_qasymm)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2);
56 }
57
58 const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
59
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Giorgio Arena70623822017-11-27 15:50:10 +000061
62 // Validate in case of configured output
Georgios Pinitascbf39c62018-09-10 15:07:45 +010063 if(output.total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000064 {
Georgios Pinitascbf39c62018-09-10 15:07:45 +010065 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output);
66 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output.data_type() == DataType::U8) && ((input1.data_type() != DataType::U8) || (input2.data_type() != DataType::U8)),
Giorgio Arena70623822017-11-27 15:50:10 +000068 "Output can only be U8 if both inputs are U8");
Georgios Pinitascbf39c62018-09-10 15:07:45 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
70 "Wrong shape for output");
71 if(is_qasymm)
72 {
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output);
74 }
Giorgio Arena70623822017-11-27 15:50:10 +000075 }
76
Georgios Pinitas631c41a2017-12-06 11:53:03 +000077 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000078}
79
Georgios Pinitascbf39c62018-09-10 15:07:45 +010080std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
Giorgio Arena70623822017-11-27 15:50:10 +000081{
Georgios Pinitascbf39c62018-09-10 15:07:45 +010082 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2);
83 const TensorShape &out_shape = broadcast_pair.first;
84 const ValidRegion &valid_region = broadcast_pair.second;
Giorgio Arena70623822017-11-27 15:50:10 +000085
Georgios Pinitascbf39c62018-09-10 15:07:45 +010086 // Auto initialize output if not initialized
87 {
88 set_shape_if_empty(output, out_shape);
Giorgio Arena70623822017-11-27 15:50:10 +000089
Georgios Pinitascbf39c62018-09-10 15:07:45 +010090 if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16)
91 {
92 set_format_if_unknown(output, Format::S16);
93 }
94 else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16)
95 {
96 set_format_if_unknown(output, Format::F16);
97 }
98 else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32)
99 {
100 set_format_if_unknown(output, Format::F32);
101 }
102 }
Giorgio Arena70623822017-11-27 15:50:10 +0000103
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100104 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
105 Window win_input1 = win.broadcast_if_dimension_le_one(input1);
106 Window win_input2 = win.broadcast_if_dimension_le_one(input2);
107
108 AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration);
109 AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration);
110 AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration);
111
112 bool window_changed = update_window_and_padding(win_input1, input1_access)
113 || update_window_and_padding(win_input2, input2_access)
114 || update_window_and_padding(win, output_access);
Giorgio Arena70623822017-11-27 15:50:10 +0000115
116 output_access.set_valid_region(win, valid_region);
117
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000118 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000119 return std::make_pair(err, win);
120}
121} // namespace
122
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123CLArithmeticSubtractionKernel::CLArithmeticSubtractionKernel()
124 : _input1(nullptr), _input2(nullptr), _output(nullptr)
125{
126}
127
128void CLArithmeticSubtractionKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, ConvertPolicy policy)
129{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100130 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100131 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy));
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100132
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100133 // Configure kernel window
134 auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info());
135 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
137 _input1 = input1;
138 _input2 = input2;
139 _output = output;
140
141 bool has_float_out = is_data_type_float(output->info()->data_type());
142
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100143 // Setup kernel
144 std::string kernel_name = "arithmetic_sub";
145
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 // Set kernel build options
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100147 CLBuildOptions build_opts;
148 build_opts.add_option_if_else(policy == ConvertPolicy::WRAP || has_float_out, "-DWRAP", "-DSATURATE");
149 build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type()));
150 build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type()));
151 build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
152 if(is_data_type_quantized_asymmetric(input1->info()->data_type()))
153 {
154 build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(input1->info()->quantization_info().offset));
155 build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(input2->info()->quantization_info().offset));
156 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(output->info()->quantization_info().offset));
157 build_opts.add_option("-DSCALE_IN1=" + support::cpp11::to_string(input1->info()->quantization_info().scale));
158 build_opts.add_option("-DSCALE_IN2=" + support::cpp11::to_string(input2->info()->quantization_info().scale));
159 build_opts.add_option("-DSCALE_OUT=" + support::cpp11::to_string(output->info()->quantization_info().scale));
160 kernel_name += "_quantized";
161 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162
163 // Create kernel
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100164 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 // Configure kernel window
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100167 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168}
169
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000170Status CLArithmeticSubtractionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000171{
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100172 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
173
174 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy));
175 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000176
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000177 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000178}
179
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180void CLArithmeticSubtractionKernel::run(const Window &window, cl::CommandQueue &queue)
181{
182 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
183 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
184
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100185 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
186 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
187 const TensorShape &out_shape = _output->info()->tensor_shape();
188
189 // Collapse only if broadcast dimensions is less than 2, or in case of no broadcasting
190 bool can_collapse = true;
191 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
192 {
193 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
194 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
195 {
196 can_collapse = (in_shape1[d] == in_shape2[d]);
197 }
198 }
199
200 bool has_collapsed = false;
201 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
202
203 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
204 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
205
206 Window slice = collapsed.first_slice_window_3D();
207 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
208 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
210 do
211 {
212 unsigned int idx = 0;
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100213
214 add_3D_tensor_argument(idx, _input1, slice_input1);
215 add_3D_tensor_argument(idx, _input2, slice_input2);
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000216 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100217
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 enqueue(queue, *this, slice);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100219
220 collapsed.slide_window_slice_3D(slice_input1);
221 collapsed.slide_window_slice_3D(slice_input2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000223 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224}
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100225
226BorderSize CLArithmeticSubtractionKernel::border_size() const
227{
228 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
229 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
230 return BorderSize(0, border, 0, 0);
231}
232} // namespace arm_compute