Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 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; |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | /** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options. |
| 45 | * |
| 46 | * Prepares these build options: |
| 47 | * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier. |
| 48 | * -DIFF_MIN - threshold difference between maximum value of input data and current processed value, |
| 49 | * it defines whether the value will be taken into account or not. |
| 50 | * |
| 51 | * @param[in] build_opts Build options to extend |
| 52 | * @param[in] input_scale Input scaling factor |
| 53 | * @param[in] beta Exponent scaling factor beta |
| 54 | */ |
| 55 | CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta) |
| 56 | { |
| 57 | // Number of integer bits in temporary fixed-point representation of current-to-max difference |
| 58 | static const int scaled_diff_int_bits = 5; |
| 59 | // Number of integer bits used in temporary fixed-point representation of exponent accumulator |
| 60 | static const int exp_accumulation_in_bits = 12; |
| 61 | |
| 62 | const double beta_multiplier = std::min( |
| 63 | 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)), |
| 64 | (1ll << 31) - 1.0); |
| 65 | int input_beta_multiplier, input_beta_left_shift; |
| 66 | quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift); |
| 67 | |
| 68 | const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1ll << (31 - scaled_diff_int_bits)) / (1ll << input_beta_left_shift); |
| 69 | const int diff_min = -1.f * std::floor(max_input_rescaled); |
| 70 | |
| 71 | CLBuildOptions build_opts; |
| 72 | build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits)); |
| 73 | build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits)); |
| 74 | build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier)); |
| 75 | build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift)); |
| 76 | build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min)); |
| 77 | |
| 78 | return build_opts; |
| 79 | } |
| 80 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
| 82 | void CLLogits1DMaxKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 83 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 84 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 86 | |
| 87 | // Softmax across the x dimension |
| 88 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 89 | output_shape.set(0, 1); |
| 90 | |
| 91 | // Output auto initialization if not yet initialized |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 92 | auto_init_if_empty(*output->info(), |
| 93 | output_shape, |
| 94 | 1, |
| 95 | input->info()->data_type(), |
| 96 | input->info()->fixed_point_position(), |
| 97 | input->info()->quantization_info()); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 101 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
| 103 | _input = input; |
| 104 | _output = output; |
| 105 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 106 | const DataType data_type = input->info()->data_type(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | // The kernel loops over all elements in steps of 16 |
| 108 | const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16); |
| 109 | |
| 110 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 111 | CLBuildOptions build_opts; |
| 112 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 113 | build_opts.add_option_if(is_data_type_fixed_point(data_type), |
| 114 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 115 | build_opts.add_option_if(data_type == DataType::F16, "-DUSE_F16"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | // Tell the kernel that the width is not a multiple of 16 |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 117 | build_opts.add_option_if((input->info()->dimension(0) % max_cl_vector_width) != 0, "-DNON_MULTIPLE_OF_16"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | // Create kernel |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 120 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "softmax_layer_max_quantized" : "softmax_layer_max"; |
| 121 | _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] | 122 | |
| 123 | // Set fixed arguments |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 124 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0)); |
| 126 | |
| 127 | // Configure kernel window |
| 128 | constexpr unsigned int num_elems_written_per_iteration = 1; |
| 129 | |
| 130 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 131 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 132 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 133 | |
| 134 | update_window_and_padding(win, input_access, output_access); |
| 135 | |
| 136 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 137 | |
| 138 | ICLKernel::configure(win); |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame^] | 139 | |
| 140 | // Set config_id for enabling LWS tuning |
| 141 | _config_id = "softmax_layer_"; |
| 142 | _config_id += lower_string(string_from_data_type(data_type)); |
| 143 | _config_id += "_"; |
| 144 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 145 | _config_id += "_"; |
| 146 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | CLLogits1DShiftExpSumKernel::CLLogits1DShiftExpSumKernel() |
| 150 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 151 | { |
| 152 | } |
| 153 | |
Pablo Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 154 | void CLLogits1DShiftExpSumKernel::configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 157 | ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output); |
| 158 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 159 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 160 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::S32 : input->info()->data_type(); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 161 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 162 | // Output auto initialization if not yet initialized |
| 163 | auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, tmp_data_type, input->info()->fixed_point_position()); |
| 164 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, tmp_data_type, input->info()->fixed_point_position()); |
| 165 | |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 166 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 167 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 168 | if(is_quantized_asymmetric) |
| 169 | { |
| 170 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, max); |
| 171 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(output, sum); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum); |
| 176 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum); |
| 177 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | |
| 179 | _input = input; |
| 180 | _max = max; |
| 181 | _output = output; |
| 182 | _sum = sum; |
| 183 | |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 184 | const DataType dt = input->info()->data_type(); |
| 185 | auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position()))); |
| 186 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | // The kernel loops over all elements in steps of 16 |
| 188 | const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16); |
| 189 | |
| 190 | // Set build options |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 191 | CLBuildOptions build_opts; |
| 192 | build_opts.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); |
| 193 | build_opts.add_option_if(is_data_type_fixed_point(dt), |
| 194 | std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()))); |
| 195 | build_opts.add_option_if(dt == DataType::F16, std::string("-DUSE_F16")); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | // Tell the kernel that the width is not a multiple of 16 |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 197 | build_opts.add_option_if((input->info()->dimension(0) % max_cl_vector_width) != 0, std::string("-DNON_MULTIPLE_OF_16")); |
| 198 | build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), std::string("-DBETA=" + support::cpp11::to_string(beta_int))); |
| 199 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), std::string("-DBETA=" + float_to_string_with_full_precision(beta))); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 200 | build_opts.add_options_if(is_quantized_asymmetric, |
| 201 | prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options()); |
Pablo Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 202 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | // Create kernel |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 204 | std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_shift_exp_sum_quantized" : "softmax_layer_shift_exp_sum"; |
| 205 | _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] | 206 | |
| 207 | // Set fixed arguments |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 208 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0)); |
| 210 | |
| 211 | // Configure window |
| 212 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 213 | |
| 214 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 215 | AccessWindowHorizontal max_access(max->info(), 0, 1); |
| 216 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 217 | AccessWindowHorizontal sum_access(sum->info(), 0, 1); |
| 218 | |
| 219 | update_window_and_padding(win, input_access, max_access, output_access, sum_access); |
| 220 | |
| 221 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 222 | sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape())); |
| 223 | |
| 224 | ICLKernel::configure(win); |
| 225 | } |
| 226 | |
| 227 | void CLLogits1DShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue) |
| 228 | { |
| 229 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 230 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 231 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 232 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 233 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | |
| 235 | do |
| 236 | { |
| 237 | unsigned int idx = 0; |
| 238 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 239 | add_3D_tensor_argument(idx, _input, slice); |
| 240 | add_3D_tensor_argument(idx, _max, slice); |
| 241 | add_3D_tensor_argument(idx, _output, slice); |
| 242 | add_3D_tensor_argument(idx, _sum, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | enqueue(queue, *this, slice); |
| 244 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 245 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 248 | /**< Grid size (obtained through auto-tuning) */ |
| 249 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64; |
| 250 | /**< Vector size in the serial case (obtained through auto-tuning) */ |
| 251 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8; |
| 252 | /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/ |
| 253 | const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4; |
| 254 | |
| 255 | CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel() |
| 256 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 257 | { |
| 258 | } |
| 259 | |
| 260 | void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta) |
| 261 | { |
| 262 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 263 | ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output); |
Chunosov | d6afedc | 2017-11-06 22:09:45 +0700 | [diff] [blame] | 264 | |
| 265 | // Output auto initialization if not yet initialized |
| 266 | auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 267 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 268 | |
| 269 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum); |
| 270 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum); |
| 271 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 272 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
| 273 | |
| 274 | _input = input; |
| 275 | _max = max; |
| 276 | _output = output; |
| 277 | _sum = sum; |
| 278 | |
| 279 | const DataType dt = input->info()->data_type(); |
| 280 | const size_t reduction_dim_size = input->info()->dimension(0); |
| 281 | auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position()))); |
| 282 | |
| 283 | // Set build options |
| 284 | CLBuildOptions build_opts; |
| 285 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)); |
| 286 | build_opts.add_option_if(is_data_type_fixed_point(dt), |
| 287 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 288 | build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16"); |
| 289 | build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), "-DBETA=" + support::cpp11::to_string(beta_int)); |
| 290 | build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta)); |
| 291 | |
| 292 | // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run(). |
| 293 | // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0]. |
| 294 | _lws_hint = cl::NullRange; |
| 295 | std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_serial"); |
| 296 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size); |
| 297 | unsigned int vector_size = std::get<1>(parallel_reduction_info); |
| 298 | |
| 299 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 300 | build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size)))); |
| 301 | build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE"); |
| 302 | |
| 303 | // Configure parallel kernel if needed |
| 304 | if(std::get<0>(parallel_reduction_info)) |
| 305 | { |
| 306 | kernel_name = std::string("softmax_layer_max_shift_exp_sum_parallel"); |
| 307 | bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0); |
| 308 | build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size)); |
| 309 | |
| 310 | // Handle boundary conditions. |
| 311 | const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size; |
| 312 | build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE"); |
| 313 | } |
| 314 | |
| 315 | // Create kernel. |
| 316 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 317 | |
| 318 | // Set static arguments. Both the kernels use the same arguments |
| 319 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
| 320 | _kernel.setArg<cl_uint>(idx++, reduction_dim_size); |
| 321 | |
| 322 | // Configure window |
| 323 | const unsigned int num_elems_x = ceil_to_multiple(input->info()->tensor_shape().x(), vector_size); |
| 324 | Window win = calculate_max_window(*input->info(), Steps(num_elems_x)); |
| 325 | |
| 326 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_x); |
| 327 | AccessWindowHorizontal max_access(max->info(), 0, 1); |
| 328 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_x); |
| 329 | AccessWindowHorizontal sum_access(sum->info(), 0, 1); |
| 330 | |
| 331 | update_window_and_padding(win, input_access, max_access, output_access, sum_access); |
| 332 | |
| 333 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 334 | sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape())); |
| 335 | |
| 336 | ICLKernel::configure(win); |
| 337 | } |
| 338 | |
| 339 | CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size) |
| 340 | { |
| 341 | bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1); |
| 342 | unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size; |
| 343 | return std::make_tuple(is_parallel_reduction, vector_size); |
| 344 | } |
| 345 | |
| 346 | void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue) |
| 347 | { |
| 348 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 349 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 350 | |
| 351 | // Collapse window in Z dimension |
| 352 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 353 | |
| 354 | // Reconfigure window in case of parallel reduction |
| 355 | ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0)); |
| 356 | if(std::get<0>(parallel_reduction_info)) |
| 357 | { |
| 358 | // To launch grid_size parallel workitems, steps.x should be modified as follows. |
| 359 | const unsigned int step = std::get<1>(parallel_reduction_info); |
| 360 | window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step)); |
| 361 | } |
| 362 | |
| 363 | // Get slices |
| 364 | Window slice = window_collapsed.first_slice_window_3D(); |
| 365 | do |
| 366 | { |
| 367 | unsigned int idx = 0; |
| 368 | // Set inputs |
| 369 | add_3D_tensor_argument(idx, _input, slice); |
| 370 | add_3D_tensor_argument(idx, _max, slice); |
| 371 | add_3D_tensor_argument(idx, _output, slice); |
| 372 | add_3D_tensor_argument(idx, _sum, slice); |
| 373 | enqueue(queue, *this, slice, _lws_hint); |
| 374 | } |
| 375 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 376 | } |
| 377 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 378 | CLLogits1DNormKernel::CLLogits1DNormKernel() |
| 379 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 380 | { |
| 381 | } |
| 382 | |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 383 | 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] | 384 | { |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 385 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::S32, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 386 | ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 387 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum); |
| 388 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum); |
| 389 | |
| 390 | // Note: output should always have a scale of 1/256 and offset 0 |
| 391 | const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0); |
| 392 | const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32); |
| 393 | 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] | 394 | |
| 395 | // Output auto initialization if not yet initialized |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 396 | auto_init_if_empty(*output->info(), |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 397 | 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] | 398 | |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 399 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 400 | if(!is_quantized_asymmetric) |
| 401 | { |
| 402 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 403 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | ARM_COMPUTE_ERROR_ON(output->info()->quantization_info() != allowed_quantization_info); |
| 408 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 409 | |
| 410 | _input = input; |
| 411 | _sum = sum; |
| 412 | _output = output; |
| 413 | |
| 414 | // Set build options |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 415 | CLBuildOptions build_opts; |
| 416 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 417 | build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()), |
| 418 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 419 | build_opts.add_options_if(is_quantized_asymmetric, |
| 420 | prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 421 | |
| 422 | // Create kernel |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 423 | std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm"; |
| 424 | _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] | 425 | |
| 426 | // Configure window |
| 427 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 428 | |
| 429 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 430 | |
| 431 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 432 | AccessWindowStatic sum_access(sum->info(), 0, 0, 1, sum->info()->dimension(1)); |
| 433 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 434 | |
| 435 | update_window_and_padding(win, input_access, sum_access, output_access); |
| 436 | |
| 437 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 438 | |
| 439 | ICLKernel::configure(win); |
| 440 | } |
| 441 | |
| 442 | void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue) |
| 443 | { |
| 444 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 445 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 446 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 447 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 448 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 449 | |
| 450 | do |
| 451 | { |
| 452 | Window sum_slice = slice; |
| 453 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 454 | |
| 455 | unsigned int idx = 0; |
| 456 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 457 | add_3D_tensor_argument(idx, _input, slice); |
| 458 | add_3D_tensor_argument(idx, _sum, sum_slice); |
| 459 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 460 | enqueue(queue, *this, slice); |
| 461 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 462 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 463 | } |