Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClMulKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/CL/CLValidate.h" |
| 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
| 34 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 39 | namespace opencl |
| 40 | { |
| 41 | namespace kernels |
| 42 | { |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale, |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 46 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 47 | { |
| 48 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 49 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 50 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 54 | 1, |
| 55 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
Suhail Munshi | 448cb45 | 2021-04-23 16:23:25 +0100 | [diff] [blame] | 56 | DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32, |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 57 | DataType::F32); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 59 | 1, |
| 60 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
Suhail Munshi | 448cb45 | 2021-04-23 16:23:25 +0100 | [diff] [blame] | 61 | DataType::S16, DataType::QSYMM16, DataType::F16, DataType::S32, |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 62 | DataType::F32); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative."); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type())); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 65 | |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 66 | // Check whether it is in_place calculation |
| 67 | const bool in_place = (src1 == dst) || (src2 == dst); |
| 68 | const bool src1_in_place = in_place && (src1 == dst); |
| 69 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 70 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 71 | |
| 72 | 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] | 73 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 74 | // Validate in case of configured dst |
| 75 | if(dst->total_size() > 0) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 76 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 78 | 1, |
| 79 | DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 80 | DataType::S16, DataType::QSYMM16, DataType::F16, |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 81 | DataType::S32, DataType::F32); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::U8 && (src1->data_type() != DataType::U8 || src2->data_type() != DataType::U8), |
| 83 | "Dst can only be U8 if both src are U8"); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8 && (src1->data_type() != DataType::QASYMM8 || src2->data_type() != DataType::QASYMM8), |
| 85 | "Dst can only be QASYMM8 if both src are QASYMM8"); |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QASYMM8_SIGNED && (src1->data_type() != DataType::QASYMM8_SIGNED || src2->data_type() != DataType::QASYMM8_SIGNED), |
| 87 | "Dst can only be QASYMM8_SIGNED if both src are QASYMM8_SIGNED"); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::QSYMM16 && (src1->data_type() != DataType::QSYMM16 || src2->data_type() != DataType::QSYMM16), |
| 89 | "Dst can only be QSYMM16 if both src are QSYMM16"); |
Suhail Munshi | 448cb45 | 2021-04-23 16:23:25 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((src1->data_type() == DataType::S32 || src2->data_type() == DataType::S32) && (dst->data_type() != DataType::S32), |
| 91 | "Dst must be S32 if source tensors are S32"); |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 92 | if(in_place) |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, src1_in_place ? src1->tensor_shape() : src2->tensor_shape(), 0), |
| 95 | "Wrong shape for dst, cannot do in_place calculation"); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), |
| 100 | "Wrong shape for dst"); |
| 101 | } |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 104 | return Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 105 | } |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 106 | } // namespace |
| 107 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 108 | ClMulKernel::ClMulKernel() |
| 109 | { |
| 110 | _type = CLKernelType::ELEMENTWISE; |
| 111 | } |
| 112 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 113 | void ClMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale, |
| 114 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 116 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 117 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst, |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 118 | scale, overflow_policy, rounding_policy, act_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 120 | auto padding_info = get_padding_info({ src1, src2, dst }); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 121 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 122 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
| 123 | auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | |
| 125 | int scale_int = -1; |
| 126 | // Extract sign, exponent and mantissa |
| 127 | int exponent = 0; |
| 128 | float normalized_mantissa = std::frexp(scale, &exponent); |
| 129 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 130 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 131 | // Moreover, it will be negative as we deal with 1/2^n |
| 132 | if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)) |
| 133 | { |
| 134 | // Store the positive exponent. We know that we compute 1/2^n |
| 135 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 136 | scale_int = std::abs(exponent - 1); |
| 137 | } |
| 138 | |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 139 | std::string acc_type; |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 140 | // Check if it has float src and dst |
| 141 | if(is_data_type_float(src1->data_type()) || is_data_type_float(src2->data_type())) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 143 | scale_int = -1; |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 144 | acc_type = (src1->data_type() == DataType::F32 || src2->data_type() == DataType::F32) ? "float" : "half"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
Suhail Munshi | 448cb45 | 2021-04-23 16:23:25 +0100 | [diff] [blame] | 148 | if(src1->element_size() == 4 || src2->element_size() == 4) |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 149 | { |
Suhail Munshi | 448cb45 | 2021-04-23 16:23:25 +0100 | [diff] [blame] | 150 | // use 64 bit accumulator for 32-bit input |
| 151 | acc_type = "long"; |
| 152 | } |
| 153 | else if(src1->element_size() == 2 || src2->element_size() == 2) |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 154 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 155 | // Use 32-bit accumulator for 16-bit input |
| 156 | acc_type = "int"; |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 157 | } |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 158 | else |
| 159 | { |
Michele Di Giorgio | 7a0212a | 2020-04-14 16:08:32 +0100 | [diff] [blame] | 160 | // Use 16-bit accumulator for 8-bit input |
| 161 | acc_type = "ushort"; |
Michele Di Giorgio | ab0a77e | 2017-06-21 15:36:24 +0100 | [diff] [blame] | 162 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 165 | const bool is_quantized = is_data_type_quantized(src1->data_type()); |
| 166 | const unsigned int vec_size = adjust_vec_size(16 / dst->element_size(), dst->dimension(0)); |
| 167 | const unsigned int vec_size_leftover = dst->dimension(0) % vec_size; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | |
| 169 | // Set kernel build options |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 170 | std::string kernel_name = "pixelwise_mul"; |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 171 | CLBuildOptions build_opts; |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 172 | build_opts.add_option("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(src1->data_type())); |
| 173 | build_opts.add_option("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(src2->data_type())); |
| 174 | build_opts.add_option("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(dst->data_type())); |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 175 | build_opts.add_option("-DVEC_SIZE_IN1=" + ((dst->dimension(0) != 1 && src1->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size))); |
| 176 | build_opts.add_option("-DVEC_SIZE_IN2=" + ((dst->dimension(0) != 1 && src2->dimension(0) == 1) ? "1" : support::cpp11::to_string(vec_size))); |
| 177 | build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(vec_size)); |
| 178 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover)); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 179 | if(is_quantized && (dst->data_type() != DataType::S32)) |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 180 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 181 | const UniformQuantizationInfo iq1_info = src1->quantization_info().uniform(); |
| 182 | const UniformQuantizationInfo iq2_info = src2->quantization_info().uniform(); |
| 183 | const UniformQuantizationInfo oq_info = dst->quantization_info().uniform(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 184 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 185 | build_opts.add_option_if(is_data_type_quantized_asymmetric(src1->data_type()), |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 186 | "-DOFFSET_IN1=" + support::cpp11::to_string(iq1_info.offset)); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 187 | build_opts.add_option_if(is_data_type_quantized_asymmetric(src2->data_type()), |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 188 | "-DOFFSET_IN2=" + support::cpp11::to_string(iq2_info.offset)); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 189 | build_opts.add_option_if(is_data_type_quantized_asymmetric(dst->data_type()), |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 190 | "-DOFFSET_OUT=" + support::cpp11::to_string(oq_info.offset)); |
| 191 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale)); |
| 192 | build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale)); |
| 193 | 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] | 194 | kernel_name += "_quantized"; |
| 195 | } |
| 196 | else |
| 197 | { |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 198 | kernel_name += (scale_int >= 0) ? "_int" : "_float"; |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 199 | build_opts.add_option_if_else(overflow_policy == ConvertPolicy::WRAP || is_data_type_float(dst->data_type()), "-DWRAP", "-DSATURATE"); |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 200 | 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] | 201 | build_opts.add_option("-DACC_DATA_TYPE=" + acc_type); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 202 | if(act_info.enabled()) |
| 203 | { |
| 204 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
| 205 | build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 206 | build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
| 207 | } |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 208 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 210 | // Check whether it is in_place calculation |
| 211 | const bool in_place = (src1 == dst) || (src2 == dst); |
| 212 | const bool src1_in_place = in_place && (src1 == dst); |
| 213 | build_opts.add_option_if(in_place, "-DIN_PLACE"); |
| 214 | build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE"); |
| 215 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 217 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 218 | |
| 219 | // Set scale argument |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 220 | unsigned int idx = (in_place ? 2 : 3) * num_arguments_per_3D_tensor(); // Skip the src and dst parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | |
Georgios Pinitas | bf28a3c | 2018-09-18 14:34:48 +0100 | [diff] [blame] | 222 | if(scale_int >= 0 && !is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | { |
| 224 | _kernel.setArg(idx++, scale_int); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | _kernel.setArg(idx++, scale); |
| 229 | } |
| 230 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 231 | Window win = calculate_max_window(*dst, Steps(vec_size)); |
| 232 | ICLKernel::configure_internal(win); |
| 233 | |
| 234 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Gian Marco Iodice | 8ed3ac1 | 2021-04-29 13:32:37 +0100 | [diff] [blame] | 235 | |
| 236 | // Set config_id for enabling LWS tuning |
| 237 | _config_id = kernel_name; |
| 238 | _config_id += "_"; |
| 239 | _config_id += lower_string(string_from_data_type(dst->data_type())); |
| 240 | _config_id += "_"; |
| 241 | _config_id += support::cpp11::to_string(src1->dimension(0)); |
| 242 | _config_id += "_"; |
| 243 | _config_id += support::cpp11::to_string(src1->dimension(1)); |
| 244 | _config_id += "_"; |
| 245 | _config_id += support::cpp11::to_string(src1->dimension(2)); |
| 246 | _config_id += "_"; |
| 247 | _config_id += support::cpp11::to_string(src2->dimension(0)); |
| 248 | _config_id += "_"; |
| 249 | _config_id += support::cpp11::to_string(src2->dimension(1)); |
| 250 | _config_id += "_"; |
| 251 | _config_id += support::cpp11::to_string(src2->dimension(2)); |
| 252 | _config_id += "_"; |
| 253 | _config_id += support::cpp11::to_string(dst->dimension(0)); |
| 254 | _config_id += "_"; |
| 255 | _config_id += support::cpp11::to_string(dst->dimension(1)); |
| 256 | _config_id += "_"; |
| 257 | _config_id += support::cpp11::to_string(dst->dimension(2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 260 | Status ClMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale, |
| 261 | ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 262 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 263 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 264 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy, act_info)); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 265 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 266 | return Status{}; |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 269 | void ClMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 270 | { |
| 271 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 272 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 273 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 274 | const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 275 | const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 276 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Michalis Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 277 | |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 278 | ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst); |
| 279 | |
Michalis Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 280 | const TensorShape &in_shape1 = src_0->info()->tensor_shape(); |
| 281 | const TensorShape &in_shape2 = src_1->info()->tensor_shape(); |
| 282 | const TensorShape &out_shape = dst->info()->tensor_shape(); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 283 | |
| 284 | bool can_collapse = true; |
| 285 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 286 | { |
| 287 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 288 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 289 | { |
| 290 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | bool has_collapsed = false; |
| 295 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 296 | |
| 297 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 298 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 299 | |
| 300 | Window slice = collapsed.first_slice_window_3D(); |
| 301 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 302 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 303 | |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 304 | // Check whether it is in_place calculation |
| 305 | const bool in_place = (src_0 == dst) || (src_1 == dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | do |
| 307 | { |
| 308 | unsigned int idx = 0; |
Michalis Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 309 | add_3D_tensor_argument(idx, src_0, slice_input1); |
| 310 | add_3D_tensor_argument(idx, src_1, slice_input2); |
Sheri Zhang | a387e27 | 2021-06-29 17:34:06 +0100 | [diff] [blame] | 311 | if(!in_place) |
| 312 | { |
| 313 | add_3D_tensor_argument(idx, dst, slice); |
| 314 | } |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 315 | enqueue(queue, *this, slice, lws_hint()); |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 316 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 317 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 318 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | } |
Georgios Pinitas | 1d08a31 | 2018-01-03 12:29:22 +0000 | [diff] [blame] | 320 | while(collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 321 | } |
Michele Di Giorgio | 6259e5f | 2018-01-17 17:29:33 +0000 | [diff] [blame] | 322 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 323 | namespace |
| 324 | { |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 325 | constexpr unsigned int vec_size_complex = 1; |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 326 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 327 | Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 328 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 329 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F16, DataType::F32); |
| 330 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F16, DataType::F32); |
| 331 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 332 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 333 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 334 | |
| 335 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 336 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type())); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 337 | |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 338 | // Validate in case of configured dst |
| 339 | if(dst->total_size() > 0) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 340 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 341 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F16, DataType::F32); |
| 342 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst); |
| 343 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), "Wrong shape for dst"); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return Status{}; |
| 347 | } |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 348 | } // namespace |
| 349 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 350 | ClComplexMulKernel::ClComplexMulKernel() |
| 351 | { |
| 352 | _type = CLKernelType::ELEMENTWISE; |
| 353 | } |
| 354 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 355 | void ClComplexMulKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 356 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 357 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 358 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst, act_info)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 359 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 360 | auto padding_info = get_padding_info({ src1, src2, dst }); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 361 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 362 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
| 363 | auto_init_if_empty(*dst, src1->clone()->set_tensor_shape(out_shape)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 364 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 365 | CLBuildOptions build_opts; |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 366 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type())); |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 367 | if(act_info.enabled()) |
| 368 | { |
| 369 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
| 370 | build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 371 | build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
| 372 | } |
| 373 | |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 374 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 375 | _kernel = create_kernel(compile_context, "pixelwise_mul_complex", build_opts.options()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 376 | |
Giorgio Arena | ada6cbc | 2021-04-16 17:03:39 +0100 | [diff] [blame] | 377 | Window win = calculate_max_window(*dst, Steps(vec_size_complex)); |
| 378 | ICLKernel::configure_internal(win); |
| 379 | |
| 380 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 383 | Status ClComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 384 | { |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 385 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 386 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst, act_info)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 387 | |
| 388 | return Status{}; |
| 389 | } |
| 390 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 391 | void ClComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 392 | { |
| 393 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 394 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 395 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 396 | const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 397 | const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 398 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Michalis Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 399 | |
| 400 | const TensorShape &in_shape1 = src_0->info()->tensor_shape(); |
| 401 | const TensorShape &in_shape2 = src_1->info()->tensor_shape(); |
| 402 | const TensorShape &out_shape = dst->info()->tensor_shape(); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 403 | |
| 404 | bool can_collapse = true; |
| 405 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 406 | { |
| 407 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 408 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 409 | { |
| 410 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | bool has_collapsed = false; |
| 415 | Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 416 | |
| 417 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 418 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 419 | |
| 420 | Window slice = collapsed.first_slice_window_3D(); |
| 421 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 422 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 423 | |
| 424 | do |
| 425 | { |
| 426 | unsigned int idx = 0; |
Michalis Spyrou | 1009e87 | 2020-07-27 12:48:34 +0100 | [diff] [blame] | 427 | add_3D_tensor_argument(idx, src_0, slice_input1); |
| 428 | add_3D_tensor_argument(idx, src_1, slice_input2); |
| 429 | add_3D_tensor_argument(idx, dst, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 430 | enqueue(queue, *this, slice, lws_hint()); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 431 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 432 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1)); |
| 433 | ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2)); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 434 | } |
| 435 | while(collapsed.slide_window_slice_3D(slice)); |
| 436 | } |
Sheri Zhang | f9ab9f9 | 2021-03-16 12:09:15 +0000 | [diff] [blame] | 437 | } // namespace kernels |
| 438 | } // namespace opencl |
Michele Di Giorgio | d8a468f | 2019-06-19 15:34:41 +0100 | [diff] [blame] | 439 | } // namespace arm_compute |