Compute Library
 21.02
arm_compute::utils Namespace Reference

Namespaces

 cast
 
 cpu
 
 info_helpers
 
 iterable
 
 math
 
 mmap_io
 
 random
 
 requires
 
 rounding
 
 traits
 

Data Structures

class  CommandLineParser
 Class to parse command line arguments. More...
 
class  CommonGraphOptions
 Common command line options used to configure the graph examples. More...
 
struct  CommonGraphParams
 Structure holding all the common graph parameters. More...
 
class  CommonGraphValidateOptions
 CommonGraphValidateOptions command line options used to configure the graph examples. More...
 
struct  CommonParams
 Structure holding all the graph Example parameters. More...
 
struct  ConvolutionParams
 Structure holding all the Convolution layer graph parameters. More...
 
class  EnumListOption
 Implementation of an option that accepts any number of values from a fixed set. More...
 
class  EnumOption
 Implementation of a simple option that accepts a value from a fixed set. More...
 
class  Example
 Abstract Example class. More...
 
struct  ExampleParams
 Structure holding all the graph Example parameters. More...
 
class  FileImageFeeder
 File Image feeder concrete implementation. More...
 
struct  FrameworkParams
 Structure holding all the common graph parameters. More...
 
struct  FullyConnectedParams
 Structure holding all the fully_connected layer graph parameters. More...
 
class  GraphValidateExample
 
class  IImageDataFeeder
 Image feeder interface. More...
 
class  IImageLoader
 Image loader interface. More...
 
class  ImageLoaderFactory
 Factory for generating appropriate image loader. More...
 
class  JPEGLoader
 Class to load the content of a JPEG file into an Image. More...
 
class  ListOption
 Implementation of an option that accepts any number of values. More...
 
class  MemoryImageFeeder
 Memory Image feeder concrete implementation. More...
 
class  NPYLoader
 Numpy data loader. More...
 
class  Option
 Abstract base class for a command line option. More...
 
class  PPMLoader
 PPM Image loader concrete implementation. More...
 
class  SimpleOption
 Implementation of an option that accepts a single value. More...
 
struct  TensorParams
 Structure holding all the input tensor graph parameters. More...
 
class  ToggleOption
 Implementation of an option that can be either true or false. More...
 
class  uniform_real_distribution_16bit
 Specialized class to generate random non-zero FP16 values. More...
 
class  ValidateExample
 Abstract ValidateExample class. More...
 
struct  VerificationParams
 Structure holding all the verification graph parameters. More...
 
class  VerifyAccessor
 Graph example validation accessor class. More...
 

Enumerations

enum  ConvolutionPaddingMode { Valid, Same, Manual }
 
enum  ImageType { UNKNOWN, PPM, JPEG }
 Supported image types. More...
 

Functions

const std::string & string_from_scheduler_type (Scheduler::Type t)
 Convert a Scheduler::Type into a string. More...
 
void schedule_kernel_on_ctx (IRuntimeContext *ctx, ICPPKernel *kernel, const IScheduler::Hints &hints)
 Schedules a kernel using the context if not nullptr else uses the legacy scheduling flow. More...
 
unsigned int calculate_number_of_stages_only_x_axis (size_t input_x_dimension, unsigned int axis)
 Calculate number of stages for parallel implementations. More...
 
int run_example (int argc, char **argv, std::unique_ptr< Example > example)
 Run an example and handle the potential exceptions it throws. More...
 
inline ::std::istream & operator>> (::std::istream &stream, ConvolutionPaddingMode &Mode)
 Stream Input operator for the ConvolutionPaddingMode type. More...
 
inline ::std::ostream & operator<< (::std::ostream &os, ConvolutionPaddingMode Mode)
 Formatted output of the ConvolutionPaddingMode type. More...
 
PadStrideInfo calculate_convolution_padding (ExampleParams params)
 Calculate stride information. More...
 
void consume_common_graph_parameters (CommonGraphValidateOptions &options, CommonParams &common_params)
 Consumes the consume_common_graph_parameters graph options and creates a structure containing any information. More...
 
std::unique_ptr< graph::ITensorAccessorget_accessor (const TensorParams &tensor, PixelValue lower, PixelValue upper, const std::random_device::result_type seed=0)
 Generates appropriate accessor according to the specified graph parameters. More...
 
template<template< typename D > class VerifyAccessorT>
std::unique_ptr< graph::ITensorAccessorget_verify_accessor (ExampleParams params)
 Generates appropriate convolution verify accessor. More...
 
int run_example (int argc, char **argv, std::unique_ptr< ValidateExample > example)
 Run an example and handle the potential exceptions it throws. More...
 
::std::ostream & operator<< (::std::ostream &os, const CommonGraphParams &common_params)
 Formatted output of the CommonGraphParams type. More...
 
CommonGraphParams consume_common_graph_parameters (CommonGraphOptions &options)
 Consumes the common graph options and creates a structure containing any information. More...
 
void draw_detection_rectangle (arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b)
 Draw a RGB rectangular window for the detected object. More...
 
ImageType get_image_type_from_file (const std::string &filename)
 Gets image type given a file. More...
 
std::tuple< unsigned int, unsigned int, int > parse_ppm_header (std::ifstream &fs)
 Parse the ppm header from an input file stream. More...
 
std::tuple< std::vector< unsigned long >, bool, std::string > parse_npy_header (std::ifstream &fs)
 Parse the npy header from an input file stream. More...
 
uint64_t get_mem_free_from_meminfo ()
 This function returns the amount of memory free reading from /proc/meminfo. More...
 
template<typename T >
int run_example (int argc, char **argv)
 
std::string get_typestring (DataType data_type)
 Obtain numpy type string from DataType. More...
 
template<typename T >
void map (T &tensor, bool blocking)
 Maps a tensor if needed. More...
 
template<typename T >
void unmap (T &tensor)
 Unmaps a tensor if needed. More...
 
template<typename T >
void save_to_ppm (T &tensor, const std::string &ppm_filename)
 Template helper function to save a tensor image to a PPM file. More...
 
template<typename T , typename U = float>
void save_to_npy (T &tensor, const std::string &npy_filename, bool fortran_order)
 Template helper function to save a tensor image to a NPY file. More...
 
template<typename T >
void load_trained_data (T &tensor, const std::string &filename)
 Load the tensor with pre-trained data from a binary file. More...
 
template<typename T , typename TensorType >
void fill_tensor_value (TensorType &tensor, T value)
 
template<typename T , typename TensorType >
void fill_tensor_zero (TensorType &tensor)
 
template<typename T , typename TensorType >
void fill_tensor_vector (TensorType &tensor, std::vector< T > vec)
 
template<typename T , typename TensorType >
void fill_random_tensor (TensorType &tensor, std::random_device::result_type seed, T lower_bound=std::numeric_limits< T >::lowest(), T upper_bound=std::numeric_limits< T >::max())
 
