blob: 1b5a2666bcd76d189a237ee5f8abca5d97d844d1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2021, 2023 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000026#include "arm_compute/core/CL/ICLTensor.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000027#include "arm_compute/core/experimental/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/core/Utils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000029#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Chunosovf450caa2017-11-08 16:09:35 +070030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000036#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Giorgio Arena2d1a8352020-10-26 15:04:08 +000039namespace arm_compute
40{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000041namespace opencl
42{
43namespace kernels
44{
Chunosovf450caa2017-11-08 16:09:35 +070045namespace
46{
47/** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
48 *
49 * Prepares these build options:
50 * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
51 * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
52 * it defines whether the value will be taken into account or not.
53 *
54 * @param[in] build_opts Build options to extend
55 * @param[in] input_scale Input scaling factor
56 * @param[in] beta Exponent scaling factor beta
57 */
58CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
59{
60 // Number of integer bits in temporary fixed-point representation of current-to-max difference
61 static const int scaled_diff_int_bits = 5;
62 // Number of integer bits used in temporary fixed-point representation of exponent accumulator
63 static const int exp_accumulation_in_bits = 12;
64
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 const double beta_multiplier =
66 std::min(1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)), (1LL << 31) - 1.0);
Michalis Spyroua4f378d2019-04-26 14:54:54 +010067 int input_beta_multiplier;
68 int input_beta_left_shift;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier,
70 &input_beta_left_shift);
Chunosovf450caa2017-11-08 16:09:35 +070071
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 const double max_input_rescaled =
73 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift);
74 const int diff_min = -1.f * std::floor(max_input_rescaled);
Chunosovf450caa2017-11-08 16:09:35 +070075
76 CLBuildOptions build_opts;
77 build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
78 build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
79 build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
80 build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
81 build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
82
83 return build_opts;
84}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo &src,
87 const ITensorInfo &max,
88 const ITensorInfo &dst,
89 const ITensorInfo &sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000090{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000091 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
93 DataType::F16, DataType::F32);
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000094 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000095
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +000096 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
Georgios Pinitas30902ed2017-11-14 15:32:57 +000097
98 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 if (dst.total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000100 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 if (is_quantized_asymmetric)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000102 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::S32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000104 }
105 else
106 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000108 }
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000109 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000110 }
111
112 // Checks performed when sum is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113 if (sum.total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000114 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 if (is_quantized_asymmetric)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000116 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000117 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&sum, 1, DataType::S32);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000118 }
119 else
120 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&max, &sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000122 }
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000123 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&max, &sum);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000124 }
125
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000126 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000127}
128
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129Status validate_arguments_1DNorm(const ITensorInfo &src,
130 const ITensorInfo &sum,
131 const ITensorInfo &dst,
132 const SoftmaxKernelInfo &info)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000133{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000134 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
135 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::S32, DataType::F16, DataType::F32);
136 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &sum);
Sang-Hoon Parka0205b92020-07-07 09:36:09 +0100137 ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type));
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000138
139 // Note: output should always have a scale of 1/256 and offset 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 const QuantizationInfo allowed_quantization_info =
141 get_softmax_output_quantization_info(info.input_data_type, info.is_log);
142 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000143
144 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 if (dst.total_size() != 0)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000146 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000147 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (!is_quantized_asymmetric)
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000149 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000150 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000151 }
152 else
153 {
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000154 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
155 ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != allowed_quantization_info);
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000156 }
157 }
158
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000159 return Status{};
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000160}
Giorgio Arenab8ab9972017-11-29 15:09:39 +0000161} // namespace
162
Chunosovd6afedc2017-11-06 22:09:45 +0700163/**< Grid size (obtained through auto-tuning) */
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000164const unsigned int ClLogits1DMaxShiftExpSumKernel::_grid_size = 64;
Chunosovd6afedc2017-11-06 22:09:45 +0700165/**< Vector size in the serial case (obtained through auto-tuning) */
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000166const unsigned int ClLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
Chunosovd6afedc2017-11-06 22:09:45 +0700167/**< 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 +0000168const unsigned int ClLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
Chunosovd6afedc2017-11-06 22:09:45 +0700169
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100170ClLogits1DMaxShiftExpSumKernel::ClLogits1DMaxShiftExpSumKernel()
171{
172 _type = CLKernelType::ELEMENTWISE;
173}
174
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175void ClLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context,
176 const ITensorInfo &src,
177 ITensorInfo &max,
178 ITensorInfo &dst,
179 ITensorInfo &sum,
180 const SoftmaxKernelInfo &info)
Chunosovd6afedc2017-11-06 22:09:45 +0700181{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182 auto padding_info = get_padding_info({&src, &max, &dst, &sum});
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000183
Chunosovd6afedc2017-11-06 22:09:45 +0700184 // Output auto initialization if not yet initialized
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000185 auto_init_if_empty(sum, src.clone()->set_tensor_shape(max.tensor_shape()));
186 auto_init_if_empty(dst, *src.clone());
Chunosovd6afedc2017-11-06 22:09:45 +0700187
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000188 // Perform validation step
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000189 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
Chunosovd6afedc2017-11-06 22:09:45 +0700190
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000191 const DataType dt = src.data_type();
192 const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
193 const size_t reduction_dim_size = src.dimension(0);
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000194 const float beta = info.beta;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000195 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
196 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Chunosovd6afedc2017-11-06 22:09:45 +0700197
Adnan AlSinanb0608062021-09-29 16:50:46 +0100198 const unsigned int vector_size = adjust_vec_size(_serial_vector_size, reduction_dim_size);
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000199
Chunosovd6afedc2017-11-06 22:09:45 +0700200 // Set build options
201 CLBuildOptions build_opts;
202 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000203 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Chunosovd6afedc2017-11-06 22:09:45 +0700204 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000205 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size));
206 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(reduction_dim_size % vector_size));
Chunosovd6afedc2017-11-06 22:09:45 +0700207 build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
208 build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000209 build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f),
211 "-DBETA=" + float_to_string_with_full_precision(beta));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000212 build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX")
214 : std::string("-FLT_MAX")));
215 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt),
216 "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
217 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt),
218 "-DBETA=" + float_to_string_with_full_precision(beta));
219 build_opts.add_options_if(is_data_type_quantized_asymmetric(dt),
220 prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000221
222 cl::NDRange lws_hint(cl::NullRange);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") +
224 (is_data_type_quantized_asymmetric(dt) ? "quantized_" : "") + "serial";
Chunosovd6afedc2017-11-06 22:09:45 +0700225
226 // Create kernel.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100227 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Chunosovd6afedc2017-11-06 22:09:45 +0700228
Chunosovd6afedc2017-11-06 22:09:45 +0700229 // Configure window
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000230 Window win = calculate_max_window(src, Steps(reduction_dim_size));
231 IClKernel::configure_internal(win, lws_hint);
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000232
233 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Chunosovd6afedc2017-11-06 22:09:45 +0700234}
235
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100236Status ClLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo &src,
237 const ITensorInfo &max,
238 const ITensorInfo &dst,
239 const ITensorInfo &sum)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000240{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000241 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000242 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000243}
244
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000245ClLogits1DMaxShiftExpSumKernel::ParallelReductionInfo ClLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
Chunosovd6afedc2017-11-06 22:09:45 +0700246{
247 bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
248 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
249 return std::make_tuple(is_parallel_reduction, vector_size);
250}
251
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000252void ClLogits1DMaxShiftExpSumKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Chunosovd6afedc2017-11-06 22:09:45 +0700253{
254 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
255 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
256
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000257 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
258 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
259 auto max = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
260 auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_1));
261
262 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, max, sum);
263
Chunosovd6afedc2017-11-06 22:09:45 +0700264 // Collapse window in Z dimension
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000265 Window window_collapsed = window.collapse_if_possible(IClKernel::window(), Window::DimZ);
Chunosovd6afedc2017-11-06 22:09:45 +0700266
267 // Reconfigure window in case of parallel reduction
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000268 ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(src->info()->dimension(0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100269 if (std::get<0>(parallel_reduction_info))
Chunosovd6afedc2017-11-06 22:09:45 +0700270 {
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000271 // Launch grid_size parallel work items
272 window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1));
Chunosovd6afedc2017-11-06 22:09:45 +0700273 }
274
275 // Get slices
276 Window slice = window_collapsed.first_slice_window_3D();
277 do
278 {
279 unsigned int idx = 0;
280 // Set inputs
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000281 add_3D_tensor_argument(idx, src, slice);
282 add_3D_tensor_argument(idx, max, slice);
283 add_3D_tensor_argument(idx, dst, slice);
284 add_3D_tensor_argument(idx, sum, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100285 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100286 } while (window_collapsed.slide_window_slice_3D(slice));
Chunosovd6afedc2017-11-06 22:09:45 +0700287}
288
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100289ClLogits1DNormKernel::ClLogits1DNormKernel()
290{
291 _type = CLKernelType::ELEMENTWISE;
292}
293
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100294void ClLogits1DNormKernel::configure(const CLCompileContext &compile_context,
295 const ITensorInfo &src,
296 const ITensorInfo &sum,
297 ITensorInfo &dst,
298 const SoftmaxKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 auto padding_info = get_padding_info({&src, &dst, &sum});
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000301
Chunosovf450caa2017-11-08 16:09:35 +0700302 // Note: output should always have a scale of 1/256 and offset 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100303 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
304 const DataType output_data_type = info.input_data_type;
305 const QuantizationInfo allowed_quantization_info =
306 get_softmax_output_quantization_info(info.input_data_type, info.is_log);
307 const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
Georgios Pinitasd368df32017-07-04 11:06:15 +0100308
309 // Output auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100310 auto_init_if_empty(dst,
311 src.clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
Georgios Pinitasd368df32017-07-04 11:06:15 +0100312
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000313 // Perform validation step
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000314 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(src, sum, dst, info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000316 const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
317 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000318 const unsigned int vector_size = adjust_vec_size(16, src.dimension(0));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000319
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 // Set build options
Chunosovf450caa2017-11-08 16:09:35 +0700321 CLBuildOptions build_opts;
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000322 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type));
323 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000324 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000325 build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(src.dimension(0) % vector_size));
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000326 build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED");
Chunosovf450caa2017-11-08 16:09:35 +0700327 build_opts.add_options_if(is_quantized_asymmetric,
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000328 prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
329 build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
Adnan AlSinanb0608062021-09-29 16:50:46 +0100330 build_opts.add_option_if(is_quantized_asymmetric, "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
331 build_opts.add_option_if(is_quantized_asymmetric, "-DBETA=" + float_to_string_with_full_precision(info.beta));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332
333 // Create kernel
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000334 std::string kernel_name = std::string("softmax_layer_norm") + (is_quantized_asymmetric ? "_quantized" : "");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100335 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336
337 // Configure window
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000338 auto win = calculate_max_window(src, Steps(vector_size));
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000339 ICLKernel::configure_internal(win);
340
341 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342}
343
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100344Status ClLogits1DNormKernel::validate(const ITensorInfo &src,
345 const ITensorInfo &sum,
346 const ITensorInfo &dst,
347 const SoftmaxKernelInfo &info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000348{
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000349 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(src, sum, dst, info));
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000350
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000351 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000352}
353
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000354void ClLogits1DNormKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355{
356 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
357 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
358
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000359 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
360 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
361 auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
362
363 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, sum);
364
steniu010d523cc2017-07-13 14:24:23 +0100365 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
366 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367
368 do
369 {
370 Window sum_slice = slice;
371 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
372
373 unsigned int idx = 0;
374 // Set inputs
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000375 add_3D_tensor_argument(idx, src, slice);
376 add_3D_tensor_argument(idx, sum, sum_slice);
377 add_3D_tensor_argument(idx, dst, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100378 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100379 } while (window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380}
Sang-Hoon Park201e0fe2021-01-27 13:14:56 +0000381} // namespace kernels
382} // namespace opencl
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100383} // namespace arm_compute