Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLSoftmaxLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 26 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 27 | #include "src/core/CL/CLValidate.h" |
| 28 | #include "src/core/helpers/AutoConfiguration.h" |
| 29 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 30 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 32 | namespace arm_compute |
| 33 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 34 | namespace |
| 35 | { |
| 36 | /** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options. |
| 37 | * |
| 38 | * Prepares these build options: |
| 39 | * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier. |
| 40 | * -DIFF_MIN - threshold difference between maximum value of input data and current processed value, |
| 41 | * it defines whether the value will be taken into account or not. |
| 42 | * |
| 43 | * @param[in] build_opts Build options to extend |
| 44 | * @param[in] input_scale Input scaling factor |
| 45 | * @param[in] beta Exponent scaling factor beta |
| 46 | */ |
| 47 | CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta) |
| 48 | { |
| 49 | // Number of integer bits in temporary fixed-point representation of current-to-max difference |
| 50 | static const int scaled_diff_int_bits = 5; |
| 51 | // Number of integer bits used in temporary fixed-point representation of exponent accumulator |
| 52 | static const int exp_accumulation_in_bits = 12; |
| 53 | |
| 54 | const double beta_multiplier = std::min( |
| 55 | 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)), |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 56 | (1LL << 31) - 1.0); |
| 57 | int input_beta_multiplier; |
| 58 | int input_beta_left_shift; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 59 | quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift); |
| 60 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 61 | 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] | 62 | const int diff_min = -1.f * std::floor(max_input_rescaled); |
| 63 | |
| 64 | CLBuildOptions build_opts; |
| 65 | build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits)); |
| 66 | build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits)); |
| 67 | build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier)); |
| 68 | build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift)); |
| 69 | build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min)); |
| 70 | |
| 71 | return build_opts; |
| 72 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 74 | Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 75 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output); |
| 79 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max); |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 82 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type()); |
| 83 | |
| 84 | // Checks performed when output is configured |
| 85 | if(output->total_size() != 0) |
| 86 | { |
| 87 | if(is_quantized_asymmetric) |
| 88 | { |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 94 | } |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Checks performed when sum is configured |
| 99 | if(sum->total_size() != 0) |
| 100 | { |
| 101 | if(is_quantized_asymmetric) |
| 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum); |
| 108 | } |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 112 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 115 | Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 116 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32, DataType::F16, DataType::F32); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 119 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output); |
| 120 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Sang-Hoon Park | a0205b9 | 2020-07-07 09:36:09 +0100 | [diff] [blame] | 121 | 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] | 122 | |
| 123 | // 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] | 124 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
| 125 | 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] | 126 | |
| 127 | // Checks performed when output is configured |
| 128 | if(output->total_size() != 0) |
| 129 | { |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 131 | if(!is_quantized_asymmetric) |
| 132 | { |
| 133 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 134 | } |
| 135 | else |
| 136 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 137 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 138 | ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info); |
| 139 | } |
| 140 | } |
| 141 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 142 | return Status{}; |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 143 | } |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 144 | } // namespace |
| 145 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 146 | /**< Grid size (obtained through auto-tuning) */ |
| 147 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64; |
| 148 | /**< Vector size in the serial case (obtained through auto-tuning) */ |
| 149 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8; |
| 150 | /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/ |
| 151 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4; |
| 152 | |
| 153 | CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel() |
| 154 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 155 | { |
| 156 | } |
| 157 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 158 | void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info) |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 159 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 160 | configure(CLKernelLibrary::get().get_compile_context(), input, max, output, sum, info); |
| 161 | } |
| 162 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 163 | void CLLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 165 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 166 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 167 | auto padding_info = get_padding_info({ input, max, output, sum }); |
| 168 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 169 | // Output auto initialization if not yet initialized |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 170 | auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape())); |
| 171 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 172 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 173 | // Perform validation step |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 174 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info())); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 175 | |
| 176 | _input = input; |
| 177 | _max = max; |
| 178 | _output = output; |
| 179 | _sum = sum; |
| 180 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 181 | const DataType dt = input->info()->data_type(); |
| 182 | const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform(); |
| 183 | const size_t reduction_dim_size = input->info()->dimension(0); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 184 | const float beta = info.beta; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 185 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 186 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 187 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 188 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size); |
| 189 | const unsigned int vector_size = adjust_vec_size(std::get<1>(parallel_reduction_info), reduction_dim_size); |
| 190 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 191 | // Set build options |
| 192 | CLBuildOptions build_opts; |
| 193 | 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] | 194 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 195 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 196 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size)); |
| 197 | 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] | 198 | build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size)))); |
| 199 | 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^] | 200 | build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED"); |
| 201 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta)); |
| 202 | build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX"); |
| 203 | build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX"))); |
| 204 | build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options()); |
| 205 | |
| 206 | cl::NDRange lws_hint(cl::NullRange); |
| 207 | std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") + (is_data_type_quantized_asymmetric(dt) ? "quantized_" : ""); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 208 | |
| 209 | // Configure parallel kernel if needed |
| 210 | if(std::get<0>(parallel_reduction_info)) |
| 211 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 212 | kernel_name += "parallel"; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 213 | bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0); |
| 214 | build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size)); |
| 215 | |
| 216 | // Handle boundary conditions. |
| 217 | const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size; |
| 218 | build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE"); |
Georgios Pinitas | 11f0999 | 2017-11-27 11:18:34 +0000 | [diff] [blame] | 219 | // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run(). |
| 220 | // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0]. |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 221 | lws_hint = cl::NDRange(_grid_size); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 222 | } |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 223 | else |
| 224 | { |
| 225 | kernel_name += "serial"; |
| 226 | } |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 227 | |
| 228 | // Create kernel. |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 229 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 230 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 231 | // Configure window |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 232 | Window win = calculate_max_window(*(input->info()), Steps(reduction_dim_size)); |
| 233 | ICLKernel::configure_internal(win, lws_hint); |
| 234 | |
| 235 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 236 | } |
| 237 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 238 | Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 239 | { |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 240 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 241 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 244 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size) |
| 245 | { |
| 246 | bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1); |
| 247 | unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size; |
| 248 | return std::make_tuple(is_parallel_reduction, vector_size); |
| 249 | } |
| 250 | |
| 251 | void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue) |
| 252 | { |
| 253 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 254 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 255 | |
| 256 | // Collapse window in Z dimension |
| 257 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 258 | |
| 259 | // Reconfigure window in case of parallel reduction |
| 260 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0)); |
| 261 | if(std::get<0>(parallel_reduction_info)) |
| 262 | { |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 263 | // Launch grid_size parallel work items |
| 264 | window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1)); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Get slices |
| 268 | Window slice = window_collapsed.first_slice_window_3D(); |
| 269 | do |
| 270 | { |
| 271 | unsigned int idx = 0; |
| 272 | // Set inputs |
| 273 | add_3D_tensor_argument(idx, _input, slice); |
| 274 | add_3D_tensor_argument(idx, _max, slice); |
| 275 | add_3D_tensor_argument(idx, _output, slice); |
| 276 | add_3D_tensor_argument(idx, _sum, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 277 | enqueue(queue, *this, slice, lws_hint()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 278 | } |
| 279 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 280 | } |
| 281 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 282 | CLLogits1DNormKernel::CLLogits1DNormKernel() |
| 283 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 284 | { |
| 285 | } |
| 286 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 287 | void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 288 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 289 | configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, info); |
| 290 | } |
| 291 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 292 | void CLLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 293 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 294 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 295 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 296 | auto padding_info = get_padding_info({ input, output, sum }); |
| 297 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 298 | // 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] | 299 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type); |
| 300 | const DataType output_data_type = info.input_data_type; |
| 301 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 302 | const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform(); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 303 | |
| 304 | // Output auto initialization if not yet initialized |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 305 | auto_init_if_empty(*output->info(), |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 306 | input->info()->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] | 307 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 308 | // Perform validation step |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 309 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info(), info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 310 | |
| 311 | _input = input; |
| 312 | _sum = sum; |
| 313 | _output = output; |
| 314 | |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 315 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 316 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
| 317 | const unsigned int vector_size = adjust_vec_size(16, input->info()->dimension(0)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 318 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 320 | CLBuildOptions build_opts; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 321 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type)); |
| 322 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 323 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 324 | build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size)); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 325 | 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] | 326 | build_opts.add_options_if(is_quantized_asymmetric, |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 327 | prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options()); |
| 328 | build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 329 | |
| 330 | // Create kernel |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 331 | 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] | 332 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | |
| 334 | // Configure window |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 335 | auto win = calculate_max_window(*(input->info()), Steps(vector_size)); |
| 336 | ICLKernel::configure_internal(win); |
| 337 | |
| 338 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 341 | Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 342 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 343 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output, info)); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 344 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 345 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue) |
| 349 | { |
| 350 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 351 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 352 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 353 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 354 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 355 | |
| 356 | do |
| 357 | { |
| 358 | Window sum_slice = slice; |
| 359 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 360 | |
| 361 | unsigned int idx = 0; |
| 362 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 363 | add_3D_tensor_argument(idx, _input, slice); |
| 364 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 365 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 366 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 367 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 368 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 369 | } |
Giorgio Arena | 2d1a835 | 2020-10-26 15:04:08 +0000 | [diff] [blame^] | 370 | } // namespace arm_compute |