ArmNN
 21.08
NetworkExecutionUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/IRuntime.hpp>
9 #include <armnn/Types.hpp>
10 #include <armnn/Logging.hpp>
12 
13 #include <mapbox/variant.hpp>
14 
15 #include <iostream>
16 #include <fstream>
17 
18 
19 std::vector<unsigned int> ParseArray(std::istream& stream);
20 
21 /// Splits a given string at every accurance of delimiter into a vector of string
22 std::vector<std::string> ParseStringList(const std::string& inputString, const char* delimiter);
23 
25 {
26  TensorPrinter(const std::string& binding,
27  const armnn::TensorInfo& info,
28  const std::string& outputTensorFile,
29  bool dequantizeOutput);
30 
31  void operator()(const std::vector<float>& values);
32 
33  void operator()(const std::vector<uint8_t>& values);
34 
35  void operator()(const std::vector<int>& values);
36 
37  void operator()(const std::vector<int8_t>& values);
38 
39 private:
40  template<typename Container, typename Delegate>
41  void ForEachValue(const Container& c, Delegate delegate);
42 
43  template<typename T>
44  void WriteToFile(const std::vector<T>& values);
45 
46  std::string m_OutputBinding;
47  float m_Scale;
48  int m_Offset;
49  std::string m_OutputTensorFile;
50  bool m_DequantizeOutput;
51 };
52 
53 using TContainer =
54  mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t>>;
55 using QuantizationParams = std::pair<float, int32_t>;
56 
57 void PopulateTensorWithData(TContainer& tensorData,
58  unsigned int numElements,
59  const std::string& dataTypeStr,
61  const armnn::Optional<std::string>& dataFile);
62 
63 /**
64  * Verifies if the given string is a valid path. Reports invalid paths to std::err.
65  * @param file string - A string containing the path to check
66  * @param expectFile bool - If true, checks for a regular file.
67  * @return bool - True if given string is a valid path., false otherwise.
68  * */
69 bool ValidatePath(const std::string& file, const bool expectFile);
70 
71 /**
72  * Verifies if a given vector of strings are valid paths. Reports invalid paths to std::err.
73  * @param fileVec vector of string - A vector of string containing the paths to check
74  * @param expectFile bool - If true, checks for a regular file.
75  * @return bool - True if all given strings are valid paths., false otherwise.
76  * */
77 bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile);
78 
79 template<typename T, typename TParseElementFunc>
80 std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char* chars = "\t ,:")
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 }
106 
107 template <typename T, typename TParseElementFunc>
108 void PopulateTensorWithDataGeneric(std::vector<T>& tensorData,
109  unsigned int numElements,
110  const armnn::Optional<std::string>& dataFile,
111  TParseElementFunc parseFunction)
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 }
std::vector< std::string > StringTokenizer(const std::string &str, const char *delimiters, bool tokenCompression=true)
Function to take a string and a list of delimiters and split the string into tokens based on those de...
Definition: StringUtils.hpp:20
bool ValidatePath(const std::string &file, const bool expectFile)
Verifies if the given string is a valid path.
void operator()(const std::vector< float > &values)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
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.
bool has_value() const noexcept
Definition: Optional.hpp:53
std::vector< T > ParseArrayImpl(std::istream &stream, TParseElementFunc parseElementFunc, const char *chars="\,:")
bool ValidatePaths(const std::vector< std::string > &fileVec, const bool expectFile)
Verifies if a given vector of strings are valid paths.
TensorPrinter(const std::string &binding, const armnn::TensorInfo &info, const std::string &outputTensorFile, bool dequantizeOutput)
void PopulateTensorWithData(TContainer &tensorData, unsigned int numElements, const std::string &dataTypeStr, const armnn::Optional< QuantizationParams > &qParams, const armnn::Optional< std::string > &dataFile)
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
std::pair< float, int32_t > QuantizationParams
void PopulateTensorWithDataGeneric(std::vector< T > &tensorData, unsigned int numElements, const armnn::Optional< std::string > &dataFile, TParseElementFunc parseFunction)