Compute Library
 23.08
arm_compute::utils Namespace Reference

Namespaces

 cast
 
 detail
 
 info_helpers
 
 iterable
 
 math
 
 memory
 
 mmap_io
 
 random
 
 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

template<typename E , typename SE >
constexpr E as_cenum (const SE v) noexcept
 Convert a strongly typed enum to an old plain c enum. More...
 
template<typename SE , typename E >
constexpr SE as_enum (const E val) noexcept
 Convert plain old enumeration to a strongly typed enum. More...
 
template<typename E >
bool is_in (E check, std::initializer_list< E > list)
 Check if the given value is in the given enum value list. More...
 
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...
 
npy::header_t 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...
 

Enumeration Type Documentation

◆ ConvolutionPaddingMode

Enumerator
Valid 
Same 
Manual 

Definition at line 38 of file graph_validate_utils.h.

39 {
40  Valid,
41  Same,
42  Manual
43 };

◆ ImageType

enum ImageType
strong

Supported image types.

Enumerator
UNKNOWN 
PPM 
JPEG 

Definition at line 63 of file Utils.h.

64 {
65  UNKNOWN,
66  PPM,
67  JPEG
68 };

Function Documentation

◆ as_cenum()

constexpr E arm_compute::utils::as_cenum ( const SE  v)
constexprnoexcept

Convert a strongly typed enum to an old plain c enum.

Template Parameters
EPlain old C enum
SEStrongly typed resulting enum
Parameters
[in]vValue to convert
Returns
A corresponding plain old C enumeration

Definition at line 45 of file Utils.h.

46 {
47  return static_cast<E>(static_cast<std::underlying_type_t<SE>>(v));
48 }

◆ as_enum()

constexpr SE arm_compute::utils::as_enum ( const E  val)
constexprnoexcept

Convert plain old enumeration to a strongly typed enum.

Template Parameters
SEStrongly typed resulting enum
EPlain old C enum
Parameters
[in]valValue to convert
Returns
A corresponding strongly typed enumeration

Definition at line 60 of file Utils.h.

61 {
62  return static_cast<SE>(val);
63 }

◆ 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.

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 }

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.

◆ 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.

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 811 of file Utils.h.

812 {
814  ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
815 
816  int num_mismatches = 0;
817  Window window;
818  window.use_tensor_dimensions(tensor1.info()->tensor_shape());
819 
820  map(tensor1, true);
821  map(tensor2, true);
822 
823  Iterator itensor1(&tensor1, window);
824  Iterator itensor2(&tensor2, window);
825 
826  execute_window_loop(window, [&](const Coordinates &)
827  {
828  if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
829  {
830  ++num_mismatches;
831  }
832  },
833  itensor1, itensor2);
834 
835  unmap(itensor1);
836  unmap(itensor2);
837 
838  return num_mismatches;
839 }

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().

◆ 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 194 of file CommonGraphOptions.cpp.

195 {
196  FastMathHint fast_math_hint_value = options.fast_math_hint->value() ? FastMathHint::Enabled : FastMathHint::Disabled;
197  auto validation_range = parse_validation_range(options.validation_range->value());
198 
199  CommonGraphParams common_params;
200  common_params.help = options.help->is_set() ? options.help->value() : false;
201  common_params.threads = options.threads->value();
202  common_params.batches = options.batches->value();
203  common_params.target = options.target->value();
204  common_params.data_type = options.data_type->value();
205  if(options.data_layout->is_set())
206  {
207  common_params.data_layout = options.data_layout->value();
208  }
209  common_params.enable_tuner = options.enable_tuner->is_set() ? options.enable_tuner->value() : false;
210  common_params.enable_cl_cache = common_params.target == arm_compute::graph::Target::NEON ? false : (options.enable_cl_cache->is_set() ? options.enable_cl_cache->value() : true);
211  common_params.tuner_mode = options.tuner_mode->value();
212  common_params.fast_math_hint = options.fast_math_hint->is_set() ? fast_math_hint_value : FastMathHint::Disabled;
213  common_params.data_path = options.data_path->value();
214  common_params.image = options.image->value();
215  common_params.labels = options.labels->value();
216  common_params.validation_file = options.validation_file->value();
217  common_params.validation_path = options.validation_path->value();
218  common_params.validation_range_start = validation_range.first;
219  common_params.validation_range_end = validation_range.second;
220  common_params.tuner_file = options.tuner_file->value();
221  common_params.mlgo_file = options.mlgo_file->value();
222 
223  return common_params;
224 }

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

