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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h" |
| 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
| 30 | #include "arm_compute/core/Helpers.h" |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/KernelDescriptors.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 34 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/AccessWindowStatic.h" |
| 36 | #include "src/core/CL/CLValidate.h" |
| 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 39 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | |
| 41 | #include <set> |
| 42 | #include <string> |
| 43 | |
| 44 | using namespace arm_compute; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 45 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 46 | namespace |
| 47 | { |
| 48 | /** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options. |
| 49 | * |
| 50 | * Prepares these build options: |
| 51 | * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier. |
| 52 | * -DIFF_MIN - threshold difference between maximum value of input data and current processed value, |
| 53 | * it defines whether the value will be taken into account or not. |
| 54 | * |
| 55 | * @param[in] build_opts Build options to extend |
| 56 | * @param[in] input_scale Input scaling factor |
| 57 | * @param[in] beta Exponent scaling factor beta |
| 58 | */ |
| 59 | CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta) |
| 60 | { |
| 61 | // Number of integer bits in temporary fixed-point representation of current-to-max difference |
| 62 | static const int scaled_diff_int_bits = 5; |
| 63 | // Number of integer bits used in temporary fixed-point representation of exponent accumulator |
| 64 | static const int exp_accumulation_in_bits = 12; |
| 65 | |
| 66 | const double beta_multiplier = std::min( |
| 67 | 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)), |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 68 | (1LL << 31) - 1.0); |
| 69 | int input_beta_multiplier; |
| 70 | int input_beta_left_shift; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 71 | quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift); |
| 72 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 73 | 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] | 74 | const int diff_min = -1.f * std::floor(max_input_rescaled); |
| 75 | |
| 76 | CLBuildOptions build_opts; |
| 77 | build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits)); |
| 78 | build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits)); |
| 79 | build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier)); |
| 80 | build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift)); |
| 81 | build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min)); |
| 82 | |
| 83 | return build_opts; |
| 84 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 86 | 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] | 87 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 89 | 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] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output); |
| 91 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max); |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 93 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 94 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type()); |
| 95 | |
| 96 | // Checks performed when output is configured |
| 97 | if(output->total_size() != 0) |
| 98 | { |
| 99 | if(is_quantized_asymmetric) |
| 100 | { |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 106 | } |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Checks performed when sum is configured |
| 111 | if(sum->total_size() != 0) |
| 112 | { |
| 113 | if(is_quantized_asymmetric) |
| 114 | { |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum); |
| 120 | } |
| 121 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 124 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 127 | 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] | 128 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 130 | 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] | 131 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output); |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
Sang-Hoon Park | a0205b9 | 2020-07-07 09:36:09 +0100 | [diff] [blame] | 133 | 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] | 134 | |
| 135 | // 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] | 136 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
| 137 | 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] | 138 | |
| 139 | // Checks performed when output is configured |
| 140 | if(output->total_size() != 0) |
| 141 | { |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 143 | if(!is_quantized_asymmetric) |
| 144 | { |
| 145 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 146 | } |
| 147 | else |
| 148 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 149 | 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] | 150 | ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info); |
| 151 | } |
| 152 | } |
| 153 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 154 | return Status{}; |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Window validation |
| 158 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 159 | std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 160 | { |
| 161 | // Output auto initialization if not yet initialized |
| 162 | auto_init_if_empty(*sum, input->clone()->set_tensor_shape(max->tensor_shape())); |
| 163 | auto_init_if_empty(*output, *input->clone()); |
| 164 | |
| 165 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo parallel_reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(input->dimension(0)); |
| 166 | unsigned int vector_size = std::get<1>(parallel_reduction_info); |
| 167 | const unsigned int num_elems_x = ceil_to_multiple(input->tensor_shape().x(), vector_size); |
| 168 | Window win = calculate_max_window(*input, Steps(num_elems_x)); |
| 169 | |
| 170 | AccessWindowHorizontal input_access(input, 0, num_elems_x); |
| 171 | AccessWindowHorizontal max_access(max, 0, 1); |
| 172 | AccessWindowHorizontal output_access(output, 0, num_elems_x); |
| 173 | AccessWindowHorizontal sum_access(sum, 0, 1); |
| 174 | |
| 175 | bool window_changed = update_window_and_padding(win, input_access, max_access, output_access, sum_access); |
| 176 | |
| 177 | output_access.set_valid_region(win, input->valid_region()); |
| 178 | sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->tensor_shape())); |
| 179 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 180 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 181 | return std::make_pair(err, win); |
| 182 | } |
| 183 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 184 | std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum, const SoftmaxKernelInfo &info) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 185 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 186 | const DataType output_data_type = info.input_data_type; |
| 187 | const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 188 | |
| 189 | // Output auto initialization if not yet initialized |
| 190 | auto_init_if_empty(*output, |
| 191 | input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info)); |
| 192 | |
| 193 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 194 | |
| 195 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 196 | |
| 197 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 198 | AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1)); |
| 199 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 200 | |
| 201 | bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access); |
| 202 | |
| 203 | output_access.set_valid_region(win, input->valid_region()); |
| 204 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 205 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 206 | return std::make_pair(err, win); |
| 207 | } |
| 208 | |
| 209 | } // namespace |
| 210 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 211 | /**< Grid size (obtained through auto-tuning) */ |
| 212 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64; |
| 213 | /**< Vector size in the serial case (obtained through auto-tuning) */ |
| 214 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8; |
| 215 | /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/ |
| 216 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4; |
| 217 | |
| 218 | CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel() |
| 219 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 220 | { |
| 221 | } |
| 222 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 223 | 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] | 224 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 225 | configure(CLKernelLibrary::get().get_compile_context(), input, max, output, sum, info); |
| 226 | } |
| 227 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 228 | 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] | 229 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 230 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 231 | |
| 232 | // Output auto initialization if not yet initialized |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 233 | auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape())); |
| 234 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 235 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 236 | // Perform validation step |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 237 | 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] | 238 | |
| 239 | _input = input; |
| 240 | _max = max; |
| 241 | _output = output; |
| 242 | _sum = sum; |
| 243 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 244 | const DataType dt = input->info()->data_type(); |
| 245 | const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform(); |
| 246 | const size_t reduction_dim_size = input->info()->dimension(0); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 247 | const float beta = info.beta; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 248 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 249 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 250 | |
| 251 | // Set build options |
| 252 | CLBuildOptions build_opts; |
| 253 | 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] | 254 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
| 255 | build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 256 | build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 257 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta)); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 258 | build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options()); |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 259 | build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 260 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 261 | cl::NDRange lws_hint(cl::NullRange); |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 262 | std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") : |
| 263 | std::string("softmax_layer_max_shift_exp_sum_serial"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 264 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size); |
| 265 | unsigned int vector_size = std::get<1>(parallel_reduction_info); |
| 266 | |
| 267 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 268 | build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size)))); |
| 269 | build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE"); |
| 270 | |
| 271 | // Configure parallel kernel if needed |
| 272 | if(std::get<0>(parallel_reduction_info)) |
| 273 | { |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 274 | kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_parallel") : std::string("softmax_layer_max_shift_exp_sum_parallel"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 275 | bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0); |
| 276 | build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size)); |
| 277 | |
| 278 | // Handle boundary conditions. |
| 279 | const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size; |
| 280 | 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] | 281 | // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run(). |
| 282 | // 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] | 283 | lws_hint = cl::NDRange(_grid_size); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Create kernel. |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 287 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 288 | |
| 289 | // Set static arguments. Both the kernels use the same arguments |
| 290 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
| 291 | _kernel.setArg<cl_uint>(idx++, reduction_dim_size); |
| 292 | |
| 293 | // Configure window |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 294 | auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()); |
| 295 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 296 | ICLKernel::configure_internal(win_config.second, lws_hint); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 297 | } |
| 298 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 299 | 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] | 300 | { |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 301 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum)); |
| 302 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DMaxShiftExpSum(input->clone().get(), max->clone().get(), output->clone().get(), sum->clone().get()).first); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 303 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 304 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 307 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size) |
| 308 | { |
| 309 | bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1); |
| 310 | unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size; |
| 311 | return std::make_tuple(is_parallel_reduction, vector_size); |
| 312 | } |
| 313 | |
| 314 | void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue) |
| 315 | { |
| 316 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 317 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 318 | |
| 319 | // Collapse window in Z dimension |
| 320 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 321 | |
| 322 | // Reconfigure window in case of parallel reduction |
| 323 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0)); |
| 324 | if(std::get<0>(parallel_reduction_info)) |
| 325 | { |
| 326 | // To launch grid_size parallel workitems, steps.x should be modified as follows. |
| 327 | const unsigned int step = std::get<1>(parallel_reduction_info); |
| 328 | window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step)); |
| 329 | } |
| 330 | |
| 331 | // Get slices |
| 332 | Window slice = window_collapsed.first_slice_window_3D(); |
| 333 | do |
| 334 | { |
| 335 | unsigned int idx = 0; |
| 336 | // Set inputs |
| 337 | add_3D_tensor_argument(idx, _input, slice); |
| 338 | add_3D_tensor_argument(idx, _max, slice); |
| 339 | add_3D_tensor_argument(idx, _output, slice); |
| 340 | add_3D_tensor_argument(idx, _sum, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 341 | enqueue(queue, *this, slice, lws_hint()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 342 | } |
| 343 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 344 | } |
| 345 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 346 | CLLogits1DNormKernel::CLLogits1DNormKernel() |
| 347 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 348 | { |
| 349 | } |
| 350 | |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 351 | 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] | 352 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 353 | configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, info); |
| 354 | } |
| 355 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 356 | 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] | 357 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 358 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 359 | |
| 360 | // 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] | 361 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type); |
| 362 | const DataType output_data_type = info.input_data_type; |
| 363 | 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] | 364 | const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform(); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 365 | |
| 366 | // Output auto initialization if not yet initialized |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 367 | auto_init_if_empty(*output->info(), |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 368 | 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] | 369 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 370 | // Perform validation step |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 371 | 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] | 372 | |
| 373 | _input = input; |
| 374 | _sum = sum; |
| 375 | _output = output; |
| 376 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 377 | const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type); |
| 378 | const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0; |
| 379 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 380 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 381 | CLBuildOptions build_opts; |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 382 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type)); |
| 383 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value)); |
| 384 | 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] | 385 | build_opts.add_options_if(is_quantized_asymmetric, |
Sang-Hoon Park | 62eeb53 | 2019-10-29 13:13:19 +0000 | [diff] [blame] | 386 | prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options()); |
| 387 | build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 388 | |
| 389 | // Create kernel |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 390 | std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 391 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 392 | |
| 393 | // Configure window |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 394 | auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info(), info); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 395 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 396 | ICLKernel::configure_internal(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 399 | 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] | 400 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 401 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output, info)); |
| 402 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DNorm(input->clone().get(), output->clone().get(), sum->clone().get(), info).first); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 403 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 404 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 407 | void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue) |
| 408 | { |
| 409 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 410 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 411 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 412 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 413 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 414 | |
| 415 | do |
| 416 | { |
| 417 | Window sum_slice = slice; |
| 418 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 419 | |
| 420 | unsigned int idx = 0; |
| 421 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 422 | add_3D_tensor_argument(idx, _input, slice); |
| 423 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 424 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 425 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 426 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 427 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 428 | } |