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