◆ 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.

Definition at line 313 of file graph_validate_utils.h.

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

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.

◆ 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.

131 {
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 }

References ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN, arm_compute::test::validation::b, Window::DimY, DetectionWindow::height, arm_compute::RGB888, tensor, DetectionWindow::width, DetectionWindow::x, and DetectionWindow::y.

◆ 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 758 of file Utils.h.

759 {
760  constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
761  constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
762 
763  using fp_dist_type = typename std::conditional<is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
764  using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist_type>::type;
765 
766  std::mt19937 gen(seed);
767  dist_type dist(lower_bound, upper_bound);
768 
769  map(tensor, true);
770 
771  Window window;
772  window.use_tensor_dimensions(tensor.info()->tensor_shape());
773 
774  Iterator it(&tensor, window);
775  execute_window_loop(window, [&](const Coordinates &)
776  {
777  *reinterpret_cast<T *>(it.ptr()) = dist(gen);
778  },
779  it);
780 
781  unmap(tensor);
782 }

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

Referenced by fill_random_tensor(), and main().

◆ 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 785 of file Utils.h.

786 {
787  std::random_device rd;
788  fill_random_tensor(tensor, rd(), lower_bound, upper_bound);
789 }

References fill_random_tensor(), and tensor.

◆ fill_tensor_value()

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

Definition at line 713 of file Utils.h.

714 {
715  map(tensor, true);
716 
717  Window window;
718  window.use_tensor_dimensions(tensor.info()->tensor_shape());
719 
720  Iterator it_tensor(&tensor, window);
721  execute_window_loop(window, [&](const Coordinates &)
722  {
723  *reinterpret_cast<T *>(it_tensor.ptr()) = value;
724  },
725  it_tensor);
726 
727  unmap(tensor);
728 }

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

Referenced by fill_tensor_zero().

◆ fill_tensor_vector()

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

Definition at line 737 of file Utils.h.

738 {
739  ARM_COMPUTE_ERROR_ON(tensor.info()->tensor_shape().total_size() != vec.size());
740 
741  map(tensor, true);
742 
743  Window window;
744  window.use_tensor_dimensions(tensor.info()->tensor_shape());
745 
746  int i = 0;
747  Iterator it_tensor(&tensor, window);
748  execute_window_loop(window, [&](const Coordinates &)
749  {
750  *reinterpret_cast<T *>(it_tensor.ptr()) = vec.at(i++);
751  },
752  it_tensor);
753 
754  unmap(tensor);
755 }

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

◆ fill_tensor_zero()

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

Definition at line 731 of file Utils.h.

732 {
733  fill_tensor_value(tensor, T(0));
734 }

References fill_tensor_value(), and tensor.

◆ 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 333 of file graph_validate_utils.h.

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

References tensor.

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

◆ 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.

168 {
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 }

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

Referenced by ImageLoaderFactory::create().

◆ 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 253 of file Utils.cpp.

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

◆ 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 154 of file Utils.h.