template<typename T , typename TensorType >
void fill_random_tensor (TensorType &tensor, T lower_bound=std::numeric_limits< T >::lowest(), T upper_bound=std::numeric_limits< T >::max())
 
template<typename T >
void init_sgemm_output (T &dst, T &src0, T &src1, arm_compute::DataType dt)
 
template<typename T >
int compare_tensor (ITensor &tensor1, ITensor &tensor2, T tolerance)
 Compare two tensors. More...
 

Variables

 _allowed_values
 

Enumeration Type Documentation

◆ ConvolutionPaddingMode

◆ ImageType

enum ImageType
strong

Supported image types.

Enumerator
UNKNOWN 
PPM 
JPEG 

Definition at line 67 of file Utils.h.

Function Documentation

◆ calculate_convolution_padding()

PadStrideInfo arm_compute::utils::calculate_convolution_padding ( ExampleParams  params)
inline

Calculate stride information.

Depending on the selected padding mode create the desired PadStrideInfo

Parameters
[in]paramsConvolution parameters supplied by the user.
Returns
PadStrideInfo with the correct padding mode.

Definition at line 194 of file graph_validate_utils.h.

References ARM_COMPUTE_ERROR, arm_compute::calculate_same_pad(), ExampleParams::convolution, arm_compute::FLOOR, TensorParams::height, CommonParams::input, Manual, ConvolutionParams::padding_bottom, ConvolutionParams::padding_left, ConvolutionParams::padding_mode, ConvolutionParams::padding_right, ConvolutionParams::padding_stride_x, ConvolutionParams::padding_stride_y, ConvolutionParams::padding_top, Same, Valid, CommonParams::weights, and TensorParams::width.

