blob: 6a18e5ffce8d7dd8d257e1af869e3ef3d3a88e5f [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);
Giorgio Arena4402cb92018-02-15 13:37:40 +000085 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QS8, DataType::QS16, 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);
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, max);
90
Georgios Pinitas30902ed2017-11-14 15:32:57 +000091 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type());
92
93 // Checks performed when output is configured
94 if(output->total_size() != 0)
95 {
96 if(is_quantized_asymmetric)
97 {
98 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
99 }
100 else
101 {
102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
103 }
104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
106 }
107
108 // Checks performed when sum is configured
109 if(sum->total_size() != 0)
110 {
111 if(is_quantized_asymmetric)
112 {
113 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32);
114 }
115 else
116 {
117 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum);
118 }
119 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum);
120 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(max, sum);
121 }
122
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000123 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000124}
125
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000126Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000127{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100128 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000129 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::S32, DataType::F16, DataType::F32);
130 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
131 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
132 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum);
133
134 // Note: output should always have a scale of 1/256 and offset 0
135 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
136 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
137
138 // Checks performed when output is configured
139 if(output->total_size() != 0)
140 {
141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
143 if(!is_quantized_asymmetric)
144 {
145 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
146 }
147 else
148 {
149 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
150 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
151 }
152 }
153
Georgios 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
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000184std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000185{
186 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
187 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
188 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->data_type();
189
190 // Output auto initialization if not yet initialized
191 auto_init_if_empty(*output,
192 input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
193
194 constexpr unsigned int num_elems_processed_per_iteration = 16;
195
196 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
197
198 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
199 AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1));
200 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
201
202 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
203
204 output_access.set_valid_region(win, input->valid_region());
205
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000206 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000207 return std::make_pair(err, win);
208}
209
210} // namespace
211
Chunosovd6afedc2017-11-06 22:09:45 +0700212/**< Grid size (obtained through auto-tuning) */
213const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
214/**< Vector size in the serial case (obtained through auto-tuning) */
215const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
216/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
217const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
218
219CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
220 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
221{
222}
223
224void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
225{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000226 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700227
228 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000229 auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape()));
230 auto_init_if_empty(*output->info(), *input->info()->clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700231
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000232 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000233 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700234
235 _input = input;
236 _max = max;
237 _output = output;
238 _sum = sum;
239
240 const DataType dt = input->info()->data_type();
241 const size_t reduction_dim_size = input->info()->dimension(0);
242 auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position())));
243
244 // Set build options
245 CLBuildOptions build_opts;
246 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
247 build_opts.add_option_if(is_data_type_fixed_point(dt),
248 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
249 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
250 build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), "-DBETA=" + support::cpp11::to_string(beta_int));
251 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
Giorgio Arena4402cb92018-02-15 13:37:40 +0000252 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 +0700253
Giorgio Arena4402cb92018-02-15 13:37:40 +0000254 _lws_hint = cl::NullRange;
255 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") :
256 std::string("softmax_layer_max_shift_exp_sum_serial");
Chunosovd6afedc2017-11-06 22:09:45 +0700257 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
258 unsigned int vector_size = std::get<1>(parallel_reduction_info);
259
260 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
261 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
262 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
263
264 // Configure parallel kernel if needed
265 if(std::get<0>(parallel_reduction_info))
266 {
Giorgio Arena4402cb92018-02-15 13:37:40 +0000267 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 +0700268 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
269 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
270
271 // Handle boundary conditions.
272 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
273 build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE");
Georgios Pinitas11f09992017-11-27 11:18:34 +0000274 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
275 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
276 _lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700277 }
278
279 // Create kernel.
280 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
281
282 // Set static arguments. Both the kernels use the same arguments
283 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
284 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
285
286 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000287 auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info());
288 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
289 ICLKernel::configure(win_config.second);
Chunosovd6afedc2017-11-06 22:09:45 +0700290}
291
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000292Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000293{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000294 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
295 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DMaxShiftExpSum(input->clone().get(), max->clone().get(), output->clone().get(), sum->clone().get()).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000296
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000297 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000298}
299
Chunosovd6afedc2017-11-06 22:09:45 +0700300CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
301{
302 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
303 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
304 return std::make_tuple(is_parallel_reduction, vector_size);
305}
306
307void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
308{
309 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
310 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
311
312 // Collapse window in Z dimension
313 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
314
315 // Reconfigure window in case of parallel reduction
316 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
317 if(std::get<0>(parallel_reduction_info))
318 {
319 // To launch grid_size parallel workitems, steps.x should be modified as follows.
320 const unsigned int step = std::get<1>(parallel_reduction_info);
321 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
322 }
323
324 // Get slices
325 Window slice = window_collapsed.first_slice_window_3D();
326 do
327 {
328 unsigned int idx = 0;
329 // Set inputs
330 add_3D_tensor_argument(idx, _input, slice);
331 add_3D_tensor_argument(idx, _max, slice);
332 add_3D_tensor_argument(idx, _output, slice);
333 add_3D_tensor_argument(idx, _sum, slice);
334 enqueue(queue, *this, slice, _lws_hint);
335 }
336 while(window_collapsed.slide_window_slice_3D(slice));
337}
338
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339CLLogits1DNormKernel::CLLogits1DNormKernel()
340 : _input(nullptr), _sum(nullptr), _output(nullptr)
341{
342}
343
Chunosovf450caa2017-11-08 16:09:35 +0700344void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000346 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700347
348 // Note: output should always have a scale of 1/256 and offset 0
349 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
350 const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32);
351 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100352
353 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700354 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000355 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100356
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000357 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000358 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359
360 _input = input;
361 _sum = sum;
362 _output = output;
363
364 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700365 CLBuildOptions build_opts;
366 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
367 build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()),
368 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
369 build_opts.add_options_if(is_quantized_asymmetric,
370 prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100371
372 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700373 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
374 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375
376 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000377 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info());
378 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
379 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380}
381
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000382Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000383{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000384 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output));
385 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DNorm(input->clone().get(), output->clone().get(), sum->clone().get()).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000386
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000387 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000388}
389
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
391{
392 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
393 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
394
steniu010d523cc2017-07-13 14:24:23 +0100395 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
396 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100397
398 do
399 {
400 Window sum_slice = slice;
401 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
402
403 unsigned int idx = 0;
404 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100405 add_3D_tensor_argument(idx, _input, slice);
406 add_3D_tensor_argument(idx, _sum, sum_slice);
407 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitas11f09992017-11-27 11:18:34 +0000408 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409 }
steniu010d523cc2017-07-13 14:24:23 +0100410 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411}