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