195 {
196  switch(params.convolution.padding_mode)
197  {
198  case ConvolutionPaddingMode::Manual:
199  {
200  return PadStrideInfo(params.convolution.padding_stride_x, params.convolution.padding_stride_y, params.convolution.padding_left, params.convolution.padding_right, params.convolution.padding_top,
201  params.convolution.padding_bottom, DimensionRoundingType::FLOOR);
202  }
203  case ConvolutionPaddingMode::Valid:
204  {
205  return PadStrideInfo();
206  }
207  case ConvolutionPaddingMode::Same:
208  {
209  return arm_compute::calculate_same_pad(TensorShape(params.input.width, params.input.height), TensorShape(params.weights.width, params.weights.height),
210  PadStrideInfo(params.convolution.padding_stride_x,
211  params.convolution.padding_stride_y));
212  }
213  default:
214  ARM_COMPUTE_ERROR("NOT SUPPORTED!");
215  }
216 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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.
Definition: Utils.cpp:357

◆ calculate_number_of_stages_only_x_axis()

unsigned int calculate_number_of_stages_only_x_axis ( size_t  input_x_dimension,
unsigned int  axis 
)

Calculate number of stages for parallel implementations.

Parameters
[in]input_x_dimensioninput tensor x dimension
[in]axisaxis to be used

Definition at line 68 of file Utils.cpp.

Referenced by CLArgMinMaxLayer::configure(), CLReductionOperation::configure(), CLArgMinMaxLayer::validate(), and CLReductionOperation::validate().

69 {
70  // We need only 1 stage for all axis except x-axis
71  if(axis != 0)
72  {
73  return 1;
74  }
75  // Calculate number of WGs. 16 elements per thread, 8 threads per WG
76  const auto num_of_wg = static_cast<unsigned int>(ceil(input_x_dimension / 128.f));
77 
78  // Calculate number of stages. First stage performs op and the rest reduction sum
79  // depending on the size of the input. Last stage should have only 1 WG.
80  const unsigned int num_of_stages = num_of_wg / 128 + 2;
81  return num_of_stages;
82 }

◆ compare_tensor()

int arm_compute::utils::compare_tensor ( ITensor tensor1,
ITensor tensor2,
tolerance 
)

Compare two tensors.

Parameters
[in]tensor1First tensor to be compared.
[in]tensor2Second tensor to be compared.
[in]toleranceTolerance used for the comparison.
Returns
The number of mismatches

Definition at line 827 of file Utils.h.

References ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES, arm_compute::execute_window_loop(), ITensor::info(), map(), Iterator::ptr(), ITensorInfo::tensor_shape(), unmap(), and Window::use_tensor_dimensions().

828 {
830  ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
831 
832  int num_mismatches = 0;
833  Window window;
834  window.use_tensor_dimensions(tensor1.info()->tensor_shape());
835 
836  map(tensor1, true);
837  map(tensor2, true);
838 
839  Iterator itensor1(&tensor1, window);
840  Iterator itensor2(&tensor2, window);
841 
842  execute_window_loop(window, [&](const Coordinates &)
843  {
844  if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
845  {
846  ++num_mismatches;
847  }
848  },
849  itensor1, itensor2);
850 
851  unmap(itensor1);
852  unmap(itensor2);
853 
854  return num_mismatches;
855 }
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77

◆ consume_common_graph_parameters() [1/2]

CommonGraphParams consume_common_graph_parameters ( CommonGraphOptions options)

Consumes the common graph options and creates a structure containing any information.

Parameters
[in]optionsOptions to consume
Returns
Structure containing the common graph parameters

Definition at line 191 of file CommonGraphOptions.cpp.

References arm_compute::graph::CL, CommonGraphOptions::data_layout, CommonGraphOptions::data_path, CommonGraphOptions::data_type, CommonGraphOptions::enable_cl_cache, CommonGraphOptions::enable_tuner, CommonGraphOptions::fast_math_hint, CommonGraphParams::help, CommonGraphOptions::help, CommonGraphOptions::image, Option::is_set(), CommonGraphOptions::labels, CommonGraphOptions::mlgo_file, CommonGraphOptions::target, CommonGraphOptions::threads, CommonGraphOptions::tuner_file, CommonGraphOptions::tuner_mode, CommonGraphOptions::validation_file, CommonGraphOptions::validation_path, CommonGraphOptions::validation_range, EnumOption< T >::value(), and SimpleOption< T >::value().

192 {
193  FastMathHint fast_math_hint_value = options.fast_math_hint->value() ? FastMathHint::Enabled : FastMathHint::Disabled;
194  auto validation_range = parse_validation_range(options.validation_range->value());
195 
196  CommonGraphParams common_params;
197  common_params.help = options.help->is_set() ? options.help->value() : false;
198  common_params.threads = options.threads->value();
199  common_params.target = options.target->value();
200  common_params.data_type = options.data_type->value();
201  if(options.data_layout->is_set())
202  {
203  common_params.data_layout = options.data_layout->value();
204  }
205  common_params.enable_tuner = options.enable_tuner->is_set() ? options.enable_tuner->value() : false;
206  common_params.enable_cl_cache = common_params.target == arm_compute::graph::Target::CL ? (options.enable_cl_cache->is_set() ? options.enable_cl_cache->value() : true) : false;
207  common_params.tuner_mode = options.tuner_mode->value();
208  common_params.fast_math_hint = options.fast_math_hint->is_set() ? fast_math_hint_value : FastMathHint::Disabled;
209  common_params.data_path = options.data_path->value();
210  common_params.image = options.image->value();
211  common_params.labels = options.labels->value();
212  common_params.validation_file = options.validation_file->value();
213  common_params.validation_path = options.validation_path->value();
214  common_params.validation_range_start = validation_range.first;
215  common_params.validation_range_end = validation_range.second;
216  common_params.tuner_file = options.tuner_file->value();
217  common_params.mlgo_file = options.mlgo_file->value();
218 
219  return common_params;
220 }
FastMathHint
Enable or disable fast math for Convolution layer.
Definition: Types.h:135
OpenCL capable target device.

◆ consume_common_graph_parameters() [2/2]

void arm_compute::utils::consume_common_graph_parameters ( CommonGraphValidateOptions options,
CommonParams common_params 
)

Consumes the consume_common_graph_parameters graph options and creates a structure containing any information.

Parameters
[in]optionsOptions to consume
[out]common_paramsparams structure to consume.
Returns
consume_common_graph_parameters structure containing the common graph parameters

Definition at line 316 of file graph_validate_utils.h.

References VerificationParams::absolute_tolerance, CommonGraphValidateOptions::absolute_tolerance, CommonParams::common_params, FrameworkParams::help, CommonGraphValidateOptions::help, Option::is_set(), VerificationParams::relative_tolerance, CommonGraphValidateOptions::relative_tolerance, FrameworkParams::target, CommonGraphValidateOptions::target, FrameworkParams::threads, CommonGraphValidateOptions::threads, VerificationParams::tolerance_number, CommonGraphValidateOptions::tolerance_number, EnumOption< T >::value(), SimpleOption< T >::value(), and CommonParams::verification.

317 {
318  common_params.common_params.help = options.help->is_set() ? options.help->value() : false;
319  common_params.common_params.threads = options.threads->value();
320  common_params.common_params.target = options.target->value();
321 
322  common_params.verification.absolute_tolerance = options.absolute_tolerance->value();
323  common_params.verification.relative_tolerance = options.relative_tolerance->value();
324  common_params.verification.tolerance_number = options.tolerance_number->value();
325 }

◆ draw_detection_rectangle()

void draw_detection_rectangle ( arm_compute::ITensor tensor,
const arm_compute::DetectionWindow rect,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Draw a RGB rectangular window for the detected object.

Parameters
[in,out]tensorInput tensor where the rectangle will be drawn on. Format supported: RGB888
[in]rectGeometry of the rectangular window
[in]rRed colour to use
[in]gGreen colour to use
[in]bBlue colour to use

Definition at line 130 of file Utils.cpp.

References ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN, arm_compute::test::validation::b, ITensor::buffer(), Window::DimY, DetectionWindow::height, ITensor::info(), ITensorInfo::offset_element_in_bytes(), arm_compute::RGB888, ITensorInfo::strides_in_bytes(), DetectionWindow::width, DetectionWindow::x, and DetectionWindow::y.

Referenced by run_example().

131 {
132  ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(tensor, Format::RGB888);
133 
134  uint8_t *top = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y)) + tensor->buffer();
135  uint8_t *bottom = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y + rect.height)) + tensor->buffer();
136  uint8_t *left = top;
137  uint8_t *right = tensor->info()->offset_element_in_bytes(Coordinates(rect.x + rect.width, rect.y)) + tensor->buffer();
138  size_t stride = tensor->info()->strides_in_bytes()[Window::DimY];
139 
140  for(size_t x = 0; x < rect.width; ++x)
141  {
142  top[0] = r;
143  top[1] = g;
144  top[2] = b;
145  bottom[0] = r;
146  bottom[1] = g;
147  bottom[2] = b;
148 
149  top += 3;
150  bottom += 3;
151  }
152 
153  for(size_t y = 0; y < rect.height; ++y)
154  {
155  left[0] = r;
156  left[1] = g;
157  left[2] = b;
158  right[0] = r;
159  right[1] = g;
160  right[2] = b;
161 
162  left += stride;
163  right += stride;
164  }
165 }
virtual int32_t offset_element_in_bytes(const Coordinates &pos) const =0
The offset in bytes from the beginning of the memory allocation to access the element at position (x...
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
Definition: Validate.h:643
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
ushort y
Top-left y coordinate.
Definition: types.h:50
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
ushort height
Height of the detection window.
Definition: types.h:52
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
ushort x
Top-left x coordinate.
Definition: types.h:49
ushort width
Width of the detection window.
Definition: types.h:51

◆ fill_random_tensor() [1/2]

void arm_compute::utils::fill_random_tensor ( TensorType tensor,
std::random_device::result_type  seed,
lower_bound = std::numeric_limits<T>::lowest(),
upper_bound = std::numeric_limits<T>::max() 
)

Definition at line 774 of file Utils.h.

Referenced by fill_random_tensor(), and main().

775 {
776  constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
777  constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
778 
779  using fp_dist_type = typename std::conditional<is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
780  using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist_type>::type;
781 
782  std::mt19937 gen(seed);
783  dist_type dist(lower_bound, upper_bound);
784 
785  map(tensor, true);
786 
787  Window window;
788  window.use_tensor_dimensions(tensor.info()->tensor_shape());
789 
790  Iterator it(&tensor, window);
791  execute_window_loop(window, [&](const Coordinates &)
792  {
793  *reinterpret_cast<T *>(it.ptr()) = dist(gen);
794  },
795  it);
796 
797  unmap(tensor);
798 }
decltype(strategy::transforms) typedef type
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77

◆ fill_random_tensor() [2/2]

void arm_compute::utils::fill_random_tensor ( TensorType tensor,
lower_bound = std::numeric_limits<T>::lowest(),
upper_bound = std::numeric_limits<T>::max() 
)

Definition at line 801 of file Utils.h.

References fill_random_tensor().

802 {
803  std::random_device rd;
804  fill_random_tensor(tensor, rd(), lower_bound, upper_bound);
805 }
void fill_random_tensor(TensorType &tensor, T lower_bound=std::numeric_limits< T >::lowest(), T upper_bound=std::numeric_limits< T >::max())
Definition: Utils.h:801

◆ fill_tensor_value()

void arm_compute::utils::fill_tensor_value ( TensorType tensor,
value 
)

Definition at line 729 of file Utils.h.

Referenced by fill_tensor_zero().

730 {
731  map(tensor, true);
732 
733  Window window;
734  window.use_tensor_dimensions(tensor.info()->tensor_shape());
735 
736  Iterator it_tensor(&tensor, window);
737  execute_window_loop(window, [&](const Coordinates &)
738  {
739  *reinterpret_cast<T *>(it_tensor.ptr()) = value;
740  },
741  it_tensor);
742 
743  unmap(tensor);
744 }
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77

◆ fill_tensor_vector()

void arm_compute::utils::fill_tensor_vector ( TensorType tensor,
std::vector< T >  vec 
)

Definition at line 753 of file Utils.h.

References ARM_COMPUTE_ERROR_ON, arm_compute::execute_window_loop(), map(), Iterator::ptr(), unmap(), and Window::use_tensor_dimensions().

754 {
755  ARM_COMPUTE_ERROR_ON(tensor.info()->tensor_shape().total_size() != vec.size());
756 
757  map(tensor, true);
758 
759  Window window;
760  window.use_tensor_dimensions(tensor.info()->tensor_shape());
761 
762  int i = 0;
763  Iterator it_tensor(&tensor, window);
764  execute_window_loop(window, [&](const Coordinates &)
765  {
766  *reinterpret_cast<T *>(it_tensor.ptr()) = vec.at(i++);
767  },
768  it_tensor);
769 
770  unmap(tensor);
771 }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77

◆ fill_tensor_zero()

void arm_compute::utils::fill_tensor_zero ( TensorType tensor)

Definition at line 747 of file Utils.h.

References fill_tensor_value().

748 {
749  fill_tensor_value(tensor, T(0));
750 }
void fill_tensor_value(TensorType &tensor, T value)
Definition: Utils.h:729

◆ get_accessor()

std::unique_ptr<graph::ITensorAccessor> arm_compute::utils::get_accessor ( const TensorParams tensor,
PixelValue  lower,
PixelValue  upper,
const std::random_device::result_type  seed = 0 
)
inline

Generates appropriate accessor according to the specified graph parameters.

Parameters
[in]tensorTensor parameters
[in]lowerLower random values bound
[in]upperUpper random values bound
[in]seedRandom generator seed
Returns
An appropriate tensor accessor

Definition at line 336 of file graph_validate_utils.h.

References TensorParams::npy.

Referenced by GraphValidateExample< DepthwiseConvolutionLayer, DepthConvolutionOptions, DepthConvolutionVerifyAccessor >::do_setup().

337 {
338  if(!tensor.npy.empty())
339  {
340  return std::make_unique<arm_compute::graph_utils::NumPyBinLoader>(tensor.npy);
341  }
342  else
343  {
344  return std::make_unique<arm_compute::graph_utils::RandomAccessor>(lower, upper, seed);
345  }
346 }

◆ get_image_type_from_file()

ImageType get_image_type_from_file ( const std::string &  filename)

Gets image type given a file.

Parameters
[in]filenameFile to identify its image type
Returns
Image type

Definition at line 167 of file Utils.cpp.

References ARM_COMPUTE_ERROR_VAR, JPEG, PPM, type, and UNKNOWN.

Referenced by ImageLoaderFactory::create(), and run_example().

168 {
169  ImageType type = ImageType::UNKNOWN;
170 
171  try
172  {
173  // Open file
174  std::ifstream fs;
175  fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
176  fs.open(filename, std::ios::in | std::ios::binary);
177 
178  // Identify type from magic number
179  std::array<unsigned char, 2> magic_number{ { 0 } };
180  fs >> magic_number[0] >> magic_number[1];
181 
182  // PPM check
183  if(static_cast<char>(magic_number[0]) == 'P' && static_cast<char>(magic_number[1]) == '6')
184  {
185  type = ImageType::PPM;
186  }
187  else if(magic_number[0] == 0xFF && magic_number[1] == 0xD8)
188  {
189  type = ImageType::JPEG;
190  }
191 
192  fs.close();
193  }
194  catch(std::runtime_error &e)
195  {
196  ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what());
197  }
198 
199  return type;
200 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
decltype(strategy::transforms) typedef type
ImageType
Supported image types.
Definition: Utils.h:67

◆ get_mem_free_from_meminfo()

uint64_t get_mem_free_from_meminfo ( )

This function returns the amount of memory free reading from /proc/meminfo.

Returns
The free memory in kB

Definition at line 254 of file Utils.cpp.

Referenced by init_sgemm_output().

255 {
256  std::string line_attribute;
257  std::ifstream file_meminfo("/proc/meminfo");
258 
259  if(file_meminfo.is_open())
260  {
261  while(!(file_meminfo >> line_attribute).fail())
262  {
263  //Test if is the line containing MemFree
264  if(line_attribute == "MemFree:")
265  {
266  uint64_t mem_available;
267  if(!(file_meminfo >> mem_available).fail())
268  {
269  return mem_available;
270  }
271  else
272  {
273  return 0;
274  }
275  }
276  // if it's not MemFree ignore rest of the line
277  file_meminfo.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
278  }
279  }
280  // Nothing found or an error during opening the file
281  return 0;
282 }

◆ get_typestring()

std::string arm_compute::utils::get_typestring ( DataType  data_type)
inline

Obtain numpy type string from DataType.

Parameters
[in]data_typeData type.
Returns
numpy type string.

Definition at line 158 of file Utils.h.

References ARM_COMPUTE_ERROR, arm_compute::F16, arm_compute::F32, arm_compute::F64, arm_compute::QASYMM16, arm_compute::QASYMM8, arm_compute::QSYMM16, arm_compute::QSYMM8, arm_compute::QSYMM8_PER_CHANNEL, arm_compute::S16, arm_compute::S32, arm_compute::S64, arm_compute::S8, arm_compute::SIZET, arm_compute::support::cpp11::to_string(), arm_compute::U16, arm_compute::U32, arm_compute::U64, and arm_compute::U8.

Referenced by NPYLoader::fill_tensor().

159 {
160  // Check endianness
161  const unsigned int i = 1;
162  const char *c = reinterpret_cast<const char *>(&i);
163  std::string endianness;
164  if(*c == 1)
165  {
166  endianness = std::string("<");
167  }
168  else
169  {
170  endianness = std::string(">");
171  }
172  const std::string no_endianness("|");
173 
174  switch(data_type)
175  {
176  case DataType::U8:
177  case DataType::QASYMM8:
178  return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
179  case DataType::S8:
180  case DataType::QSYMM8:
181  case DataType::QSYMM8_PER_CHANNEL:
182  return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
183  case DataType::U16:
184  case DataType::QASYMM16:
185  return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
186  case DataType::S16:
187  case DataType::QSYMM16:
188  return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
189  case DataType::U32:
190  return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
191  case DataType::S32:
192  return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
193  case DataType::U64:
194  return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
195  case DataType::S64:
196  return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
197  case DataType::F16:
198  return endianness + "f" + support::cpp11::to_string(sizeof(half));
199  case DataType::F32:
200  return endianness + "f" + support::cpp11::to_string(sizeof(float));
201  case DataType::F64:
202  return endianness + "f" + support::cpp11::to_string(sizeof(double));
203  case DataType::SIZET:
204  return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
205  default:
206  ARM_COMPUTE_ERROR("Data type not supported");
207  }
208 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
half_float::half half
16-bit floating point type
Definition: Types.h:46
const DataType data_type
Definition: Im2Col.cpp:150
std::string to_string(const ICLTensor &arg)

◆ get_verify_accessor()

std::unique_ptr<graph::ITensorAccessor> arm_compute::utils::get_verify_accessor ( ExampleParams  params)
inline

Generates appropriate convolution verify accessor.

Parameters
[in]paramsUser supplied parameters for convolution.
Returns
A convolution verify accessor for the requested datatype.

Definition at line 604 of file graph_validate_utils.h.

References ARM_COMPUTE_ERROR, CommonParams::data_type, arm_compute::F16, arm_compute::F32, and arm_compute::QASYMM8.

605 {
606  switch(params.data_type)
607  {
608  case DataType::QASYMM8:
609  {
610  return std::make_unique<VerifyAccessorT<uint8_t>>(
611  params);
612  }
613  case DataType::F16:
614  {
615  return std::make_unique<VerifyAccessorT<half>>(
616  params);
617  }
618  case DataType::F32:
619  {
620  return std::make_unique<VerifyAccessorT<float>>(
621  params);
622  }
623  default:
624  ARM_COMPUTE_ERROR("NOT SUPPORTED!");
625  }
626 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352

◆ init_sgemm_output()

void arm_compute::utils::init_sgemm_output ( T &  dst,
T &  src0,
T &  src1,
arm_compute::DataType  dt 
)

Definition at line 808 of file Utils.h.

References get_mem_free_from_meminfo().

809 {
810  dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1), src0.info()->dimension(2)), 1, dt));
811 }
DataType dt

