blob: 526d9e187d8259986a3eb76bd6b4629e934761e6 [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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLSoftmaxLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Chunosovf450caa2017-11-08 16:09:35 +070026#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010027#include "src/core/CL/CLValidate.h"
28#include "src/core/helpers/AutoConfiguration.h"
29#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000030#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Giorgio Arena2d1a8352020-10-26 15:04:08 +000032namespace arm_compute
33{
Chunosovf450caa2017-11-08 16:09:35 +070034namespace
35{
36/** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
37 *
38 * Prepares these build options:
39 * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
40 * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
41 * it defines whether the value will be taken into account or not.
42 *
43 * @param[in] build_opts Build options to extend
44 * @param[in] input_scale Input scaling factor
45 * @param[in] beta Exponent scaling factor beta
46 */
47CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
48{
49 // Number of integer bits in temporary fixed-point representation of current-to-max difference
50 static const int scaled_diff_int_bits = 5;
51 // Number of integer bits used in temporary fixed-point representation of exponent accumulator
52 static const int exp_accumulation_in_bits = 12;
53
54 const double beta_multiplier = std::min(
55 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
Michalis Spyroua4f378d2019-04-26 14:54:54 +010056 (1LL << 31) - 1.0);
57 int input_beta_multiplier;
58 int input_beta_left_shift;
Chunosovf450caa2017-11-08 16:09:35 +070059 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
60
Michalis Spyroua4f378d2019-04-26 14:54:54 +010061 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 +070062 const int diff_min = -1.f * std::floor(max_input_rescaled);
63
64 CLBuildOptions build_opts;
65 build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
66 build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
67 build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
68 build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
69 build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
70
71 return build_opts;
72}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
Giorgio Arena4402cb92018-02-15 13:37:40 +000074Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000075{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010076 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000077 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 +000078 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
79
Giorgio Arena4402cb92018-02-15 13:37:40 +000080 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
Giorgio Arena4402cb92018-02-15 13:37:40 +000081
Georgios Pinitas30902ed2017-11-14 15:32:57 +000082 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type());
83
84 // Checks performed when output is configured
85 if(output->total_size() != 0)
86 {
87 if(is_quantized_asymmetric)
88 {
89 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
90 }
91 else
92 {
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
94 }
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000096 }
97
98 // Checks performed when sum is configured
99 if(sum->total_size() != 0)
100 {
101 if(is_quantized_asymmetric)
102 {
103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32);
104 }
105 else
106 {
107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum);
108 }
109 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000110 }
111
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000112 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000113}
114
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000115Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000116{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100117 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100118 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 +0000119 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
120 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Sang-Hoon Parka0205b92020-07-07 09:36:09 +0100121 ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type));
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000122
123 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000124 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
125 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000126
127 // Checks performed when output is configured
128 if(output->total_size() != 0)
129 {
130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000131 if(!is_quantized_asymmetric)
132 {
133 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
134 }
135 else
136 {
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000137 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000138 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
139 }
140 }
141
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000142 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000143}
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000144} // namespace
145
Chunosovd6afedc2017-11-06 22:09:45 +0700146/**< Grid size (obtained through auto-tuning) */
147const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
148/**< Vector size in the serial case (obtained through auto-tuning) */
149const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
150/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
151const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
152
153CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
154 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
155{
156}
157
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000158void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info)
Chunosovd6afedc2017-11-06 22:09:45 +0700159{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100160 configure(CLKernelLibrary::get().get_compile_context(), input, max, output, sum, info);
161}
162
Manuel Bottini2803f702020-04-21 16:20:03 +0100163void 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 +0100164{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000165 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700166
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000167 auto padding_info = get_padding_info({ input, max, output, sum });
168
Chunosovd6afedc2017-11-06 22:09:45 +0700169 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000170 auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape()));
171 auto_init_if_empty(*output->info(), *input->info()->clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700172
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000173 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000174 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700175
176 _input = input;
177 _max = max;
178 _output = output;
179 _sum = sum;
180
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100181 const DataType dt = input->info()->data_type();
182 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
183 const size_t reduction_dim_size = input->info()->dimension(0);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000184 const float beta = info.beta;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000185 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
186 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700187
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000188 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
189 const unsigned int vector_size = adjust_vec_size(std::get<1>(parallel_reduction_info), reduction_dim_size);
190
Chunosovd6afedc2017-11-06 22:09:45 +0700191 // Set build options
192 CLBuildOptions build_opts;
193 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000194 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Chunosovd6afedc2017-11-06 22:09:45 +0700195 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000196 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size));
197 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(reduction_dim_size % vector_size));
Chunosovd6afedc2017-11-06 22:09:45 +0700198 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
199 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000200 build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
201 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
202 build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX");
203 build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX")));
204 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
205
206 cl::NDRange lws_hint(cl::NullRange);
207 std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") + (is_data_type_quantized_asymmetric(dt) ? "quantized_" : "");
Chunosovd6afedc2017-11-06 22:09:45 +0700208
209 // Configure parallel kernel if needed
210 if(std::get<0>(parallel_reduction_info))
211 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000212 kernel_name += "parallel";
Chunosovd6afedc2017-11-06 22:09:45 +0700213 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
214 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
215
216 // Handle boundary conditions.
217 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
218 build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE");
Georgios Pinitas11f09992017-11-27 11:18:34 +0000219 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
220 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100221 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700222 }
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000223 else
224 {
225 kernel_name += "serial";
226 }
Chunosovd6afedc2017-11-06 22:09:45 +0700227
228 // Create kernel.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100229 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Chunosovd6afedc2017-11-06 22:09:45 +0700230
Chunosovd6afedc2017-11-06 22:09:45 +0700231 // Configure window
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000232 Window win = calculate_max_window(*(input->info()), Steps(reduction_dim_size));
233 ICLKernel::configure_internal(win, lws_hint);
234
235 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Chunosovd6afedc2017-11-06 22:09:45 +0700236}
237
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000238Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000239{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000240 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000241 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000242}
243
Chunosovd6afedc2017-11-06 22:09:45 +0700244CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
245{
246 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
247 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
248 return std::make_tuple(is_parallel_reduction, vector_size);
249}
250
251void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
252{
253 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
254 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
255
256 // Collapse window in Z dimension
257 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
258
259 // Reconfigure window in case of parallel reduction
260 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
261 if(std::get<0>(parallel_reduction_info))
262 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000263 // Launch grid_size parallel work items
264 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1));
Chunosovd6afedc2017-11-06 22:09:45 +0700265 }
266
267 // Get slices
268 Window slice = window_collapsed.first_slice_window_3D();
269 do
270 {
271 unsigned int idx = 0;
272 // Set inputs
273 add_3D_tensor_argument(idx, _input, slice);
274 add_3D_tensor_argument(idx, _max, slice);
275 add_3D_tensor_argument(idx, _output, slice);
276 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100277 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700278 }
279 while(window_collapsed.slide_window_slice_3D(slice));
280}
281
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282CLLogits1DNormKernel::CLLogits1DNormKernel()
283 : _input(nullptr), _sum(nullptr), _output(nullptr)
284{
285}
286
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000287void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100289 configure(CLKernelLibrary::get().get_compile_context(), input, sum, output, info);
290}
291
Manuel Bottini2803f702020-04-21 16:20:03 +0100292void CLLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100293{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000294 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700295
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000296 auto padding_info = get_padding_info({ input, output, sum });
297
Chunosovf450caa2017-11-08 16:09:35 +0700298 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000299 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
300 const DataType output_data_type = info.input_data_type;
301 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100302 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100303
304 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700305 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000306 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100307
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000308 // Perform validation step
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000309 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info(), info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310
311 _input = input;
312 _sum = sum;
313 _output = output;
314
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000315 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
316 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
317 const unsigned int vector_size = adjust_vec_size(16, input->info()->dimension(0));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000318
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700320 CLBuildOptions build_opts;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000321 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type));
322 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000323 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
324 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(input->info()->dimension(0) % vector_size));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000325 build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED");
Chunosovf450caa2017-11-08 16:09:35 +0700326 build_opts.add_options_if(is_quantized_asymmetric,
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000327 prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
328 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329
330 // Create kernel
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000331 std::string kernel_name = std::string("softmax_layer_norm") + (is_quantized_asymmetric ? "_quantized" : "");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100332 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333
334 // Configure window
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000335 auto win = calculate_max_window(*(input->info()), Steps(vector_size));
336 ICLKernel::configure_internal(win);
337
338 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339}
340
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000341Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000342{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000343 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output, info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000344
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000345 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000346}
347
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
349{
350 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
351 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
352
steniu010d523cc2017-07-13 14:24:23 +0100353 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
354 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355
356 do
357 {
358 Window sum_slice = slice;
359 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
360
361 unsigned int idx = 0;
362 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100363 add_3D_tensor_argument(idx, _input, slice);
364 add_3D_tensor_argument(idx, _sum, sum_slice);
365 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100366 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367 }
steniu010d523cc2017-07-13 14:24:23 +0100368 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369}
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000370} // namespace arm_compute