ArmNN
 21.05
NetworkExecutionUtils.hpp File Reference
#include <armnn/IRuntime.hpp>
#include <armnn/Types.hpp>
#include <armnn/Logging.hpp>
#include <armnn/utility/StringUtils.hpp>
#include <mapbox/variant.hpp>
#include <iostream>
#include <fstream>

Go to the source code of this file.

Classes

struct  TensorPrinter
 

Typedefs

using TContainer = mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > >
 
using QuantizationParams = std::pair< float, int32_t >
 

Functions

std::vector< unsigned int > ParseArray (std::istream &stream)
 
std::vector< std::string > ParseStringList (const std::string &inputString, const char *delimiter)
 Splits a given string at every accurance of delimiter into a vector of string. More...
 
void PopulateTensorWithData (TContainer &tensorData, unsigned int numElements, const std::string &dataTypeStr, const armnn::Optional< QuantizationParams > &qParams, const armnn::Optional< std::string > &dataFile)
 
bool ValidatePath (const std::string &file, const bool expectFile)
 Verifies if the given string is a valid path. More...
 
bool ValidatePaths (const std::vector< std::string > &fileVec, const bool expectFile)
 Verifies if a given vector of strings are valid paths. More...
 
template<typename T , typename TParseElementFunc >
std::vector< T > ParseArrayImpl (std::istream &stream, TParseElementFunc parseElementFunc, const char *chars="\,:")
 
template<typename T , typename TParseElementFunc >
void PopulateTensorWithDataGeneric (std::vector< T > &tensorData, unsigned int numElements, const armnn::Optional< std::string > &dataFile, TParseElementFunc parseFunction)
 

Typedef Documentation

◆ QuantizationParams

using QuantizationParams = std::pair<float, int32_t>

Definition at line 55 of file NetworkExecutionUtils.hpp.

◆ TContainer

using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t> >

Definition at line 54 of file NetworkExecutionUtils.hpp.

Function Documentation

◆ ParseArray()

std::vector<unsigned int> ParseArray ( std::istream &  stream)

Definition at line 81 of file NetworkExecutionUtils.cpp.

References armnn::numeric_cast().

Referenced by ProgramOptions::ParseOptions().

82 {
83  return ParseArrayImpl<unsigned int>(
84  stream,
85  [](const std::string& s) { return armnn::numeric_cast<unsigned int>(std::stoi(s)); });
86 }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35

◆ ParseArrayImpl()

std::vector<T> ParseArrayImpl ( std::istream &  stream,
TParseElementFunc  parseElementFunc,
const char *  chars = "\t ,:" 
)

Definition at line 80 of file NetworkExecutionUtils.hpp.

References ARMNN_LOG, and armnn::stringUtils::StringTokenizer().

80  :")
81 {
82  std::vector<T> result;
83  // Processes line-by-line.
84  std::string line;
85  while (std::getline(stream, line))
86  {
87  std::vector<std::string> tokens = armnn::stringUtils::StringTokenizer(line, chars);
88  for (const std::string& token : tokens)
89  {
90  if (!token.empty()) // See https://stackoverflow.com/questions/10437406/
91  {
92  try
93  {
94  result.push_back(parseElementFunc(token));
95  }
96  catch (const std::exception&)
97  {
98  ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored.";
99  }
100  }
101  }
102  }
103 
104  return result;
105 }

◆ ParseStringList()

std::vector<std::string> ParseStringList ( const std::string &  inputString,
const char *  delimiter 
)

Splits a given string at every accurance of delimiter into a vector of string.

Definition at line 88 of file NetworkExecutionUtils.cpp.

References armnn::stringUtils::StringTrimCopy().

Referenced by GetBackendIDs(), and ProgramOptions::ParseOptions().