155 {
156  // Check endianness
157  const unsigned int i = 1;
158  const char *c = reinterpret_cast<const char *>(&i);
159  std::string endianness;
160  if(*c == 1)
161  {
162  endianness = std::string("<");
163  }
164  else
165  {
166  endianness = std::string(">");
167  }
168  const std::string no_endianness("|");
169 
170  switch(data_type)
171  {
172  case DataType::U8:
173  case DataType::QASYMM8:
174  return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
175  case DataType::S8:
176  case DataType::QSYMM8:
177  case DataType::QSYMM8_PER_CHANNEL:
178  return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
179  case DataType::U16:
180  case DataType::QASYMM16:
181  return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
182  case DataType::S16:
183  case DataType::QSYMM16:
184  return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
185  case DataType::U32:
186  return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
187  case DataType::S32:
188  return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
189  case DataType::U64:
190  return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
191  case DataType::S64:
192  return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
193  case DataType::F16:
194  return endianness + "f" + support::cpp11::to_string(sizeof(half));
195  case DataType::F32:
196  return endianness + "f" + support::cpp11::to_string(sizeof(float));
197  case DataType::F64:
198  return endianness + "f" + support::cpp11::to_string(sizeof(double));
199  case DataType::SIZET:
200  return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
201  default:
202  ARM_COMPUTE_ERROR("Data type not supported");
203  }
204 }

References ARM_COMPUTE_ERROR, arm_compute::test::validation::data_type, 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().

◆ 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 591 of file graph_validate_utils.h.

592 {
593  switch(params.data_type)
594  {
595  case DataType::QASYMM8:
596  {
597  return std::make_unique<VerifyAccessorT<uint8_t>>(
598  params);
599  }
600  case DataType::F16:
601  {
602  return std::make_unique<VerifyAccessorT<half>>(
603  params);
604  }
605  case DataType::F32:
606  {
607  return std::make_unique<VerifyAccessorT<float>>(
608  params);
609  }
610  default:
611  ARM_COMPUTE_ERROR("NOT SUPPORTED!");
612  }
613 }

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

◆ init_sgemm_output()

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

Definition at line 792 of file Utils.h.

793 {
794  dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1), src0.info()->dimension(2)), 1, dt));
795 }

References arm_compute::test::validation::dst, and dt.

◆ is_in()

bool arm_compute::utils::is_in ( check,
std::initializer_list< E >  list 
)

Check if the given value is in the given enum value list.

Template Parameters
EThe type of the enum
Parameters
[in]checkValue to check
[in]listList of enum values to check against
Returns
True if the given value is found in the list

Definition at line 75 of file Utils.h.

76 {
77  return std::any_of(list.begin(), list.end(), [&check](E e)
78  {
79  return check == e;
80  });
81 }

◆ 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 666 of file Utils.h.

667 {
669 
670  std::ifstream fs;
671 
672  try
673  {
674  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
675  // Open file
676  fs.open(filename, std::ios::in | std::ios::binary);
677 
678  if(!fs.good())
679  {
680  throw std::runtime_error("Could not load binary data: " + filename);
681  }
682 
683  // Map buffer if creating a CLTensor
684  map(tensor, true);
685 
686  Window window;
687 
689 
690  for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
691  {
692  window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
693  }
694 
695  arm_compute::Iterator in(&tensor, window);
696 
697  execute_window_loop(window, [&](const Coordinates &)
698  {
699  fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
700  },
701  in);
702 
703  // Unmap buffer if creating a CLTensor
704  unmap(tensor);
705  }
706  catch(const std::ofstream::failure &e)
707  {
708  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", filename.c_str(), e.what());
709  }
710 }

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(), tensor, and unmap().

◆ map()

◆ 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.

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 }

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.

◆ 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.

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 }

References Manual, Same, and Valid.

◆ 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.

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 }

References Manual, Same, arm_compute::utility::tolower(), and Valid.

◆ parse_npy_header()

npy::header_t 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.

234 {
235  // Read header
236  std::string header_s = npy::read_header(fs);
237 
238  // Parse header
239  npy::header_t header = npy::parse_header(header_s);
240 
241  bool fortran_order = false;
242  std::vector<unsigned long> shape = header.shape;
243 
244  std::reverse(shape.begin(), shape.end());
245 
246  return npy::header_t{ header.dtype, fortran_order, shape };
247 }

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

