blob: 447d6eeafa63cd8f9acfa4f102ca26e28a4510a9 [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"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
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{
Giorgio Arena4402cb92018-02-15 13:37:40 +000084 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 +000085 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
86
Giorgio Arena4402cb92018-02-15 13:37:40 +000087 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, max);
89
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);
104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
105 }
106
107 // Checks performed when sum is configured
108 if(sum->total_size() != 0)
109 {
110 if(is_quantized_asymmetric)
111 {
112 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(sum, 1, DataType::S32);
113 }
114 else
115 {
116 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(max, sum);
117 }
118 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(max, sum);
119 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(max, sum);
120 }
121
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000122 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000123}
124
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000125Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000126{
127 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::S32, DataType::F16, DataType::F32);
128 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum);
131
132 // Note: output should always have a scale of 1/256 and offset 0
133 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
134 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
135
136 // Checks performed when output is configured
137 if(output->total_size() != 0)
138 {
139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
140 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
141 if(!is_quantized_asymmetric)
142 {
143 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
144 }
145 else
146 {
147 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
148 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
149 }
150 }
151
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000152 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000153}
154
155// Window validation
156
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000157std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000158{
159 // Output auto initialization if not yet initialized
160 auto_init_if_empty(*sum, input->clone()->set_tensor_shape(max->tensor_shape()));
161 auto_init_if_empty(*output, *input->clone());
162
163 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo parallel_reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(input->dimension(0));
164 unsigned int vector_size = std::get<1>(parallel_reduction_info);
165 const unsigned int num_elems_x = ceil_to_multiple(input->tensor_shape().x(), vector_size);
166 Window win = calculate_max_window(*input, Steps(num_elems_x));
167
168 AccessWindowHorizontal input_access(input, 0, num_elems_x);
169 AccessWindowHorizontal max_access(max, 0, 1);
170 AccessWindowHorizontal output_access(output, 0, num_elems_x);
171 AccessWindowHorizontal sum_access(sum, 0, 1);
172
173 bool window_changed = update_window_and_padding(win, input_access, max_access, output_access, sum_access);
174
175 output_access.set_valid_region(win, input->valid_region());
176 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->tensor_shape()));
177
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000178 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000179 return std::make_pair(err, win);
180}
181
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000182std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000183{
184 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
185 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
186 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->data_type();
187
188 // Output auto initialization if not yet initialized
189 auto_init_if_empty(*output,
190 input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
191
192 constexpr unsigned int num_elems_processed_per_iteration = 16;
193
194 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
195
196 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
197 AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1));
198 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
199
200 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
201
202 output_access.set_valid_region(win, input->valid_region());
203
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000204 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000205 return std::make_pair(err, win);
206}
207
208} // namespace
209
Chunosovd6afedc2017-11-06 22:09:45 +0700210/**< Grid size (obtained through auto-tuning) */
211const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
212/**< Vector size in the serial case (obtained through auto-tuning) */
213const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
214/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
215const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
216
217CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
218 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
219{
220}
221
222void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
223{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000224 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700225
226 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000227 auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape()));
228 auto_init_if_empty(*output->info(), *input->info()->clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700229
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000230 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000231 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700232
233 _input = input;
234 _max = max;
235 _output = output;
236 _sum = sum;
237
238 const DataType dt = input->info()->data_type();
239 const size_t reduction_dim_size = input->info()->dimension(0);
240 auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position())));
241
242 // Set build options
243 CLBuildOptions build_opts;
244 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
245 build_opts.add_option_if(is_data_type_fixed_point(dt),
246 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
247 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
248 build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), "-DBETA=" + support::cpp11::to_string(beta_int));
249 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 +0000250 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 +0700251
Giorgio Arena4402cb92018-02-15 13:37:40 +0000252 _lws_hint = cl::NullRange;
253 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") :
254 std::string("softmax_layer_max_shift_exp_sum_serial");
Chunosovd6afedc2017-11-06 22:09:45 +0700255 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
256 unsigned int vector_size = std::get<1>(parallel_reduction_info);
257
258 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
259 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
260 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
261
262 // Configure parallel kernel if needed
263 if(std::get<0>(parallel_reduction_info))
264 {
Giorgio Arena4402cb92018-02-15 13:37:40 +0000265 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 +0700266 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
267 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
268
269 // Handle boundary conditions.
270 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
271 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 +0000272 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
273 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
274 _lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700275 }
276
277 // Create kernel.
278 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
279
280 // Set static arguments. Both the kernels use the same arguments
281 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
282 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
283
284 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000285 auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info());
286 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
287 ICLKernel::configure(win_config.second);
Chunosovd6afedc2017-11-06 22:09:45 +0700288}
289
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000290Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000291{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000292 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
293 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 +0000294
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000295 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000296}
297
Chunosovd6afedc2017-11-06 22:09:45 +0700298CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
299{
300 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
301 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
302 return std::make_tuple(is_parallel_reduction, vector_size);
303}
304
305void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
306{
307 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
308 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
309
310 // Collapse window in Z dimension
311 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
312
313 // Reconfigure window in case of parallel reduction
314 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
315 if(std::get<0>(parallel_reduction_info))
316 {
317 // To launch grid_size parallel workitems, steps.x should be modified as follows.
318 const unsigned int step = std::get<1>(parallel_reduction_info);
319 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
320 }
321
322 // Get slices
323 Window slice = window_collapsed.first_slice_window_3D();
324 do
325 {
326 unsigned int idx = 0;
327 // Set inputs
328 add_3D_tensor_argument(idx, _input, slice);
329 add_3D_tensor_argument(idx, _max, slice);
330 add_3D_tensor_argument(idx, _output, slice);
331 add_3D_tensor_argument(idx, _sum, slice);
332 enqueue(queue, *this, slice, _lws_hint);
333 }
334 while(window_collapsed.slide_window_slice_3D(slice));
335}
336
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100337CLLogits1DNormKernel::CLLogits1DNormKernel()
338 : _input(nullptr), _sum(nullptr), _output(nullptr)
339{
340}
341
Chunosovf450caa2017-11-08 16:09:35 +0700342void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000344 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700345
346 // Note: output should always have a scale of 1/256 and offset 0
347 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
348 const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32);
349 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100350
351 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700352 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000353 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100354
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000355 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000356 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100357
358 _input = input;
359 _sum = sum;
360 _output = output;
361
362 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700363 CLBuildOptions build_opts;
364 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
365 build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()),
366 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
367 build_opts.add_options_if(is_quantized_asymmetric,
368 prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369
370 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700371 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
372 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373
374 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000375 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info());
376 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
377 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378}
379
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000380Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000381{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000382 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output));
383 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 +0000384
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000385 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000386}
387
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
389{
390 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
391 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
392
steniu010d523cc2017-07-13 14:24:23 +0100393 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
394 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395
396 do
397 {
398 Window sum_slice = slice;
399 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
400
401 unsigned int idx = 0;
402 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100403 add_3D_tensor_argument(idx, _input, slice);
404 add_3D_tensor_argument(idx, _sum, sum_slice);
405 add_3D_tensor_argument(idx, _output, slice);
Georgios Pinitas11f09992017-11-27 11:18:34 +0000406 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407 }
steniu010d523cc2017-07-13 14:24:23 +0100408 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409}