blob: 403256baae5327ce0a1e8be929c57e8dcfbf76d5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena4402cb92018-02-15 13:37:40 +00002 * Copyright (c) 2017-2018 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
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 Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#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 Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Window.h"
Chunosovf450caa2017-11-08 16:09:35 +070036#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <set>
39#include <string>
40
41using namespace arm_compute;
Georgios Pinitas30902ed2017-11-14 15:32:57 +000042
Chunosovf450caa2017-11-08 16:09:35 +070043namespace
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 */
56CLBuildOptions 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 Barbier6ff3b192017-09-04 18:44:23 +010081
Giorgio Arena4402cb92018-02-15 13:37:40 +000082Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000083{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010084 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010085 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000086 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
87
Giorgio Arena4402cb92018-02-15 13:37:40 +000088 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
Giorgio Arena4402cb92018-02-15 13:37:40 +000089
Georgios Pinitas30902ed2017-11-14 15:32:57 +000090 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type());
91
92 // Checks performed when output is configured
93 if(output->total_size() != 0)
94 {
95 if(is_quantized_asymmetric)
96 {
97 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
98 }
99 else
100 {
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
102 }
103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000104 }
105
106 // Checks performed when sum is configured
107 if(sum->total_size() != 0)
108 {
109 if(is_quantized_asymmetric)
110 {
111 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32);
112 }
113 else
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum);
116 }
117 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000118 }
119
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000120 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000121}
122
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000123Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000124{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100125 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100126 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 +0000127 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000129
130 // Note: output should always have a scale of 1/256 and offset 0
131 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
132 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
133
134 // Checks performed when output is configured
135 if(output->total_size() != 0)
136 {
137 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000138 if(!is_quantized_asymmetric)
139 {
140 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
141 }
142 else
143 {
144 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
145 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
146 }
147 }
148
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000149 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000150}
151
152// Window validation
153
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000154std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000155{
156 // Output auto initialization if not yet initialized
157 auto_init_if_empty(*sum, input->clone()->set_tensor_shape(max->tensor_shape()));
158 auto_init_if_empty(*output, *input->clone());
159
160 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo parallel_reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(input->dimension(0));
161 unsigned int vector_size = std::get<1>(parallel_reduction_info);
162 const unsigned int num_elems_x = ceil_to_multiple(input->tensor_shape().x(), vector_size);
163 Window win = calculate_max_window(*input, Steps(num_elems_x));
164
165 AccessWindowHorizontal input_access(input, 0, num_elems_x);
166 AccessWindowHorizontal max_access(max, 0, 1);
167 AccessWindowHorizontal output_access(output, 0, num_elems_x);
168 AccessWindowHorizontal sum_access(sum, 0, 1);
169
170 bool window_changed = update_window_and_padding(win, input_access, max_access, output_access, sum_access);
171
172 output_access.set_valid_region(win, input->valid_region());
173 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->tensor_shape()));
174
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000175 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000176 return std::make_pair(err, win);
177}
178
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000179std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000180{
181 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
182 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
183 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->data_type();
184
185 // Output auto initialization if not yet initialized
186 auto_init_if_empty(*output,
187 input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
188
189 constexpr unsigned int num_elems_processed_per_iteration = 16;
190
191 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
192
193 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
194 AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1));
195 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
196
197 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
198
199 output_access.set_valid_region(win, input->valid_region());
200
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000201 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000202 return std::make_pair(err, win);
203}
204
205} // namespace
206
Chunosovd6afedc2017-11-06 22:09:45 +0700207/**< Grid size (obtained through auto-tuning) */
208const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
209/**< Vector size in the serial case (obtained through auto-tuning) */
210const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
211/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
212const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
213
214CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
215 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
216{
217}
218
219void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
220{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000221 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700222
223 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000224 auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape()));
225 auto_init_if_empty(*output->info(), *input->info()->clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700226
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000227 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000228 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700229
230 _input = input;
231 _max = max;
232 _output = output;
233 _sum = sum;
234
235 const DataType dt = input->info()->data_type();
236 const size_t reduction_dim_size = input->info()->dimension(0);
Chunosovd6afedc2017-11-06 22:09:45 +0700237
238 // Set build options
239 CLBuildOptions build_opts;
240 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Chunosovd6afedc2017-11-06 22:09:45 +0700241 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
Chunosovd6afedc2017-11-06 22:09:45 +0700242 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
Giorgio Arena4402cb92018-02-15 13:37:40 +0000243 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Chunosovd6afedc2017-11-06 22:09:45 +0700244
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100245 cl::NDRange lws_hint(cl::NullRange);
Giorgio Arena4402cb92018-02-15 13:37:40 +0000246 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") :
247 std::string("softmax_layer_max_shift_exp_sum_serial");
Chunosovd6afedc2017-11-06 22:09:45 +0700248 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
249 unsigned int vector_size = std::get<1>(parallel_reduction_info);
250
251 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
252 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
253 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
254
255 // Configure parallel kernel if needed
256 if(std::get<0>(parallel_reduction_info))
257 {
Giorgio Arena4402cb92018-02-15 13:37:40 +0000258 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 +0700259 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
260 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
261
262 // Handle boundary conditions.
263 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
264 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 +0000265 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
266 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100267 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700268 }
269
270 // Create kernel.
271 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
272
273 // Set static arguments. Both the kernels use the same arguments
274 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
275 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
276
277 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000278 auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info());
279 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100280 ICLKernel::configure_internal(win_config.second, lws_hint);
Chunosovd6afedc2017-11-06 22:09:45 +0700281}
282
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000283Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000284{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000285 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
286 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 +0000287
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000288 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000289}
290
Chunosovd6afedc2017-11-06 22:09:45 +0700291CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
292{
293 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
294 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
295 return std::make_tuple(is_parallel_reduction, vector_size);
296}
297
298void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
299{
300 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
301 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
302
303 // Collapse window in Z dimension
304 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
305
306 // Reconfigure window in case of parallel reduction
307 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
308 if(std::get<0>(parallel_reduction_info))
309 {
310 // To launch grid_size parallel workitems, steps.x should be modified as follows.
311 const unsigned int step = std::get<1>(parallel_reduction_info);
312 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
313 }
314
315 // Get slices
316 Window slice = window_collapsed.first_slice_window_3D();
317 do
318 {
319 unsigned int idx = 0;
320 // Set inputs
321 add_3D_tensor_argument(idx, _input, slice);
322 add_3D_tensor_argument(idx, _max, slice);
323 add_3D_tensor_argument(idx, _output, slice);
324 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100325 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700326 }
327 while(window_collapsed.slide_window_slice_3D(slice));
328}
329
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330CLLogits1DNormKernel::CLLogits1DNormKernel()
331 : _input(nullptr), _sum(nullptr), _output(nullptr)
332{
333}
334
Chunosovf450caa2017-11-08 16:09:35 +0700335void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000337 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700338
339 // Note: output should always have a scale of 1/256 and offset 0
340 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
341 const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32);
342 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100343
344 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700345 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000346 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100347
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000348 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000349 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350
351 _input = input;
352 _sum = sum;
353 _output = output;
354
355 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700356 CLBuildOptions build_opts;
357 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Chunosovf450caa2017-11-08 16:09:35 +0700358 build_opts.add_options_if(is_quantized_asymmetric,
359 prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360
361 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700362 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
363 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364
365 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000366 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info());
367 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100368 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369}
370
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000371Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000372{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000373 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output));
374 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DNorm(input->clone().get(), output->clone().get(), sum->clone().get()).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000375
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000376 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000377}
378
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
380{
381 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
382 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
383
steniu010d523cc2017-07-13 14:24:23 +0100384 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
385 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386
387 do
388 {
389 Window sum_slice = slice;
390 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
391
392 unsigned int idx = 0;
393 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100394 add_3D_tensor_argument(idx, _input, slice);
395 add_3D_tensor_argument(idx, _sum, sum_slice);
396 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100397 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100398 }
steniu010d523cc2017-07-13 14:24:23 +0100399 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400}