89 {
90  std::stringstream stream(inputString);
91  return ParseArrayImpl<std::string>(stream, [](const std::string& s) {
92  return armnn::stringUtils::StringTrimCopy(s); }, delimiter);
93 }
std::string StringTrimCopy(const std::string &str, const std::string &chars="\\\")
Trim from both the start and the end of a string, returns a trimmed copy of the string.
Definition: StringUtils.hpp:85

◆ PopulateTensorWithData()

void PopulateTensorWithData ( TContainer tensorData,
unsigned int  numElements,
const std::string &  dataTypeStr,
const armnn::Optional< QuantizationParams > &  qParams,
const armnn::Optional< std::string > &  dataFile 
)

Definition at line 190 of file NetworkExecutionUtils.cpp.

References ARMNN_LOG, OptionalBase::has_value(), OptionalReferenceSwitch< std::is_reference< T >::value, T >::value(), and OptionalReferenceSwitch< IsReference, T >::value().

Referenced by MainImpl().

195 {
196  const bool readFromFile = dataFile.has_value() && !dataFile.value().empty();
197  const bool quantizeData = qParams.has_value();
198 
199  std::ifstream inputTensorFile;
200  if (readFromFile)
201  {
202  inputTensorFile = std::ifstream(dataFile.value());
203  }
204 
205  if (dataTypeStr.compare("float") == 0)
206  {
207  if (quantizeData)
208  {
209  const float qScale = qParams.value().first;
210  const int qOffset = qParams.value().second;
211 
212  tensorData = readFromFile ?
213  ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile, qScale, qOffset) :
214  GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements);
215  }
216  else
217  {
218  tensorData = readFromFile ?
219  ParseDataArray<armnn::DataType::Float32>(inputTensorFile) :
220  GenerateDummyTensorData<armnn::DataType::Float32>(numElements);
221  }
222  }
223  else if (dataTypeStr.compare("int") == 0)
224  {
225  tensorData = readFromFile ?
226  ParseDataArray<armnn::DataType::Signed32>(inputTensorFile) :
227  GenerateDummyTensorData<armnn::DataType::Signed32>(numElements);
228  }
229  else if (dataTypeStr.compare("qsymms8") == 0)
230  {
231  tensorData = readFromFile ?
232  ParseDataArray<armnn::DataType::QSymmS8>(inputTensorFile) :
233  GenerateDummyTensorData<armnn::DataType::QSymmS8>(numElements);
234  }
235  else if (dataTypeStr.compare("qasymm8") == 0)
236  {
237  tensorData = readFromFile ?
238  ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile) :
239  GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements);
240  }
241  else
242  {
243  std::string errorMessage = "Unsupported tensor data type " + dataTypeStr;
244  ARMNN_LOG(fatal) << errorMessage;
245 
246  inputTensorFile.close();
247  throw armnn::Exception(errorMessage);
248  }
249 
250  inputTensorFile.close();
251 }
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
bool has_value() const noexcept
Definition: Optional.hpp:53
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46

◆ PopulateTensorWithDataGeneric()

void PopulateTensorWithDataGeneric ( std::vector< T > &  tensorData,
unsigned int  numElements,
const armnn::Optional< std::string > &  dataFile,
TParseElementFunc  parseFunction 
)

Definition at line 108 of file NetworkExecutionUtils.hpp.

References OptionalBase::has_value(), and OptionalReferenceSwitch< IsReference, T >::value().

112 {
113  const bool readFromFile = dataFile.has_value() && !dataFile.value().empty();
114 
115  std::ifstream inputTensorFile;
116  if (readFromFile)
117  {
118  inputTensorFile = std::ifstream(dataFile.value());
119  }
120 
121  tensorData = readFromFile ?
122  ParseArrayImpl<T>(inputTensorFile, parseFunction) :
123  std::vector<T>(numElements, static_cast<T>(0));
124 }
bool has_value() const noexcept
Definition: Optional.hpp:53

◆ ValidatePath()

bool ValidatePath ( const std::string &  file,
const bool  expectFile 
)

Verifies if the given string is a valid path.

Reports invalid paths to std::err.

Parameters
filestring - A string containing the path to check
expectFilebool - If true, checks for a regular file.
Returns
bool - True if given string is a valid path., false otherwise.

Definition at line 253 of file NetworkExecutionUtils.cpp.

Referenced by CheckClTuningParameter(), and ValidatePaths().

254 {
255  if (!fs::exists(file))
256  {
257  std::cerr << "Given file path '" << file << "' does not exist" << std::endl;
258  return false;
259  }
260  if (!fs::is_regular_file(file) && expectFile)
261  {
262  std::cerr << "Given file path '" << file << "' is not a regular file" << std::endl;
263  return false;
264  }
265  return true;
266 }

◆ ValidatePaths()

bool ValidatePaths ( const std::vector< std::string > &  fileVec,
const bool  expectFile 
)

Verifies if a given vector of strings are valid paths.

Reports invalid paths to std::err.

Parameters
fileVecvector of string - A vector of string containing the paths to check
expectFilebool - If true, checks for a regular file.
Returns
bool - True if all given strings are valid paths., false otherwise.

Definition at line 268 of file NetworkExecutionUtils.cpp.

References ValidatePath().

Referenced by ExecuteNetworkParams::ValidateParams().

269 {
270  bool allPathsValid = true;
271  for (auto const& file : fileVec)
272  {
273  if(!ValidatePath(file, expectFile))
274  {
275  allPathsValid = false;
276  }
277  }
278  return allPathsValid;
279 }
bool ValidatePath(const std::string &file, const bool expectFile)
Verifies if the given string is a valid path.