◆ load_trained_data()

void arm_compute::utils::load_trained_data ( T &  tensor,
const std::string &  filename 
)

Load the tensor with pre-trained data from a binary file.

Parameters
[in]tensorThe tensor to be filled. Data type supported: F32.
[in]filenameFilename of the binary file to load from.

Definition at line 682 of file Utils.h.

References ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_VAR, Window::DimX, arm_compute::execute_window_loop(), arm_compute::F32, map(), Iterator::ptr(), Window::set(), and unmap().

683 {
684  ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
685 
686  std::ifstream fs;
687 
688  try
689  {
690  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
691  // Open file
692  fs.open(filename, std::ios::in | std::ios::binary);
693 
694  if(!fs.good())
695  {
696  throw std::runtime_error("Could not load binary data: " + filename);
697  }
698 
699  // Map buffer if creating a CLTensor/GCTensor
700  map(tensor, true);
701 
702  Window window;
703 
705 
706  for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
707  {
708  window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
709  }
710 
711  arm_compute::Iterator in(&tensor, window);
712 
713  execute_window_loop(window, [&](const Coordinates &)
714  {
715  fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
716  },
717  in);
718 
719  // Unmap buffer if creating a CLTensor/GCTensor
720  unmap(tensor);
721  }
722  catch(const std::ofstream::failure &e)
723  {
724  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
725  }
726 }
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46

