blob: 4c00413469439111ce17c457b200b8d9255137f5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +00002 * Copyright (c) 2017-2021 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClSoftmaxKernel.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000025#include "arm_compute/core/CL/ICLTensor.h"
26#include "arm_compute/core/Utils.h"
27#include "arm_compute/core/experimental/Types.h"
Chunosovf450caa2017-11-08 16:09:35 +070028#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CL/CLValidate.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000032#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Giorgio Arena2d1a8352020-10-26 15:04:08 +000035namespace arm_compute
36{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000037namespace opencl
38{
39namespace kernels
40{
Chunosovf450caa2017-11-08 16:09:35 +070041namespace
42{
43/** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
44 *
45 * Prepares these build options:
46 * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
47 * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
48 * it defines whether the value will be taken into account or not.
49 *
50 * @param[in] build_opts Build options to extend
51 * @param[in] input_scale Input scaling factor
52 * @param[in] beta Exponent scaling factor beta
53 */
54CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
55{
56 // Number of integer bits in temporary fixed-point representation of current-to-max difference
57 static const int scaled_diff_int_bits = 5;
58 // Number of integer bits used in temporary fixed-point representation of exponent accumulator
59 static const int exp_accumulation_in_bits = 12;
60
61 const double beta_multiplier = std::min(
62 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
Michalis Spyroua4f378d2019-04-26 14:54:54 +010063 (1LL << 31) - 1.0);
64 int input_beta_multiplier;
65 int input_beta_left_shift;
Chunosovf450caa2017-11-08 16:09:35 +070066 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
67
Michalis Spyroua4f378d2019-04-26 14:54:54 +010068 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 +070069 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}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000081Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000082{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000083 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
84 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
85 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000086
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000087 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
Georgios Pinitas30902ed2017-11-14 15:32:57 +000088
89 // Checks performed when output is configured
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000090 if(dst.total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000091 {
92 if(is_quantized_asymmetric)
93 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000094 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::S32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000095 }
96 else
97 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000098 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000099 }
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000101 }
102
103 // Checks performed when sum is configured
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000104 if(sum.total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000105 {
106 if(is_quantized_asymmetric)
107 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000108 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&sum, 1, DataType::S32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000109 }
110 else
111 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000112 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&max, &sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000113 }
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000114 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&max, &sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000115 }
116
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000117 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000118}
119
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000120Status validate_arguments_1DNorm(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000121{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000122 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
123 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::S32, DataType::F16, DataType::F32);
124 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &sum);
Sang-Hoon Parka0205b92020-07-07 09:36:09 +0100125 ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type));
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000126
127 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000128 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
129 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000130
131 // Checks performed when output is configured
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000132 if(dst.total_size() != 0)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000133 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000134 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000135 if(!is_quantized_asymmetric)
136 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000137 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000138 }
139 else
140 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000141 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
142 ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != allowed_quantization_info);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000143 }
144 }
145
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000146 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000147}
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000148} // namespace
149
Chunosovd6afedc2017-11-06 22:09:45 +0700150/**< Grid size (obtained through auto-tuning) */
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000151const unsigned int ClLogits1DMaxShiftExpSumKernel::_grid_size = 64;
Chunosovd6afedc2017-11-06 22:09:45 +0700152/**< Vector size in the serial case (obtained through auto-tuning) */
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000153const unsigned int ClLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
Chunosovd6afedc2017-11-06 22:09:45 +0700154/**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000155const unsigned int ClLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
Chunosovd6afedc2017-11-06 22:09:45 +0700156
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100157ClLogits1DMaxShiftExpSumKernel::ClLogits1DMaxShiftExpSumKernel()
158{
159 _type = CLKernelType::ELEMENTWISE;
160}
161
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000162void ClLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, ITensorInfo &max, ITensorInfo &dst, ITensorInfo &sum, const SoftmaxKernelInfo &info)
Chunosovd6afedc2017-11-06 22:09:45 +0700163{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000164 auto padding_info = get_padding_info({ &src, &max, &dst, &sum });
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000165
Chunosovd6afedc2017-11-06 22:09:45 +0700166 // Output auto initialization if not yet initialized
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000167 auto_init_if_empty(sum, src.clone()->set_tensor_shape(max.tensor_shape()));
168 auto_init_if_empty(dst, *src.clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700169
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000170 // Perform validation step
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000171 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
Chunosovd6afedc2017-11-06 22:09:45 +0700172
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000173 const DataType dt = src.data_type();
174 const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
175 const size_t reduction_dim_size = src.dimension(0);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000176 const float beta = info.beta;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000177 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
178 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700179
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000180 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(reduction_dim_size);
181 const unsigned int vector_size = adjust_vec_size(std::get<1>(parallel_reduction_info), reduction_dim_size);
182
Chunosovd6afedc2017-11-06 22:09:45 +0700183 // Set build options
184 CLBuildOptions build_opts;
185 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000186 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Chunosovd6afedc2017-11-06 22:09:45 +0700187 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000188 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size));
189 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(reduction_dim_size % vector_size));
Chunosovd6afedc2017-11-06 22:09:45 +0700190 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
191 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000192 build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
193 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
194 build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX");
195 build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX")));
196 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
197
198 cl::NDRange lws_hint(cl::NullRange);
199 std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") + (is_data_type_quantized_asymmetric(dt) ? "quantized_" : "");
Chunosovd6afedc2017-11-06 22:09:45 +0700200
201 // Configure parallel kernel if needed
202 if(std::get<0>(parallel_reduction_info))
203 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000204 kernel_name += "parallel";
Chunosovd6afedc2017-11-06 22:09:45 +0700205 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
206 build_opts.add_option_if(is_grid_size_pow2 && _grid_size <= 256, "-DGRID_SIZE=" + support::cpp11::to_string(_grid_size));
207
208 // Handle boundary conditions.
209 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
210 build_opts.add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0), "-DNON_MULTIPLE_OF_GRID_SIZE");
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000211 // Setting _lws_hint in this way can also communicate grid_size to ClLogits1DMaxShiftExpSumKernel::run().
Georgios Pinitas11f09992017-11-27 11:18:34 +0000212 // A single workgroup performs reduction in dimension 0 in the parallel case, hence lws[0]==gws[0].
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100213 lws_hint = cl::NDRange(_grid_size);
Chunosovd6afedc2017-11-06 22:09:45 +0700214 }
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000215 else
216 {
217 kernel_name += "serial";
218 }
Chunosovd6afedc2017-11-06 22:09:45 +0700219
220 // Create kernel.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100221 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Chunosovd6afedc2017-11-06 22:09:45 +0700222
Chunosovd6afedc2017-11-06 22:09:45 +0700223 // Configure window
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000224 Window win = calculate_max_window(src, Steps(reduction_dim_size));
225 IClKernel::configure_internal(win, lws_hint);
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000226
227 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Chunosovd6afedc2017-11-06 22:09:45 +0700228}
229
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000230Status ClLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000231{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000232 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000233 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000234}
235
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000236ClLogits1DMaxShiftExpSumKernel::ParallelReductionInfo ClLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
Chunosovd6afedc2017-11-06 22:09:45 +0700237{
238 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
239 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
240 return std::make_tuple(is_parallel_reduction, vector_size);
241}
242
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000243void ClLogits1DMaxShiftExpSumKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Chunosovd6afedc2017-11-06 22:09:45 +0700244{
245 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
246 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
247
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000248 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
249 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
250 auto max = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
251 auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_1));
252
253 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, max, sum);
254
Chunosovd6afedc2017-11-06 22:09:45 +0700255 // Collapse window in Z dimension
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000256 Window window_collapsed = window.collapse_if_possible(IClKernel::window(), Window::DimZ);
Chunosovd6afedc2017-11-06 22:09:45 +0700257
258 // Reconfigure window in case of parallel reduction
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000259 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(src->info()->dimension(0));
Chunosovd6afedc2017-11-06 22:09:45 +0700260 if(std::get<0>(parallel_reduction_info))
261 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000262 // Launch grid_size parallel work items
263 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1));
Chunosovd6afedc2017-11-06 22:09:45 +0700264 }
265
266 // Get slices
267 Window slice = window_collapsed.first_slice_window_3D();
268 do
269 {
270 unsigned int idx = 0;
271 // Set inputs
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000272 add_3D_tensor_argument(idx, src, slice);
273 add_3D_tensor_argument(idx, max, slice);
274 add_3D_tensor_argument(idx, dst, slice);
275 add_3D_tensor_argument(idx, sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100276 enqueue(queue, *this, slice, lws_hint());
Chunosovd6afedc2017-11-06 22:09:45 +0700277 }
278 while(window_collapsed.slide_window_slice_3D(slice));
279}
280
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100281ClLogits1DNormKernel::ClLogits1DNormKernel()
282{
283 _type = CLKernelType::ELEMENTWISE;
284}
285
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000286void ClLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, const ITensorInfo &sum, ITensorInfo &dst, const SoftmaxKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000288 auto padding_info = get_padding_info({ &src, &dst, &sum });
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000289
Chunosovf450caa2017-11-08 16:09:35 +0700290 // Note: output should always have a scale of 1/256 and offset 0
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000291 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
292 const DataType output_data_type = info.input_data_type;
293 const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000294 const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100295
296 // Output auto initialization if not yet initialized
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000297 auto_init_if_empty(dst, src.clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100298
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000299 // Perform validation step
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000300 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(src, sum, dst, info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000302 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
303 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000304 const unsigned int vector_size = adjust_vec_size(16, src.dimension(0));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000305
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700307 CLBuildOptions build_opts;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000308 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type));
309 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000310 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000311 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(src.dimension(0) % vector_size));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000312 build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED");
Chunosovf450caa2017-11-08 16:09:35 +0700313 build_opts.add_options_if(is_quantized_asymmetric,
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000314 prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
315 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316
317 // Create kernel
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000318 std::string kernel_name = std::string("softmax_layer_norm") + (is_quantized_asymmetric ? "_quantized" : "");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100319 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320
321 // Configure window
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000322 auto win = calculate_max_window(src, Steps(vector_size));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000323 ICLKernel::configure_internal(win);
324
325 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100326}
327
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000328Status ClLogits1DNormKernel::validate(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000329{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000330 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(src, sum, dst, info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000331
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000332 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000333}
334
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000335void ClLogits1DNormKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336{
337 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
338 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
339
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000340 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
341 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
342 auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
343
344 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, sum);
345
steniu010d523cc2017-07-13 14:24:23 +0100346 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
347 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348
349 do
350 {
351 Window sum_slice = slice;
352 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
353
354 unsigned int idx = 0;
355 // Set inputs
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000356 add_3D_tensor_argument(idx, src, slice);
357 add_3D_tensor_argument(idx, sum, sum_slice);
358 add_3D_tensor_argument(idx, dst, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100359 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360 }
steniu010d523cc2017-07-13 14:24:23 +0100361 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362}
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000363} // namespace kernels
364} // namespace opencl
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000365} // namespace arm_compute