Referenced by NPYLoader::open().

◆ 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.

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 }

References ARM_COMPUTE_ERROR_ON_MSG, and ARM_COMPUTE_UNUSED.

Referenced by PPMLoader::open().

◆ run_example() [1/3]

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

Definition at line 107 of file Utils.h.

108 {
109  return run_example(argc, argv, std::make_unique<T>());
110 }

References run_example().

◆ run_example() [2/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 95 of file RunExample.cpp.

96 {
99  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
100  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
101  framework::Framework &framework = framework::Framework::get();
102 
103  parser.parse(argc, argv);
104 
105  if(options.help->is_set() && options.help->value())
106  {
107  parser.print_help(argv[0]);
108  return 0;
109  }
110 
111  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
112  g_example = std::move(example);
113  g_example_argv.clear();
114  g_example_argv.emplace_back(argv[0]);
115  for(auto &arg : example_args->value())
116  {
117  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
118  }
119 
120  if(options.log_level->value() > framework::LogLevel::NONE)
121  {
122  for(auto &p : printers)
123  {
124  p->print_global_header();
125  }
126  }
127 
128 #ifdef ARM_COMPUTE_CL
129  CLGEMMHeuristicsHandle gemm_h;
130  if(opencl_is_available())
131  {
132  CLBackendType backend_type = CLBackendType::Native;
133  for(auto &arg : example_args->value())
134  {
135  if(arg.find("--target=clvk") != std::string::npos)
136  {
137  backend_type = CLBackendType::Clvk;
138  break;
139  }
140  }
141 
142  auto ctx_dev_err = create_opencl_context_and_device(backend_type);
143  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
144  CLScheduler::get()
145  .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
146  }
147 #endif /* ARM_COMPUTE_CL */
148 
149  if(options.log_level->value() >= framework::LogLevel::CONFIG)
150  {
151  for(auto &p : printers)
152  {
153  p->print_entry("Version", build_information());
154  p->print_entry("CommandLine", command_line(argc, argv));
155 #ifdef ARM_COMPUTE_CL
156  if(opencl_is_available())
157  {
158  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
159  }
160  else
161  {
162  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
163  }
164 #endif /* ARM_COMPUTE_CL */
165  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
166  }
167  }
168 
169  // Initialize framework
171  fconfig.instruments = options.instruments->value();
172  fconfig.num_iterations = options.iterations->value();
173  fconfig.log_level = options.log_level->value();
174  framework.init(fconfig);
175 
176  for(auto &p : printers)
177  {
178  framework.add_printer(p.get());
179  }
180  framework.set_throw_errors(options.throw_errors->value());
182 
183 #ifdef BARE_METAL
184  framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
185 #else /* BARE_METAL */
186  framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
187 #endif /* BARE_METAL */
188  //func(argc, argv);
189  bool success = framework.run();
190  if(options.log_level->value() > framework::LogLevel::NONE)
191  {
192  for(auto &p : printers)
193  {
194  p->print_global_footer();
195  }
196  }
197 
198  return (success ? 0 : 1);
199 }

References CommonOptions::create_printers(), Framework::get(), CommonOptions::help, Option::is_set(), GemmTuner::parser, Option::set_help(), and SimpleOption< T >::value().

Referenced by run_example().

◆ 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 109 of file RunExample.cpp.

110 {
113  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
114  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
115  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
116  seed->set_help("Global seed for random number generation");
117  auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1);
118  validate->set_help("Enable / disable output validation (0/1)");
119 
120  framework::Framework &framework = framework::Framework::get();
121 
122  parser.parse(argc, argv);
123 
124  if(options.help->is_set() && options.help->value())
125  {
126  parser.print_help(argv[0]);
127  return 0;
128  }
129 
130  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
131  g_example = std::move(example);
132  g_example_argv.clear();
133  g_example_argv.emplace_back(argv[0]);
134  for(auto &arg : example_args->value())
135  {
136  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
137  }
138 
139  library = std::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value());
140  fixed_library = std::make_unique<AssetsLibrary>(".", fixed_seed);
141 
142  if(options.log_level->value() > framework::LogLevel::NONE)
143  {
144  for(auto &p : printers)
145  {
146  p->print_global_header();
147  }
148  }
149 
150 #ifdef ARM_COMPUTE_CL
151  if(opencl_is_available())
152  {
153  CLBackendType backend_type = CLBackendType::Native;
154  for(auto &arg : example_args->value())
155  {
156  if(arg.find("--target=clvk") != std::string::npos)
157  {
158  backend_type = CLBackendType::Clvk;
159  break;
160  }
161  }
162  auto ctx_dev_err = create_opencl_context_and_device(backend_type);
163  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
164  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr);
165  }
166 #endif /* ARM_COMPUTE_CL */
167 
168  if(options.log_level->value() >= framework::LogLevel::CONFIG)
169  {
170  for(auto &p : printers)
171  {
172  p->print_entry("Version", build_information());
173  p->print_entry("CommandLine", command_line(argc, argv));
174  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
175 #ifdef ARM_COMPUTE_CL
176  if(opencl_is_available())
177  {
178  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
179  }
180  else
181  {
182  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
183  }
184 #endif /* ARM_COMPUTE_CL */
185  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
186  g_example->print_parameters(*p);
187  }
188  }
189 
190  // Initialize framework
192  fconfig.instruments = options.instruments->value();
193  fconfig.num_iterations = options.iterations->value();
194  fconfig.log_level = options.log_level->value();
195  framework.init(fconfig);
196 
197  for(auto &p : printers)
198  {
199  framework.add_printer(p.get());
200  }
201 
202  framework.set_throw_errors(options.throw_errors->value());
204  if(validate->value() != 0)
205  {
206  framework.add_test_case<ExampleTest<true>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
207  }
208  else
209  {
210  framework.add_test_case<ExampleTest<false>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
211  }
212 
213  //func(argc, argv);
214  bool success = framework.run();
215  if(options.log_level->value() > framework::LogLevel::NONE)
216  {
217  for(auto &p : printers)
218  {
219  p->print_global_footer();
220  }
221  }
222 
223  return (success ? 0 : 1);
224 }

