24 #ifndef ARM_COMPUTE_UTILS_H 25 #define ARM_COMPUTE_UTILS_H 40 #include <type_traits> 41 #include <unordered_map> 57 template <
typename S,
typename T>
58 constexpr
auto DIV_CEIL(S val, T m) -> decltype((val + m - 1) / m)
60 return (val + m - 1) / m;
70 template <
typename S,
typename T>
71 inline auto ceil_to_multiple(S value, T divisor) -> decltype(((value + divisor - 1) / divisor) * divisor)
74 return DIV_CEIL(value, divisor) * divisor;
84 template <
typename S,
typename T>
88 return (value / divisor) * divisor;
98 std::string
read_file(
const std::string &filename,
bool binary);
133 return sizeof(size_t);
574 max =
PixelValue(static_cast<int32_t>(std::numeric_limits<uint8_t>::max()));
583 max =
PixelValue(static_cast<int32_t>(std::numeric_limits<int8_t>::max()));
590 max =
PixelValue(static_cast<int32_t>(std::numeric_limits<uint16_t>::max()));
597 max =
PixelValue(static_cast<int32_t>(std::numeric_limits<int16_t>::max()));
603 max =
PixelValue(std::numeric_limits<uint32_t>::max());
609 max =
PixelValue(std::numeric_limits<int32_t>::max());
621 max =
PixelValue(std::numeric_limits<half>::max());
627 max =
PixelValue(std::numeric_limits<float>::max());
633 return std::make_tuple(min, max);
667 inline bool separate_matrix(
const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size)
669 int32_t min_col = -1;
670 int16_t min_col_val = -1;
672 for(int32_t i = 0; i < size; ++i)
674 if(conv[i] != 0 && (min_col < 0 || abs(min_col_val) > abs(conv[i])))
677 min_col_val = conv[i];
686 for(uint32_t j = 0; j < size; ++j)
688 conv_col[j] = conv[min_col + j * size];
691 for(uint32_t i = 0; i < size; i++)
693 if(static_cast<int>(i) == min_col)
699 int16_t coeff = conv[i] / conv[min_col];
701 for(uint32_t j = 1; j < size; ++j)
703 if(conv[i + j * size] != (conv_col[j] * coeff))
729 const size_t size = matrix_size * matrix_size;
731 return std::max(1, std::abs(
std::accumulate(matrix, matrix + size, 0)));
754 output.
set(0, (output.x() + 1) & ~1
U);
760 output.
set(1, (output.y() + 1) & ~1
U);
784 output.
set(0, output.x() / 2
U);
790 output.
set(1, output.y() / 2
U);
811 auto gez = [](
const int16_t &v)
816 auto accu_neg = [](
const int &first,
const int &second)
818 return first + (second < 0 ? second : 0);
821 auto accu_pos = [](
const int &first,
const int &second)
823 return first + (second > 0 ? second : 0);
826 const bool only_positive_coefficients = std::all_of(conv_row, conv_row + size, gez) && std::all_of(conv_col, conv_col + size, gez);
828 if(only_positive_coefficients)
830 const int max_row_value =
std::accumulate(conv_row, conv_row + size, 0) * UINT8_MAX;
831 const int max_value =
std::accumulate(conv_col, conv_col + size, 0) * max_row_value;
839 const int min_row_value =
std::accumulate(conv_row, conv_row + size, 0, accu_neg) * UINT8_MAX;
840 const int max_row_value =
std::accumulate(conv_row, conv_row + size, 0, accu_pos) * UINT8_MAX;
841 const int neg_coeffs_sum =
std::accumulate(conv_col, conv_col + size, 0, accu_neg);
842 const int pos_coeffs_sum =
std::accumulate(conv_col, conv_col + size, 0, accu_pos);
843 const int min_value = neg_coeffs_sum * max_row_value + pos_coeffs_sum * min_row_value;
844 const int max_value = neg_coeffs_sum * min_row_value + pos_coeffs_sum * max_row_value;
851 return std::make_pair(first_stage, second_stage);
864 auto gez = [](
const int16_t v)
869 const bool only_positive_coefficients = std::all_of(conv, conv + size, gez);
871 if(only_positive_coefficients)
873 const int max_conv_value =
std::accumulate(conv, conv + size, 0) * UINT8_MAX;
874 if(max_conv_value <= UINT16_MAX)
885 const int min_value =
std::accumulate(conv, conv + size, 0, [](
int a,
int b)
887 return b < 0 ? a +
b : a;
891 const int max_value =
std::accumulate(conv, conv + size, 0, [](
int a,
int b)
893 return b > 0 ? a +
b : a;
897 if((INT16_MIN <= min_value) && (INT16_MAX >= max_value))
914 template <
typename T>
917 const auto old_dim = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.
begin(), dimensions.
end());
920 T dimension_val = old_dim[i];
921 dimensions.
set(perm[i], dimension_val);
950 unsigned int kernel_width,
unsigned int kernel_height,
965 int kernel_width,
int kernel_height,
1104 std::unordered_map<const ITensorInfo *, PaddingSize>
get_padding_info(std::initializer_list<const ITensorInfo *> infos);
1111 std::unordered_map<const ITensorInfo *, PaddingSize>
get_padding_info(std::initializer_list<const ITensor *> tensors);
1118 bool has_padding_changed(
const std::unordered_map<const ITensorInfo *, PaddingSize> &padding_map);
1264 std::stringstream
ss;
1265 ss.precision(std::numeric_limits<float>::max_digits10);
1268 if(val != static_cast<int>(val))
1287 return size_t(std::ceil((end - start) / step));
1298 template <
typename T>
1305 const auto val_u8 =
static_cast<uint8_t
>(val);
1312 return ((
double)val >= min && (double)val <= max);
1316 const auto val_s8 =
static_cast<int8_t
>(val);
1321 const auto val_u16 =
static_cast<uint16_t
>(val);
1326 const auto val_s16 =
static_cast<int16_t
>(val);
1331 const auto val_u32 =
static_cast<uint32_t
>(val);
1336 const auto val_s32 =
static_cast<int32_t
>(val);
1362 if((vec_size >= dim0) && (dim0 == 3))
1367 while(vec_size > dim0)
1375 #ifdef ARM_COMPUTE_ASSERTS_ENABLED 1384 template <
typename T>
1385 void print_consecutive_elements_impl(std::ostream &s,
const T *ptr,
unsigned int n,
int stream_width = 0,
const std::string &element_delim =
" ")
1387 using print_type =
typename std::conditional<std::is_floating_point<T>::value, T,
int>
::type;
1388 std::ios stream_status(
nullptr);
1389 stream_status.copyfmt(s);
1391 for(
unsigned int i = 0; i < n; ++i)
1394 if(stream_width != 0)
1396 s.width(stream_width);
1402 s << std::right << static_cast<T>(ptr[i]) << element_delim;
1407 s << std::right << float(ptr[i]) << element_delim;
1411 s << std::right << static_cast<print_type>(ptr[i]) << element_delim;
1416 s.copyfmt(stream_status);
1427 template <
typename T>
1428 int max_consecutive_elements_display_width_impl(std::ostream &s,
const T *ptr,
unsigned int n)
1430 using print_type =
typename std::conditional<std::is_floating_point<T>::value, T,
int>
::type;
1433 for(
unsigned int i = 0; i < n; ++i)
1435 std::stringstream
ss;
1441 ss << static_cast<T>(ptr[i]);
1446 ss << float(ptr[i]);
1450 ss << static_cast<print_type>(ptr[i]);
1453 max_width = std::max<int>(max_width, ss.str().size());
1467 void print_consecutive_elements(std::ostream &s,
DataType dt,
const uint8_t *ptr,
unsigned int n,
int stream_width,
const std::string &element_delim =
" ");
1478 int max_consecutive_elements_display_width(std::ostream &s,
DataType dt,
const uint8_t *ptr,
unsigned int n);
BorderMode
Methods available to handle borders.
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis)
Check if the given reduction operation should be handled in a serial way.
Class describing the value of a pixel for any image format.
A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte.
InterpolationPolicy
Interpolation method.
bool has_format_vertical_subsampling(Format format)
Return true if the given format has vertical subsampling.
quantized, symmetric fixed-point 16-bit number
int plane_idx_from_channel(Format format, Channel channel)
Return the plane index of a given channel given an input format.
TensorShape calculate_subsampled_shape(const TensorShape &shape, Format format, Channel channel=Channel::UNKNOWN)
Calculate subsampled shape for a given format and channel.
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
ReductionOperation
Available reduction operations.
const std::string & string_from_matrix_pattern(MatrixPattern pattern)
Convert a matrix pattern into a string.
void permute_strides(Dimensions< T > &dimensions, const PermutationVector &perm)
Permutes the given dimensions according the permutation vector.
size_t num_planes_from_format(Format format)
Return the number of planes for a given format.
const std::string & string_from_channel(Channel channel)
Convert a channel identity into a string.
std::pair< unsigned int, unsigned int > deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info)
Returns expected width and height of the deconvolution's output tensor.
Brain floating point representation class.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
1 channel, 1 U8 per channel
size_t element_size_from_data_type(DataType dt)
The size in bytes of the data type.
half_float::half half
16-bit floating point type
1 channel, 1 F32 per channel
DimensionRoundingType
Dimension rounding type when down-scaling on CNNs.
std::pair< DataType, DataType > data_type_for_convolution(const int16_t *conv_col, const int16_t *conv_row, size_t size)
Calculate accurary required by the horizontal and vertical convolution computations.
std::stringstream ss(mlgo_str)
std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Convert a PixelValue to a string, represented through the specific data type.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
const DataLayout data_layout
size_t num_channels_from_format(Format format)
Return the number of channels for a given single-planar pixel format.
const std::string & string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
Translates a given activation function to a string.
A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling.
QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Returns output quantization information for softmax layer.
quantized, asymmetric fixed-point 16-bit number
1 channel, 1 U16 per channel
const std::string & string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage)
Translates a given GEMMLowp output stage to a string.
std::string lower_string(const std::string &val)
Lower a given string.
bool is_data_type_quantized_symmetric(DataType dt)
Check if a given data type is of symmetric quantized type.
Activation Layer Information class.
uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size)
Calculate the scale of the given square matrix.
decltype(strategy::transforms) typedef type
auto floor_to_multiple(S value, T divisor) -> decltype((value/divisor) *divisor)
Computes the largest number smaller or equal to value that is a multiple of divisor.
A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling.
Copyright (c) 2017-2021 Arm Limited.
ActivationFunction
Available activation functions.
1 channel, 1 F16 per channel
constexpr auto DIV_CEIL(S val, T m) -> decltype((val+m - 1)/m)
Calculate the rounded up quotient of val / m.
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode...
const std::string & string_from_norm_type(NormType type)
Translates a given normalization type to a string.
const std::string & string_from_border_mode(BorderMode border_mode)
Translates a given border mode policy to a string.
static bfloat16 lowest()
Lowest representative value.
1 channel, 1 S32 per channel
16-bit brain floating-point number
3 channels, 1 U8 per channel
Quantization information.
TensorShape input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
DataType get_promoted_data_type(DataType dt)
Return the promoted data type of a given data type.
std::string read_file(const std::string &filename, bool binary)
Load an entire file in memory.
1 channel, 1 U32 per channel
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
bool check_value_range(T val, DataType dt, QuantizationInfo qinfo=QuantizationInfo())
Returns true if the value can be represented by the given data type.
Channel
Available channels.
Format
Image colour formats.
std::pair< int32_t, int32_t > get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info)
Returns a pair of minimum and maximum values for a quantized activation.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
const std::string & string_from_interpolation_policy(InterpolationPolicy policy)
Translates a given interpolation policy to a string.
bool is_data_type_quantized_asymmetric_signed(DataType dt)
Check if a given data type is of asymmetric quantized signed type.
Dimensions with dimensionality.
A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes.
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
const std::string & string_from_non_linear_filter_function(NonLinearFilterFunction function)
Translates a given non linear function to a string.
Padding and stride information class.
void end(TokenStream &in, bool &valid)
std::array< T, num_max_dimensions >::iterator begin()
Returns a read/write iterator that points to the first element in the dimension array.
GEMMLowpOutputStageType
GEMMLowp output stage type.
size_t num_of_elements_in_range(const float start, const float end, const float step)
Returns the number of elements required to go from start to end with the wanted step.
1 channel, 1 S16 per channel
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.
quantized, symmetric fixed-point 8-bit number
Num samples, channels, height, width.
TensorShape adjust_odd_shape(const TensorShape &shape, Format format)
Adjust tensor shape size if width or height are odd for a given multi-planar format.
DataType data_type_from_format(Format format)
Return the data type used by a given single-planar pixel format.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Strides of an item in bytes.
quantized, symmetric per channel fixed-point 8-bit number
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes.
4 channels, 1 U8 per channel
PoolingType
Available pooling types.
std::array< T, num_max_dimensions >::iterator end()
Returns a read/write iterator that points one past the last element in the dimension array...
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
size_t pixel_size_from_format(Format format)
The size in bytes of the pixel format.
Class for specifying the size of an image or rectangle.
const QuantizationInfo qinfo
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
2 channel, 1 U8 per channel
bool has_format_horizontal_subsampling(Format format)
Return true if the given format has horizontal subsampling.
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
bool separate_matrix(const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size)
Separate a 2D convolution into two 1D convolutions.
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
64-bit floating-point number
DataType data_type_from_name(const std::string &name)
Convert a string to DataType.
A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes.
DataType
Available data types.
static bfloat16 max()
Largest representative value.
DataLayout
[DataLayout enum definition]
const std::string & string_from_pooling_type(PoolingType type)
Translates a given pooling type to a string.
NormType
The normalization type used for the normalization layer.
MatrixPattern
Available matrix patterns.
std::tuple< PixelValue, PixelValue > get_min_max(DataType dt)
Compute the mininum and maximum values a data type can take.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
NonLinearFilterFunction
Available non linear functions.
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
int channel_idx_from_format(Format format, Channel channel)
Return the channel index of a given channel given an input format.
PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout=DataLayout::NCHW, const Size2D &dilation=Size2D(1u, 1u), const DimensionRoundingType &rounding_type=DimensionRoundingType::FLOOR)
Calculate padding requirements in case of SAME padding.
DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size)
Calculate the accuracy required by the squared convolution calculation.
inline ::std::istream & operator>>(::std::istream &stream, DataType &data_type)
Input Stream operator for DataType.
const std::string & string_from_format(Format format)
Convert a tensor format into a string.