blob: 57b8692701d9047ade51fdb0297bf1d6d4aa31e8 [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;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000036 std::string fileFormat;
Jim Flynn4951b8c2019-10-03 10:04:30 -070037
James Conroy7b4886f2019-04-11 10:23:58 +010038 double thresholdTime = 0.0;
39
telsoa01c577f2c2018-08-31 09:22:23 +010040 size_t subgraphId = 0;
41
Matthew Jackson07882f12019-09-05 15:55:55 +010042 const std::string backendsMessage = "REQUIRED: Which device to run layers on by default. Possible choices: "
Aron Virginas-Tar5cc8e562018-10-23 15:14:46 +010043 + armnn::BackendRegistryInstance().GetBackendIdsAsString();
telsoa01c577f2c2018-08-31 09:22:23 +010044 po::options_description desc("Options");
45 try
46 {
47 desc.add_options()
48 ("help", "Display usage information")
Matthew Jackson07882f12019-09-05 15:55:55 +010049 ("compute,c", po::value<std::vector<std::string>>()->multitoken()->required(),
50 backendsMessage.c_str())
telsoa01c577f2c2018-08-31 09:22:23 +010051 ("test-cases,t", po::value(&testCasesFile), "Path to a CSV file containing test cases to run. "
52 "If set, further parameters -- with the exception of compute device and concurrency -- will be ignored, "
53 "as they are expected to be defined in the file for each test in particular.")
54 ("concurrent,n", po::bool_switch()->default_value(false),
55 "Whether or not the test cases should be executed in parallel")
Matteo Martincigh49124022019-01-11 13:25:59 +000056 ("model-format,f", po::value(&modelFormat)->required(),
Aron Virginas-Tar64e4ccb2019-02-12 11:27:53 +000057 "armnn-binary, caffe-binary, caffe-text, onnx-binary, onnx-text, tflite-binary, tensorflow-binary or "
58 "tensorflow-text.")
59 ("model-path,m", po::value(&modelPath)->required(), "Path to model file, e.g. .armnn, .caffemodel, "
60 ".prototxt, .tflite, .onnx")
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010061 ("dynamic-backends-path,b", po::value(&dynamicBackendsPath),
62 "Path where to load any available dynamic backend from. "
63 "If left empty (the default), dynamic backends will not be used.")
Ferran Balaguerc602f292019-02-08 17:09:55 +000064 ("input-name,i", po::value(&inputNames),
65 "Identifier of the input tensors in the network separated by comma.")
telsoa01c577f2c2018-08-31 09:22:23 +010066 ("subgraph-number,x", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be executed."
67 "Defaults to 0")
Ferran Balaguerc602f292019-02-08 17:09:55 +000068 ("input-tensor-shape,s", po::value(&inputTensorShapes),
Francis Murtagh1555cbd2019-10-08 14:47:46 +010069 "The shape of the input tensors in the network as a flat array of integers separated by comma."
70 "Several shapes can be passed by separating them with a colon (:)."
telsoa01c577f2c2018-08-31 09:22:23 +010071 "This parameter is optional, depending on the network.")
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010072 ("input-tensor-data,d", po::value(&inputTensorDataFilePaths)->default_value(""),
Ferran Balaguerc602f292019-02-08 17:09:55 +000073 "Path to files containing the input data as a flat array separated by whitespace. "
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010074 "Several paths can be passed by separating them with a comma. If not specified, the network will be run "
75 "with dummy data (useful for profiling).")
Ferran Balaguerc602f292019-02-08 17:09:55 +000076 ("input-type,y",po::value(&inputTypes), "The type of the input tensors in the network separated by comma. "
77 "If unset, defaults to \"float\" for all defined inputs. "
Éanna Ó Catháinb3d481a2019-02-26 11:26:24 +000078 "Accepted values (float, int or qasymm8)")
Narumol Prangnawarat610256f2019-06-26 15:10:46 +010079 ("quantize-input,q",po::bool_switch()->default_value(false),
80 "If this option is enabled, all float inputs will be quantized to qasymm8. "
81 "If unset, default to not quantized. "
82 "Accepted values (true or false)")
Éanna Ó Catháinb3d481a2019-02-26 11:26:24 +000083 ("output-type,z",po::value(&outputTypes),
84 "The type of the output tensors in the network separated by comma. "
85 "If unset, defaults to \"float\" for all defined outputs. "
86 "Accepted values (float, int or qasymm8).")
Georgios Pinitas50311ba2020-02-18 13:25:23 +000087 ("dequantize-output,l",po::bool_switch()->default_value(false),
88 "If this option is enabled, all quantized outputs will be dequantized to float. "
89 "If unset, default to not get dequantized. "
90 "Accepted values (true or false)")
Ferran Balaguerc602f292019-02-08 17:09:55 +000091 ("output-name,o", po::value(&outputNames),
92 "Identifier of the output tensors in the network separated by comma.")
Sadik Armagan77086282019-09-02 11:46:28 +010093 ("write-outputs-to-file,w", po::value(&outputTensorFiles),
94 "Comma-separated list of output file paths keyed with the binding-id of the output slot. "
95 "If left empty (the default), the output tensors will not be written to a file.")
telsoa01c577f2c2018-08-31 09:22:23 +010096 ("event-based-profiling,e", po::bool_switch()->default_value(false),
Ruomei Yan2fcce082019-04-02 16:47:34 +010097 "Enables built in profiler. If unset, defaults to off.")
Andre Ghattas23ae2ea2019-08-07 12:18:38 +010098 ("visualize-optimized-model,v", po::bool_switch()->default_value(false),
99 "Enables built optimized model visualizer. If unset, defaults to off.")
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000100 ("bf16-turbo-mode", po::bool_switch()->default_value(false), "If this option is enabled, FP32 layers, "
101 "weights and biases will be converted to BFloat16 where the backend supports it")
Ruomei Yan2fcce082019-04-02 16:47:34 +0100102 ("fp16-turbo-mode,h", po::bool_switch()->default_value(false), "If this option is enabled, FP32 layers, "
James Conroy7b4886f2019-04-11 10:23:58 +0100103 "weights and biases will be converted to FP16 where the backend supports it")
104 ("threshold-time,r", po::value<double>(&thresholdTime)->default_value(0.0),
105 "Threshold time is the maximum allowed time for inference measured in milliseconds. If the actual "
106 "inference time is greater than the threshold time, the test will fail. By default, no threshold "
Matthew Jackson54658b92019-08-27 15:35:59 +0100107 "time is used.")
108 ("print-intermediate-layers,p", po::bool_switch()->default_value(false),
Jim Flynn4951b8c2019-10-03 10:04:30 -0700109 "If this option is enabled, the output of every graph layer will be printed.")
110 ("enable-external-profiling,a", po::bool_switch()->default_value(false),
111 "If enabled external profiling will be switched on")
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100112 ("timeline-profiling", po::bool_switch()->default_value(false),
113 "If enabled timeline profiling will be switched on, requires external profiling")
Jim Flynn4951b8c2019-10-03 10:04:30 -0700114 ("outgoing-capture-file,j", po::value(&outgoingCaptureFile),
115 "If specified the outgoing external profiling packets will be captured in this binary file")
116 ("incoming-capture-file,k", po::value(&incomingCaptureFile),
117 "If specified the incoming external profiling packets will be captured in this binary file")
118 ("file-only-external-profiling,g", po::bool_switch()->default_value(false),
119 "If enabled then the 'file-only' test mode of external profiling will be enabled")
120 ("counter-capture-period,u", po::value<uint32_t>(&counterCapturePeriod)->default_value(150u),
Derek Lamberti132563c2019-12-02 16:06:40 +0000121 "If profiling is enabled in 'file-only' mode this is the capture period that will be used in the test")
Finn Williams50616012020-05-13 17:19:14 +0100122 ("file-format", po::value(&fileFormat)->default_value("binary"),
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000123 "If profiling is enabled specifies the output file format")
Derek Lamberti132563c2019-12-02 16:06:40 +0000124 ("parse-unsupported", po::bool_switch()->default_value(false),
125 "Add unsupported operators as stand-in layers (where supported by parser)");
telsoa01c577f2c2018-08-31 09:22:23 +0100126 }
127 catch (const std::exception& e)
128 {
129 // Coverity points out that default_value(...) can throw a bad_lexical_cast,
130 // and that desc.add_options() can throw boost::io::too_few_args.
131 // They really won't in any of these cases.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100132 ARMNN_ASSERT_MSG(false, "Caught unexpected exception");
Derek Lamberti08446972019-11-26 16:38:31 +0000133 ARMNN_LOG(fatal) << "Fatal internal error: " << e.what();
telsoa01c577f2c2018-08-31 09:22:23 +0100134 return EXIT_FAILURE;
135 }
136
137 // Parses the command-line.
138 po::variables_map vm;
139 try
140 {
141 po::store(po::parse_command_line(argc, argv, desc), vm);
142
143 if (CheckOption(vm, "help") || argc <= 1)
144 {
145 std::cout << "Executes a neural network model using the provided input tensor. " << std::endl;
146 std::cout << "Prints the resulting output tensor." << std::endl;
147 std::cout << std::endl;
148 std::cout << desc << std::endl;
149 return EXIT_SUCCESS;
150 }
151
152 po::notify(vm);
153 }
154 catch (const po::error& e)
155 {
156 std::cerr << e.what() << std::endl << std::endl;
157 std::cerr << desc << std::endl;
158 return EXIT_FAILURE;
159 }
160
161 // Get the value of the switch arguments.
162 bool concurrent = vm["concurrent"].as<bool>();
163 bool enableProfiling = vm["event-based-profiling"].as<bool>();
Andre Ghattas23ae2ea2019-08-07 12:18:38 +0100164 bool enableLayerDetails = vm["visualize-optimized-model"].as<bool>();
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000165 bool enableBf16TurboMode = vm["bf16-turbo-mode"].as<bool>();
Ruomei Yan2fcce082019-04-02 16:47:34 +0100166 bool enableFp16TurboMode = vm["fp16-turbo-mode"].as<bool>();
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100167 bool quantizeInput = vm["quantize-input"].as<bool>();
Georgios Pinitas50311ba2020-02-18 13:25:23 +0000168 bool dequantizeOutput = vm["dequantize-output"].as<bool>();
Matthew Jackson54658b92019-08-27 15:35:59 +0100169 bool printIntermediate = vm["print-intermediate-layers"].as<bool>();
Jim Flynn4951b8c2019-10-03 10:04:30 -0700170 bool enableExternalProfiling = vm["enable-external-profiling"].as<bool>();
171 bool fileOnlyExternalProfiling = vm["file-only-external-profiling"].as<bool>();
Derek Lamberti132563c2019-12-02 16:06:40 +0000172 bool parseUnsupported = vm["parse-unsupported"].as<bool>();
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100173 bool timelineEnabled = vm["timeline-profiling"].as<bool>();
Derek Lamberti132563c2019-12-02 16:06:40 +0000174
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000175 if (enableBf16TurboMode && enableFp16TurboMode)
176 {
177 ARMNN_LOG(fatal) << "BFloat16 and Float16 turbo mode cannot be enabled at the same time.";
178 return EXIT_FAILURE;
179 }
180
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100181 // Create runtime
182 armnn::IRuntime::CreationOptions options;
183 options.m_EnableGpuProfiling = enableProfiling;
184 options.m_DynamicBackendsPath = dynamicBackendsPath;
185 options.m_ProfilingOptions.m_EnableProfiling = enableExternalProfiling;
186 options.m_ProfilingOptions.m_IncomingCaptureFile = incomingCaptureFile;
187 options.m_ProfilingOptions.m_OutgoingCaptureFile = outgoingCaptureFile;
188 options.m_ProfilingOptions.m_FileOnly = fileOnlyExternalProfiling;
189 options.m_ProfilingOptions.m_CapturePeriod = counterCapturePeriod;
190 options.m_ProfilingOptions.m_FileFormat = fileFormat;
191 options.m_ProfilingOptions.m_TimelineEnabled = timelineEnabled;
192
193 if (timelineEnabled && !enableExternalProfiling)
194 {
195 ARMNN_LOG(fatal) << "Timeline profiling requires external profiling to be turned on";
196 return EXIT_FAILURE;
197 }
telsoa01c577f2c2018-08-31 09:22:23 +0100198
199 // Check whether we have to load test cases from a file.
200 if (CheckOption(vm, "test-cases"))
201 {
202 // Check that the file exists.
203 if (!boost::filesystem::exists(testCasesFile))
204 {
Derek Lamberti08446972019-11-26 16:38:31 +0000205 ARMNN_LOG(fatal) << "Given file \"" << testCasesFile << "\" does not exist";
telsoa01c577f2c2018-08-31 09:22:23 +0100206 return EXIT_FAILURE;
207 }
208
209 // Parse CSV file and extract test cases
210 armnnUtils::CsvReader reader;
211 std::vector<armnnUtils::CsvRow> testCases = reader.ParseFile(testCasesFile);
212
213 // Check that there is at least one test case to run
214 if (testCases.empty())
215 {
Derek Lamberti08446972019-11-26 16:38:31 +0000216 ARMNN_LOG(fatal) << "Given file \"" << testCasesFile << "\" has no test cases";
telsoa01c577f2c2018-08-31 09:22:23 +0100217 return EXIT_FAILURE;
218 }
telsoa01c577f2c2018-08-31 09:22:23 +0100219 // Create runtime
telsoa01c577f2c2018-08-31 09:22:23 +0100220 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options));
221
222 const std::string executableName("ExecuteNetwork");
223
224 // Check whether we need to run the test cases concurrently
225 if (concurrent)
226 {
227 std::vector<std::future<int>> results;
228 results.reserve(testCases.size());
229
230 // Run each test case in its own thread
231 for (auto& testCase : testCases)
232 {
233 testCase.values.insert(testCase.values.begin(), executableName);
Nina Drozd549ae372018-09-10 14:26:44 +0100234 results.push_back(std::async(std::launch::async, RunCsvTest, std::cref(testCase), std::cref(runtime),
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000235 enableProfiling, enableFp16TurboMode, enableBf16TurboMode, thresholdTime,
236 printIntermediate, enableLayerDetails, parseUnsupported));
telsoa01c577f2c2018-08-31 09:22:23 +0100237 }
238
239 // Check results
240 for (auto& result : results)
241 {
242 if (result.get() != EXIT_SUCCESS)
243 {
244 return EXIT_FAILURE;
245 }
246 }
247 }
248 else
249 {
250 // Run tests sequentially
251 for (auto& testCase : testCases)
252 {
253 testCase.values.insert(testCase.values.begin(), executableName);
Matthew Jackson54658b92019-08-27 15:35:59 +0100254 if (RunCsvTest(testCase, runtime, enableProfiling,
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000255 enableFp16TurboMode, enableBf16TurboMode, thresholdTime, printIntermediate,
Derek Lamberti132563c2019-12-02 16:06:40 +0000256 enableLayerDetails, parseUnsupported) != EXIT_SUCCESS)
telsoa01c577f2c2018-08-31 09:22:23 +0100257 {
258 return EXIT_FAILURE;
259 }
260 }
261 }
262
263 return EXIT_SUCCESS;
264 }
265 else // Run single test
266 {
Aron Virginas-Tar382e21c2019-01-22 14:10:39 +0000267 // Get the preferred order of compute devices. If none are specified, default to using CpuRef
268 const std::string computeOption("compute");
Matteo Martincigh00dda4a2019-08-14 11:42:30 +0100269 std::vector<std::string> computeDevicesAsStrings =
270 CheckOption(vm, computeOption.c_str()) ?
271 vm[computeOption].as<std::vector<std::string>>() :
272 std::vector<std::string>();
Matteo Martincigh067112f2018-10-29 11:01:09 +0000273 std::vector<armnn::BackendId> computeDevices(computeDevicesAsStrings.begin(), computeDevicesAsStrings.end());
telsoa01c577f2c2018-08-31 09:22:23 +0100274
275 // Remove duplicates from the list of compute devices.
276 RemoveDuplicateDevices(computeDevices);
277
telsoa01c577f2c2018-08-31 09:22:23 +0100278 try
279 {
280 CheckOptionDependencies(vm);
281 }
282 catch (const po::error& e)
283 {
284 std::cerr << e.what() << std::endl << std::endl;
285 std::cerr << desc << std::endl;
286 return EXIT_FAILURE;
287 }
Colm Donelanb682d842019-10-16 12:24:20 +0100288 // Create runtime
Colm Donelanb682d842019-10-16 12:24:20 +0100289 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options));
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +0100290
Matteo Martincigh00dda4a2019-08-14 11:42:30 +0100291 return RunTest(modelFormat, inputTensorShapes, computeDevices, dynamicBackendsPath, modelPath, inputNames,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100292 inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames,
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000293 outputTensorFiles, dequantizeOutput, enableProfiling, enableFp16TurboMode, enableBf16TurboMode,
294 thresholdTime, printIntermediate, subgraphId, enableLayerDetails, parseUnsupported, runtime);
telsoa014fcda012018-03-09 14:13:49 +0000295 }
296}