blob: 3c0301010d1575e84843fe5ed7698f63901ed8c2 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
telsoa01c577f2c2018-08-31 09:22:23 +01005
Francis Murtaghbee4bc92019-06-18 12:30:37 +01006#include "../NetworkExecutionUtils/NetworkExecutionUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01007
James Conroy7b4886f2019-04-11 10:23:58 +01008// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +01009int main(int argc, const char* argv[])
10{
11 // Configures logging for both the ARMNN library and this test program.
12#ifdef NDEBUG
13 armnn::LogSeverity level = armnn::LogSeverity::Info;
14#else
15 armnn::LogSeverity level = armnn::LogSeverity::Debug;
16#endif
17 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +010018
19 std::string testCasesFile;
20
21 std::string modelFormat;
22 std::string modelPath;
Ferran Balaguerc602f292019-02-08 17:09:55 +000023 std::string inputNames;
24 std::string inputTensorShapes;
25 std::string inputTensorDataFilePaths;
26 std::string outputNames;
27 std::string inputTypes;
Éanna Ó Catháinb3d481a2019-02-26 11:26:24 +000028 std::string outputTypes;
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010029 std::string dynamicBackendsPath;
Sadik Armagan77086282019-09-02 11:46:28 +010030 std::string outputTensorFiles;
telsoa01c577f2c2018-08-31 09:22:23 +010031
Jim Flynn4951b8c2019-10-03 10:04:30 -070032 // external profiling parameters
33 std::string outgoingCaptureFile;
34 std::string incomingCaptureFile;
35 uint32_t counterCapturePeriod;
36
James Conroy7b4886f2019-04-11 10:23:58 +010037 double thresholdTime = 0.0;
38
telsoa01c577f2c2018-08-31 09:22:23 +010039 size_t subgraphId = 0;
40
Matthew Jackson07882f12019-09-05 15:55:55 +010041 const std::string backendsMessage = "REQUIRED: Which device to run layers on by default. Possible choices: "
Aron Virginas-Tar5cc8e562018-10-23 15:14:46 +010042 + armnn::BackendRegistryInstance().GetBackendIdsAsString();
telsoa01c577f2c2018-08-31 09:22:23 +010043 po::options_description desc("Options");
44 try
45 {
46 desc.add_options()
47 ("help", "Display usage information")
Matthew Jackson07882f12019-09-05 15:55:55 +010048 ("compute,c", po::value<std::vector<std::string>>()->multitoken()->required(),
49 backendsMessage.c_str())
telsoa01c577f2c2018-08-31 09:22:23 +010050 ("test-cases,t", po::value(&testCasesFile), "Path to a CSV file containing test cases to run. "
51 "If set, further parameters -- with the exception of compute device and concurrency -- will be ignored, "
52 "as they are expected to be defined in the file for each test in particular.")
53 ("concurrent,n", po::bool_switch()->default_value(false),
54 "Whether or not the test cases should be executed in parallel")
Matteo Martincigh49124022019-01-11 13:25:59 +000055 ("model-format,f", po::value(&modelFormat)->required(),
Aron Virginas-Tar64e4ccb2019-02-12 11:27:53 +000056 "armnn-binary, caffe-binary, caffe-text, onnx-binary, onnx-text, tflite-binary, tensorflow-binary or "
57 "tensorflow-text.")
58 ("model-path,m", po::value(&modelPath)->required(), "Path to model file, e.g. .armnn, .caffemodel, "
59 ".prototxt, .tflite, .onnx")
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010060 ("dynamic-backends-path,b", po::value(&dynamicBackendsPath),
61 "Path where to load any available dynamic backend from. "
62 "If left empty (the default), dynamic backends will not be used.")
Ferran Balaguerc602f292019-02-08 17:09:55 +000063 ("input-name,i", po::value(&inputNames),
64 "Identifier of the input tensors in the network separated by comma.")
telsoa01c577f2c2018-08-31 09:22:23 +010065 ("subgraph-number,x", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be executed."
66 "Defaults to 0")
Ferran Balaguerc602f292019-02-08 17:09:55 +000067 ("input-tensor-shape,s", po::value(&inputTensorShapes),
Francis Murtagh1555cbd2019-10-08 14:47:46 +010068 "The shape of the input tensors in the network as a flat array of integers separated by comma."
69 "Several shapes can be passed by separating them with a colon (:)."
telsoa01c577f2c2018-08-31 09:22:23 +010070 "This parameter is optional, depending on the network.")
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010071 ("input-tensor-data,d", po::value(&inputTensorDataFilePaths)->default_value(""),
Ferran Balaguerc602f292019-02-08 17:09:55 +000072 "Path to files containing the input data as a flat array separated by whitespace. "
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010073 "Several paths can be passed by separating them with a comma. If not specified, the network will be run "
74 "with dummy data (useful for profiling).")
Ferran Balaguerc602f292019-02-08 17:09:55 +000075 ("input-type,y",po::value(&inputTypes), "The type of the input tensors in the network separated by comma. "
76 "If unset, defaults to \"float\" for all defined inputs. "
Éanna Ó Catháinb3d481a2019-02-26 11:26:24 +000077 "Accepted values (float, int or qasymm8)")
Narumol Prangnawarat610256f2019-06-26 15:10:46 +010078 ("quantize-input,q",po::bool_switch()->default_value(false),
79 "If this option is enabled, all float inputs will be quantized to qasymm8. "
80 "If unset, default to not quantized. "
81 "Accepted values (true or false)")
Éanna Ó Catháinb3d481a2019-02-26 11:26:24 +000082 ("output-type,z",po::value(&outputTypes),
83 "The type of the output tensors in the network separated by comma. "
84 "If unset, defaults to \"float\" for all defined outputs. "
85 "Accepted values (float, int or qasymm8).")
Georgios Pinitas50311ba2020-02-18 13:25:23 +000086 ("dequantize-output,l",po::bool_switch()->default_value(false),
87 "If this option is enabled, all quantized outputs will be dequantized to float. "
88 "If unset, default to not get dequantized. "
89 "Accepted values (true or false)")
Ferran Balaguerc602f292019-02-08 17:09:55 +000090 ("output-name,o", po::value(&outputNames),
91 "Identifier of the output tensors in the network separated by comma.")
Sadik Armagan77086282019-09-02 11:46:28 +010092 ("write-outputs-to-file,w", po::value(&outputTensorFiles),
93 "Comma-separated list of output file paths keyed with the binding-id of the output slot. "
94 "If left empty (the default), the output tensors will not be written to a file.")
telsoa01c577f2c2018-08-31 09:22:23 +010095 ("event-based-profiling,e", po::bool_switch()->default_value(false),
Ruomei Yan2fcce082019-04-02 16:47:34 +010096 "Enables built in profiler. If unset, defaults to off.")
Andre Ghattas23ae2ea2019-08-07 12:18:38 +010097 ("visualize-optimized-model,v", po::bool_switch()->default_value(false),
98 "Enables built optimized model visualizer. If unset, defaults to off.")
Ruomei Yan2fcce082019-04-02 16:47:34 +010099 ("fp16-turbo-mode,h", po::bool_switch()->default_value(false), "If this option is enabled, FP32 layers, "
James Conroy7b4886f2019-04-11 10:23:58 +0100100 "weights and biases will be converted to FP16 where the backend supports it")
101 ("threshold-time,r", po::value<double>(&thresholdTime)->default_value(0.0),
102 "Threshold time is the maximum allowed time for inference measured in milliseconds. If the actual "
103 "inference time is greater than the threshold time, the test will fail. By default, no threshold "
Matthew Jackson54658b92019-08-27 15:35:59 +0100104 "time is used.")
105 ("print-intermediate-layers,p", po::bool_switch()->default_value(false),
Jim Flynn4951b8c2019-10-03 10:04:30 -0700106 "If this option is enabled, the output of every graph layer will be printed.")
107 ("enable-external-profiling,a", po::bool_switch()->default_value(false),
108 "If enabled external profiling will be switched on")
109 ("outgoing-capture-file,j", po::value(&outgoingCaptureFile),
110 "If specified the outgoing external profiling packets will be captured in this binary file")
111 ("incoming-capture-file,k", po::value(&incomingCaptureFile),
112 "If specified the incoming external profiling packets will be captured in this binary file")
113 ("file-only-external-profiling,g", po::bool_switch()->default_value(false),
114 "If enabled then the 'file-only' test mode of external profiling will be enabled")
115 ("counter-capture-period,u", po::value<uint32_t>(&counterCapturePeriod)->default_value(150u),
Derek Lamberti132563c2019-12-02 16:06:40 +0000116 "If profiling is enabled in 'file-only' mode this is the capture period that will be used in the test")
117 ("parse-unsupported", po::bool_switch()->default_value(false),
118 "Add unsupported operators as stand-in layers (where supported by parser)");
telsoa01c577f2c2018-08-31 09:22:23 +0100119 }
120 catch (const std::exception& e)
121 {
122 // Coverity points out that default_value(...) can throw a bad_lexical_cast,
123 // and that desc.add_options() can throw boost::io::too_few_args.
124 // They really won't in any of these cases.
125 BOOST_ASSERT_MSG(false, "Caught unexpected exception");
Derek Lamberti08446972019-11-26 16:38:31 +0000126 ARMNN_LOG(fatal) << "Fatal internal error: " << e.what();
telsoa01c577f2c2018-08-31 09:22:23 +0100127 return EXIT_FAILURE;
128 }
129
130 // Parses the command-line.
131 po::variables_map vm;
132 try
133 {
134 po::store(po::parse_command_line(argc, argv, desc), vm);
135
136 if (CheckOption(vm, "help") || argc <= 1)
137 {
138 std::cout << "Executes a neural network model using the provided input tensor. " << std::endl;
139 std::cout << "Prints the resulting output tensor." << std::endl;
140 std::cout << std::endl;
141 std::cout << desc << std::endl;
142 return EXIT_SUCCESS;
143 }
144
145 po::notify(vm);
146 }
147 catch (const po::error& e)
148 {
149 std::cerr << e.what() << std::endl << std::endl;
150 std::cerr << desc << std::endl;
151 return EXIT_FAILURE;
152 }
153
154 // Get the value of the switch arguments.
155 bool concurrent = vm["concurrent"].as<bool>();
156 bool enableProfiling = vm["event-based-profiling"].as<bool>();
Andre Ghattas23ae2ea2019-08-07 12:18:38 +0100157 bool enableLayerDetails = vm["visualize-optimized-model"].as<bool>();
Ruomei Yan2fcce082019-04-02 16:47:34 +0100158 bool enableFp16TurboMode = vm["fp16-turbo-mode"].as<bool>();
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100159 bool quantizeInput = vm["quantize-input"].as<bool>();
Georgios Pinitas50311ba2020-02-18 13:25:23 +0000160 bool dequantizeOutput = vm["dequantize-output"].as<bool>();
Matthew Jackson54658b92019-08-27 15:35:59 +0100161 bool printIntermediate = vm["print-intermediate-layers"].as<bool>();
Jim Flynn4951b8c2019-10-03 10:04:30 -0700162 bool enableExternalProfiling = vm["enable-external-profiling"].as<bool>();
163 bool fileOnlyExternalProfiling = vm["file-only-external-profiling"].as<bool>();
Derek Lamberti132563c2019-12-02 16:06:40 +0000164 bool parseUnsupported = vm["parse-unsupported"].as<bool>();
165
telsoa01c577f2c2018-08-31 09:22:23 +0100166
167 // Check whether we have to load test cases from a file.
168 if (CheckOption(vm, "test-cases"))
169 {
170 // Check that the file exists.
171 if (!boost::filesystem::exists(testCasesFile))
172 {
Derek Lamberti08446972019-11-26 16:38:31 +0000173 ARMNN_LOG(fatal) << "Given file \"" << testCasesFile << "\" does not exist";
telsoa01c577f2c2018-08-31 09:22:23 +0100174 return EXIT_FAILURE;
175 }
176
177 // Parse CSV file and extract test cases
178 armnnUtils::CsvReader reader;
179 std::vector<armnnUtils::CsvRow> testCases = reader.ParseFile(testCasesFile);
180
181 // Check that there is at least one test case to run
182 if (testCases.empty())
183 {
Derek Lamberti08446972019-11-26 16:38:31 +0000184 ARMNN_LOG(fatal) << "Given file \"" << testCasesFile << "\" has no test cases";
telsoa01c577f2c2018-08-31 09:22:23 +0100185 return EXIT_FAILURE;
186 }
187
188 // Create runtime
189 armnn::IRuntime::CreationOptions options;
Nina Drozd549ae372018-09-10 14:26:44 +0100190 options.m_EnableGpuProfiling = enableProfiling;
Nikhil Raj0898b332019-10-22 17:19:23 +0100191 options.m_DynamicBackendsPath = dynamicBackendsPath;
Jim Flynn4951b8c2019-10-03 10:04:30 -0700192 options.m_ProfilingOptions.m_EnableProfiling = enableExternalProfiling;
193 options.m_ProfilingOptions.m_IncomingCaptureFile = incomingCaptureFile;
194 options.m_ProfilingOptions.m_OutgoingCaptureFile = outgoingCaptureFile;
195 options.m_ProfilingOptions.m_FileOnly = fileOnlyExternalProfiling;
196 options.m_ProfilingOptions.m_CapturePeriod = counterCapturePeriod;
telsoa01c577f2c2018-08-31 09:22:23 +0100197 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options));
198
199 const std::string executableName("ExecuteNetwork");
200
201 // Check whether we need to run the test cases concurrently
202 if (concurrent)
203 {
204 std::vector<std::future<int>> results;
205 results.reserve(testCases.size());
206
207 // Run each test case in its own thread
208 for (auto& testCase : testCases)
209 {
210 testCase.values.insert(testCase.values.begin(), executableName);
Nina Drozd549ae372018-09-10 14:26:44 +0100211 results.push_back(std::async(std::launch::async, RunCsvTest, std::cref(testCase), std::cref(runtime),
Andre Ghattas23ae2ea2019-08-07 12:18:38 +0100212 enableProfiling, enableFp16TurboMode, thresholdTime, printIntermediate,
Derek Lamberti132563c2019-12-02 16:06:40 +0000213 enableLayerDetails, parseUnsupported));
telsoa01c577f2c2018-08-31 09:22:23 +0100214 }
215
216 // Check results
217 for (auto& result : results)
218 {
219 if (result.get() != EXIT_SUCCESS)
220 {
221 return EXIT_FAILURE;
222 }
223 }
224 }
225 else
226 {
227 // Run tests sequentially
228 for (auto& testCase : testCases)
229 {
230 testCase.values.insert(testCase.values.begin(), executableName);
Matthew Jackson54658b92019-08-27 15:35:59 +0100231 if (RunCsvTest(testCase, runtime, enableProfiling,
Andre Ghattas23ae2ea2019-08-07 12:18:38 +0100232 enableFp16TurboMode, thresholdTime, printIntermediate,
Derek Lamberti132563c2019-12-02 16:06:40 +0000233 enableLayerDetails, parseUnsupported) != EXIT_SUCCESS)
telsoa01c577f2c2018-08-31 09:22:23 +0100234 {
235 return EXIT_FAILURE;
236 }
237 }
238 }
239
240 return EXIT_SUCCESS;
241 }
242 else // Run single test
243 {
Aron Virginas-Tar382e21c2019-01-22 14:10:39 +0000244 // Get the preferred order of compute devices. If none are specified, default to using CpuRef
245 const std::string computeOption("compute");
Matteo Martincigh00dda4a2019-08-14 11:42:30 +0100246 std::vector<std::string> computeDevicesAsStrings =
247 CheckOption(vm, computeOption.c_str()) ?
248 vm[computeOption].as<std::vector<std::string>>() :
249 std::vector<std::string>();
Matteo Martincigh067112f2018-10-29 11:01:09 +0000250 std::vector<armnn::BackendId> computeDevices(computeDevicesAsStrings.begin(), computeDevicesAsStrings.end());
telsoa01c577f2c2018-08-31 09:22:23 +0100251
252 // Remove duplicates from the list of compute devices.
253 RemoveDuplicateDevices(computeDevices);
254
telsoa01c577f2c2018-08-31 09:22:23 +0100255 try
256 {
257 CheckOptionDependencies(vm);
258 }
259 catch (const po::error& e)
260 {
261 std::cerr << e.what() << std::endl << std::endl;
262 std::cerr << desc << std::endl;
263 return EXIT_FAILURE;
264 }
Colm Donelanb682d842019-10-16 12:24:20 +0100265 // Create runtime
266 armnn::IRuntime::CreationOptions options;
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +0100267 options.m_EnableGpuProfiling = enableProfiling;
268 options.m_DynamicBackendsPath = dynamicBackendsPath;
269 options.m_ProfilingOptions.m_EnableProfiling = enableExternalProfiling;
Colm Donelanb682d842019-10-16 12:24:20 +0100270 options.m_ProfilingOptions.m_IncomingCaptureFile = incomingCaptureFile;
271 options.m_ProfilingOptions.m_OutgoingCaptureFile = outgoingCaptureFile;
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +0100272 options.m_ProfilingOptions.m_FileOnly = fileOnlyExternalProfiling;
273 options.m_ProfilingOptions.m_CapturePeriod = counterCapturePeriod;
Colm Donelanb682d842019-10-16 12:24:20 +0100274 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options));
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +0100275
Matteo Martincigh00dda4a2019-08-14 11:42:30 +0100276 return RunTest(modelFormat, inputTensorShapes, computeDevices, dynamicBackendsPath, modelPath, inputNames,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100277 inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames,
Georgios Pinitas50311ba2020-02-18 13:25:23 +0000278 outputTensorFiles, dequantizeOutput, enableProfiling, enableFp16TurboMode, thresholdTime,
279 printIntermediate, subgraphId, enableLayerDetails, parseUnsupported, runtime);
telsoa014fcda012018-03-09 14:13:49 +0000280 }
281}