blob: b0177ab8b6189ad2dcd2d88163cece74952a69df [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +00002 * 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/CLArithmeticAdditionKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010027#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30using namespace arm_compute;
31
Giorgio Arena70623822017-11-27 15:50:10 +000032namespace
33{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000034constexpr unsigned int num_elems_processed_per_iteration = 16;
35
36Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy)
Giorgio Arena70623822017-11-27 15:50:10 +000037{
38 ARM_COMPUTE_UNUSED(policy);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010039 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input1);
Diego Lopez Recas0021d752017-12-18 14:42:56 +000040 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010041 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input2);
Diego Lopez Recas0021d752017-12-18 14:42:56 +000042 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
43
44 const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
45
46 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(&input1, &input2);
Giorgio Arena70623822017-11-27 15:50:10 +000048
49 // Validate in case of configured output
Diego Lopez Recas0021d752017-12-18 14:42:56 +000050 if(output.total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000051 {
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010052 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output);
Diego Lopez Recas0021d752017-12-18 14:42:56 +000053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
54 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 +000055 "Output can only be U8 if both inputs are U8");
Diego Lopez Recas0021d752017-12-18 14:42:56 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
57 "Wrong shape for output");
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(&input1, &output);
Giorgio Arena70623822017-11-27 15:50:10 +000059 }
60
Georgios Pinitas631c41a2017-12-06 11:53:03 +000061 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000062}
63
Diego Lopez Recas0021d752017-12-18 14:42:56 +000064std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
Giorgio Arena70623822017-11-27 15:50:10 +000065{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000066 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2);
67 const TensorShape &out_shape = broadcast_pair.first;
68 const ValidRegion &valid_region = broadcast_pair.second;
Giorgio Arena70623822017-11-27 15:50:10 +000069
Diego Lopez Recas0021d752017-12-18 14:42:56 +000070 // Auto initialize output if not initialized
71 {
72 set_shape_if_empty(output, out_shape);
Giorgio Arena70623822017-11-27 15:50:10 +000073
Diego Lopez Recas0021d752017-12-18 14:42:56 +000074 if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16)
75 {
76 set_format_if_unknown(output, Format::S16);
77 }
78 else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16)
79 {
80 set_format_if_unknown(output, Format::F16);
81 }
82 else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32)
83 {
84 set_format_if_unknown(output, Format::F32);
85 }
86 }
Giorgio Arena70623822017-11-27 15:50:10 +000087
Diego Lopez Recas0021d752017-12-18 14:42:56 +000088 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
89 Window win_input1 = win.broadcast_if_dimension_le_one(input1);
90 Window win_input2 = win.broadcast_if_dimension_le_one(input2);
Giorgio Arena70623822017-11-27 15:50:10 +000091
Diego Lopez Recas0021d752017-12-18 14:42:56 +000092 AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration);
93 AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration);
94 AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration);
95
96 bool window_changed = update_window_and_padding(win_input1, input1_access)
97 || update_window_and_padding(win_input2, input2_access)
98 || update_window_and_padding(win, output_access);
Giorgio Arena70623822017-11-27 15:50:10 +000099
100 output_access.set_valid_region(win, valid_region);
101
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000102 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000103 return std::make_pair(err, win);
104}
105} // namespace
106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107CLArithmeticAdditionKernel::CLArithmeticAdditionKernel()
108 : _input1(nullptr), _input2(nullptr), _output(nullptr)
109{
110}
111
112void CLArithmeticAdditionKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, ConvertPolicy policy)
113{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100114 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000115 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy));
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100116
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000117 // Configure kernel window
118 auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info());
119 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121 _input1 = input1;
122 _input2 = input2;
123 _output = output;
124
125 const bool has_float_out = is_data_type_float(output->info()->data_type());
126
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 // Set kernel build options
128 std::set<std::string> build_opts;
129 build_opts.emplace((policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
130 build_opts.emplace("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type()));
131 build_opts.emplace("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type()));
132 build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
Michele Di Giorgio72818342017-07-13 11:03:35 +0100133 if(is_data_type_fixed_point(input1->info()->data_type()))
134 {
135 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input1->info()->fixed_point_position()));
136 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
138 // Create kernel
139 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("arithmetic_add", build_opts));
140
Giorgio Arena70623822017-11-27 15:50:10 +0000141 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142}
143
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000144Status CLArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000145{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000146 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
147
148 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy));
149 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000150
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000151 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000152}
153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154void CLArithmeticAdditionKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
158
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000159 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
160 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
161 const TensorShape &out_shape = _output->info()->tensor_shape();
162
163 bool can_collapse = true;
164 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
165 {
166 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
167 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
168 {
169 can_collapse = (in_shape1[d] == in_shape2[d]);
170 }
171 }
172
173 bool has_collapsed = false;
174 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
175
176 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
177 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
178
179 Window slice = collapsed.first_slice_window_3D();
180 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
181 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000182
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 do
184 {
185 unsigned int idx = 0;
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000186
187 add_3D_tensor_argument(idx, _input1, slice_input1);
188 add_3D_tensor_argument(idx, _input2, slice_input2);
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000189 add_3D_tensor_argument(idx, _output, slice);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000190
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 enqueue(queue, *this, slice);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000192
193 collapsed.slide_window_slice_3D(slice_input1);
194 collapsed.slide_window_slice_3D(slice_input2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000196 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197}
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000198
199BorderSize CLArithmeticAdditionKernel::border_size() const
200{
201 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
202 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
203 return BorderSize(0, border, 0, 0);
204}