blob: 0cccb66f639c021ba355880d9a4c0c683e4ca525 [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
Francis Murtaghddb1d062020-03-10 13:51:45 +000070 std::vector<std::string> supportedSchemes =
71 {
72 "QAsymmS8",
73 "QAsymmU8",
Sadik Armagandc2f7f42019-04-26 17:11:47 +010074 "QSymm16"
75 };
76
77 auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
78 if (iterator == supportedSchemes.end())
79 {
80 std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
81 return false;
82 }
83
84 return true;
85}
86
Jim Flynn3091b062019-02-15 14:45:04 +000087bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
88{
89 namespace po = boost::program_options;
90
91 po::options_description desc("Options");
92 try
93 {
94 desc.add_options()
95 ("help,h", "Display help messages")
96 ("infile,f", po::value<std::string>(&m_InputFileName)->required(),
97 "Input file containing float 32 ArmNN Input Graph")
Francis Murtaghddb1d062020-03-10 13:51:45 +000098 ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymmU8"),
99 "Quantization scheme,"
100 " \"QAsymmU8\" or \"QAsymmS8\" or \"QSymm16\","
101 " default value QAsymmU8")
Sadik Armagan2b03d642019-04-12 15:17:02 +0100102 ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
103 "CSV file containing paths for RAW input tensors")
Éanna Ó Catháin5696bff2019-05-10 13:29:13 +0100104 ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
105 "Preserve the input and output data types")
Jim Flynn3091b062019-02-15 14:45:04 +0000106 ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
107 "Directory that output file will be written to")
Finn Williamsb6e17562019-05-16 16:40:41 +0100108 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
Jim Flynn3091b062019-02-15 14:45:04 +0000109 }
110 catch (const std::exception& e)
111 {
112 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
113 return false;
114 }
115
116 po::variables_map vm;
117
118 try
119 {
120 po::store(po::parse_command_line(argc, argv, desc), vm);
121
Finn Williamsb6e17562019-05-16 16:40:41 +0100122 if (vm.count("help") || argc <= 1)
Jim Flynn3091b062019-02-15 14:45:04 +0000123 {
Finn Williamsb6e17562019-05-16 16:40:41 +0100124 std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
125 std::cout << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +0000126 std::cout << desc << std::endl;
127 return false;
128 }
129
130 po::notify(vm);
131 }
132 catch (const po::error& e)
133 {
134 std::cerr << e.what() << std::endl << std::endl;
135 std::cerr << desc << std::endl;
136 return false;
137 }
138
Sadik Armagan2b03d642019-04-12 15:17:02 +0100139 if (!armnnQuantizer::ValidateProvidedFile(m_InputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +0000140 {
141 return false;
142 }
143
Sadik Armagandc2f7f42019-04-26 17:11:47 +0100144 if (!ValidateQuantizationScheme(m_QuantizationScheme))
145 {
146 return false;
147 }
148
Sadik Armagan2b03d642019-04-12 15:17:02 +0100149 if (m_CsvFileName != "")
150 {
151 if (!armnnQuantizer::ValidateProvidedFile(m_CsvFileName))
152 {
153 return false;
154 }
155 else
156 {
157 boost::filesystem::path csvFilePath(m_CsvFileName);
158 m_CsvFileDirectory = csvFilePath.parent_path().c_str();
159 }
Nina Drozd59e15b02019-04-25 15:45:20 +0100160
161 // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
162 m_QuantizationDataSet = QuantizationDataSet(m_CsvFileName);
Sadik Armagan2b03d642019-04-12 15:17:02 +0100163 }
164
Jim Flynn3091b062019-02-15 14:45:04 +0000165 if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory))
166 {
167 return false;
168 }
169
170 std::string output(m_OutputDirectory);
171 output.append(m_OutputFileName);
Nina Drozd59e15b02019-04-25 15:45:20 +0100172
Jim Flynn3091b062019-02-15 14:45:04 +0000173 if (boost::filesystem::exists(output))
174 {
175 std::cerr << "Output file [" << output << "] already exists" << std::endl;
176 return false;
177 }
178
179 return true;
180}
181
182} // namespace armnnQuantizer