Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 32 | namespace |
| 33 | { |
Michele Di Giorgio | a1422fb | 2018-10-24 12:20:19 +0100 | [diff] [blame] | 34 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 35 | |
| 36 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 37 | { |
| 38 | ARM_COMPUTE_UNUSED(policy); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 39 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 40 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input2); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32); |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 43 | |
| 44 | const bool is_qasymm = is_data_type_quantized_asymmetric(input1.data_type()) || is_data_type_quantized_asymmetric(input2.data_type()); |
| 45 | if(is_qasymm) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2); |
| 48 | } |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 49 | |
| 50 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 51 | |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 53 | |
| 54 | // Validate in case of configured output |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 55 | if(output.total_size() > 0) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 56 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output.data_type() == DataType::U8) && ((input1.data_type() != DataType::U8) || (input2.data_type() != DataType::U8)), |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 60 | "Output can only be U8 if both inputs are U8"); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 62 | "Wrong shape for output"); |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 63 | if(is_qasymm) |
| 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output); |
| 66 | } |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 69 | return Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 72 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 73 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 74 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); |
| 75 | const TensorShape &out_shape = broadcast_pair.first; |
| 76 | const ValidRegion &valid_region = broadcast_pair.second; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 77 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 78 | // Auto initialize output if not initialized |
| 79 | { |
| 80 | set_shape_if_empty(output, out_shape); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 81 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 82 | if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16) |
| 83 | { |
| 84 | set_format_if_unknown(output, Format::S16); |
| 85 | } |
| 86 | else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16) |
| 87 | { |
| 88 | set_format_if_unknown(output, Format::F16); |
| 89 | } |
| 90 | else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32) |
| 91 | { |
| 92 | set_format_if_unknown(output, Format::F32); |
| 93 | } |
| 94 | } |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 95 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 96 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 97 | Window win_input1 = win.broadcast_if_dimension_le_one(input1); |
| 98 | Window win_input2 = win.broadcast_if_dimension_le_one(input2); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 99 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 100 | AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration); |
| 101 | AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration); |
| 102 | AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); |
| 103 | |
| 104 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 105 | || update_window_and_padding(win_input2, input2_access) |
| 106 | || update_window_and_padding(win, output_access); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 107 | |
| 108 | output_access.set_valid_region(win, valid_region); |
| 109 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 110 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 111 | return std::make_pair(err, win); |
| 112 | } |
| 113 | } // namespace |
| 114 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | CLArithmeticAdditionKernel::CLArithmeticAdditionKernel() |
| 116 | : _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | void CLArithmeticAdditionKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, ConvertPolicy policy) |
| 121 | { |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 123 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy)); |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 124 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 125 | // Configure kernel window |
| 126 | auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info()); |
| 127 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
| 129 | _input1 = input1; |
| 130 | _input2 = input2; |
| 131 | _output = output; |
| 132 | |
| 133 | const bool has_float_out = is_data_type_float(output->info()->data_type()); |
| 134 | |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 135 | std::string kernel_name = "arithmetic_add"; |
| 136 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | // Set kernel build options |
| 138 | std::set<std::string> build_opts; |
| 139 | build_opts.emplace((policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE"); |
| 140 | build_opts.emplace("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type())); |
| 141 | build_opts.emplace("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type())); |
| 142 | build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type())); |
Michele Di Giorgio | a1422fb | 2018-10-24 12:20:19 +0100 | [diff] [blame] | 143 | build_opts.emplace("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 144 | if(is_data_type_quantized_asymmetric(input1->info()->data_type())) |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 145 | { |
Pablo Tello | 76259ca | 2018-07-12 11:14:20 +0100 | [diff] [blame] | 146 | build_opts.emplace("-DOFFSET_IN1=" + support::cpp11::to_string(input1->info()->quantization_info().offset)); |
| 147 | build_opts.emplace("-DOFFSET_IN2=" + support::cpp11::to_string(input2->info()->quantization_info().offset)); |
| 148 | build_opts.emplace("-DOFFSET_OUT=" + support::cpp11::to_string(output->info()->quantization_info().offset)); |
| 149 | build_opts.emplace("-DSCALE_IN1=" + support::cpp11::to_string(input1->info()->quantization_info().scale)); |
| 150 | build_opts.emplace("-DSCALE_IN2=" + support::cpp11::to_string(input2->info()->quantization_info().scale)); |
| 151 | build_opts.emplace("-DSCALE_OUT=" + support::cpp11::to_string(output->info()->quantization_info().scale)); |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 152 | kernel_name += "_quantized"; |
| 153 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | |
| 155 | // Create kernel |
Michele Di Giorgio | 4622ac1 | 2018-06-27 16:41:17 +0100 | [diff] [blame] | 156 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 158 | ICLKernel::configure_internal(win_config.second); |
Michele Di Giorgio | a1422fb | 2018-10-24 12:20:19 +0100 | [diff] [blame] | 159 | |
| 160 | // Set config_id for enabling LWS tuning |
| 161 | _config_id = kernel_name; |
| 162 | _config_id += "_"; |
| 163 | _config_id += lower_string(string_from_data_type(input1->info()->data_type())); |
| 164 | _config_id += "_"; |
| 165 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 166 | _config_id += "_"; |
| 167 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 168 | _config_id += (policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_"; |
| 169 | _config_id += lower_string(string_from_data_layout(input1->info()->data_layout())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 172 | Status CLArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 173 | { |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 174 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 175 | |
| 176 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy)); |
| 177 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 178 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 179 | return Status{}; |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | void CLArithmeticAdditionKernel::run(const Window &window, cl::CommandQueue &queue) |
| 183 | { |
| 184 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 185 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 186 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 187 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 188 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 189 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 190 | |
Michele Di Giorgio | a1422fb | 2018-10-24 12:20:19 +0100 | [diff] [blame] | 191 | bool can_collapse = true; |
| 192 | const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1; |
| 193 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 194 | { |
| 195 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 196 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++) |
| 197 | { |
| 198 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | bool has_collapsed = false; |
| 203 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 204 | |
| 205 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 206 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 207 | |
| 208 | Window slice = collapsed.first_slice_window_3D(); |
| 209 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 210 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
Georgios Pinitas | 1d08a31 | 2018-01-03 12:29:22 +0000 | [diff] [blame] | 211 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | do |
| 213 | { |
| 214 | unsigned int idx = 0; |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 215 | |
| 216 | add_3D_tensor_argument(idx, _input1, slice_input1); |
| 217 | add_3D_tensor_argument(idx, _input2, slice_input2); |
Georgios Pinitas | 1d08a31 | 2018-01-03 12:29:22 +0000 | [diff] [blame] | 218 | add_3D_tensor_argument(idx, _output, slice); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 219 | |
Michele Di Giorgio | a1422fb | 2018-10-24 12:20:19 +0100 | [diff] [blame] | 220 | enqueue(queue, *this, slice, lws_hint()); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 221 | |
| 222 | collapsed.slide_window_slice_3D(slice_input1); |
| 223 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | } |
Georgios Pinitas | 1d08a31 | 2018-01-03 12:29:22 +0000 | [diff] [blame] | 225 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | } |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 227 | |
| 228 | BorderSize CLArithmeticAdditionKernel::border_size() const |
| 229 | { |
| 230 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 231 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
| 232 | return BorderSize(0, border, 0, 0); |
| 233 | } |