giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [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/CLElementwiseOperationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLValidate.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 29 | #include "support/StringSupport.h" |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 30 | #include <map> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace |
| 35 | { |
| 36 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 37 | |
| 38 | std::map<ArithmeticOperation, std::string> supported_arithmetic_ops = |
| 39 | { |
| 40 | { ArithmeticOperation::ADD, "ADD" }, |
| 41 | { ArithmeticOperation::SUB, "SUB" }, |
| 42 | { ArithmeticOperation::DIV, "DIV" }, |
| 43 | { ArithmeticOperation::SQUARED_DIFF, "SQUARED_DIFF" }, |
| 44 | { ArithmeticOperation::MIN, "MIN" }, |
| 45 | { ArithmeticOperation::MAX, "MAX" }, |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 46 | { ArithmeticOperation::POWER, "POWER" }, |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 47 | { ArithmeticOperation::PRELU, "PRELU" }, |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | std::map<ArithmeticOperation, std::string> supported_sat_arithmetic_ops = |
| 51 | { |
| 52 | { ArithmeticOperation::ADD, "ADD" }, |
| 53 | { ArithmeticOperation::SUB, "SUB" }, |
| 54 | }; |
| 55 | |
| 56 | std::string generate_id_for_tuning_common(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) |
| 57 | { |
| 58 | std::string config_id; |
| 59 | // Set config_id for enabling LWS tuning |
| 60 | config_id = kernel_name; |
| 61 | config_id += "_"; |
| 62 | config_id += lower_string(string_from_data_type(input1.data_type())); |
| 63 | config_id += "_"; |
| 64 | config_id += support::cpp11::to_string(output.dimension(0)); |
| 65 | config_id += "_"; |
| 66 | config_id += support::cpp11::to_string(output.dimension(1)); |
| 67 | return config_id; |
| 68 | } |
| 69 | |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 70 | Status validate_arguments_with_float_only_supported_rules(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(&input1, &input2, &output); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input1); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::F16, DataType::F32); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2); |
| 76 | |
| 77 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 78 | |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 80 | |
| 81 | // Validate in case of configured output |
| 82 | if(output.total_size() > 0) |
| 83 | { |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::F16, DataType::F32); |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output); |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 87 | "Wrong shape for output"); |
| 88 | } |
| 89 | |
| 90 | return Status{}; |
| 91 | } |
| 92 | |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 93 | Status validate_arguments_with_arithmetic_rules(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 94 | { |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input1); |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame^] | 96 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 97 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 98 | DataType::S32, DataType::F32); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input2); |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame^] | 100 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 101 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 102 | DataType::S32, DataType::F32); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 103 | |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 104 | const bool is_quantized = is_data_type_quantized(input1.data_type()) || is_data_type_quantized(input2.data_type()); |
| 105 | if(is_quantized) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 106 | { |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2); |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 108 | |
| 109 | if(is_data_type_quantized_symmetric(input1.data_type())) |
| 110 | { |
| 111 | const int32_t in1_offset = input1.quantization_info().uniform().offset; |
| 112 | const int32_t in2_offset = input2.quantization_info().uniform().offset; |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero"); |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero"); |
| 115 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 119 | |
| 120 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 121 | |
| 122 | // Validate in case of configured output |
| 123 | if(output.total_size() > 0) |
| 124 | { |
| 125 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output); |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame^] | 126 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 127 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 128 | DataType::S32, DataType::F32); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output.data_type() == DataType::U8) && ((input1.data_type() != DataType::U8) || (input2.data_type() != DataType::U8)), |
| 130 | "Output can only be U8 if both inputs are U8"); |
| 131 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 132 | "Wrong shape for output"); |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 133 | |
| 134 | if(is_quantized) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 135 | { |
| 136 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output); |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 137 | |
| 138 | if(is_data_type_quantized_symmetric(output.data_type())) |
| 139 | { |
| 140 | const int32_t offset = output.quantization_info().uniform().offset; |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero"); |
| 142 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return Status{}; |
| 146 | } |
| 147 | |
| 148 | CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, const std::string &operation_string) |
| 149 | { |
| 150 | CLBuildOptions build_opts; |
| 151 | |
| 152 | build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1.data_type())); |
| 153 | build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2.data_type())); |
| 154 | build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output.data_type())); |
| 155 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 156 | build_opts.add_option("-DOP=" + operation_string); |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 157 | if(is_data_type_quantized(input1.data_type())) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 158 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 159 | const UniformQuantizationInfo iq1info = input1.quantization_info().uniform(); |
| 160 | const UniformQuantizationInfo iq2info = input2.quantization_info().uniform(); |
| 161 | const UniformQuantizationInfo oqinfo = output.quantization_info().uniform(); |
| 162 | |
| 163 | build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(iq1info.offset)); |
| 164 | build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(iq2info.offset)); |
| 165 | build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(oqinfo.offset)); |
| 166 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1info.scale)); |
| 167 | build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2info.scale)); |
| 168 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oqinfo.scale)); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 169 | } |
| 170 | return build_opts; |
| 171 | } |
| 172 | |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 173 | std::pair<Status, Window> configure_window_arithmetic_common(const ValidRegion &valid_region, ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
| 174 | { |
| 175 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 176 | Window win_input1 = win.broadcast_if_dimension_le_one(input1); |
| 177 | Window win_input2 = win.broadcast_if_dimension_le_one(input2); |
| 178 | |
| 179 | AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration); |
| 180 | AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration); |
| 181 | AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); |
| 182 | |
| 183 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 184 | || update_window_and_padding(win_input2, input2_access) |
| 185 | || update_window_and_padding(win, output_access); |
| 186 | |
| 187 | output_access.set_valid_region(win, valid_region); |
| 188 | |
| 189 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 190 | return std::make_pair(err, win); |
| 191 | } |
| 192 | |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 193 | std::pair<Status, Window> validate_and_configure_window_for_arithmetic_operators(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
| 194 | { |
| 195 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); |
| 196 | const TensorShape &out_shape = broadcast_pair.first; |
| 197 | const ValidRegion &valid_region = broadcast_pair.second; |
| 198 | |
| 199 | set_shape_if_empty(output, out_shape); |
| 200 | |
| 201 | if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16) |
| 202 | { |
| 203 | set_format_if_unknown(output, Format::S16); |
| 204 | } |
Georgios Pinitas | d7d7e90 | 2019-12-18 15:40:54 +0000 | [diff] [blame] | 205 | else if(input1.data_type() == DataType::F16 || input2.data_type() == DataType::F16) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 206 | { |
| 207 | set_format_if_unknown(output, Format::F16); |
| 208 | } |
| 209 | else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32) |
| 210 | { |
| 211 | set_format_if_unknown(output, Format::F32); |
| 212 | } |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 213 | else if(input1.data_type() == DataType::QASYMM8 || input2.data_type() == DataType::QASYMM8) |
| 214 | { |
| 215 | set_data_type_if_unknown(output, DataType::QASYMM8); |
| 216 | } |
Kurtis Charnock | ec0c412 | 2019-12-05 14:13:46 +0000 | [diff] [blame] | 217 | else if(input1.data_type() == DataType::QASYMM8_SIGNED || input2.data_type() == DataType::QASYMM8_SIGNED) |
| 218 | { |
| 219 | set_data_type_if_unknown(output, DataType::QASYMM8_SIGNED); |
| 220 | } |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 221 | else if(input1.data_type() == DataType::QSYMM16 || input2.data_type() == DataType::QSYMM16) |
| 222 | { |
| 223 | set_data_type_if_unknown(output, DataType::QSYMM16); |
| 224 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 225 | |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 226 | return configure_window_arithmetic_common(valid_region, input1, input2, output); |
| 227 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 228 | |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 229 | std::pair<Status, Window> validate_and_configure_window_for_division(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
| 230 | { |
| 231 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); |
| 232 | const TensorShape &out_shape = broadcast_pair.first; |
| 233 | const ValidRegion &valid_region = broadcast_pair.second; |
| 234 | auto_init_if_empty(output, out_shape, 1, input1.data_type()); |
| 235 | return configure_window_arithmetic_common(valid_region, input1, input2, output); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 236 | } |
| 237 | } // namespace |
| 238 | |
| 239 | CLElementwiseOperationKernel::CLElementwiseOperationKernel() |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 240 | : _act_info(), _input1(nullptr), _input2(nullptr), _output(nullptr) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 241 | { |
| 242 | } |
| 243 | |
| 244 | void CLElementwiseOperationKernel::configure_common(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output) |
| 245 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 246 | configure_common(CLKernelLibrary::get().get_compile_context(), input1, input2, output); |
| 247 | } |
| 248 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 249 | void CLElementwiseOperationKernel::configure_common(const CLCompileContext &compile_context, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 250 | { |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 251 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 252 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info())); |
| 253 | |
| 254 | // Configure kernel window |
| 255 | auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info()); |
| 256 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 257 | |
| 258 | _input1 = input1; |
| 259 | _input2 = input2; |
| 260 | _output = output; |
| 261 | |
| 262 | std::string kernel_name = "elementwise_operation_" + name(); |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 263 | if(is_data_type_quantized(input1->info()->data_type())) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 264 | { |
| 265 | kernel_name += "_quantized"; |
| 266 | } |
| 267 | |
| 268 | // Set kernel build options |
| 269 | CLBuildOptions build_opts = generate_build_options(*input1->info(), *input2->info(), *output->info()); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 270 | if(_act_info.enabled()) |
| 271 | { |
| 272 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation()))); |
| 273 | build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a())); |
| 274 | build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b())); |
| 275 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 276 | |
| 277 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 278 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 279 | |
| 280 | ICLKernel::configure_internal(win_config.second); |
| 281 | |
| 282 | _config_id = generate_id_for_tuning(kernel_name, *input1->info(), *output->info()); |
| 283 | } |
| 284 | |
| 285 | void CLElementwiseOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 286 | { |
| 287 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 288 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 289 | |
| 290 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 291 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 292 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 293 | |
| 294 | bool can_collapse = true; |
| 295 | const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1; |
| 296 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector) |
| 297 | { |
| 298 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 299 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++) |
| 300 | { |
| 301 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | bool has_collapsed = false; |
| 306 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 307 | |
| 308 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 309 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 310 | |
| 311 | Window slice = collapsed.first_slice_window_3D(); |
| 312 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 313 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 314 | |
| 315 | do |
| 316 | { |
| 317 | unsigned int idx = 0; |
| 318 | |
| 319 | add_3D_tensor_argument(idx, _input1, slice_input1); |
| 320 | add_3D_tensor_argument(idx, _input2, slice_input2); |
| 321 | add_3D_tensor_argument(idx, _output, slice); |
| 322 | |
| 323 | enqueue(queue, *this, slice, lws_hint()); |
| 324 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 325 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 326 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 327 | } |
| 328 | while(collapsed.slide_window_slice_3D(slice)); |
| 329 | } |
| 330 | |
| 331 | BorderSize CLElementwiseOperationKernel::border_size() const |
| 332 | { |
| 333 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 334 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 335 | return BorderSize{ 0, border, 0, 0 }; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** Arithmetic operations with saturation*/ |
| 339 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 340 | void CLSaturatedArithmeticOperationKernel::configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy, |
| 341 | const ActivationLayerInfo &act_info) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 342 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 343 | configure(CLKernelLibrary::get().get_compile_context(), op, input1, input2, output, policy, act_info); |
| 344 | } |
| 345 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 346 | void CLSaturatedArithmeticOperationKernel::configure(const CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 347 | const ConvertPolicy &policy, |
| 348 | const ActivationLayerInfo &act_info) |
| 349 | { |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 350 | _policy = policy; |
| 351 | _op = op; |
| 352 | _act_info = act_info; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 353 | configure_common(compile_context, input1, input2, output); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 356 | Status CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy, |
| 357 | const ActivationLayerInfo &act_info) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 358 | { |
| 359 | ARM_COMPUTE_UNUSED(op, policy); |
| 360 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 361 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output)); |
| 362 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 363 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type())); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 364 | |
| 365 | return Status{}; |
| 366 | } |
| 367 | |
| 368 | std::pair<Status, Window> CLSaturatedArithmeticOperationKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
| 369 | { |
| 370 | return validate_and_configure_window_for_arithmetic_operators(input1, input2, output); |
| 371 | } |
| 372 | |
| 373 | Status CLSaturatedArithmeticOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 374 | { |
| 375 | return validate_arguments_with_arithmetic_rules(input1, input2, output); |
| 376 | } |
| 377 | |
| 378 | CLBuildOptions CLSaturatedArithmeticOperationKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 379 | { |
| 380 | const bool has_float_out = is_data_type_float(output.data_type()); |
| 381 | auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name()); |
| 382 | build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE"); |
| 383 | return build_options; |
| 384 | } |
| 385 | std::string CLSaturatedArithmeticOperationKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) |
| 386 | { |
| 387 | auto config_id = generate_id_for_tuning_common(kernel_name, input1, output); |
| 388 | config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_"; |
| 389 | config_id += lower_string(string_from_data_layout(input1.data_layout())); |
| 390 | return config_id; |
| 391 | } |
| 392 | |
| 393 | std::string CLSaturatedArithmeticOperationKernel::name() |
| 394 | { |
| 395 | return supported_sat_arithmetic_ops[_op]; |
| 396 | } |
| 397 | |
| 398 | /** Arithmetic operations*/ |
| 399 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 400 | void CLArithmeticOperationKernel::configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 401 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 402 | configure(CLKernelLibrary::get().get_compile_context(), op, input1, input2, output, act_info); |
| 403 | } |
| 404 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 405 | void CLArithmeticOperationKernel::configure(const CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 406 | const ActivationLayerInfo &act_info) |
| 407 | { |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 408 | _op = op; |
| 409 | _act_info = act_info; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 410 | configure_common(compile_context, input1, input2, output); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 413 | Status CLArithmeticOperationKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 414 | { |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 415 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 416 | if(op == ArithmeticOperation::DIV || op == ArithmeticOperation::POWER) |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 417 | { |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 418 | // Division and Power operators don't support integer arithmetic |
| 419 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*input1, *input2, *output)); |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 420 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*input1->clone(), *input2->clone(), *output->clone()).first); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output)); |
| 425 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first); |
| 426 | } |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 427 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type())); |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 428 | |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 429 | return Status{}; |
| 430 | } |
| 431 | std::pair<Status, Window> CLArithmeticOperationKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
| 432 | { |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 433 | if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER) |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 434 | { |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 435 | // Division and Power operators don't support integer arithmetic |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 436 | return validate_and_configure_window_for_division(input1, input2, output); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | return validate_and_configure_window_for_arithmetic_operators(input1, input2, output); |
| 441 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 442 | } |
| 443 | Status CLArithmeticOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 444 | { |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 445 | if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER) |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 446 | { |
Usama Arif | 52c54f6 | 2019-05-14 10:22:36 +0100 | [diff] [blame] | 447 | // Division and Power operators don't support integer arithmetic |
| 448 | return validate_arguments_with_float_only_supported_rules(input1, input2, output); |
giuros01 | 49f7c02 | 2018-12-03 19:25:22 +0000 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
| 452 | return validate_arguments_with_arithmetic_rules(input1, input2, output); |
| 453 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | CLBuildOptions CLArithmeticOperationKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 457 | { |
| 458 | return generate_build_options_with_arithmetic_rules(input1, input2, output, name()); |
| 459 | } |
| 460 | std::string CLArithmeticOperationKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) |
| 461 | { |
| 462 | return generate_id_for_tuning_common(kernel_name, input1, output); |
| 463 | } |
| 464 | |
| 465 | std::string CLArithmeticOperationKernel::name() |
| 466 | { |
| 467 | return supported_arithmetic_ops[_op]; |
| 468 | } |
| 469 | } // namespace arm_compute |