◆ map()

void arm_compute::utils::map ( T &  tensor,
bool  blocking 
)
inline

◆ operator<<() [1/2]

std::ostream & operator<< ( ::std::ostream &  os,
const CommonGraphParams common_params 
)

Formatted output of the CommonGraphParams type.

Parameters
[out]osOutput stream.
[in]common_paramsCommon parameters to output
Returns
Modified output stream.

Definition at line 77 of file CommonGraphOptions.cpp.

References CommonGraphParams::data_layout, CommonGraphParams::data_path, CommonGraphParams::data_type, CommonGraphParams::enable_cl_cache, CommonGraphParams::enable_tuner, arm_compute::graph::Enabled, CommonGraphParams::fast_math_hint, CommonGraphParams::image, CommonGraphParams::labels, CommonGraphParams::mlgo_file, CommonGraphParams::target, CommonGraphParams::threads, CommonGraphParams::tuner_file, CommonGraphParams::tuner_mode, CommonGraphParams::validation_file, CommonGraphParams::validation_path, CommonGraphParams::validation_range_end, and CommonGraphParams::validation_range_start.

78 {
79  std::string false_str = std::string("false");
80  std::string true_str = std::string("true");
81 
82  os << "Threads : " << common_params.threads << std::endl;
83  os << "Target : " << common_params.target << std::endl;
84  os << "Data type : " << common_params.data_type << std::endl;
85  os << "Data layout : " << common_params.data_layout << std::endl;
86  os << "Tuner enabled? : " << (common_params.enable_tuner ? true_str : false_str) << std::endl;
87  os << "Cache enabled? : " << (common_params.enable_cl_cache ? true_str : false_str) << std::endl;
88  os << "Tuner mode : " << common_params.tuner_mode << std::endl;
89  os << "Tuner file : " << common_params.tuner_file << std::endl;
90  os << "MLGO file : " << common_params.mlgo_file << std::endl;
91  os << "Fast math enabled? : " << (common_params.fast_math_hint == FastMathHint::Enabled ? true_str : false_str) << std::endl;
92  if(!common_params.data_path.empty())
93  {
94  os << "Data path : " << common_params.data_path << std::endl;
95  }
96  if(!common_params.image.empty())
97  {
98  os << "Image file : " << common_params.image << std::endl;
99  }
100  if(!common_params.labels.empty())
101  {
102  os << "Labels file : " << common_params.labels << std::endl;
103  }
104  if(!common_params.validation_file.empty())
105  {
106  os << "Validation range : " << common_params.validation_range_start << "-" << common_params.validation_range_end << std::endl;
107  os << "Validation file : " << common_params.validation_file << std::endl;
108  if(!common_params.validation_path.empty())
109  {
110  os << "Validation path : " << common_params.validation_path << std::endl;
111  }
112  }
113 
114  return os;
115 }

◆ operator<<() [2/2]

inline ::std::ostream& arm_compute::utils::operator<< ( ::std::ostream &  os,
ConvolutionPaddingMode  Mode 
)

Formatted output of the ConvolutionPaddingMode type.

Parameters
[out]osOutput stream.
[in]ModeConvolutionPaddingMode to output
Returns
Modified output stream.

Definition at line 85 of file graph_validate_utils.h.

References Manual, Same, and Valid.

86 {
87  switch(Mode)
88  {
89  case ConvolutionPaddingMode::Valid:
90  os << "Valid";
91  break;
92  case ConvolutionPaddingMode::Same:
93  os << "Same";
94  break;
95  case ConvolutionPaddingMode::Manual:
96  os << "Manual";
97  break;
98  default:
99  throw std::invalid_argument("Unsupported padding mode format");
100  }
101 
102  return os;
103 }

◆ operator>>()

inline ::std::istream& arm_compute::utils::operator>> ( ::std::istream &  stream,
ConvolutionPaddingMode Mode 
)

Stream Input operator for the ConvolutionPaddingMode type.

