blob: 215fa838c4b3f2097a1c6a11b535ed4d6bbc924e [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"
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000033#include "arm_compute/core/KernelDescriptors.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Window.h"
Chunosovf450caa2017-11-08 16:09:35 +070037#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <set>
40#include <string>
41
42using namespace arm_compute;
Georgios Pinitas30902ed2017-11-14 15:32:57 +000043
Chunosovf450caa2017-11-08 16:09:35 +070044namespace
45{
46/** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
47 *
48 * Prepares these build options:
49 * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
50 * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
51 * it defines whether the value will be taken into account or not.
52 *
53 * @param[in] build_opts Build options to extend
54 * @param[in] input_scale Input scaling factor
55 * @param[in] beta Exponent scaling factor beta
56 */
57CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
58{
59 // Number of integer bits in temporary fixed-point representation of current-to-max difference
60 static const int scaled_diff_int_bits = 5;
61 // Number of integer bits used in temporary fixed-point representation of exponent accumulator
62 static const int exp_accumulation_in_bits = 12;
63
64 const double beta_multiplier = std::min(
65 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
Michalis Spyroua4f378d2019-04-26 14:54:54 +010066 (1LL << 31) - 1.0);
67 int input_beta_multiplier;
68 int input_beta_left_shift;
Chunosovf450caa2017-11-08 16:09:35 +070069 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
70
Michalis Spyroua4f378d2019-04-26 14:54:54 +010071 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 +070072 const int diff_min = -1.f * std::floor(max_input_rescaled);
73
74 CLBuildOptions build_opts;
75 build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
76 build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
77 build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
78 build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
79 build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
80
81 return build_opts;
82}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
Giorgio Arena4402cb92018-02-15 13:37:40 +000084Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000085{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010086 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000087 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000088 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(max, sum, output);
89
Giorgio Arena4402cb92018-02-15 13:37:40 +000090 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, max);
Giorgio Arena4402cb92018-02-15 13:37:40 +000091
Georgios Pinitas30902ed2017-11-14 15:32:57 +000092 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->data_type());
93
94 // Checks performed when output is configured
95 if(output->total_size() != 0)
96 {
97 if(is_quantized_asymmetric)
98 {
99 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
100 }
101 else
102 {
103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
104 }
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000106 }
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);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000120 }
121
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000122 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000123}
124
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000125Status validate_arguments_1DNorm(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000126{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +0100127 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100128 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 +0000129 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(sum, output);
130 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000131
132 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000133 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
134 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000135
136 // Checks performed when output is configured
137 if(output->total_size() != 0)
138 {
139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000140 if(!is_quantized_asymmetric)
141 {
142 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
143 }
144 else
145 {
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000146 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000147 ARM_COMPUTE_RETURN_ERROR_ON(output->quantization_info() != allowed_quantization_info);
148 }
149 }
150
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000151 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000152}
153
154// Window validation
155
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000156std::pair<Status, Window> validate_and_configure_window_1DMaxShiftExpSum(ITensorInfo *input, ITensorInfo *max, ITensorInfo *output, ITensorInfo *sum)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000157{
158 // Output auto initialization if not yet initialized
159 auto_init_if_empty(*sum, input->clone()->set_tensor_shape(max->tensor_shape()));
160 auto_init_if_empty(*output, *input->clone());
161
162 CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo parallel_reduction_info = CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(input->dimension(0));
163 unsigned int vector_size = std::get<1>(parallel_reduction_info);
164 const unsigned int num_elems_x = ceil_to_multiple(input->tensor_shape().x(), vector_size);
165 Window win = calculate_max_window(*input, Steps(num_elems_x));
166
167 AccessWindowHorizontal input_access(input, 0, num_elems_x);
168 AccessWindowHorizontal max_access(max, 0, 1);
169 AccessWindowHorizontal output_access(output, 0, num_elems_x);
170 AccessWindowHorizontal sum_access(sum, 0, 1);
171
172 bool window_changed = update_window_and_padding(win, input_access, max_access, output_access, sum_access);
173
174 output_access.set_valid_region(win, input->valid_region());
175 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->tensor_shape()));
176
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000177 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000178 return std::make_pair(err, win);
179}
180
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000181std::pair<Status, Window> validate_and_configure_window_1DNorm(ITensorInfo *input, ITensorInfo *output, ITensorInfo *sum, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000182{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000183 const DataType output_data_type = info.input_data_type;
184 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000185
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
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000220void CLLogits1DMaxShiftExpSumKernel::configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info)
Chunosovd6afedc2017-11-06 22:09:45 +0700221{
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);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000239 const float beta = info.beta;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000240 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
241 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700242
243 // Set build options
244 CLBuildOptions build_opts;
245 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000246 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
247 build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
Chunosovd6afedc2017-11-06 22:09:45 +0700248 build_opts.add_option_if(dt == DataType::F16, "-DUSE_F16");
Chunosovd6afedc2017-11-06 22:09:45 +0700249 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 +0100250 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000251 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Chunosovd6afedc2017-11-06 22:09:45 +0700252
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100253 cl::NDRange lws_hint(cl::NullRange);
Giorgio Arena4402cb92018-02-15 13:37:40 +0000254 std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("softmax_layer_max_shift_exp_sum_quantized_serial") :
255 std::string("softmax_layer_max_shift_exp_sum_serial");
Chunosovd6afedc2017-11-06 22:09:45 +0700256 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
257 unsigned int vector_size = std::get<1>(parallel_reduction_info);
258
259 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
260 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
261 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
262
263 // Configure parallel kernel if needed
264 if(std::get<0>(parallel_reduction_info))
265 {
Giorgio Arena4402cb92018-02-15 13:37:40 +0000266 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 +0700267 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
268 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
269
270 // Handle boundary conditions.
271 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
272 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 +0000273 // Setting _lws_hint in this way can also communicate grid_size to CLLogits1DMaxShiftExpSumKernel::run().
274 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100275 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700276 }
277
278 // Create kernel.
279 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
280
281 // Set static arguments. Both the kernels use the same arguments
282 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
283 _kernel.setArg<cl_uint>(idx++, reduction_dim_size);
284
285 // Configure window
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000286 auto win_config = validate_and_configure_window_1DMaxShiftExpSum(input->info(), max->info(), output->info(), sum->info());
287 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100288 ICLKernel::configure_internal(win_config.second, lws_hint);
Chunosovd6afedc2017-11-06 22:09:45 +0700289}
290
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000291Status CLLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000292{
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000293 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(input, max, output, sum));
294 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 +0000295
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000296 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000297}
298
Chunosovd6afedc2017-11-06 22:09:45 +0700299CLLogits1DMaxShiftExpSumKernel::ParallelReductionInfo CLLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
300{
301 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
302 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
303 return std::make_tuple(is_parallel_reduction, vector_size);
304}
305
306void CLLogits1DMaxShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
307{
308 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
309 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
310
311 // Collapse window in Z dimension
312 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
313
314 // Reconfigure window in case of parallel reduction
315 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(_input->info()->dimension(0));
316 if(std::get<0>(parallel_reduction_info))
317 {
318 // To launch grid_size parallel workitems, steps.x should be modified as follows.
319 const unsigned int step = std::get<1>(parallel_reduction_info);
320 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size * step, step));
321 }
322
323 // Get slices
324 Window slice = window_collapsed.first_slice_window_3D();
325 do
326 {
327 unsigned int idx = 0;
328 // Set inputs
329 add_3D_tensor_argument(idx, _input, slice);
330 add_3D_tensor_argument(idx, _max, slice);
331 add_3D_tensor_argument(idx, _output, slice);
332 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100333 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700334 }
335 while(window_collapsed.slide_window_slice_3D(slice));
336}
337
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338CLLogits1DNormKernel::CLLogits1DNormKernel()
339 : _input(nullptr), _sum(nullptr), _output(nullptr)
340{
341}
342
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000343void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344{
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000345 ARM_COMPUTE_ERROR_ON_NULLPTR(input, sum, output);
Chunosovf450caa2017-11-08 16:09:35 +0700346
347 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000348 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
349 const DataType output_data_type = info.input_data_type;
350 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100351 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
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
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000358 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(input->info(), sum->info(), output->info(), info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359
360 _input = input;
361 _sum = sum;
362 _output = output;
363
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000364 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
365 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
366
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700368 CLBuildOptions build_opts;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000369 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type));
370 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
371 build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED");
Chunosovf450caa2017-11-08 16:09:35 +0700372 build_opts.add_options_if(is_quantized_asymmetric,
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000373 prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
374 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375
376 // Create kernel
Chunosovf450caa2017-11-08 16:09:35 +0700377 std::string kernel_name = is_quantized_asymmetric ? "softmax_layer_norm_quantized" : "softmax_layer_norm";
378 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379
380 // Configure window
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000381 auto win_config = validate_and_configure_window_1DNorm(input->info(), output->info(), sum->info(), info);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000382 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100383 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384}
385
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000386Status CLLogits1DNormKernel::validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000387{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000388 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(input, sum, output, info));
389 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_1DNorm(input->clone().get(), output->clone().get(), sum->clone().get(), info).first);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000390
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000391 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000392}
393
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
395{
396 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
397 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
398
steniu010d523cc2017-07-13 14:24:23 +0100399 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
400 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100401
402 do
403 {
404 Window sum_slice = slice;
405 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
406
407 unsigned int idx = 0;
408 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100409 add_3D_tensor_argument(idx, _input, slice);
410 add_3D_tensor_argument(idx, _sum, sum_slice);
411 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100412 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 }
steniu010d523cc2017-07-13 14:24:23 +0100414 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415}