References CommonOptions::create_printers(), Framework::get(), CommonOptions::help, Option::is_set(), GemmTuner::parser, Option::set_help(), arm_compute::validate(), and SimpleOption< T >::value().

◆ 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 611 of file Utils.h.

612 {
614 
615  std::ofstream fs;
616  try
617  {
618  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
619  fs.open(npy_filename, std::ios::out | std::ios::binary);
620 
621  std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
622 
623  for(unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j)
624  {
625  shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
626  }
627 
628  // Map buffer if creating a CLTensor
629  map(tensor, true);
630 
631  using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type;
632 
633  std::vector<typestring_type> tmp; /* Used only to get the typestring */
634  const npy::dtype_t dtype = npy::dtype_map.at(std::type_index(typeid(tmp)));
635 
636  std::ofstream stream(npy_filename, std::ofstream::binary);
637  npy::header_t header{ dtype, fortran_order, shape };
638  npy::write_header(stream, header);
639 
640  arm_compute::Window window;
641  window.use_tensor_dimensions(tensor.info()->tensor_shape());
642 
643  arm_compute::Iterator in(&tensor, window);
644 
646  {
647  stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type));
648  },
649  in);
650 
651  // Unmap buffer if creating a CLTensor
652  unmap(tensor);
653  }
654  catch(const std::ofstream::failure &e)
655  {
656  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what());
657  }
658 }

References ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN, ARM_COMPUTE_ERROR_VAR, arm_compute::execute_window_loop(), arm_compute::F32, arm_compute::mlgo::parser::header(), map(), Iterator::ptr(), arm_compute::QASYMM8, arm_compute::test::validation::shape, tensor, type, unmap(), and Window::use_tensor_dimensions().

Referenced by SaveNumPyAccessor::access_tensor().

◆ 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 531 of file Utils.h.

532 {
534  ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
535 
536  std::ofstream fs;
537 
538  try
539  {
540  fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
541  fs.open(ppm_filename, std::ios::out | std::ios::binary);
542 
543  const unsigned int width = tensor.info()->tensor_shape()[0];
544  const unsigned int height = tensor.info()->tensor_shape()[1];
545 
546  fs << "P6\n"
547  << width << " " << height << " 255\n";
548 
549  // Map buffer if creating a CLTensor
550  map(tensor, true);
551 
552  switch(tensor.info()->format())
553  {
555  {
556  arm_compute::Window window;
559 
560  arm_compute::Iterator in(&tensor, window);
561 
563  {
564  const unsigned char value = *in.ptr();
565 
566  fs << value << value << value;
567  },
568  in);
569 
570  break;
571  }
573  {
574  arm_compute::Window window;
577 
578  arm_compute::Iterator in(&tensor, window);
579 
581  {
582  fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
583  },
584  in);
585 
586  break;
587  }
588  default:
589  ARM_COMPUTE_ERROR("Unsupported format");
590  }
591 
592  // Unmap buffer if creating a CLTensor
593  unmap(tensor);
594  }
595  catch(const std::ofstream::failure &e)
596  {
597  ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
598  }
599 }

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN, ARM_COMPUTE_ERROR_VAR, Window::DimX, Window::DimY, arm_compute::execute_window_loop(), map(), Iterator::ptr(), arm_compute::RGB888, Window::set(), tensor, arm_compute::U8, and unmap().

Referenced by PPMWriter::access_tensor().

◆ 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.

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 }

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

Referenced by INESimpleFunctionNoBorder::run().

◆ 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.

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 }

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

◆ unmap()

