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