ArmNN
 20.08
CommandLineProcessor.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include <Filesystem.hpp>
8 
9 #include <boost/program_options.hpp>
10 
11 namespace armnnQuantizer
12 {
13 
14 bool ValidateOutputDirectory(std::string& dir)
15 {
16  if (dir.empty())
17  {
18  std::cerr << "No output directory specified" << std::endl;
19  return false;
20  }
21 
22  if (dir[dir.length() - 1] != '/')
23  {
24  dir += "/";
25  }
26 
27  if (!fs::exists(dir))
28  {
29  std::cerr << "Output directory [" << dir << "] does not exist" << std::endl;
30  return false;
31  }
32 
33  if (!fs::is_directory(dir))
34  {
35  std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl;
36  return false;
37  }
38 
39  return true;
40 }
41 
42 bool ValidateProvidedFile(const std::string& inputFileName)
43 {
44  if (!fs::exists(inputFileName))
45  {
46  std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
47  return false;
48  }
49 
50  if (fs::is_directory(inputFileName))
51  {
52  std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
53  return false;
54  }
55 
56  return true;
57 }
58 
59 bool ValidateQuantizationScheme(const std::string& scheme)
60 {
61  if (scheme.empty())
62  {
63  std::cerr << "No Quantization Scheme specified" << std::endl;
64  return false;
65  }
66 
67  std::vector<std::string> supportedSchemes =
68  {
69  "QAsymmS8",
70  "QAsymmU8",
71  "QSymm16"
72  };
73 
74  auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
75  if (iterator == supportedSchemes.end())
76  {
77  std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
78  return false;
79  }
80 
81  return true;
82 }
83 
84 bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
85 {
86  namespace po = boost::program_options;
87 
88  po::options_description desc("Options");
89  try
90  {
91  desc.add_options()
92  ("help,h", "Display help messages")
93  ("infile,f", po::value<std::string>(&m_InputFileName)->required(),
94  "Input file containing float 32 ArmNN Input Graph")
95  ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymmU8"),
96  "Quantization scheme,"
97  " \"QAsymmU8\" or \"QAsymmS8\" or \"QSymm16\","
98  " default value QAsymmU8")
99  ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
100  "CSV file containing paths for RAW input tensors")
101  ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
102  "Preserve the input and output data types")
103  ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
104  "Directory that output file will be written to")
105  ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
106  }
107  catch (const std::exception& e)
108  {
109  std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
110  return false;
111  }
112 
113  po::variables_map vm;
114 
115  try
116  {
117  po::store(po::parse_command_line(argc, argv, desc), vm);
118 
119  if (vm.count("help") || argc <= 1)
120  {
121  std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
122  std::cout << std::endl;
123  std::cout << desc << std::endl;
124  return false;
125  }
126 
127  po::notify(vm);
128  }
129  catch (const po::error& e)
130  {
131  std::cerr << e.what() << std::endl << std::endl;
132  std::cerr << desc << std::endl;
133  return false;
134  }
135 
137  {
138  return false;
139  }
140 
142  {
143  return false;
144  }
145 
146  if (m_CsvFileName != "")
147  {
149  {
150  return false;
151  }
152  else
153  {
154  fs::path csvFilePath(m_CsvFileName);
155  m_CsvFileDirectory = csvFilePath.parent_path().c_str();
156  }
157 
158  // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
160  }
161 
163  {
164  return false;
165  }
166 
167  std::string output(m_OutputDirectory);
168  output.append(m_OutputFileName);
169 
170  if (fs::exists(output))
171  {
172  std::cerr << "Output file [" << output << "] already exists" << std::endl;
173  return false;
174  }
175 
176  return true;
177 }
178 
179 } // namespace armnnQuantizer
bool ValidateQuantizationScheme(const std::string &scheme)
QuantizationDataSet is a structure which is created after parsing a quantization CSV file...
bool ProcessCommandLine(int argc, char *argv[])
bool ValidateOutputDirectory(std::string &dir)
bool ValidateProvidedFile(const std::string &inputFileName)