Parameters
[in]streamInput stream.
[out]ModeConvolution parameters to output
Returns
input stream.

Definition at line 52 of file graph_validate_utils.h.

References Manual, Same, and Valid.

53 {
54  static const std::map<std::string, ConvolutionPaddingMode> modes =
55  {
56  { "valid", ConvolutionPaddingMode::Valid },
57  { "same", ConvolutionPaddingMode::Same },
58  { "manual", ConvolutionPaddingMode::Manual }
59  };
60  std::string value;
61  stream >> value;
62 #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
63  try
64  {
65 #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
66  Mode = modes.at(arm_compute::utility::tolower(value));
67 #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
68  }
69  catch(const std::out_of_range &)
70  {
71  throw std::invalid_argument(value);
72  }
73 #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
74 
75  return stream;
76 }
std::string tolower(std::string string)
Convert string to lower case.
Definition: Utility.h:203

◆ parse_npy_header()

std::tuple< std::vector< unsigned long >, bool, std::string > parse_npy_header ( std::ifstream &  fs)

Parse the npy header from an input file stream.

At the end of the execution, the file position pointer will be located at the first pixel stored in the npy file //TODO

Parameters
[in]fsInput file stream to parse
Returns
The width and height stored in the header of the NPY file

Definition at line 233 of file Utils.cpp.

References arm_compute::mlgo::parser::header(), arm_compute::test::validation::reference::reverse(), and arm_compute::test::validation::shape.

Referenced by NPYLoader::open(), and run_example().

