blob: 3eae9e574984461b6c997f1d55bb58845265a1ba [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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;
Chunosovf450caa2017-11-08 16:09:35 +070042namespace
43{
44/** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
45 *
46 * Prepares these build options:
47 * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
48 * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
49 * it defines whether the value will be taken into account or not.
50 *
51 * @param[in] build_opts Build options to extend
52 * @param[in] input_scale Input scaling factor
53 * @param[in] beta Exponent scaling factor beta
54 */
55CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
56{
57 // Number of integer bits in temporary fixed-point representation of current-to-max difference
58 static const int scaled_diff_int_bits = 5;
59 // Number of integer bits used in temporary fixed-point representation of exponent accumulator
60 static const int exp_accumulation_in_bits = 12;
61
62 const double beta_multiplier = std::min(
63 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
64 (1ll << 31) - 1.0);
65 int input_beta_multiplier, input_beta_left_shift;
66 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
67
68 const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1ll << (31 - scaled_diff_int_bits)) / (1ll << input_beta_left_shift);
69 const int diff_min = -1.f * std::floor(max_input_rescaled);
70
71 CLBuildOptions build_opts;
72 build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
73 build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
74 build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
75 build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
76 build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
77
78 return build_opts;
79}
80} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
82void CLLogits1DMaxKernel::configure(const ICLTensor *input, ICLTensor *output)
83{
Chunosovf450caa2017-11-08 16:09:35 +070084 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +010085 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
86
87 // Softmax across the x dimension
88 TensorShape output_shape{ input->info()->tensor_shape() };
89 output_shape.set(0, 1);
90
91 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +070092 auto_init_if_empty(*output->info(),
93 output_shape,
94 1,
95 input->info()->data_type(),
96 input->info()->fixed_point_position(),
97 input->info()->quantization_info());
Georgios Pinitasd368df32017-07-04 11:06:15 +010098
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100100 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
101 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
103 _input = input;
104 _output = output;
105
Chunosovf450caa2017-11-08 16:09:35 +0700106 const DataType data_type = input->info()->data_type();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 // The kernel loops over all elements in steps of 16
108 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16);
109
110 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700111 CLBuildOptions build_opts;
112 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
113 build_opts.add_option_if(is_data_type_fixed_point(data_type),
114 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
115 build_opts.add_option_if(data_type == DataType::F16, "-DUSE_F16");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 // Tell the kernel that the width is not a multiple of 16
Chunosovf450caa2017-11-08 16:09:35 +0700117 build_opts.add_option_if((input->info()->dimension(0) % max_cl_vector_width) != 0, "-DNON_MULTIPLE_OF_16");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700120 std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "softmax_layer_max_quantized" : "softmax_layer_max";
121 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 // Set fixed arguments
steniu010d523cc2017-07-13 14:24:23 +0100124 unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0));
126
127 // Configure kernel window
128 constexpr unsigned int num_elems_written_per_iteration = 1;
129
130 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
131 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
132 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
133
134 update_window_and_padding(win, input_access, output_access);
135
136 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
137
138 ICLKernel::configure(win);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000139
140 // Set config_id for enabling LWS tuning
141 _config_id = "softmax_layer_";
142 _config_id += lower_string(string_from_data_type(data_type));
143 _config_id += "_";
144 _config_id += support::cpp11::to_string(input->info()->dimension(0));
145 _config_id += "_";
146 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147}
148
149CLLogits1DShiftExpSumKernel::CLLogits1DShiftExpSumKernel()
150 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
151{
152}
153
Pablo Palmier48a60f92017-10-18 11:03:08 +0100154void CLLogits1DShiftExpSumKernel::configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155{
Chunosovf450caa2017-11-08 16:09:35 +0700156 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100157 ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
158
Chunosovf450caa2017-11-08 16:09:35 +0700159 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->info()->data_type());
160 const DataType tmp_data_type = is_quantized_asymmetric ? DataType::S32 : input->info()->data_type();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100161
Chunosovf450caa2017-11-08 16:09:35 +0700162 // Output auto initialization if not yet initialized
163 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, tmp_data_type, input->info()->fixed_point_position());
164 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, tmp_data_type, input->info()->fixed_point_position());
165
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100166 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100167 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
Chunosovf450caa2017-11-08 16:09:35 +0700168 if(is_quantized_asymmetric)
169 {
170 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
171 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(output, sum);
172 }
173 else
174 {
175 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
176 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum);
177 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178
179 _input = input;
180 _max = max;
181 _output = output;
182 _sum = sum;
183
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000184 const DataType dt = input->info()->data_type();
185 auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position())));
186
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 // The kernel loops over all elements in steps of 16
188 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16);
189
190 // Set build options
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000191 CLBuildOptions build_opts;
192 build_opts.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
193 build_opts.add_option_if(is_data_type_fixed_point(dt),
194 std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
195 build_opts.add_option_if(dt == DataType::F16, std::string("-DUSE_F16"));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 // Tell the kernel that the width is not a multiple of 16
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000197 build_opts.add_option_if((input->info()->dimension(0) % max_cl_vector_width) != 0, std::string("-DNON_MULTIPLE_OF_16"));
198 build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), std::string("-DBETA=" + support::cpp11::to_string(beta_int)));
199 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), std::string("-DBETA=" + float_to_string_with_full_precision(beta)));
Chunosovf450caa2017-11-08 16:09:35 +0700200 build_opts.add_options_if(is_quantized_asymmetric,
201 prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Pablo Palmier48a60f92017-10-18 11:03:08 +0100202
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700204 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_shift_exp_sum_quantized" : "softmax_layer_shift_exp_sum";
205 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206
207 // Set fixed arguments
steniu010d523cc2017-07-13 14:24:23 +0100208 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0));
210
211 // Configure window
212 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
213
214 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
215 AccessWindowHorizontal max_access(max->info(), 0, 1);
216 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
217 AccessWindowHorizontal sum_access(sum->info(), 0, 1);
218
219 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
220
221 output_access.set_valid_region(win, input->info()->valid_region());
222 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
223
224 ICLKernel::configure(win);
225}
226
227void CLLogits1DShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
228{
229 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
230 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
231
steniu010d523cc2017-07-13 14:24:23 +0100232 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
233 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 do
236 {
237 unsigned int idx = 0;
238 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100239 add_3D_tensor_argument(idx, _input, slice);
240 add_3D_tensor_argument(idx, _max, slice);
241 add_3D_tensor_argument(idx, _output, slice);
242 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243 enqueue(queue, *this, slice);
244 }
steniu010d523cc2017-07-13 14:24:23 +0100245 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246}
247
Chunosovd6afedc2017-11-06 22:09:45 +0700248/**< Grid size (obtained through auto-tuning) */
249const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
250/**< Vector size in the serial case (obtained through auto-tuning) */
251const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
252/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
253const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
254
255CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
256 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
257{
258}
259
260void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
261{
262 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
263 ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700264
265 // Output auto initialization if not yet initialized
266 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
267 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
268
269 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
270 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum);
271 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
272 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
273
274 _input = input;
275 _max = max;
276 _output = output;
277 _sum = sum;
278
279 const DataType dt = input->info()->data_type();
280 const size_t reduction_dim_size = input->info()->dimension(0);
281 auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position())));
282
283 // Set build options
284 CLBuildOptions build_opts;
285 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
286 build_opts.add_option_if(is_data_type_fixed_point(dt),
287 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
288 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
289 build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), "-DBETA=" + support::cpp11::to_string(beta_int));
290 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
291
292 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
293 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
294 _lws_hint = cl::NullRange;
295 std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_serial");
296 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
297 unsigned int vector_size = std::get<1>(parallel_reduction_info);
298
299 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
300 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
301 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
302
303 // Configure parallel kernel if needed
304 if(std::get<0>(parallel_reduction_info))
305 {
306 kernel_name = std::string("softmax_layer_max_shift_exp_sum_parallel");
307 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
308 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
309
310 // Handle boundary conditions.
311 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
312 build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE");
313 }
314
315 // Create kernel.
316 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
317
318 // Set static arguments. Both the kernels use the same arguments
319 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
320 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
321
322 // Configure window
323 const unsigned int num_elems_x = ceil_to_multiple(input->info()->tensor_shape().x(), vector_size);
324 Window win = calculate_max_window(*input->info(), Steps(num_elems_x));
325
326 AccessWindowHorizontal input_access(input->info(), 0, num_elems_x);
327 AccessWindowHorizontal max_access(max->info(), 0, 1);
328 AccessWindowHorizontal output_access(output->info(), 0, num_elems_x);
329 AccessWindowHorizontal sum_access(sum->info(), 0, 1);
330
331 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
332
333 output_access.set_valid_region(win, input->info()->valid_region());
334 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
335
336 ICLKernel::configure(win);
337}
338
339CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
340{
341 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
342 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
343 return std::make_tuple(is_parallel_reduction, vector_size);
344}
345
346void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
347{
348 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
349 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
350
351 // Collapse window in Z dimension
352 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
353
354 // Reconfigure window in case of parallel reduction
355 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
356 if(std::get<0>(parallel_reduction_info))
357 {
358 // To launch grid_size parallel workitems, steps.x should be modified as follows.
359 const unsigned int step = std::get<1>(parallel_reduction_info);
360 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
361 }
362
363 // Get slices
364 Window slice = window_collapsed.first_slice_window_3D();
365 do
366 {
367 unsigned int idx = 0;
368 // Set inputs
369 add_3D_tensor_argument(idx, _input, slice);
370 add_3D_tensor_argument(idx, _max, slice);
371 add_3D_tensor_argument(idx, _output, slice);
372 add_3D_tensor_argument(idx, _sum, slice);
373 enqueue(queue, *this, slice, _lws_hint);
374 }
375 while(window_collapsed.slide_window_slice_3D(slice));
376}
377
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378CLLogits1DNormKernel::CLLogits1DNormKernel()
379 : _input(nullptr), _sum(nullptr), _output(nullptr)
380{
381}
382
Chunosovf450caa2017-11-08 16:09:35 +0700383void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384{
Chunosovf450caa2017-11-08 16:09:35 +0700385 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::S32, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100386 ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700387 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
388 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum);
389
390 // Note: output should always have a scale of 1/256 and offset 0
391 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
392 const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32);
393 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100394
395 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700396 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000397 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100398
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100399 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Chunosovf450caa2017-11-08 16:09:35 +0700400 if(!is_quantized_asymmetric)
401 {
402 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
403 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
404 }
405 else
406 {
407 ARM_COMPUTE_ERROR_ON(output->info()->quantization_info() != allowed_quantization_info);
408 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409
410 _input = input;
411 _sum = sum;
412 _output = output;
413
414 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700415 CLBuildOptions build_opts;
416 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
417 build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()),
418 "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
419 build_opts.add_options_if(is_quantized_asymmetric,
420 prepare_quantized_softmax_build_options(input->info()->quantization_info().scale, beta).options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100421
422 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700423 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
424 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425
426 // Configure window
427 constexpr unsigned int num_elems_processed_per_iteration = 16;
428
429 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
430
431 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
432 AccessWindowStatic sum_access(sum->info(), 0, 0, 1, sum->info()->dimension(1));
433 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
434
435 update_window_and_padding(win, input_access, sum_access, output_access);
436
437 output_access.set_valid_region(win, input->info()->valid_region());
438
439 ICLKernel::configure(win);
440}
441
442void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
443{
444 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
445 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
446
steniu010d523cc2017-07-13 14:24:23 +0100447 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
448 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449
450 do
451 {
452 Window sum_slice = slice;
453 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
454
455 unsigned int idx = 0;
456 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100457 add_3D_tensor_argument(idx, _input, slice);
458 add_3D_tensor_argument(idx, _sum, sum_slice);
459 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100460 enqueue(queue, *this, slice);
461 }
steniu010d523cc2017-07-13 14:24:23 +0100462 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100463}