void arm_compute::utils::unmap ( T &  tensor)
inline
arm_compute::opencl_is_available
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:203
arm_compute::Format::RGB888
@ RGB888
3 channels, 1 U8 per channel
arm_compute::test::framework::FrameworkConfig
Framework configuration structure.
Definition: Framework.h:57
arm_compute::test::framework::CommonOptions
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
type
decltype(strategy::transforms) typedef type
Definition: gemm_interleaved.hpp:261
arm_compute::utility::tolower
std::string tolower(std::string string)
Convert string to lower case.
Definition: Utility.h:206
DetectionWindow::width
ushort width
Width of the detection window.
Definition: types.h:51
arm_compute::mlgo::parser::header
void header(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:481
arm_compute::test::framework::FrameworkConfig::num_iterations
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:875
arm_compute::test::framework::Framework::add_printer
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:678
arm_compute::create_opencl_context_and_device
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device(CLBackendType cl_backend_type)
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:126
arm_compute::test::framework::Framework::add_test_case
void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Add a test case to the framework.
Definition: Framework.h:376
arm_compute::test::framework::TestCaseFactory::Status::ACTIVE
@ ACTIVE
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::calculate_same_pad
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:226
arm_compute::test::validation::dst
auto dst
Definition: DFT.cpp:170
ARM_COMPUTE_ERROR_VAR
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:347
arm_compute::graph::Target::NEON
@ NEON
Arm® Neon™ capable target device.
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
DetectionWindow::height
ushort height
Height of the detection window.
Definition: types.h:52
DetectionWindow::x
ushort x
Top-left x coordinate.
Definition: types.h:49
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:353
arm_compute::Window::use_tensor_dimensions
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:276
GemmTuner.parser
parser
Definition: GemmTuner.py:640
arm_compute::test::framework::FrameworkConfig::log_level
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
arm_compute::utils::ListOption
Implementation of an option that accepts any number of values.
Definition: ListOption.h:41
arm_compute::utils::ImageType
ImageType
Supported image types.
Definition: Utils.h:63
arm_compute::utils::ImageType::PPM
@ PPM
arm_compute::utils::run_example
int run_example(int argc, char **argv)
Definition: Utils.h:107
arm_compute::half
half_float::half half
16-bit floating point type
Definition: CoreTypes.h:35
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
arm_compute::utils::CommandLineParser
Class to parse command line arguments.
Definition: CommandLineParser.h:45
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
arm_compute::test::framework::Framework
Main framework class.
Definition: Framework.h:97
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:457
arm_compute::utils::ConvolutionPaddingMode::Same
@ Same
arm_compute::test::framework::Framework::set_throw_errors
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:267
arm_compute::test::framework::FrameworkConfig::instruments
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
arm_compute::test::framework::Framework::run
bool run()
Run all enabled test cases.
Definition: Framework.cpp:546
arm_compute::CLGEMMHeuristicsHandle
Handle for loading and retrieving GEMM heuristics.
Definition: CLGEMMHeuristicsHandle.h:38
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
dt
DataType dt
Definition: NEBatchNormalizationLayerKernel.cpp:51
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:66
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::utils::map
void map(T &tensor, bool blocking)
Maps a tensor if needed.
Definition: Utils.h:212
arm_compute::test::fixed_library
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:80
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
arm_compute::Format::U8
@ U8
1 channel, 1 U8 per channel
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
arm_compute::CLBackendType
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:55
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:223
arm_compute::utils::Option::set_help
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
arm_compute::test::library
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:77
arm_compute::test::framework::detail::TestSuiteRegistrar
Helper class to statically begin and end a test suite.
Definition: Registrars.h:66
arm_compute::graph::FastMathHint
FastMathHint
Enable or disable fast math for Convolution layer.
Definition: Types.h:147
DetectionWindow::y
ushort y
Top-left y coordinate.
Definition: types.h:50
arm_compute::utils::ConvolutionPaddingMode::Valid
@ Valid
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::utils::unmap
void unmap(T &tensor)
Unmaps a tensor if needed.
Definition: Utils.h:223
ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
Definition: Validate.h:728
arm_compute::build_information
std::string build_information()
Returns the arm_compute library build information.
arm_compute::test::validation::b
SimpleTensor< float > b
Definition: DFT.cpp:157
arm_compute::to_string
std::string to_string(const ClComponentElementwiseBinary::Attributes::ElementwiseOp &op)
Formatted output of the arm_compute::experimental::dynamic_fusion::ClComponentElementwiseBinary::Attr...
Definition: ElementwiseBinary.h:70
arm_compute::test::validation::reference::reverse
SimpleTensor< T > reverse(const SimpleTensor< T > &src, const SimpleTensor< uint32_t > &axis)
Definition: Reverse.cpp:38
arm_compute::UNKNOWN
@ UNKNOWN
Unknown CL kernel type.
Definition: CLTypes.h:82
arm_compute::utils::fill_tensor_value
void fill_tensor_value(TensorType &tensor, T value)
Definition: Utils.h:713
arm_compute::qasymm8_t
uint8_t qasymm8_t
8 bit quantized asymmetric scalar value
Definition: QuantizationInfo.h:36
arm_compute::validate
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)
Definition: CPPBoxWithNonMaximaSuppressionLimit.cpp:214
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::test::framework::Framework::init
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
arm_compute::utils::fill_random_tensor
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:785
ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:777
arm_compute::execute_window_loop
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
arm_compute::utils::SimpleOption
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39
tf_frozen_model_extractor.t
t
Definition: tf_frozen_model_extractor.py:49
arm_compute::utils::ImageType::JPEG
@ JPEG
ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:628
arm_compute::utils::ConvolutionPaddingMode::Manual
@ Manual
ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:526