blob: ace4a506fb17e489550173ce7014e5915de7f6a9 [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"
Francis Murtagh532a29d2020-06-29 11:50:01 +01007#include <Filesystem.hpp>
Jim Flynn3091b062019-02-15 14:45:04 +00008
9#include <boost/program_options.hpp>
Jim Flynn3091b062019-02-15 14:45:04 +000010
11namespace armnnQuantizer
12{
13
14bool 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
Francis Murtagh532a29d2020-06-29 11:50:01 +010027 if (!fs::exists(dir))
Jim Flynn3091b062019-02-15 14:45:04 +000028 {
29 std::cerr << "Output directory [" << dir << "] does not exist" << std::endl;
30 return false;
31 }
32
Francis Murtagh532a29d2020-06-29 11:50:01 +010033 if (!fs::is_directory(dir))
Jim Flynn3091b062019-02-15 14:45:04 +000034 {
35 std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl;
36 return false;
37 }
38
39 return true;
40}
41
Sadik Armagan2b03d642019-04-12 15:17:02 +010042bool ValidateProvidedFile(const std::string& inputFileName)
Jim Flynn3091b062019-02-15 14:45:04 +000043{
Francis Murtagh532a29d2020-06-29 11:50:01 +010044 if (!fs::exists(inputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +000045 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010046 std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000047 return false;
48 }
49
Francis Murtagh532a29d2020-06-29 11:50:01 +010050 if (fs::is_directory(inputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +000051 {
Sadik Armagan2b03d642019-04-12 15:17:02 +010052 std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +000053 return false;
54 }
55
56 return true;
57}
58
Sadik Armagandc2f7f42019-04-26 17:11:47 +010059bool 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
Francis Murtaghddb1d062020-03-10 13:51:45 +000067 std::vector<std::string> supportedSchemes =
68 {
69 "QAsymmS8",
70 "QAsymmU8",
Sadik Armagandc2f7f42019-04-26 17:11:47 +010071 "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
Jim Flynn3091b062019-02-15 14:45:04 +000084bool 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")
Francis Murtaghddb1d062020-03-10 13:51:45 +000095 ("scheme,s", po::value<std::string>(&m_QuantizationScheme)->default_value("QAsymmU8"),
96 "Quantization scheme,"
97 " \"QAsymmU8\" or \"QAsymmS8\" or \"QSymm16\","
98 " default value QAsymmU8")
Sadik Armagan2b03d642019-04-12 15:17:02 +010099 ("csvfile,c", po::value<std::string>(&m_CsvFileName)->default_value(""),
100 "CSV file containing paths for RAW input tensors")
Éanna Ó Catháin5696bff2019-05-10 13:29:13 +0100101 ("preserve-data-type,p", po::bool_switch(&m_PreserveDataType)->default_value(false),
102 "Preserve the input and output data types")
Jim Flynn3091b062019-02-15 14:45:04 +0000103 ("outdir,d", po::value<std::string>(&m_OutputDirectory)->required(),
104 "Directory that output file will be written to")
Finn Williamsb6e17562019-05-16 16:40:41 +0100105 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(), "ArmNN output file name");
Jim Flynn3091b062019-02-15 14:45:04 +0000106 }
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
Finn Williamsb6e17562019-05-16 16:40:41 +0100119 if (vm.count("help") || argc <= 1)
Jim Flynn3091b062019-02-15 14:45:04 +0000120 {
Finn Williamsb6e17562019-05-16 16:40:41 +0100121 std::cout << "Convert a Fp32 ArmNN model to a quantized ArmNN model." << std::endl;
122 std::cout << std::endl;
Jim Flynn3091b062019-02-15 14:45:04 +0000123 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
Sadik Armagan2b03d642019-04-12 15:17:02 +0100136 if (!armnnQuantizer::ValidateProvidedFile(m_InputFileName))
Jim Flynn3091b062019-02-15 14:45:04 +0000137 {
138 return false;
139 }
140
Sadik Armagandc2f7f42019-04-26 17:11:47 +0100141 if (!ValidateQuantizationScheme(m_QuantizationScheme))
142 {
143 return false;
144 }
145
Sadik Armagan2b03d642019-04-12 15:17:02 +0100146 if (m_CsvFileName != "")
147 {
148 if (!armnnQuantizer::ValidateProvidedFile(m_CsvFileName))
149 {
150 return false;
151 }
152 else
153 {
Francis Murtagh532a29d2020-06-29 11:50:01 +0100154 fs::path csvFilePath(m_CsvFileName);
Sadik Armagan2b03d642019-04-12 15:17:02 +0100155 m_CsvFileDirectory = csvFilePath.parent_path().c_str();
156 }
Nina Drozd59e15b02019-04-25 15:45:20 +0100157
158 // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
159 m_QuantizationDataSet = QuantizationDataSet(m_CsvFileName);
Sadik Armagan2b03d642019-04-12 15:17:02 +0100160 }
161
Jim Flynn3091b062019-02-15 14:45:04 +0000162 if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory))
163 {
164 return false;
165 }
166
167 std::string output(m_OutputDirectory);
168 output.append(m_OutputFileName);
Nina Drozd59e15b02019-04-25 15:45:20 +0100169
Francis Murtagh532a29d2020-06-29 11:50:01 +0100170 if (fs::exists(output))
Jim Flynn3091b062019-02-15 14:45:04 +0000171 {
172 std::cerr << "Output file [" << output << "] already exists" << std::endl;
173 return false;
174 }
175
176 return true;
177}
178
179} // namespace armnnQuantizer