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