blob: a9c08703c0afaf492e790072027525386c2c1d0e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +01002 * Copyright (c) 2017-2019 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)),
Michalis Spyroua4f378d2019-04-26 14:54:54 +010065 (1LL << 31) - 1.0);
66 int input_beta_multiplier;
67 int input_beta_left_shift;
Chunosovf450caa2017-11-08 16:09:35 +070068 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
69
Michalis Spyroua4f378d2019-04-26 14:54:54 +010070 const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift);
Chunosovf450caa2017-11-08 16:09:35 +070071 const int diff_min = -1.f * std::floor(max_input_rescaled);
72
73 CLBuildOptions build_opts;
74 build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
75 build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
76 build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
77 build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
78 build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
79
80 return build_opts;
81}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
Giorgio Arena4402cb92018-02-15 13:37:40 +000083Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000084{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010085 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010086 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 +000087 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
88
Giorgio Arena4402cb92018-02-15 13:37:40 +000089 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
Giorgio Arena4402cb92018-02-15 13:37:40 +000090
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);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000105 }
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);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000119 }
120
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000121 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000122}
123
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000124Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000125{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100126 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100127 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 +0000128 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
129 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000130
131 // Note: output should always have a scale of 1/256 and offset 0
132 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
133 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
134
135 // Checks performed when output is configured
136 if(output->total_size() != 0)
137 {
138 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000139 if(!is_quantized_asymmetric)
140 {
141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
142 }
143 else
144 {
145 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
146 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
147 }
148 }
149
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000150 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000151}
152
153// Window validation
154
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000155std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000156{
157 // Output auto initialization if not yet initialized
158 auto_init_if_empty(*sum, input->clone()->set_tensor_shape(max->tensor_shape()));
159 auto_init_if_empty(*output, *input->clone());
160
161 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo parallel_reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(input->dimension(0));
162 unsigned int vector_size = std::get<1>(parallel_reduction_info);
163 const unsigned int num_elems_x = ceil_to_multiple(input->tensor_shape().x(), vector_size);
164 Window win = calculate_max_window(*input, Steps(num_elems_x));
165
166 AccessWindowHorizontal input_access(input, 0, num_elems_x);
167 AccessWindowHorizontal max_access(max, 0, 1);
168 AccessWindowHorizontal output_access(output, 0, num_elems_x);
169 AccessWindowHorizontal sum_access(sum, 0, 1);
170
171 bool window_changed = update_window_and_padding(win, input_access, max_access, output_access, sum_access);
172
173 output_access.set_valid_region(win, input->valid_region());
174 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->tensor_shape()));
175
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000176 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000177 return std::make_pair(err, win);
178}
179
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000180std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000181{
182 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.f / 256, 0);
183 const bool is_quantized_asymmetric = (input->data_type() == DataType::S32);
184 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->data_type();
185
186 // Output auto initialization if not yet initialized
187 auto_init_if_empty(*output,
188 input->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
189
190 constexpr unsigned int num_elems_processed_per_iteration = 16;
191
192 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
193
194 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
195 AccessWindowStatic sum_access(sum, 0, 0, 1, sum->dimension(1));
196 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
197
198 bool window_changed = update_window_and_padding(win, input_access, sum_access, output_access);
199
200 output_access.set_valid_region(win, input->valid_region());
201
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000202 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000203 return std::make_pair(err, win);
204}
205
206} // namespace
207
Chunosovd6afedc2017-11-06 22:09:45 +0700208/**< Grid size (obtained through auto-tuning) */
209const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
210/**< Vector size in the serial case (obtained through auto-tuning) */
211const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
212/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
213const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
214
215CLLogits1DMaxShiftExpSumKernel::CLLogits1DMaxShiftExpSumKernel()
216 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
217{
218}
219
220void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
221{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000222 ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, sum, output);
Chunosovd6afedc2017-11-06 22:09:45 +0700223
224 // Output auto initialization if not yet initialized
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000225 auto_init_if_empty(*sum->info(), input->info()->clone()->set_tensor_shape(max->info()->tensor_shape()));
226 auto_init_if_empty(*output->info(), *input->info()->clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700227
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000228 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000229 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info()));
Chunosovd6afedc2017-11-06 22:09:45 +0700230
231 _input = input;
232 _max = max;
233 _output = output;
234 _sum = sum;
235
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100236 const DataType dt = input->info()->data_type();
237 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
238 const size_t reduction_dim_size = input->info()->dimension(0);
Chunosovd6afedc2017-11-06 22:09:45 +0700239
240 // Set build options
241 CLBuildOptions build_opts;
242 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Chunosovd6afedc2017-11-06 22:09:45 +0700243 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
Chunosovd6afedc2017-11-06 22:09:45 +0700244 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100245 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
Chunosovd6afedc2017-11-06 22:09:45 +0700246
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100247 cl::NDRange lws_hint(cl::NullRange);
Giorgio Arena4402cb92018-02-15 13:37:40 +0000248 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") :
249 std::string("softmax_layer_max_shift_exp_sum_serial");
Chunosovd6afedc2017-11-06 22:09:45 +0700250 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
251 unsigned int vector_size = std::get<1>(parallel_reduction_info);
252
253 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
254 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
255 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
256
257 // Configure parallel kernel if needed
258 if(std::get<0>(parallel_reduction_info))
259 {
Giorgio Arena4402cb92018-02-15 13:37:40 +0000260 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 +0700261 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
262 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
263
264 // Handle boundary conditions.
265 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
266 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 +0000267 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
268 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100269 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700270 }
271
272 // Create kernel.
273 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
274
275 // Set static arguments. Both the kernels use the same arguments
276 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
277 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
278
279 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000280 auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info());
281 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100282 ICLKernel::configure_internal(win_config.second, lws_hint);
Chunosovd6afedc2017-11-06 22:09:45 +0700283}
284
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000285Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000286{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000287 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
288 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 +0000289
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000290 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000291}
292
Chunosovd6afedc2017-11-06 22:09:45 +0700293CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
294{
295 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
296 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
297 return std::make_tuple(is_parallel_reduction, vector_size);
298}
299
300void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
301{
302 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
303 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
304
305 // Collapse window in Z dimension
306 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
307
308 // Reconfigure window in case of parallel reduction
309 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
310 if(std::get<0>(parallel_reduction_info))
311 {
312 // To launch grid_size parallel workitems, steps.x should be modified as follows.
313 const unsigned int step = std::get<1>(parallel_reduction_info);
314 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
315 }
316
317 // Get slices
318 Window slice = window_collapsed.first_slice_window_3D();
319 do
320 {
321 unsigned int idx = 0;
322 // Set inputs
323 add_3D_tensor_argument(idx, _input, slice);
324 add_3D_tensor_argument(idx, _max, slice);
325 add_3D_tensor_argument(idx, _output, slice);
326 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100327 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700328 }
329 while(window_collapsed.slide_window_slice_3D(slice));
330}
331
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332CLLogits1DNormKernel::CLLogits1DNormKernel()
333 : _input(nullptr), _sum(nullptr), _output(nullptr)
334{
335}
336
Chunosovf450caa2017-11-08 16:09:35 +0700337void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000339 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700340
341 // Note: output should always have a scale of 1/256 and offset 0
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100342 const QuantizationInfo allowed_quantization_info = QuantizationInfo(1.F / 256, 0);
343 const bool is_quantized_asymmetric = (input->info()->data_type() == DataType::S32);
344 const DataType output_data_type = is_quantized_asymmetric ? DataType::QASYMM8 : input->info()->data_type();
345 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100346
347 // Output auto initialization if not yet initialized
Chunosovf450caa2017-11-08 16:09:35 +0700348 auto_init_if_empty(*output->info(),
Georgios Pinitas283c1792017-11-10 18:14:06 +0000349 input->info()->clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100350
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000351 // Perform validation step
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000352 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353
354 _input = input;
355 _sum = sum;
356 _output = output;
357
358 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700359 CLBuildOptions build_opts;
360 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Chunosovf450caa2017-11-08 16:09:35 +0700361 build_opts.add_options_if(is_quantized_asymmetric,
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100362 prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363
364 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700365 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
366 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367
368 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000369 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info());
370 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100371 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372}
373
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000374Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000375{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000376 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output));
377 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 +0000378
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000379 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000380}
381
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100382void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
383{
384 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
385 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
386
steniu010d523cc2017-07-13 14:24:23 +0100387 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
388 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100389
390 do
391 {
392 Window sum_slice = slice;
393 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
394
395 unsigned int idx = 0;
396 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100397 add_3D_tensor_argument(idx, _input, slice);
398 add_3D_tensor_argument(idx, _sum, sum_slice);
399 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100400 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401 }
steniu010d523cc2017-07-13 14:24:23 +0100402 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403}