Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 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/ClSoftmaxKernel.h" |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/CL/ICLTensor.h" |
| 26 | #include "arm_compute/core/Utils.h" |
| 27 | #include "arm_compute/core/experimental/Types.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/StringUtils.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" |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 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 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 39 | namespace opencl |
| 40 | { |
| 41 | namespace kernels |
| 42 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | /** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options. |
| 46 | * |
| 47 | * Prepares these build options: |
| 48 | * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier. |
| 49 | * -DIFF_MIN - threshold difference between maximum value of input data and current processed value, |
| 50 | * it defines whether the value will be taken into account or not. |
| 51 | * |
| 52 | * @param[in] build_opts Build options to extend |
| 53 | * @param[in] input_scale Input scaling factor |
| 54 | * @param[in] beta Exponent scaling factor beta |
| 55 | */ |
| 56 | CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta) |
| 57 | { |
| 58 | // Number of integer bits in temporary fixed-point representation of current-to-max difference |
| 59 | static const int scaled_diff_int_bits = 5; |
| 60 | // Number of integer bits used in temporary fixed-point representation of exponent accumulator |
| 61 | static const int exp_accumulation_in_bits = 12; |
| 62 | |
| 63 | const double beta_multiplier = std::min( |
| 64 | 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)), |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 65 | (1LL << 31) - 1.0); |
| 66 | int input_beta_multiplier; |
| 67 | int input_beta_left_shift; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 68 | quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift); |
| 69 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 70 | const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 71 | const int diff_min = -1.f * std::floor(max_input_rescaled); |
| 72 | |
| 73 | CLBuildOptions build_opts; |
| 74 | build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits)); |
| 75 | build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits)); |
| 76 | build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier)); |
| 77 | build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift)); |
| 78 | build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min)); |
| 79 | |
| 80 | return build_opts; |
| 81 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 83 | Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 84 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src); |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 88 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 89 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type()); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 90 | |
| 91 | // Checks performed when output is configured |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 92 | if(dst.total_size() != 0) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 93 | { |
| 94 | if(is_quantized_asymmetric) |
| 95 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 96 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::S32); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 97 | } |
| 98 | else |
| 99 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 100 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 101 | } |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Checks performed when sum is configured |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 106 | if(sum.total_size() != 0) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 107 | { |
| 108 | if(is_quantized_asymmetric) |
| 109 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&sum, 1, DataType::S32); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&max, &sum); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 115 | } |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 116 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&max, &sum); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 119 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 122 | Status validate_arguments_1DNorm(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 123 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 124 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src); |
| 125 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::S32, DataType::F16, DataType::F32); |
| 126 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &sum); |
Sang-Hoon Park | a0205b9 | 2020-07-07 09:36:09 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type)); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 128 | |
| 129 | // Note: output should always have a scale of 1/256 and offset 0 |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 130 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
| 131 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 132 | |
| 133 | // Checks performed when output is configured |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 134 | if(dst.total_size() != 0) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 135 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 136 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 137 | if(!is_quantized_asymmetric) |
| 138 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 139 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 143 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
| 144 | ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != allowed_quantization_info); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 148 | return Status{}; |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 149 | } |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 150 | } // namespace |
| 151 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 152 | /**< Grid size (obtained through auto-tuning) */ |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 153 | const unsigned int ClLogits1DMaxShiftExpSumKernel::_grid_size = 64; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 154 | /**< Vector size in the serial case (obtained through auto-tuning) */ |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 155 | const unsigned int ClLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 156 | /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/ |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 157 | const unsigned int ClLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 158 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 159 | ClLogits1DMaxShiftExpSumKernel::ClLogits1DMaxShiftExpSumKernel() |
| 160 | { |
| 161 | _type = CLKernelType::ELEMENTWISE; |
| 162 | } |
| 163 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 164 | void ClLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, ITensorInfo &max, ITensorInfo &dst, ITensorInfo &sum, const SoftmaxKernelInfo &info) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 165 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 166 | auto padding_info = get_padding_info({ &src, &max, &dst, &sum }); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 167 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 168 | // Output auto initialization if not yet initialized |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 169 | auto_init_if_empty(sum, src.clone()->set_tensor_shape(max.tensor_shape())); |
| 170 | auto_init_if_empty(dst, *src.clone()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 171 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 172 | // Perform validation step |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 173 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 174 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 175 | const DataType dt = src.data_type(); |
| 176 | const UniformQuantizationInfo qinfo = src.quantization_info().uniform(); |
| 177 | const size_t reduction_dim_size = src.dimension(0); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 178 | const float beta = info.beta; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 179 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 180 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 181 | |
Adnan AlSinan | b060806 | 2021-09-29 16:50:46 +0100 | [diff] [blame] | 182 | const unsigned int vector_size = adjust_vec_size(_serial_vector_size, reduction_dim_size); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 183 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 184 | // Set build options |
| 185 | CLBuildOptions build_opts; |
| 186 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 187 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 188 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 189 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size)); |
| 190 | build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(reduction_dim_size % vector_size)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 191 | build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size)))); |
| 192 | build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE"); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 193 | build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED"); |
| 194 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta)); |
| 195 | build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX"); |
| 196 | build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX"))); |
Adnan AlSinan | b060806 | 2021-09-29 16:50:46 +0100 | [diff] [blame] | 197 | build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale)); |
| 198 | build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DBETA=" + float_to_string_with_full_precision(beta)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 199 | build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options()); |
| 200 | |
| 201 | cl::NDRange lws_hint(cl::NullRange); |
Adnan AlSinan | b060806 | 2021-09-29 16:50:46 +0100 | [diff] [blame] | 202 | std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") + (is_data_type_quantized_asymmetric(dt) ? "quantized_" : "") + "serial"; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 203 | |
| 204 | // Create kernel. |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 205 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 206 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 207 | // Configure window |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 208 | Window win = calculate_max_window(src, Steps(reduction_dim_size)); |
| 209 | IClKernel::configure_internal(win, lws_hint); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 210 | |
| 211 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 212 | } |
| 213 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 214 | Status ClLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 215 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 216 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 217 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 220 | ClLogits1DMaxShiftExpSumKernel::ParallelReductionInfo ClLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 221 | { |
| 222 | bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1); |
| 223 | unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size; |
| 224 | return std::make_tuple(is_parallel_reduction, vector_size); |
| 225 | } |
| 226 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 227 | void ClLogits1DMaxShiftExpSumKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 228 | { |
| 229 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 230 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 231 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 232 | auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 233 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 234 | auto max = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0)); |
| 235 | auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_1)); |
| 236 | |
| 237 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, max, sum); |
| 238 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 239 | // Collapse window in Z dimension |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 240 | Window window_collapsed = window.collapse_if_possible(IClKernel::window(), Window::DimZ); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 241 | |
| 242 | // Reconfigure window in case of parallel reduction |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 243 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(src->info()->dimension(0)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 244 | if(std::get<0>(parallel_reduction_info)) |
| 245 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 246 | // Launch grid_size parallel work items |
| 247 | window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Get slices |
| 251 | Window slice = window_collapsed.first_slice_window_3D(); |
| 252 | do |
| 253 | { |
| 254 | unsigned int idx = 0; |
| 255 | // Set inputs |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 256 | add_3D_tensor_argument(idx, src, slice); |
| 257 | add_3D_tensor_argument(idx, max, slice); |
| 258 | add_3D_tensor_argument(idx, dst, slice); |
| 259 | add_3D_tensor_argument(idx, sum, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 260 | enqueue(queue, *this, slice, lws_hint()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 261 | } |
| 262 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 263 | } |
| 264 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 265 | ClLogits1DNormKernel::ClLogits1DNormKernel() |
| 266 | { |
| 267 | _type = CLKernelType::ELEMENTWISE; |
| 268 | } |
| 269 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 270 | void ClLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, const ITensorInfo &sum, ITensorInfo &dst, const SoftmaxKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 272 | auto padding_info = get_padding_info({ &src, &dst, &sum }); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 273 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 274 | // Note: output should always have a scale of 1/256 and offset 0 |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 275 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type); |
| 276 | const DataType output_data_type = info.input_data_type; |
| 277 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 278 | const UniformQuantizationInfo qinfo = src.quantization_info().uniform(); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 279 | |
| 280 | // Output auto initialization if not yet initialized |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 281 | auto_init_if_empty(dst, src.clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info)); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 282 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 283 | // Perform validation step |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 284 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(src, sum, dst, info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 285 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 286 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 287 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 288 | const unsigned int vector_size = adjust_vec_size(16, src.dimension(0)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 289 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 290 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 291 | CLBuildOptions build_opts; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 292 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type)); |
| 293 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 294 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 295 | build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(src.dimension(0) % vector_size)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 296 | build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED"); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 297 | build_opts.add_options_if(is_quantized_asymmetric, |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 298 | prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options()); |
| 299 | build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX"); |
Adnan AlSinan | b060806 | 2021-09-29 16:50:46 +0100 | [diff] [blame] | 300 | build_opts.add_option_if(is_quantized_asymmetric, "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale)); |
| 301 | build_opts.add_option_if(is_quantized_asymmetric, "-DBETA=" + float_to_string_with_full_precision(info.beta)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | |
| 303 | // Create kernel |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 304 | std::string kernel_name = std::string("softmax_layer_norm") + (is_quantized_asymmetric ? "_quantized" : ""); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 305 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | |
| 307 | // Configure window |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 308 | auto win = calculate_max_window(src, Steps(vector_size)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 309 | ICLKernel::configure_internal(win); |
| 310 | |
| 311 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 314 | Status ClLogits1DNormKernel::validate(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 315 | { |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 316 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(src, sum, dst, info)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 317 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 318 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 321 | void ClLogits1DNormKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | { |
| 323 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 324 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 325 | |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 326 | auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 327 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 328 | auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0)); |
| 329 | |
| 330 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, sum); |
| 331 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 332 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 333 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 334 | |
| 335 | do |
| 336 | { |
| 337 | Window sum_slice = slice; |
| 338 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 339 | |
| 340 | unsigned int idx = 0; |
| 341 | // Set inputs |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 342 | add_3D_tensor_argument(idx, src, slice); |
| 343 | add_3D_tensor_argument(idx, sum, sum_slice); |
| 344 | add_3D_tensor_argument(idx, dst, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 345 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 346 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 347 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | } |
Sang-Hoon Park | 201e0fe | 2021-01-27 13:14:56 +0000 | [diff] [blame] | 349 | } // namespace kernels |
| 350 | } // namespace opencl |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame] | 351 | } // namespace arm_compute |