1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClSoftmaxKernel.h"
25*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
26*c217d954SCole Faust #include "arm_compute/core/Utils.h"
27*c217d954SCole Faust #include "arm_compute/core/experimental/Types.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
31*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
32*c217d954SCole Faust #include "support/Cast.h"
33*c217d954SCole Faust #include "support/StringSupport.h"
34*c217d954SCole Faust
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace opencl
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace kernels
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace
42*c217d954SCole Faust {
43*c217d954SCole Faust /** Calculates softmax parameters from the quantized input scale and scaling factor for the exponent and places them as build options.
44*c217d954SCole Faust *
45*c217d954SCole Faust * Prepares these build options:
46*c217d954SCole Faust * -INPUT_BETA_MULTIPLIER, INPUT_BETA_LEFT_SHIFT - quantized representation of beta multiplier.
47*c217d954SCole Faust * -DIFF_MIN - threshold difference between maximum value of input data and current processed value,
48*c217d954SCole Faust * it defines whether the value will be taken into account or not.
49*c217d954SCole Faust *
50*c217d954SCole Faust * @param[in] build_opts Build options to extend
51*c217d954SCole Faust * @param[in] input_scale Input scaling factor
52*c217d954SCole Faust * @param[in] beta Exponent scaling factor beta
53*c217d954SCole Faust */
prepare_quantized_softmax_build_options(float input_scale,float beta)54*c217d954SCole Faust CLBuildOptions prepare_quantized_softmax_build_options(float input_scale, float beta)
55*c217d954SCole Faust {
56*c217d954SCole Faust // Number of integer bits in temporary fixed-point representation of current-to-max difference
57*c217d954SCole Faust static const int scaled_diff_int_bits = 5;
58*c217d954SCole Faust // Number of integer bits used in temporary fixed-point representation of exponent accumulator
59*c217d954SCole Faust static const int exp_accumulation_in_bits = 12;
60*c217d954SCole Faust
61*c217d954SCole Faust const double beta_multiplier = std::min(
62*c217d954SCole Faust 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
63*c217d954SCole Faust (1LL << 31) - 1.0);
64*c217d954SCole Faust int input_beta_multiplier;
65*c217d954SCole Faust int input_beta_left_shift;
66*c217d954SCole Faust quantization::calculate_quantized_multiplier_greater_than_one(beta_multiplier, &input_beta_multiplier, &input_beta_left_shift);
67*c217d954SCole Faust
68*c217d954SCole Faust const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift);
69*c217d954SCole Faust const int diff_min = -1.f * std::floor(max_input_rescaled);
70*c217d954SCole Faust
71*c217d954SCole Faust CLBuildOptions build_opts;
72*c217d954SCole Faust build_opts.add_option("-DSCALED_DIFF_INT_BITS=" + support::cpp11::to_string(scaled_diff_int_bits));
73*c217d954SCole Faust build_opts.add_option("-DEXP_ACCUMULATION_INT_BITS=" + support::cpp11::to_string(exp_accumulation_in_bits));
74*c217d954SCole Faust build_opts.add_option("-DINPUT_BETA_MULTIPLIER=" + support::cpp11::to_string(input_beta_multiplier));
75*c217d954SCole Faust build_opts.add_option("-DINPUT_BETA_LEFT_SHIFT=" + support::cpp11::to_string(input_beta_left_shift));
76*c217d954SCole Faust build_opts.add_option("-DDIFF_MIN=" + support::cpp11::to_string(diff_min));
77*c217d954SCole Faust
78*c217d954SCole Faust return build_opts;
79*c217d954SCole Faust }
80*c217d954SCole Faust
validate_arguments_1DMaxShiftExpSum(const ITensorInfo & src,const ITensorInfo & max,const ITensorInfo & dst,const ITensorInfo & sum)81*c217d954SCole Faust Status validate_arguments_1DMaxShiftExpSum(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum)
82*c217d954SCole Faust {
83*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
84*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
85*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max);
86*c217d954SCole Faust
87*c217d954SCole Faust const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
88*c217d954SCole Faust
89*c217d954SCole Faust // Checks performed when output is configured
90*c217d954SCole Faust if(dst.total_size() != 0)
91*c217d954SCole Faust {
92*c217d954SCole Faust if(is_quantized_asymmetric)
93*c217d954SCole Faust {
94*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::S32);
95*c217d954SCole Faust }
96*c217d954SCole Faust else
97*c217d954SCole Faust {
98*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
99*c217d954SCole Faust }
100*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
101*c217d954SCole Faust }
102*c217d954SCole Faust
103*c217d954SCole Faust // Checks performed when sum is configured
104*c217d954SCole Faust if(sum.total_size() != 0)
105*c217d954SCole Faust {
106*c217d954SCole Faust if(is_quantized_asymmetric)
107*c217d954SCole Faust {
108*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&sum, 1, DataType::S32);
109*c217d954SCole Faust }
110*c217d954SCole Faust else
111*c217d954SCole Faust {
112*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&max, &sum);
113*c217d954SCole Faust }
114*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&max, &sum);
115*c217d954SCole Faust }
116*c217d954SCole Faust
117*c217d954SCole Faust return Status{};
118*c217d954SCole Faust }
119*c217d954SCole Faust
validate_arguments_1DNorm(const ITensorInfo & src,const ITensorInfo & sum,const ITensorInfo & dst,const SoftmaxKernelInfo & info)120*c217d954SCole Faust Status validate_arguments_1DNorm(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info)
121*c217d954SCole Faust {
122*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src);
123*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::S32, DataType::F16, DataType::F32);
124*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &sum);
125*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(info.is_log && !is_data_type_float(info.input_data_type));
126*c217d954SCole Faust
127*c217d954SCole Faust // Note: output should always have a scale of 1/256 and offset 0
128*c217d954SCole Faust const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
129*c217d954SCole Faust const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
130*c217d954SCole Faust
131*c217d954SCole Faust // Checks performed when output is configured
132*c217d954SCole Faust if(dst.total_size() != 0)
133*c217d954SCole Faust {
134*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
135*c217d954SCole Faust if(!is_quantized_asymmetric)
136*c217d954SCole Faust {
137*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
138*c217d954SCole Faust }
139*c217d954SCole Faust else
140*c217d954SCole Faust {
141*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
142*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != allowed_quantization_info);
143*c217d954SCole Faust }
144*c217d954SCole Faust }
145*c217d954SCole Faust
146*c217d954SCole Faust return Status{};
147*c217d954SCole Faust }
148*c217d954SCole Faust } // namespace
149*c217d954SCole Faust
150*c217d954SCole Faust /**< Grid size (obtained through auto-tuning) */
151*c217d954SCole Faust const unsigned int ClLogits1DMaxShiftExpSumKernel::_grid_size = 64;
152*c217d954SCole Faust /**< Vector size in the serial case (obtained through auto-tuning) */
153*c217d954SCole Faust const unsigned int ClLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
154*c217d954SCole Faust /**< Vector size in the parallel case (obtained through auto-tuning, enables the best memory access pattern for Bifrost) .*/
155*c217d954SCole Faust const unsigned int ClLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
156*c217d954SCole Faust
ClLogits1DMaxShiftExpSumKernel()157*c217d954SCole Faust ClLogits1DMaxShiftExpSumKernel::ClLogits1DMaxShiftExpSumKernel()
158*c217d954SCole Faust {
159*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
160*c217d954SCole Faust }
161*c217d954SCole Faust
configure(const CLCompileContext & compile_context,const ITensorInfo & src,ITensorInfo & max,ITensorInfo & dst,ITensorInfo & sum,const SoftmaxKernelInfo & info)162*c217d954SCole Faust void ClLogits1DMaxShiftExpSumKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, ITensorInfo &max, ITensorInfo &dst, ITensorInfo &sum, const SoftmaxKernelInfo &info)
163*c217d954SCole Faust {
164*c217d954SCole Faust auto padding_info = get_padding_info({ &src, &max, &dst, &sum });
165*c217d954SCole Faust
166*c217d954SCole Faust // Output auto initialization if not yet initialized
167*c217d954SCole Faust auto_init_if_empty(sum, src.clone()->set_tensor_shape(max.tensor_shape()));
168*c217d954SCole Faust auto_init_if_empty(dst, *src.clone());
169*c217d954SCole Faust
170*c217d954SCole Faust // Perform validation step
171*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
172*c217d954SCole Faust
173*c217d954SCole Faust const DataType dt = src.data_type();
174*c217d954SCole Faust const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
175*c217d954SCole Faust const size_t reduction_dim_size = src.dimension(0);
176*c217d954SCole Faust const float beta = info.beta;
177*c217d954SCole Faust const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
178*c217d954SCole Faust const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
179*c217d954SCole Faust
180*c217d954SCole Faust const unsigned int vector_size = adjust_vec_size(_serial_vector_size, reduction_dim_size);
181*c217d954SCole Faust
182*c217d954SCole Faust // Set build options
183*c217d954SCole Faust CLBuildOptions build_opts;
184*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
185*c217d954SCole Faust build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
186*c217d954SCole Faust build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
187*c217d954SCole Faust build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(reduction_dim_size));
188*c217d954SCole Faust build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(reduction_dim_size % vector_size));
189*c217d954SCole Faust build_opts.add_option("-DLOG_VECTOR_SIZE=" + support::cpp11::to_string(lround(log2(vector_size))));
190*c217d954SCole Faust build_opts.add_option_if((reduction_dim_size % vector_size) != 0, "-DNON_MULTIPLE_OF_VECTOR_SIZE");
191*c217d954SCole Faust build_opts.add_option_if(is_signed_qasymm8, "-DQASYMM8_SIGNED");
192*c217d954SCole Faust build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), "-DBETA=" + float_to_string_with_full_precision(beta));
193*c217d954SCole Faust build_opts.add_option_if(is_data_type_float(dt) && info.is_log, "-DLOG_SOFTMAX");
194*c217d954SCole Faust build_opts.add_option_if(is_data_type_float(dt), "-DMINVAL=" + ((dt == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX")));
195*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
196*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DBETA=" + float_to_string_with_full_precision(beta));
197*c217d954SCole Faust build_opts.add_options_if(is_data_type_quantized_asymmetric(dt), prepare_quantized_softmax_build_options(qinfo.scale, beta).options());
198*c217d954SCole Faust
199*c217d954SCole Faust cl::NDRange lws_hint(cl::NullRange);
200*c217d954SCole Faust std::string kernel_name = std::string("softmax_layer_max_shift_exp_sum_") + (is_data_type_quantized_asymmetric(dt) ? "quantized_" : "") + "serial";
201*c217d954SCole Faust
202*c217d954SCole Faust // Create kernel.
203*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
204*c217d954SCole Faust
205*c217d954SCole Faust // Configure window
206*c217d954SCole Faust Window win = calculate_max_window(src, Steps(reduction_dim_size));
207*c217d954SCole Faust IClKernel::configure_internal(win, lws_hint);
208*c217d954SCole Faust
209*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
210*c217d954SCole Faust }
211*c217d954SCole Faust
validate(const ITensorInfo & src,const ITensorInfo & max,const ITensorInfo & dst,const ITensorInfo & sum)212*c217d954SCole Faust Status ClLogits1DMaxShiftExpSumKernel::validate(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum)
213*c217d954SCole Faust {
214*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DMaxShiftExpSum(src, max, dst, sum));
215*c217d954SCole Faust return Status{};
216*c217d954SCole Faust }
217*c217d954SCole Faust
is_parallel_reduction(size_t size)218*c217d954SCole Faust ClLogits1DMaxShiftExpSumKernel::ParallelReductionInfo ClLogits1DMaxShiftExpSumKernel::is_parallel_reduction(size_t size)
219*c217d954SCole Faust {
220*c217d954SCole Faust bool is_parallel_reduction = (size >= (_grid_size * _serial_vector_size)) && (_grid_size > 1);
221*c217d954SCole Faust unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
222*c217d954SCole Faust return std::make_tuple(is_parallel_reduction, vector_size);
223*c217d954SCole Faust }
224*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)225*c217d954SCole Faust void ClLogits1DMaxShiftExpSumKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
226*c217d954SCole Faust {
227*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
229*c217d954SCole Faust
230*c217d954SCole Faust auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
231*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
232*c217d954SCole Faust auto max = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
233*c217d954SCole Faust auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_1));
234*c217d954SCole Faust
235*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, max, sum);
236*c217d954SCole Faust
237*c217d954SCole Faust // Collapse window in Z dimension
238*c217d954SCole Faust Window window_collapsed = window.collapse_if_possible(IClKernel::window(), Window::DimZ);
239*c217d954SCole Faust
240*c217d954SCole Faust // Reconfigure window in case of parallel reduction
241*c217d954SCole Faust ParallelReductionInfo parallel_reduction_info = is_parallel_reduction(src->info()->dimension(0));
242*c217d954SCole Faust if(std::get<0>(parallel_reduction_info))
243*c217d954SCole Faust {
244*c217d954SCole Faust // Launch grid_size parallel work items
245*c217d954SCole Faust window_collapsed.set(Window::DimX, Window::Dimension(0, _grid_size, 1));
246*c217d954SCole Faust }
247*c217d954SCole Faust
248*c217d954SCole Faust // Get slices
249*c217d954SCole Faust Window slice = window_collapsed.first_slice_window_3D();
250*c217d954SCole Faust do
251*c217d954SCole Faust {
252*c217d954SCole Faust unsigned int idx = 0;
253*c217d954SCole Faust // Set inputs
254*c217d954SCole Faust add_3D_tensor_argument(idx, src, slice);
255*c217d954SCole Faust add_3D_tensor_argument(idx, max, slice);
256*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
257*c217d954SCole Faust add_3D_tensor_argument(idx, sum, slice);
258*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
259*c217d954SCole Faust }
260*c217d954SCole Faust while(window_collapsed.slide_window_slice_3D(slice));
261*c217d954SCole Faust }
262*c217d954SCole Faust
ClLogits1DNormKernel()263*c217d954SCole Faust ClLogits1DNormKernel::ClLogits1DNormKernel()
264*c217d954SCole Faust {
265*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
266*c217d954SCole Faust }
267*c217d954SCole Faust
configure(const CLCompileContext & compile_context,const ITensorInfo & src,const ITensorInfo & sum,ITensorInfo & dst,const SoftmaxKernelInfo & info)268*c217d954SCole Faust void ClLogits1DNormKernel::configure(const CLCompileContext &compile_context, const ITensorInfo &src, const ITensorInfo &sum, ITensorInfo &dst, const SoftmaxKernelInfo &info)
269*c217d954SCole Faust {
270*c217d954SCole Faust auto padding_info = get_padding_info({ &src, &dst, &sum });
271*c217d954SCole Faust
272*c217d954SCole Faust // Note: output should always have a scale of 1/256 and offset 0
273*c217d954SCole Faust const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(info.input_data_type);
274*c217d954SCole Faust const DataType output_data_type = info.input_data_type;
275*c217d954SCole Faust const QuantizationInfo allowed_quantization_info = get_softmax_output_quantization_info(info.input_data_type, info.is_log);
276*c217d954SCole Faust const UniformQuantizationInfo qinfo = src.quantization_info().uniform();
277*c217d954SCole Faust
278*c217d954SCole Faust // Output auto initialization if not yet initialized
279*c217d954SCole Faust auto_init_if_empty(dst, src.clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
280*c217d954SCole Faust
281*c217d954SCole Faust // Perform validation step
282*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_1DNorm(src, sum, dst, info));
283*c217d954SCole Faust
284*c217d954SCole Faust const auto is_signed_qasymm8 = is_data_type_quantized_asymmetric_signed(info.input_data_type);
285*c217d954SCole Faust const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
286*c217d954SCole Faust const unsigned int vector_size = adjust_vec_size(16, src.dimension(0));
287*c217d954SCole Faust
288*c217d954SCole Faust // Set build options
289*c217d954SCole Faust CLBuildOptions build_opts;
290*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(info.input_data_type));
291*c217d954SCole Faust build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(min_value));
292*c217d954SCole Faust build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size));
293*c217d954SCole Faust build_opts.add_option("-DVECTOR_SIZE_LEFTOVER=" + support::cpp11::to_string(src.dimension(0) % vector_size));
294*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric_signed(info.input_data_type), "-DQASYMM8_SIGNED");
295*c217d954SCole Faust build_opts.add_options_if(is_quantized_asymmetric,
296*c217d954SCole Faust prepare_quantized_softmax_build_options(qinfo.scale, info.beta).options());
297*c217d954SCole Faust build_opts.add_option_if(info.is_log, "-DLOG_SOFTMAX");
298*c217d954SCole Faust build_opts.add_option_if(is_quantized_asymmetric, "-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
299*c217d954SCole Faust build_opts.add_option_if(is_quantized_asymmetric, "-DBETA=" + float_to_string_with_full_precision(info.beta));
300*c217d954SCole Faust
301*c217d954SCole Faust // Create kernel
302*c217d954SCole Faust std::string kernel_name = std::string("softmax_layer_norm") + (is_quantized_asymmetric ? "_quantized" : "");
303*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
304*c217d954SCole Faust
305*c217d954SCole Faust // Configure window
306*c217d954SCole Faust auto win = calculate_max_window(src, Steps(vector_size));
307*c217d954SCole Faust ICLKernel::configure_internal(win);
308*c217d954SCole Faust
309*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
310*c217d954SCole Faust }
311*c217d954SCole Faust
validate(const ITensorInfo & src,const ITensorInfo & sum,const ITensorInfo & dst,const SoftmaxKernelInfo & info)312*c217d954SCole Faust Status ClLogits1DNormKernel::validate(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info)
313*c217d954SCole Faust {
314*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_1DNorm(src, sum, dst, info));
315*c217d954SCole Faust
316*c217d954SCole Faust return Status{};
317*c217d954SCole Faust }
318*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)319*c217d954SCole Faust void ClLogits1DNormKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
320*c217d954SCole Faust {
321*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
322*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
323*c217d954SCole Faust
324*c217d954SCole Faust auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
325*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
326*c217d954SCole Faust auto sum = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_INT_0));
327*c217d954SCole Faust
328*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, sum);
329*c217d954SCole Faust
330*c217d954SCole Faust Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
331*c217d954SCole Faust Window slice = window_collapsed.first_slice_window_3D();
332*c217d954SCole Faust
333*c217d954SCole Faust do
334*c217d954SCole Faust {
335*c217d954SCole Faust Window sum_slice = slice;
336*c217d954SCole Faust sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
337*c217d954SCole Faust
338*c217d954SCole Faust unsigned int idx = 0;
339*c217d954SCole Faust // Set inputs
340*c217d954SCole Faust add_3D_tensor_argument(idx, src, slice);
341*c217d954SCole Faust add_3D_tensor_argument(idx, sum, sum_slice);
342*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
343*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
344*c217d954SCole Faust }
345*c217d954SCole Faust while(window_collapsed.slide_window_slice_3D(slice));
346*c217d954SCole Faust }
347*c217d954SCole Faust } // namespace kernels
348*c217d954SCole Faust } // namespace opencl
349*c217d954SCole Faust } // namespace arm_compute