blob: 5c0acda41a75ebdcdaa2749614eb0f20bec6973c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Barbier6ff3b192017-09-04 18:44:23 +010026#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 Park62eeb532019-10-29 13:13:19 +000031#include "arm_compute/core/KernelDescriptors.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
Chunosovf450caa2017-11-08 16:09:35 +070034#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#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 Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <set>
42#include <string>
43
44using namespace arm_compute;
Georgios Pinitas30902ed2017-11-14 15:32:57 +000045
Chunosovf450caa2017-11-08 16:09:35 +070046namespace
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 */
59CLBuildOptions 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 Spyroua4f378d2019-04-26 14:54:54 +010068 (1LL << 31) - 1.0);
69 int input_beta_multiplier;
70 int input_beta_left_shift;
Chunosovf450caa2017-11-08 16:09:35 +070071 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
72
Michalis Spyroua4f378d2019-04-26 14:54:54 +010073 const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift);
Chunosovf450caa2017-11-08 16:09:35 +070074 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 Barbier6ff3b192017-09-04 18:44:23 +010085
Giorgio Arena4402cb92018-02-15 13:37:40 +000086Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010088 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000089 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000090 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
91
Giorgio Arena4402cb92018-02-15 13:37:40 +000092 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
Giorgio Arena4402cb92018-02-15 13:37:40 +000093
Georgios Pinitas30902ed2017-11-14 15:32:57 +000094 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 Pinitas30902ed2017-11-14 15:32:57 +0000108 }
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 Pinitas30902ed2017-11-14 15:32:57 +0000122 }
123
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000124 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000125}
126
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000127Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000128{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100129 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100130 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32, DataType::F16, DataType::F32);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000131 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
132 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Sang-Hoon Parka0205b92020-07-07 09:36:09 +0100133 ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type));
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000134
135 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000136 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 Arenab8ab9972017-11-29 15:09:39 +0000138
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 Arenab8ab9972017-11-29 15:09:39 +0000143 if(!is_quantized_asymmetric)
144 {
145 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
146 }
147 else
148 {
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000149 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000150 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
151 }
152 }
153
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000154 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000155}
156
157// Window validation
158
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000159std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000160{
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 Pinitas631c41a2017-12-06 11:53:03 +0000180 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000181 return std::make_pair(err, win);
182}
183
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000184std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000185{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000186 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 Arenab8ab9972017-11-29 15:09:39 +0000188
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 Pinitas631c41a2017-12-06 11:53:03 +0000205 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000206 return std::make_pair(err, win);
207}
208
209} // namespace
210
Chunosovd6afedc2017-11-06 22:09:45 +0700211/**< Grid size (obtained through auto-tuning) */
212const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
213/**< Vector size in the serial case (obtained through auto-tuning) */
214const 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) .*/
216const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
217
218CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
219 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
220{
221}
222
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000223void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info)
Chunosovd6afedc2017-11-06 22:09:45 +0700224{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100225 configure(CLKernelLibrary::get().get_compile_context(), input, max, output, sum, info);
226}
227
Manuel Bottini2803f702020-04-21 16:20:03 +0100228void CLLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100229{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000230 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700231
232 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000233 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());
Chunosovd6afedc2017-11-06 22:09:45 +0700235
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000236 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000237 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700238
239 _input = input;
240 _max = max;
241 _output = output;
242 _sum = sum;
243
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100244 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 Park62eeb532019-10-29 13:13:19 +0000247 const float beta = info.beta;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000248 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;
Chunosovd6afedc2017-11-06 22:09:45 +0700250
251 // Set build options
252 CLBuildOptions build_opts;
253 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000254 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
255 build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
Chunosovd6afedc2017-11-06 22:09:45 +0700256 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
Chunosovd6afedc2017-11-06 22:09:45 +0700257 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100258 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000259 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Chunosovd6afedc2017-11-06 22:09:45 +0700260
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100261 cl::NDRange lws_hint(cl::NullRange);
Giorgio Arena4402cb92018-02-15 13:37:40 +0000262 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");
Chunosovd6afedc2017-11-06 22:09:45 +0700264 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 Arena4402cb92018-02-15 13:37:40 +0000274 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");
Chunosovd6afedc2017-11-06 22:09:45 +0700275 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 Pinitas11f09992017-11-27 11:18:34 +0000281 // 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 Barbierb6eb3532018-08-08 13:20:04 +0100283 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700284 }
285
286 // Create kernel.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100287 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Chunosovd6afedc2017-11-06 22:09:45 +0700288
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 Arenab8ab9972017-11-29 15:09:39 +0000294 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 Barbierb6eb3532018-08-08 13:20:04 +0100296 ICLKernel::configure_internal(win_config.second, lws_hint);
Chunosovd6afedc2017-11-06 22:09:45 +0700297}
298
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000299Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000300{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000301 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 Pinitas30902ed2017-11-14 15:32:57 +0000303
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000304 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000305}
306
Chunosovd6afedc2017-11-06 22:09:45 +0700307CLLogits1DMaxShiftExpSumKernel::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
314void 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 Barbierb6eb3532018-08-08 13:20:04 +0100341 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700342 }
343 while(window_collapsed.slide_window_slice_3D(slice));
344}
345
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346CLLogits1DNormKernel::CLLogits1DNormKernel()
347 : _input(nullptr), _sum(nullptr), _output(nullptr)
348{
349}
350
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000351void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100353 configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, info);
354}
355
Manuel Bottini2803f702020-04-21 16:20:03 +0100356void CLLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100357{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000358 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700359
360 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000361 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 Pinitas4c5469b2019-05-21 13:32:43 +0100364 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100365
366 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700367 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000368 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100369
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000370 // Perform validation step
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000371 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info(), info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372
373 _input = input;
374 _sum = sum;
375 _output = output;
376
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000377 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 Barbier6ff3b192017-09-04 18:44:23 +0100380 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700381 CLBuildOptions build_opts;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000382 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");
Chunosovf450caa2017-11-08 16:09:35 +0700385 build_opts.add_options_if(is_quantized_asymmetric,
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000386 prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
387 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388
389 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700390 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100391 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392
393 // Configure window
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000394 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info(), info);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000395 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100396 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100397}
398
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000399Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000400{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000401 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 Pinitas30902ed2017-11-14 15:32:57 +0000403
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000404 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000405}
406
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407void 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
steniu010d523cc2017-07-13 14:24:23 +0100412 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
413 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414
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
steniu010d523cc2017-07-13 14:24:23 +0100422 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 Barbierb6eb3532018-08-08 13:20:04 +0100425 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 }
steniu010d523cc2017-07-13 14:24:23 +0100427 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428}