47 CLBuildOptions prepare_quantized_softmax_build_options(
float input_scale,
float beta)
50 static const int scaled_diff_int_bits = 5;
52 static const int exp_accumulation_in_bits = 12;
54 const double beta_multiplier = std::min(
55 1.0 * beta * input_scale * (1 << (31 - scaled_diff_int_bits)),
57 int input_beta_multiplier;
58 int input_beta_left_shift;
61 const double max_input_rescaled = 1.0 * ((1 << scaled_diff_int_bits) - 1) * (1LL << (31 - scaled_diff_int_bits)) / (1LL << input_beta_left_shift);
62 const int diff_min = -1.f * std::floor(max_input_rescaled);
64 CLBuildOptions build_opts;
74 Status validate_arguments_1DMaxShiftExpSum(
const ITensorInfo *
input,
const ITensorInfo *max,
const ITensorInfo *output,
const ITensorInfo *
sum)
85 if(output->total_size() != 0)
87 if(is_quantized_asymmetric)
99 if(sum->total_size() != 0)
101 if(is_quantized_asymmetric)
115 Status validate_arguments_1DNorm(
const ITensorInfo *input,
const ITensorInfo *sum,
const ITensorInfo *output,
const SoftmaxKernelInfo &
info)
128 if(output->total_size() != 0)
131 if(!is_quantized_asymmetric)
147 const unsigned int CLLogits1DMaxShiftExpSumKernel::_grid_size = 64;
149 const unsigned int CLLogits1DMaxShiftExpSumKernel::_serial_vector_size = 8;
151 const unsigned int CLLogits1DMaxShiftExpSumKernel::_parallel_vector_size = 4;
154 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
183 const size_t reduction_dim_size = input->
info()->
dimension(0);
184 const float beta = info.
beta;
186 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
189 const unsigned int vector_size =
adjust_vec_size(std::get<1>(parallel_reduction_info), reduction_dim_size);
199 build_opts.
add_option_if((reduction_dim_size % vector_size) != 0,
"-DNON_MULTIPLE_OF_VECTOR_SIZE");
200 build_opts.
add_option_if(is_signed_qasymm8,
"-DQASYMM8_SIGNED");
206 cl::NDRange
lws_hint(cl::NullRange);
210 if(std::get<0>(parallel_reduction_info))
212 kernel_name +=
"parallel";
213 bool is_grid_size_pow2 = (_grid_size != 0) && ((_grid_size & (_grid_size - 1)) == 0);
217 const unsigned int multiple_grid_size = (reduction_dim_size / vector_size) % _grid_size;
218 build_opts.
add_option_if((multiple_grid_size != 0) || ((reduction_dim_size % vector_size) != 0),
"-DNON_MULTIPLE_OF_GRID_SIZE");
221 lws_hint = cl::NDRange(_grid_size);
225 kernel_name +=
"serial";
233 ICLKernel::configure_internal(win, lws_hint);
247 unsigned int vector_size = is_parallel_reduction ? _parallel_vector_size : _serial_vector_size;
248 return std::make_tuple(is_parallel_reduction, vector_size);
261 if(std::get<0>(parallel_reduction_info))
271 unsigned int idx = 0;
283 : _input(nullptr), _sum(nullptr), _output(nullptr)
306 input->
info()->
clone()->set_data_type(output_data_type).set_quantization_info(allowed_quantization_info));
316 const int min_value = is_signed_qasymm8 ? CL_SCHAR_MIN : 0;
326 build_opts.add_options_if(is_quantized_asymmetric,
327 prepare_quantized_softmax_build_options(qinfo.
scale, info.
beta).options());
328 build_opts.add_option_if(info.
is_log,
"-DLOG_SOFTMAX");
331 std::string
kernel_name = std::string(
"softmax_layer_norm") + (is_quantized_asymmetric ?
"_quantized" :
"");
332 _kernel =
create_kernel(compile_context, kernel_name, build_opts.options());
336 ICLKernel::configure_internal(win);
361 unsigned int idx = 0;
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 enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, const SoftmaxKernelInfo &info)
Static function to check if given info will lead to a valid configuration of CLLogits1DNormKernel.
const StringSet & options() const
Gets the current options list set.
float beta
A scaling factor for the exponent with default value 1.0.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
#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.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
CLLogits1DMaxShiftExpSumKernel()
Default constructor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
CLLogits1DNormKernel()
Default constructor.
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.
#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...
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
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.
DataType input_data_type
Input tensor data type.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
bool is_log
Flag used to perform Log Softmax operation.
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...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
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.
void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, const SoftmaxKernelInfo &info)
Set the input and output tensors.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Interface for OpenCL tensor.
static ParallelReductionInfo is_parallel_reduction(size_t size)
Checks if the given size is eligible for parallel reduction.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum)
Static function to check if given info will lead to a valid configuration of CLLogits1DMaxShiftExpSum...
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,...)
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
std::tuple< bool, unsigned int > ParallelReductionInfo
Info for whether a parallel reduction will be run and the vector size of the execution.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
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, getting rounded down to its closest valid vector size.
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.
void configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, const SoftmaxKernelInfo &info)
Set the input and output tensors.
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...