234 {
235  std::vector<unsigned long> shape; // NOLINT
236 
237  // Read header
238  std::string header = npy::read_header(fs);
239 
240  // Parse header
241  bool fortran_order = false;
242  std::string typestr;
243  npy::parse_header(header, typestr, fortran_order, shape);
244 
245  std::reverse(shape.begin(), shape.end());
246 
247  return std::make_tuple(shape, fortran_order, typestr);
248 }
SimpleTensor< T > reverse(const SimpleTensor< T > &src, const SimpleTensor< uint32_t > &axis)
Definition: Reverse.cpp:38
void header(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:481

◆ parse_ppm_header()

std::tuple< unsigned int, unsigned int, int > parse_ppm_header ( std::ifstream &  fs)

Parse the ppm header from an input file stream.

At the end of the execution, the file position pointer will be located at the first pixel stored in the ppm file

Parameters
[in]fsInput file stream to parse
Returns
The width, height and max value stored in the header of the PPM file

Definition at line 202 of file Utils.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, and ARM_COMPUTE_UNUSED.

Referenced by PPMLoader::open(), and run_example().

203 {
204  // Check the PPM magic number is valid
205  std::array<char, 2> magic_number{ { 0 } };
206  fs >> magic_number[0] >> magic_number[1];
207  ARM_COMPUTE_ERROR_ON_MSG(magic_number[0] != 'P' || magic_number[1] != '6', "Invalid file type");
208  ARM_COMPUTE_UNUSED(magic_number);
209 
210  discard_comments_and_spaces(fs);
211 
212  unsigned int width = 0;
213  fs >> width;
214 
215  discard_comments_and_spaces(fs);
216 
217  unsigned int height = 0;
218  fs >> height;
219 
220  discard_comments_and_spaces(fs);
221 
222  int max_val = 0;
223  fs >> max_val;
224 
225  discard_comments(fs);
226 
227  ARM_COMPUTE_ERROR_ON_MSG(isspace(fs.peek()) == 0, "Invalid PPM header");
228  fs.ignore(1);
229 
230  return std::make_tuple(width, height, max_val);
231 }
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456

◆ run_example() [1/3]

int run_example ( int  argc,
char **  argv,
std::unique_ptr< Example example 
)

Run an example and handle the potential exceptions it throws.

Parameters
[in]argcNumber of command line arguments
[in]argvCommand line arguments
[in]exampleExample to run

Definition at line 97 of file RunExample.cpp.

References TestCaseFactory::ACTIVE, CommandLineParser::add_option(), Framework::add_printer(), Framework::add_test_case(), arm_compute::test::framework::ALL, ARM_COMPUTE_ERROR_ON_MSG, arm_compute::build_information(), arm_compute::test::framework::CONFIG, arm_compute::create_opencl_context_and_device(), CommonOptions::create_printers(), CLScheduler::default_init_with_context(), CLKernelLibrary::get(), CLScheduler::get(), Framework::get(), CommonOptions::help, Framework::init(), FrameworkConfig::instruments, CommonOptions::instruments, Option::is_set(), CommonOptions::iterations, FrameworkConfig::log_level, CommonOptions::log_level, arm_compute::test::framework::NONE, FrameworkConfig::num_iterations, arm_compute::opencl_is_available(), CommandLineParser::parse(), GemmTuner::parser, CommandLineParser::print_help(), Framework::run(), Option::set_help(), Framework::set_throw_errors(), CommonOptions::throw_errors, arm_compute::support::cpp11::to_string(), EnumOption< T >::value(), EnumListOption< T >::value(), and SimpleOption< T >::value().

Referenced by Example::do_teardown(), ValidateExample::print_parameters(), and run_example().

98 {
100  framework::CommonOptions options(parser);
101  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
102  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
103  framework::Framework &framework = framework::Framework::get();
104 
105  parser.parse(argc, argv);
106 
107  if(options.help->is_set() && options.help->value())
108  {
109  parser.print_help(argv[0]);
110  return 0;
111  }
112 
113  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
114  g_example = std::move(example);
115  g_example_argv.clear();
116  g_example_argv.emplace_back(argv[0]);
117  for(auto &arg : example_args->value())
118  {
119  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
120  }
121 
122  if(options.log_level->value() > framework::LogLevel::NONE)
123  {
124  for(auto &p : printers)
125  {
126  p->print_global_header();
127  }
128  }
129 
130 #ifdef ARM_COMPUTE_CL
131  CLGEMMHeuristicsHandle gemm_h;
132  if(opencl_is_available())
133  {
134  auto ctx_dev_err = create_opencl_context_and_device();
135  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
136  CLScheduler::get()
137  .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
138  }
139 #endif /* ARM_COMPUTE_CL */
140 
141  if(options.log_level->value() >= framework::LogLevel::CONFIG)
142  {
143  for(auto &p : printers)
144  {
145  p->print_entry("Version", build_information());
146  p->print_entry("CommandLine", command_line(argc, argv));
147 #ifdef ARM_COMPUTE_CL
148  if(opencl_is_available())
149  {
150  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
151  }
152  else
153  {
154  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
155  }
156 #endif /* ARM_COMPUTE_CL */
157  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
158  }
159  }
160 
161  // Initialize framework
163  fconfig.instruments = options.instruments->value();
164  fconfig.num_iterations = options.iterations->value();
165  fconfig.log_level = options.log_level->value();
166  framework.init(fconfig);
167 
168  for(auto &p : printers)
169  {
170  framework.add_printer(p.get());
171  }
172  framework.set_throw_errors(options.throw_errors->value());
174 
175 #ifdef BARE_METAL
176  framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
177 #else /* BARE_METAL */
178  framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
179 #endif /* BARE_METAL */
180  //func(argc, argv);
181  bool success = framework.run();
182  if(options.log_level->value() > framework::LogLevel::NONE)
183  {
184  for(auto &p : printers)
185  {
186  p->print_global_footer();
187  }
188  }
189 
190  return (success ? 0 : 1);
191 }
Framework configuration structure.
Definition: Framework.h:57
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:263
void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Add a test case to the framework.
Definition: Framework.h:356
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device()
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:89
Class to parse command line arguments.
Implementation of an option that accepts any number of values.
Definition: ListOption.h:41
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
bool run()
Run all enabled test cases.
Definition: Framework.cpp:540
void parse(int argc, char **argv)
Parses the command line arguments and updates the options accordingly.
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Helper class to statically begin and end a test suite.
Definition: Registrars.h:66
Handle for loading and retrieving GEMM heuristics.
std::string to_string(const ICLTensor &arg)
std::string build_information()
Returns the arm_compute library build information.
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:668
void print_help(const std::string &program_name) const
Prints a help message for all configured options.
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:152

◆ run_example() [2/3]

int arm_compute::utils::run_example ( int  argc,
char **  argv 
)

Definition at line 111 of file Utils.h.

References arm_compute::test::validation::b, draw_detection_rectangle(), get_image_type_from_file(), parse_npy_header(), parse_ppm_header(), and run_example().

112 {
113  return run_example(argc, argv, std::make_unique<T>());
114 }
int run_example(int argc, char **argv)
Definition: Utils.h:111

◆ run_example() [3/3]

int run_example ( int  argc,
char **  argv,
std::unique_ptr< ValidateExample example 
)

Run an example and handle the potential exceptions it throws.

Parameters
[in]argcNumber of command line arguments
[in]argvCommand line arguments
[in]exampleExample to run

Definition at line 112 of file RunExample.cpp.

References TestCaseFactory::ACTIVE, CommandLineParser::add_option(), Framework::add_printer(), Framework::add_test_case(), arm_compute::test::framework::ALL, ARM_COMPUTE_ERROR_ON_MSG, arm_compute::build_information(), arm_compute::test::framework::CONFIG, arm_compute::create_opencl_context_and_device(), CommonOptions::create_printers(), CLScheduler::default_init_with_context(), arm_compute::test::fixed_library, CLKernelLibrary::get(), CLScheduler::get(), Framework::get(), CommonOptions::help, Framework::init(), FrameworkConfig::instruments, CommonOptions::instruments, Option::is_set(), CommonOptions::iterations, arm_compute::test::library, FrameworkConfig::log_level, CommonOptions::log_level, arm_compute::test::framework::NONE, FrameworkConfig::num_iterations, arm_compute::opencl_is_available(), CommandLineParser::parse(), GemmTuner::parser, CommandLineParser::print_help(), Framework::run(), Option::set_help(), Framework::set_throw_errors(), CommonOptions::throw_errors, arm_compute::support::cpp11::to_string(), arm_compute::validate(), EnumOption< T >::value(), EnumListOption< T >::value(), and SimpleOption< T >::value().

113 {
115  framework::CommonOptions options(parser);
116  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
117  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
118  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
119  seed->set_help("Global seed for random number generation");
120  auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1);
121  validate->set_help("Enable / disable output validation (0/1)");
122 
123  framework::Framework &framework = framework::Framework::get();
124 
125  parser.parse(argc, argv);
126 
127  if(options.help->is_set() && options.help->value())
128  {
129  parser.print_help(argv[0]);
130  return 0;
131  }
132 
133  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
134  g_example = std::move(example);
135  g_example_argv.clear();
136  g_example_argv.emplace_back(argv[0]);
137  for(auto &arg : example_args->value())
138  {
139  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
140  }
141 
142  library = std::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value());
143  fixed_library = std::make_unique<AssetsLibrary>(".", fixed_seed);
144 
145  if(options.log_level->value() > framework::LogLevel::NONE)
146  {
147  for(auto &p : printers)
148  {
149  p->print_global_header();
150  }
151  }
152 
153 #ifdef ARM_COMPUTE_CL
154  if(opencl_is_available())
155  {
156  auto ctx_dev_err = create_opencl_context_and_device();
157  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
158  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr);
159  }
160 #endif /* ARM_COMPUTE_CL */
161 
162  if(options.log_level->value() >= framework::LogLevel::CONFIG)
163  {
164  for(auto &p : printers)
165  {
166  p->print_entry("Version", build_information());
167  p->print_entry("CommandLine", command_line(argc, argv));
168  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
169 #ifdef ARM_COMPUTE_CL
170  if(opencl_is_available())
171  {
172  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
173  }
174  else
175  {
176  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
177  }
178 #endif /* ARM_COMPUTE_CL */
179  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
180  g_example->print_parameters(*p);
181  }
182  }
183 
184  // Initialize framework
186  fconfig.instruments = options.instruments->value();
187  fconfig.num_iterations = options.iterations->value();
188  fconfig.log_level = options.log_level->value();
189  framework.init(fconfig);
190 
191  for(auto &p : printers)
192  {
193  framework.add_printer(p.get());
194  }
195 
196  framework.set_throw_errors(options.throw_errors->value());
198  if(validate->value() != 0)
199  {
200  framework.add_test_case<ExampleTest<true>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
201  }
202  else
203  {
204  framework.add_test_case<ExampleTest<false>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
205  }
206 
207  //func(argc, argv);
208  bool success = framework.run();
209  if(options.log_level->value() > framework::LogLevel::NONE)
210  {
211  for(auto &p : printers)
212  {
213  p->print_global_footer();
214  }
215  }
216 
217  return (success ? 0 : 1);
218 }
Framework configuration structure.
Definition: Framework.h:57
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:263
void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Add a test case to the framework.
Definition: Framework.h:356
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device()
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:89
Class to parse command line arguments.
Implementation of an option that accepts any number of values.
Definition: ListOption.h:41
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
bool run()
Run all enabled test cases.
Definition: Framework.cpp:540
void parse(int argc, char **argv)
Parses the command line arguments and updates the options accordingly.
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:78
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Helper class to statically begin and end a test suite.
Definition: Registrars.h:66
std::string to_string(const ICLTensor &arg)
std::string build_information()
Returns the arm_compute library build information.
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:668
void print_help(const std::string &program_name) const
Prints a help message for all configured options.
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
Status validate(const ITensorInfo *scores_in, const ITensorInfo *boxes_in, const ITensorInfo *batch_splits_in, const ITensorInfo *scores_out, const ITensorInfo *boxes_out, const ITensorInfo *classes, const ITensorInfo *batch_splits_out, const ITensorInfo *keeps, const ITensorInfo *keeps_size, const BoxNMSLimitInfo info)
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:152
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:81

