Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 36 | namespace |
| 37 | { |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 38 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 39 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 40 | Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 41 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 42 | { |
| 43 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 44 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 45 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input1); |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, |
| 49 | 1, |
| 50 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 51 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 52 | DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, |
| 54 | 1, |
| 55 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 56 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 57 | DataType::F32); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative."); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type())); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 60 | |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 61 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 62 | |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 64 | |
| 65 | // Validate in case of configured output |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 66 | if(output->total_size() > 0) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 67 | { |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, |
| 69 | 1, |
| 70 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 71 | DataType::S16, DataType::QSYMM16, DataType::F16, |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 72 | DataType::S32, DataType::F32); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8), |
| 74 | "Output can only be U8 if both inputs are U8"); |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8 && (input1->data_type() != DataType::QASYMM8 || input2->data_type() != DataType::QASYMM8), |
| 76 | "Output can only be QASYMM8 if both inputs are QASYMM8"); |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QASYMM8_SIGNED && (input1->data_type() != DataType::QASYMM8_SIGNED || input2->data_type() != DataType::QASYMM8_SIGNED), |
| 78 | "Output can only be QASYMM8_SIGNED if both inputs are QASYMM8_SIGNED"); |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::QSYMM16 && (input1->data_type() != DataType::QSYMM16 || input2->data_type() != DataType::QSYMM16), |
| 80 | "Output can only be QSYMM16 if both inputs are QSYMM16"); |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 81 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::S32 && (input1->data_type() != DataType::QSYMM16 || input2->data_type() != DataType::QSYMM16), |
| 82 | "Output can only be S32 if both inputs are QSYMM16"); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 83 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 86 | return Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 89 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 90 | { |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 91 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 92 | const TensorShape &out_shape = broadcast_pair.first; |
| 93 | const ValidRegion &valid_region = broadcast_pair.second; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 94 | |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 95 | // Auto initialize output if not initialized |
| 96 | { |
| 97 | set_shape_if_empty(*output, out_shape); |
| 98 | |
| 99 | if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16) |
| 100 | { |
| 101 | set_format_if_unknown(*output, Format::S16); |
| 102 | } |
| 103 | else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) |
| 104 | { |
| 105 | set_format_if_unknown(*output, Format::F32); |
| 106 | } |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 107 | else if(input1->data_type() == DataType::QASYMM8) |
| 108 | { |
| 109 | set_data_type_if_unknown(*output, DataType::QASYMM8); |
| 110 | } |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 111 | else if(input1->data_type() == DataType::QASYMM8_SIGNED) |
| 112 | { |
| 113 | set_data_type_if_unknown(*output, DataType::QASYMM8_SIGNED); |
| 114 | } |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 115 | else if(input1->data_type() == DataType::QSYMM16) |
| 116 | { |
| 117 | set_data_type_if_unknown(*output, DataType::QSYMM16); |
| 118 | } |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 122 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 123 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 124 | |
| 125 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration); |
| 126 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration); |
| 127 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 128 | |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 129 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 130 | || update_window_and_padding(win_input2, input2_access) |
| 131 | || update_window_and_padding(win, output_access); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 132 | |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 133 | output_access.set_valid_region(win, valid_region); |
| 134 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 135 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 136 | return std::make_pair(err, win); |
| 137 | } |
| 138 | } // namespace |
| 139 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | CLPixelWiseMultiplicationKernel::CLPixelWiseMultiplicationKernel() |
| 141 | : _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | void CLPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float scale, |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 146 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | { |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 149 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 150 | scale, overflow_policy, rounding_policy, act_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 152 | // Configure kernel window |
| 153 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); |
| 154 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 155 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | _input1 = input1; |
| 157 | _input2 = input2; |
| 158 | _output = output; |
| 159 | |
| 160 | int scale_int = -1; |
| 161 | // Extract sign, exponent and mantissa |
| 162 | int exponent = 0; |
| 163 | float normalized_mantissa = std::frexp(scale, &exponent); |
| 164 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 165 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 166 | // Moreover, it will be negative as we deal with 1/2^n |
| 167 | if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)) |
| 168 | { |
| 169 | // Store the positive exponent. We know that we compute 1/2^n |
| 170 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 171 | scale_int = std::abs(exponent - 1); |
| 172 | } |
| 173 | |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 174 | std::string acc_type; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | // Check if it has float inputs and output |
| 176 | if(is_data_type_float(input1->info()->data_type()) || is_data_type_float(input2->info()->data_type())) |
| 177 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 178 | scale_int = -1; |
| 179 | acc_type = (input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32) ? "float" : "half"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |
| 181 | else |
| 182 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 183 | if(input1->info()->element_size() == 2 || input2->info()->element_size() == 2) |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 184 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 185 | // Use 32-bit accumulator for 16-bit input |
| 186 | acc_type = "int"; |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 187 | } |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 188 | else |
| 189 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 190 | // Use 16-bit accumulator for 8-bit input |
| 191 | acc_type = "ushort"; |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 192 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 195 | const bool is_quantized = is_data_type_quantized(input1->info()->data_type()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | |
| 197 | // Set kernel build options |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 198 | std::string kernel_name = "pixelwise_mul"; |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 199 | CLBuildOptions build_opts; |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 200 | build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type())); |
| 201 | build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type())); |
| 202 | build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type())); |
| 203 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 204 | if(is_quantized && (output->info()->data_type() != DataType::S32)) |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 205 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 206 | const UniformQuantizationInfo iq1_info = input1->info()->quantization_info().uniform(); |
| 207 | const UniformQuantizationInfo iq2_info = input2->info()->quantization_info().uniform(); |
| 208 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 209 | |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 210 | build_opts.add_option_if(is_data_type_quantized_asymmetric(input1->info()->data_type()), |
| 211 | "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset)); |
| 212 | build_opts.add_option_if(is_data_type_quantized_asymmetric(input2->info()->data_type()), |
| 213 | "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset)); |
| 214 | build_opts.add_option_if(is_data_type_quantized_asymmetric(output->info()->data_type()), |
| 215 | "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset)); |
| 216 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale)); |
| 217 | build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale)); |
| 218 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale)); |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 219 | kernel_name += "_quantized"; |
| 220 | } |
| 221 | else |
| 222 | { |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 223 | kernel_name += (scale_int >= 0) ? "_int" : "_float"; |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 224 | build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(output->info()->data_type()), "-DWRAP", "-DSATURATE"); |
| 225 | build_opts.add_option_if_else(rounding_policy == RoundingPolicy::TO_ZERO, "-DROUND=_rtz", "-DROUND=_rte"); |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 226 | build_opts.add_option("-DACC_DATA_TYPE=" + acc_type); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 227 | if(act_info.enabled()) |
| 228 | { |
| 229 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
| 230 | build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 231 | build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
| 232 | } |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 233 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | |
| 235 | // Create kernel |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 236 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | |
| 238 | // Set scale argument |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 239 | unsigned int idx = 3 * num_arguments_per_3D_tensor(); // Skip the inputs and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 240 | |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 241 | if(scale_int >= 0 && !is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 242 | { |
| 243 | _kernel.setArg(idx++, scale_int); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | _kernel.setArg(idx++, scale); |
| 248 | } |
| 249 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 250 | ICLKernel::configure_internal(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 253 | Status CLPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 254 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 255 | { |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 256 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 257 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy, act_info)); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 258 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 259 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 260 | return Status{}; |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 263 | void CLPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 264 | { |
| 265 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 266 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 267 | |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 268 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 269 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 270 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 271 | |
| 272 | bool can_collapse = true; |
| 273 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 274 | { |
| 275 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 276 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 277 | { |
| 278 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | bool has_collapsed = false; |
| 283 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 284 | |
| 285 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 286 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 287 | |
| 288 | Window slice = collapsed.first_slice_window_3D(); |
| 289 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 290 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | |
| 292 | do |
| 293 | { |
| 294 | unsigned int idx = 0; |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 295 | add_3D_tensor_argument(idx, _input1, slice_input1); |
| 296 | add_3D_tensor_argument(idx, _input2, slice_input2); |
Anthony Barbier | 9a7182e | 2017-07-11 18:36:40 +0100 | [diff] [blame] | 297 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 298 | enqueue(queue, *this, slice, lws_hint()); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 299 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 300 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 301 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | } |
Georgios Pinitas | 1d08a31 | 2018-01-03 12:29:22 +0000 | [diff] [blame] | 303 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | } |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 305 | |
| 306 | BorderSize CLPixelWiseMultiplicationKernel::border_size() const |
| 307 | { |
| 308 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 309 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 310 | return BorderSize{ 0, border, 0, 0 }; |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 311 | } |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 312 | |
| 313 | namespace |
| 314 | { |
| 315 | constexpr unsigned int num_elems_processed_per_iteration_complex = 1; |
| 316 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 317 | Status validate_arguments_complex(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 318 | { |
| 319 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 2, DataType::F32); |
| 320 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 2, DataType::F32); |
| 321 | |
| 322 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 323 | |
| 324 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 325 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type())); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 326 | |
| 327 | // Validate in case of configured output |
| 328 | if(output->total_size() > 0) |
| 329 | { |
| 330 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 2, DataType::F32); |
| 331 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
| 332 | } |
| 333 | |
| 334 | return Status{}; |
| 335 | } |
| 336 | |
| 337 | std::pair<Status, Window> validate_and_configure_window_complex(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
| 338 | { |
| 339 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 340 | const TensorShape &out_shape = broadcast_pair.first; |
| 341 | const ValidRegion &valid_region = broadcast_pair.second; |
| 342 | |
| 343 | // Auto initialize output if not initialized |
| 344 | const TensorInfo out_info(out_shape, input1->num_channels(), input1->data_type()); |
| 345 | auto_init_if_empty(*output, out_info); |
| 346 | |
| 347 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration_complex)); |
| 348 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 349 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
| 350 | |
| 351 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration_complex); |
| 352 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration_complex); |
| 353 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_complex); |
| 354 | |
| 355 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 356 | || update_window_and_padding(win_input2, input2_access) |
| 357 | || update_window_and_padding(win, output_access); |
| 358 | |
| 359 | output_access.set_valid_region(win, valid_region); |
| 360 | |
| 361 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 362 | return std::make_pair(err, win); |
| 363 | } |
| 364 | } // namespace |
| 365 | |
| 366 | CLComplexPixelWiseMultiplicationKernel::CLComplexPixelWiseMultiplicationKernel() |
| 367 | : _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 368 | { |
| 369 | } |
| 370 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 371 | void CLComplexPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 372 | { |
| 373 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 374 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(input1->info(), input2->info(), output->info(), act_info)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 375 | |
| 376 | // Configure kernel window |
| 377 | auto win_config = validate_and_configure_window_complex(input1->info(), input2->info(), output->info()); |
| 378 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 379 | |
| 380 | _input1 = input1; |
| 381 | _input2 = input2; |
| 382 | _output = output; |
| 383 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 384 | CLBuildOptions build_opts; |
| 385 | if(act_info.enabled()) |
| 386 | { |
| 387 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
| 388 | build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 389 | build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
| 390 | } |
| 391 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 392 | // Create kernel |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 393 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("pixelwise_mul_complex", build_opts.options())); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 394 | |
| 395 | ICLKernel::configure_internal(win_config.second); |
| 396 | } |
| 397 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 398 | Status CLComplexPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 399 | { |
| 400 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 401 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(input1, input2, output, act_info)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 402 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_complex(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
| 403 | |
| 404 | return Status{}; |
| 405 | } |
| 406 | |
| 407 | void CLComplexPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 408 | { |
| 409 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 410 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 411 | |
| 412 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 413 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 414 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 415 | |
| 416 | bool can_collapse = true; |
| 417 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 418 | { |
| 419 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 420 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 421 | { |
| 422 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | bool has_collapsed = false; |
| 427 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 428 | |
| 429 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 430 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 431 | |
| 432 | Window slice = collapsed.first_slice_window_3D(); |
| 433 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 434 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 435 | |
| 436 | do |
| 437 | { |
| 438 | unsigned int idx = 0; |
| 439 | add_3D_tensor_argument(idx, _input1, slice_input1); |
| 440 | add_3D_tensor_argument(idx, _input2, slice_input2); |
| 441 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 442 | enqueue(queue, *this, slice, lws_hint()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 443 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 444 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 445 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 446 | } |
| 447 | while(collapsed.slide_window_slice_3D(slice)); |
| 448 | } |
| 449 | |
| 450 | BorderSize CLComplexPixelWiseMultiplicationKernel::border_size() const |
| 451 | { |
| 452 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 453 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration_complex - 1U, replicateSize); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 454 | return BorderSize{ 0, border, 0, 0 }; |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 455 | } |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 456 | } // namespace arm_compute |