54 CLBuildOptions prepare_quantized_softmax_build_options(
float input_scale,
float beta)
57 static const int scaled_diff_int_bits = 5;
59 static const int exp_accumulation_in_bits = 12;
61 const double beta_multiplier = std::min(
62 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
64 int input_beta_multiplier;
65 int input_beta_left_shift;
68 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 const int diff_min = -1.f * std::floor(max_input_rescaled);
71 CLBuildOptions build_opts;
81 Status validate_arguments_1DMaxShiftExpSum(
const ITensorInfo &
src,
const ITensorInfo &max,
const ITensorInfo &
dst,
const ITensorInfo &sum)
90 if(
dst.total_size() != 0)
92 if(is_quantized_asymmetric)
104 if(sum.total_size() != 0)
106 if(is_quantized_asymmetric)
120 Status validate_arguments_1DNorm(
const ITensorInfo &
src,
const ITensorInfo &sum,
const ITensorInfo &
dst,
const SoftmaxKernelInfo &
info)
132 if(
dst.total_size() != 0)
135 if(!is_quantized_asymmetric)
151 const unsigned int ClLogits1DMaxShiftExpSumKernel::_grid_size = 64;
153 const unsigned int ClLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
155 const unsigned int ClLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
170 const size_t reduction_dim_size =
src.dimension(0);
171 const float beta =
info.beta;
173 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
176 const unsigned int vector_size =
adjust_vec_size(std::get<1>(parallel_reduction_info), reduction_dim_size);
186 build_opts.
add_option_if((reduction_dim_size % vector_size) != 0,
"-DNON_MULTIPLE_OF_VECTOR_SIZE");
187 build_opts.
add_option_if(is_signed_qasymm8,
"-DQASYMM8_SIGNED");
193 cl::NDRange
lws_hint(cl::NullRange);
197 if(std::get<0>(parallel_reduction_info))
200 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
204 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
205 build_opts.
add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0),
"-DNON_MULTIPLE_OF_GRID_SIZE");
220 IClKernel::configure_internal(win,
lws_hint);
255 if(std::get<0>(parallel_reduction_info))
265 unsigned int idx = 0;
287 auto_init_if_empty(
dst,
src.clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
293 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
303 build_opts.add_options_if(is_quantized_asymmetric,
304 prepare_quantized_softmax_build_options(
qinfo.
scale,
info.beta).options());
305 build_opts.add_option_if(
info.is_log,
"-DLOG_SOFTMAX");
308 std::string
kernel_name = std::string(
"softmax_layer_norm") + (is_quantized_asymmetric ?
"_quantized" :
"");
313 ICLKernel::configure_internal(win);
344 unsigned int idx = 0;
void configure(const CLCompileContext &compile_context, const ITensorInfo &src, ITensorInfo &max, ITensorInfo &dst, ITensorInfo &sum, const SoftmaxKernelInfo &info)
Configure the kernel using the given information about tensors.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
const Window & window() const
The maximum window the kernel can be executed on.
void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override
static Status validate(const ITensorInfo &src, const ITensorInfo &sum, const ITensorInfo &dst, const SoftmaxKernelInfo &info)
Static function to check if given info will lead to a valid configuration of ClLogits1DNormKernel.
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
std::string to_string(T &&value)
Convert integer and float values to string.
1 channel, 1 F32 per channel
static ParallelReductionInfo is_parallel_reduction(size_t size)
Checks if the given size is eligible for parallel reduction.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Store the tensor's metadata.
QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Returns output quantization information for softmax layer.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Describe one of the image's dimensions with a start, end and step.
Status calculate_quantized_multiplier_greater_than_one(float multiplier, int32_t *quantized_multiplier, int32_t *left_shift)
Calculate quantized representation of multiplier having value greater than one.
std::tuple< bool, unsigned int > ParallelReductionInfo
Info for whether a parallel reduction will be run and the vector size of the execution.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
SimpleTensor< float > src
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
static Status validate(const ITensorInfo &src, const ITensorInfo &max, const ITensorInfo &dst, const ITensorInfo &sum)
Static function to check if given info will lead to a valid configuration of ClLogits1DMaxShiftExpSum...
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Quantization information.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
bool is_data_type_quantized_asymmetric_signed(DataType dt)
Check if a given data type is of asymmetric quantized signed type.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
const std::vector< float > & scale() const
Scale vector accessor.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
const QuantizationInfo qinfo
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension,...
void configure(const CLCompileContext &compile_context, const ITensorInfo &src, const ITensorInfo &sum, ITensorInfo &dst, const SoftmaxKernelInfo &info)
Set the input and output tensors.
quantized, asymmetric fixed-point 8-bit number signed
Descriptor used by the softmax kernels.
Window first_slice_window_3D() const
First 3D slice of the window.
DataType
Available data types.
Describe a multidimensional execution window.
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
void add_options_if(bool cond, const StringSet &options)
Appends given build options to the current's objects options if a given condition is true.