◆ save_to_npy()

void arm_compute::utils::save_to_npy ( T &  tensor,
const std::string &  npy_filename,
bool  fortran_order 
)

Template helper function to save a tensor image to a NPY file.

Note
Only F32 data type supported.
If the input tensor is a CLTensor, the function maps and unmaps the image
Parameters
[in]tensorThe tensor to save as NPY file
[in]npy_filenameFilename of the file to create.
[in]fortran_orderIf true, save matrix in fortran order.

Definition at line 627 of file Utils.h.

References ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN, arm_compute::F32, map(), arm_compute::QASYMM8, and arm_compute::test::validation::shape.

Referenced by SaveNumPyAccessor::access_tensor().

628 {
630 
631  std::ofstream fs;
632  try
633  {
634  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
635  fs.open(npy_filename, std::ios::out | std::ios::binary);
636 
637  std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
638 
639  for(unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j)
640  {
641  shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
642  }
643 
644  // Map buffer if creating a CLTensor
645  map(tensor, true);
646 
647  using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
648 
649  std::vector<typestring_type> tmp; /* Used only to get the typestring */
650  npy::Typestring typestring_o{ tmp };
651  std::string typestring = typestring_o.str();
652 
653  std::ofstream stream(npy_filename, std::ofstream::binary);
654  npy::write_header(stream, typestring, fortran_order, shape);
655 
656  arm_compute::Window window;
657  window.use_tensor_dimensions(tensor.info()->tensor_shape());
658 
659  arm_compute::Iterator in(&tensor, window);
660 
662  {
663  stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
664  },
665  in);
666 
667  // Unmap buffer if creating a CLTensor
668  unmap(tensor);
669  }
670  catch(const std::ofstream::failure &e)
671  {
672  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
673  }
674 }
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
decltype(strategy::transforms) typedef type
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:692
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
uint8_t qasymm8_t
8 bit quantized asymmetric scalar value
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39

◆ save_to_ppm()

void arm_compute::utils::save_to_ppm ( T &  tensor,
const std::string &  ppm_filename 
)

Template helper function to save a tensor image to a PPM file.

Note
Only U8 and RGB888 formats supported.
Only works with 2D tensors.
If the input tensor is a CLTensor, the function maps and unmaps the image
Parameters
[in]tensorThe tensor to save as PPM file
[in]ppm_filenameFilename of the file to create.

Definition at line 547 of file Utils.h.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN, Window::DimX, Window::DimY, arm_compute::execute_window_loop(), map(), arm_compute::RGB888, Window::set(), and arm_compute::U8.

Referenced by PPMWriter::access_tensor().

548 {
550  ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
551 
552  std::ofstream fs;
553 
554  try
555  {
556  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
557  fs.open(ppm_filename, std::ios::out | std::ios::binary);
558 
559  const unsigned int width = tensor.info()->tensor_shape()[0];
560  const unsigned int height = tensor.info()->tensor_shape()[1];
561 
562  fs << "P6\n"
563  << width << " " << height << " 255\n";
564 
565  // Map buffer if creating a CLTensor/GCTensor
566  map(tensor, true);
567 
568  switch(tensor.info()->format())
569  {
571  {
572  arm_compute::Window window;
575 
576  arm_compute::Iterator in(&tensor, window);
577 
579  {
580  const unsigned char value = *in.ptr();
581 
582  fs << value << value << value;
583  },
584  in);
585 
586  break;
587  }
589  {
590  arm_compute::Window window;
593 
594  arm_compute::Iterator in(&tensor, window);
595 
597  {
598  fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
599  },
600  in);
601 
602  break;
603  }
604  default:
605  ARM_COMPUTE_ERROR("Unsupported format");
606  }
607 
608  // Unmap buffer if creating a CLTensor/GCTensor
609  unmap(tensor);
610  }
611  catch(const std::ofstream::failure &e)
612  {
613  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
614  }
615 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
Definition: Validate.h:643
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:227
3 channels, 1 U8 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Coordinates of an item.
Definition: Coordinates.h:37
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:216
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39

◆ schedule_kernel_on_ctx()

void schedule_kernel_on_ctx ( IRuntimeContext ctx,
ICPPKernel kernel,
const IScheduler::Hints hints 
)

Schedules a kernel using the context if not nullptr else uses the legacy scheduling flow.

Parameters
[in]ctxContext to use.
[in]kernelKernel to schedule.
[in]hintsHints to use.

Definition at line 55 of file Utils.cpp.

References ARM_COMPUTE_ERROR_ON, Scheduler::get(), IScheduler::schedule(), and IRuntimeContext::scheduler().

Referenced by INESimpleFunctionNoBorder::run().

56 {
57  if(ctx)
58  {
59  ARM_COMPUTE_ERROR_ON(ctx->scheduler() == nullptr);
60  ctx->scheduler()->schedule(kernel, hints);
61  }
62  else
63  {
64  NEScheduler::get().schedule(kernel, hints);
65  }
66 }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466

◆ string_from_scheduler_type()

const std::string & string_from_scheduler_type ( Scheduler::Type  t)

Convert a Scheduler::Type into a string.

Parameters
[in]tScheduler::Type to be translated to string.
Returns
The string describing the scheduler type.

Definition at line 42 of file Utils.cpp.

References Scheduler::CPP, Scheduler::CUSTOM, Scheduler::OMP, Scheduler::ST, and tf_frozen_model_extractor::t.

43 {
44  static std::map<Scheduler::Type, const std::string> scheduler_type_map =
45  {
46  { Scheduler::Type::ST, "Single Thread" },
47  { Scheduler::Type::CPP, "C++11 Threads" },
48  { Scheduler::Type::OMP, "OpenMP Threads" },
49  { Scheduler::Type::CUSTOM, "Custom" }
50  };
51 
52  return scheduler_type_map[t];
53 }

◆ unmap()

void arm_compute::utils::unmap ( T &  tensor)
inline

Unmaps a tensor if needed.

Parameters
tensorTensor to be unmapped

Definition at line 227 of file Utils.h.

References ARM_COMPUTE_UNUSED, arm_compute::test::validation::distribution(), CLDistribution1D::map(), CLTensor::map(), GCTensor::map(), map(), CLDistribution1D::unmap(), CLTensor::unmap(), and GCTensor::unmap().

Referenced by compare_tensor(), NPYLoader::fill_tensor(), fill_tensor_vector(), and load_trained_data().

228 {
229  ARM_COMPUTE_UNUSED(tensor);
230 }
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152

Variable Documentation

◆ _allowed_values

_allowed_values
Initial value:
{ std::move(allowed_values) }
{
}
template <typename T>
inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value)
: SimpleOption<T>{ std::move(name), std::move(default_value) }
const char * name

Definition at line 73 of file EnumOption.h.

Referenced by EnumListOption< InstrumentsDescription >::EnumListOption().