Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/CL/OpenCL.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Window.h" |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 36 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | #include <set> |
| 39 | #include <string> |
| 40 | |
| 41 | using namespace arm_compute; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 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)), |
| 65 | (1ll << 31) - 1.0); |
| 66 | int input_beta_multiplier, input_beta_left_shift; |
| 67 | quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift); |
| 68 | |
| 69 | const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1ll << (31 - scaled_diff_int_bits)) / (1ll << input_beta_left_shift); |
| 70 | const int diff_min = -1.f * std::floor(max_input_rescaled); |
| 71 | |
| 72 | CLBuildOptions build_opts; |
| 73 | build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits)); |
| 74 | build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits)); |
| 75 | build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier)); |
| 76 | build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift)); |
| 77 | build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min)); |
| 78 | |
| 79 | return build_opts; |
| 80 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 82 | 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] | 83 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 84 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output); |
| 87 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, max); |
| 90 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 91 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type()); |
| 92 | |
| 93 | // Checks performed when output is configured |
| 94 | if(output->total_size() != 0) |
| 95 | { |
| 96 | if(is_quantized_asymmetric) |
| 97 | { |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 103 | } |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 106 | } |
| 107 | |
| 108 | // Checks performed when sum is configured |
| 109 | if(sum->total_size() != 0) |
| 110 | { |
| 111 | if(is_quantized_asymmetric) |
| 112 | { |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum); |
| 118 | } |
| 119 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
| 120 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(max, sum); |
| 121 | } |
| 122 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 123 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 126 | Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 127 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::S32, DataType::F16, DataType::F32); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output); |
| 131 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum); |
| 133 | |
| 134 | // Note: output should always have a scale of 1/256 and offset 0 |
| 135 | const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0); |
| 136 | const bool is_quantized_asymmetric = (input->data_type() == DataType::S32); |
| 137 | |
| 138 | // Checks performed when output is configured |
| 139 | if(output->total_size() != 0) |
| 140 | { |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 143 | if(!is_quantized_asymmetric) |
| 144 | { |
| 145 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8); |
| 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 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 184 | std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum) |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 185 | { |
| 186 | const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0); |
| 187 | const bool is_quantized_asymmetric = (input->data_type() == DataType::S32); |
| 188 | const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->data_type(); |
| 189 | |
| 190 | // Output auto initialization if not yet initialized |
| 191 | auto_init_if_empty(*output, |
| 192 | input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info)); |
| 193 | |
| 194 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 195 | |
| 196 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 197 | |
| 198 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 199 | AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1)); |
| 200 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 201 | |
| 202 | bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access); |
| 203 | |
| 204 | output_access.set_valid_region(win, input->valid_region()); |
| 205 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 206 | 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] | 207 | return std::make_pair(err, win); |
| 208 | } |
| 209 | |
| 210 | } // namespace |
| 211 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 212 | /**< Grid size (obtained through auto-tuning) */ |
| 213 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64; |
| 214 | /**< Vector size in the serial case (obtained through auto-tuning) */ |
| 215 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8; |
| 216 | /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/ |
| 217 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4; |
| 218 | |
| 219 | CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel() |
| 220 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 221 | { |
| 222 | } |
| 223 | |
| 224 | void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta) |
| 225 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 226 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 227 | |
| 228 | // Output auto initialization if not yet initialized |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 229 | auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape())); |
| 230 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 231 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 232 | // Perform validation step |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 233 | 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] | 234 | |
| 235 | _input = input; |
| 236 | _max = max; |
| 237 | _output = output; |
| 238 | _sum = sum; |
| 239 | |
| 240 | const DataType dt = input->info()->data_type(); |
| 241 | const size_t reduction_dim_size = input->info()->dimension(0); |
| 242 | auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position()))); |
| 243 | |
| 244 | // Set build options |
| 245 | CLBuildOptions build_opts; |
| 246 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)); |
| 247 | build_opts.add_option_if(is_data_type_fixed_point(dt), |
| 248 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 249 | build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16"); |
| 250 | build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), "-DBETA=" + support::cpp11::to_string(beta_int)); |
| 251 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta)); |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 252 | build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options()); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 253 | |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 254 | _lws_hint = cl::NullRange; |
| 255 | std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") : |
| 256 | std::string("softmax_layer_max_shift_exp_sum_serial"); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 257 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size); |
| 258 | unsigned int vector_size = std::get<1>(parallel_reduction_info); |
| 259 | |
| 260 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 261 | build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size)))); |
| 262 | build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE"); |
| 263 | |
| 264 | // Configure parallel kernel if needed |
| 265 | if(std::get<0>(parallel_reduction_info)) |
| 266 | { |
Giorgio Arena | 4402cb9 | 2018-02-15 13:37:40 +0000 | [diff] [blame] | 267 | 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] | 268 | bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0); |
| 269 | build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size)); |
| 270 | |
| 271 | // Handle boundary conditions. |
| 272 | const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size; |
| 273 | 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] | 274 | // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run(). |
| 275 | // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0]. |
| 276 | _lws_hint = cl::NDRange(_grid_size); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Create kernel. |
| 280 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 281 | |
| 282 | // Set static arguments. Both the kernels use the same arguments |
| 283 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
| 284 | _kernel.setArg<cl_uint>(idx++, reduction_dim_size); |
| 285 | |
| 286 | // Configure window |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 287 | auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()); |
| 288 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 289 | ICLKernel::configure(win_config.second); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 290 | } |
| 291 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 292 | 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] | 293 | { |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 294 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum)); |
| 295 | 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] | 296 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 297 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 300 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size) |
| 301 | { |
| 302 | bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1); |
| 303 | unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size; |
| 304 | return std::make_tuple(is_parallel_reduction, vector_size); |
| 305 | } |
| 306 | |
| 307 | void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue) |
| 308 | { |
| 309 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 310 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 311 | |
| 312 | // Collapse window in Z dimension |
| 313 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 314 | |
| 315 | // Reconfigure window in case of parallel reduction |
| 316 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0)); |
| 317 | if(std::get<0>(parallel_reduction_info)) |
| 318 | { |
| 319 | // To launch grid_size parallel workitems, steps.x should be modified as follows. |
| 320 | const unsigned int step = std::get<1>(parallel_reduction_info); |
| 321 | window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step)); |
| 322 | } |
| 323 | |
| 324 | // Get slices |
| 325 | Window slice = window_collapsed.first_slice_window_3D(); |
| 326 | do |
| 327 | { |
| 328 | unsigned int idx = 0; |
| 329 | // Set inputs |
| 330 | add_3D_tensor_argument(idx, _input, slice); |
| 331 | add_3D_tensor_argument(idx, _max, slice); |
| 332 | add_3D_tensor_argument(idx, _output, slice); |
| 333 | add_3D_tensor_argument(idx, _sum, slice); |
| 334 | enqueue(queue, *this, slice, _lws_hint); |
| 335 | } |
| 336 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 337 | } |
| 338 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | CLLogits1DNormKernel::CLLogits1DNormKernel() |
| 340 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 341 | { |
| 342 | } |
| 343 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 344 | void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 345 | { |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 346 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 347 | |
| 348 | // Note: output should always have a scale of 1/256 and offset 0 |
| 349 | const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0); |
| 350 | const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32); |
| 351 | const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type(); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 352 | |
| 353 | // Output auto initialization if not yet initialized |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 354 | auto_init_if_empty(*output->info(), |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 355 | 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] | 356 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 357 | // Perform validation step |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 358 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 359 | |
| 360 | _input = input; |
| 361 | _sum = sum; |
| 362 | _output = output; |
| 363 | |
| 364 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 365 | CLBuildOptions build_opts; |
| 366 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 367 | build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()), |
| 368 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 369 | build_opts.add_options_if(is_quantized_asymmetric, |
| 370 | prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 371 | |
| 372 | // Create kernel |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 373 | std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm"; |
| 374 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | |
| 376 | // Configure window |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 377 | auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info()); |
| 378 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 379 | ICLKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 382 | Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 383 | { |
Giorgio Arena | b8ab997 | 2017-11-29 15:09:39 +0000 | [diff] [blame] | 384 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output)); |
| 385 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DNorm(input->clone().get(), output->clone().get(), sum->clone().get()).first); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 386 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 387 | return Status{}; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue) |
| 391 | { |
| 392 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 393 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 394 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 395 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 396 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 397 | |
| 398 | do |
| 399 | { |
| 400 | Window sum_slice = slice; |
| 401 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 402 | |
| 403 | unsigned int idx = 0; |
| 404 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 405 | add_3D_tensor_argument(idx, _input, slice); |
| 406 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 407 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 11f0999 | 2017-11-27 11:18:34 +0000 | [diff] [blame] | 408 | enqueue(queue, *this, slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 409 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 410 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 411 | } |