blob: d2163c08694f6d693bdcc921cd81fa96740bc0c3 [file] [log] [blame]
Jim Flynn3091b062019-02-15 14:45:04 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "CommandLineProcessor.hpp"
7
8#define BOOST_FILESYSTEM_NO_DEPRECATED
9
10#include <boost/program_options.hpp>
11#include <boost/filesystem/operations.hpp>
12#include <boost/filesystem/path.hpp>
13
14namespace armnnQuantizer
15{
16
17bool ValidateOutputDirectory(std::string& dir)
18{
19 if (dir.empty())
20 {
21 std::cerr << "No output directory specified" << std::endl;
22 return false;
23 }
24
25 if (dir[dir.length() - 1] != '/')
26 {
27 dir += "/";
28 }
29
30 if (!boost::filesystem::exists(dir))
31 {
32 std::cerr << "Output directory [" << dir << "] does not exist" << std::endl;
33 return false;
34 }
35
36 if (!boost::filesystem::is_directory(dir))
37 {
38 std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl;
39 return false;
40 }
41
42 return true;
43}
44
Sadik Armagan2b03d642019-04-12 15:17:02 +010045bool ValidateProvidedFile(const std::string& inputFileName)
Jim Flynn3091b062019-02-15 14:45:04 +000046{
47 if (!boost::filesystem::exists(inputFileName))
48 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010049 std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000050 return false;
51 }
52
53 if (boost::filesystem::is_directory(inputFileName))
54 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010055 std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000056 return false;
57 }
58
59 return true;
60}
61
Sadik Armagandc2f7f42019-04-26 17:11:47 +010062bool ValidateQuantizationScheme(const std::string& scheme)
63{
64 if (scheme.empty())
65 {
66 std::cerr << "No Quantization Scheme specified" << std::endl;
67 return false;
68 }
69
70 std::vector<std::string> supportedSchemes = {
71 "QAsymm8",
72 "QSymm16"
73 };
74
75 auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
76 if (iterator == supportedSchemes.end())
77 {
78 std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
79 return false;
80 }
81
82 return true;
83}
84
Jim Flynn3091b062019-02-15 14:45:04 +000085bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
86{
87 namespace po = boost::program_options;
88
89 po::options_description desc("Options");
90 try
91 {
92 desc.add_options()
93 ("help,h", "Display help messages")
94 ("infile,f", po::value<std::string>(&m_InputFileName)->required(),
95 "Input file containing float 32 ArmNN Input Graph")
Sadik Armagandc2f7f42019-04-26 17:11:47 +010096 ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymm8"),
97 "Quantization scheme, \"QAsymm8\" or \"QSymm16\", default value QAsymm8")
Sadik Armagan2b03d642019-04-12 15:17:02 +010098 ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
99 "CSV file containing paths for RAW input tensors")
Éanna Ó Catháin5696bff2019-05-10 13:29:13 +0100100 ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
101 "Preserve the input and output data types")
Jim Flynn3091b062019-02-15 14:45:04 +0000102 ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
103 "Directory that output file will be written to")
Finn Williamsb6e17562019-05-16 16:40:41 +0100104 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
Jim Flynn3091b062019-02-15 14:45:04 +0000105 }
106 catch (const std::exception& e)
107 {
108 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
109 return false;
110 }
111
112 po::variables_map vm;
113
114 try
115 {
116 po::store(po::parse_command_line(argc, argv, desc), vm);
117
Finn Williamsb6e17562019-05-16 16:40:41 +0100118 if (vm.count("help") || argc <= 1)
Jim Flynn3091b062019-02-15 14:45:04 +0000119 {
Finn Williamsb6e17562019-05-16 16:40:41 +0100120 std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
121 std::cout << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +0000122 std::cout << desc << std::endl;
123 return false;
124 }
125
126 po::notify(vm);
127 }
128 catch (const po::error& e)
129 {
130 std::cerr << e.what() << std::endl << std::endl;
131 std::cerr << desc << std::endl;
132 return false;
133 }
134
Sadik Armagan2b03d642019-04-12 15:17:02 +0100135 if (!armnnQuantizer::ValidateProvidedFile(m_InputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +0000136 {
137 return false;
138 }
139
Sadik Armagandc2f7f42019-04-26 17:11:47 +0100140 if (!ValidateQuantizationScheme(m_QuantizationScheme))
141 {
142 return false;
143 }
144
Sadik Armagan2b03d642019-04-12 15:17:02 +0100145 if (m_CsvFileName != "")
146 {
147 if (!armnnQuantizer::ValidateProvidedFile(m_CsvFileName))
148 {
149 return false;
150 }
151 else
152 {
153 boost::filesystem::path csvFilePath(m_CsvFileName);
154 m_CsvFileDirectory = csvFilePath.parent_path().c_str();
155 }
Nina Drozd59e15b02019-04-25 15:45:20 +0100156
157 // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
158 m_QuantizationDataSet = QuantizationDataSet(m_CsvFileName);
Sadik Armagan2b03d642019-04-12 15:17:02 +0100159 }
160
Jim Flynn3091b062019-02-15 14:45:04 +0000161 if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory))
162 {
163 return false;
164 }
165
166 std::string output(m_OutputDirectory);
167 output.append(m_OutputFileName);
Nina Drozd59e15b02019-04-25 15:45:20 +0100168
Jim Flynn3091b062019-02-15 14:45:04 +0000169 if (boost::filesystem::exists(output))
170 {
171 std::cerr << "Output file [" << output << "] already exists" << std::endl;
172 return false;
173 }
174
175 return true;
176}
177
178} // namespace armnnQuantizer