blob: c84c79ea7896d4349b8d54e9faceb2160bd7e189 [file] [log] [blame]
Jan Eilers45274902020-10-15 18:34:43 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ExecuteNetworkProgramOptions.hpp"
7#include "NetworkExecutionUtils/NetworkExecutionUtils.hpp"
8#include "InferenceTest.hpp"
9
10#include <armnn/BackendRegistry.hpp>
11#include <armnn/Exceptions.hpp>
12#include <armnn/utility/Assert.hpp>
13#include <armnn/utility/StringUtils.hpp>
14#include <armnn/Logging.hpp>
15
16#include <fmt/format.h>
17
18bool CheckOption(const cxxopts::ParseResult& result,
19 const char* option)
20{
21 // Check that the given option is valid.
22 if (option == nullptr)
23 {
24 return false;
25 }
26
27 // Check whether 'option' is provided.
28 return ((result.count(option)) ? true : false);
29}
30
31void CheckOptionDependency(const cxxopts::ParseResult& result,
32 const char* option,
33 const char* required)
34{
35 // Check that the given options are valid.
36 if (option == nullptr || required == nullptr)
37 {
38 throw cxxopts::OptionParseException("Invalid option to check dependency for");
39 }
40
41 // Check that if 'option' is provided, 'required' is also provided.
42 if (CheckOption(result, option) && !result[option].has_default())
43 {
44 if (CheckOption(result, required) == 0 || result[required].has_default())
45 {
46 throw cxxopts::OptionParseException(
47 std::string("Option '") + option + "' requires option '" + required + "'.");
48 }
49 }
50}
51
52void CheckOptionDependencies(const cxxopts::ParseResult& result)
53{
54 CheckOptionDependency(result, "model-path", "model-format");
55 CheckOptionDependency(result, "input-tensor-shape", "model-path");
56 CheckOptionDependency(result, "tuning-level", "tuning-path");
57}
58
59void RemoveDuplicateDevices(std::vector<armnn::BackendId>& computeDevices)
60{
61 // Mark the duplicate devices as 'Undefined'.
62 for (auto i = computeDevices.begin(); i != computeDevices.end(); ++i)
63 {
64 for (auto j = std::next(i); j != computeDevices.end(); ++j)
65 {
66 if (*j == *i)
67 {
68 *j = armnn::Compute::Undefined;
69 }
70 }
71 }
72
73 // Remove 'Undefined' devices.
74 computeDevices.erase(std::remove(computeDevices.begin(), computeDevices.end(), armnn::Compute::Undefined),
75 computeDevices.end());
76}
77
Jan Eilersc5b84b52021-02-16 12:40:43 +000078/// Takes a vector of backend strings and returns a vector of backendIDs.
79/// Removes duplicate entries.
80/// Can handle backend strings that contain multiple backends separated by comma e.g "CpuRef,CpuAcc"
81std::vector<armnn::BackendId> GetBackendIDs(const std::vector<std::string>& backendStringsVec)
Jan Eilers45274902020-10-15 18:34:43 +010082{
83 std::vector<armnn::BackendId> backendIDs;
Jan Eilersc5b84b52021-02-16 12:40:43 +000084 for (const auto& backendStrings : backendStringsVec)
Jan Eilers45274902020-10-15 18:34:43 +010085 {
Jan Eilersc5b84b52021-02-16 12:40:43 +000086 // Each backendStrings might contain multiple backends separated by comma e.g "CpuRef,CpuAcc"
87 std::vector<std::string> backendStringVec = ParseStringList(backendStrings, ",");
88 for (const auto& b : backendStringVec)
89 {
90 backendIDs.push_back(armnn::BackendId(b));
91 }
Jan Eilers45274902020-10-15 18:34:43 +010092 }
93
94 RemoveDuplicateDevices(backendIDs);
95
96 return backendIDs;
97}
98
99/// Provides a segfault safe way to get cxxopts option values by checking if the option was defined.
100/// If the option wasn't defined it returns an empty object.
101template<typename optionType>
102optionType GetOptionValue(std::string&& optionName, const cxxopts::ParseResult& result)
103{
104 optionType out;
105 if(result.count(optionName))
106 {
107 out = result[optionName].as<optionType>();
108 }
109 return out;
110}
111
112void LogAndThrowFatal(std::string errorMessage)
113{
114 throw armnn::InvalidArgumentException (errorMessage);
115}
116
117void CheckRequiredOptions(const cxxopts::ParseResult& result)
118{
119
120 // For each option in option-group "a) Required
121 std::vector<std::string> requiredOptions{"compute",
122 "model-format",
123 "model-path",
124 "input-name",
125 "output-name"};
126
127 bool requiredMissing = false;
128 for(auto const& str : requiredOptions)
129 {
130 if(!(result.count(str) > 0))
131 {
132 ARMNN_LOG(error) << fmt::format("The program option '{}' is mandatory but wasn't provided.", str);
133 requiredMissing = true;
134 }
135 }
136 if(requiredMissing)
137 {
138 throw armnn::InvalidArgumentException ("Some required arguments are missing");
139 }
140}
141
Jan Eilersf17fcd52021-07-26 22:20:00 +0100142void CheckForDeprecatedOptions(const cxxopts::ParseResult& result)
143{
144 if(result.count("simultaneous-iterations") > 0)
145 {
146 ARMNN_LOG(warning) << "DEPRECATED: The program option 'simultaneous-iterations' is deprecated and will be "
147 "removed soon. Please use the option 'iterations' combined with 'concurrent' instead.";
148 }
149 if(result.count("armnn-tflite-delegate") > 0)
150 {
151 ARMNN_LOG(warning) << "DEPRECATED: The program option 'armnn-tflite-delegate' is deprecated and will be "
152 "removed soon. Please use the option 'tflite-executor' instead.";
153 }
154}
155
Jan Eilers45274902020-10-15 18:34:43 +0100156void ProgramOptions::ValidateExecuteNetworkParams()
157{
158 m_ExNetParams.ValidateParams();
159}
160
161void ProgramOptions::ValidateRuntimeOptions()
162{
163 if (m_RuntimeOptions.m_ProfilingOptions.m_TimelineEnabled &&
164 !m_RuntimeOptions.m_ProfilingOptions.m_EnableProfiling)
165 {
166 LogAndThrowFatal("Timeline profiling requires external profiling to be turned on");
167 }
168}
169
170
171ProgramOptions::ProgramOptions() : m_CxxOptions{"ExecuteNetwork",
172 "Executes a neural network model using the provided input "
173 "tensor. Prints the resulting output tensor."}
174{
175 try
176 {
177 // cxxopts doesn't provide a mechanism to ensure required options are given. There is a
178 // separate function CheckRequiredOptions() for that.
179 m_CxxOptions.add_options("a) Required")
180 ("c,compute",
Jan Eilersc5b84b52021-02-16 12:40:43 +0000181 "Which device to run layers on by default. If a single device doesn't support all layers in the model "
182 "you can specify a second or third to fall back on. Possible choices: "
Jan Eilers45274902020-10-15 18:34:43 +0100183 + armnn::BackendRegistryInstance().GetBackendIdsAsString()
Jan Eilersc5b84b52021-02-16 12:40:43 +0000184 + " NOTE: Multiple compute devices need to be passed as a comma separated list without whitespaces "
185 "e.g. GpuAcc,CpuAcc,CpuRef or by repeating the program option e.g. '-c Cpuacc -c CpuRef'. "
186 "Duplicates are ignored.",
Jan Eilers3dda41d2020-11-11 11:44:14 +0000187 cxxopts::value<std::vector<std::string>>())
Jan Eilers45274902020-10-15 18:34:43 +0100188
189 ("f,model-format",
Nikhil Raj5d955cf2021-04-19 16:59:48 +0100190 "armnn-binary, onnx-binary, onnx-text, tflite-binary",
Jan Eilers45274902020-10-15 18:34:43 +0100191 cxxopts::value<std::string>())
192
193 ("m,model-path",
Nikhil Raj6dd178f2021-04-02 22:04:39 +0100194 "Path to model file, e.g. .armnn, , .prototxt, .tflite, .onnx",
Jan Eilers45274902020-10-15 18:34:43 +0100195 cxxopts::value<std::string>(m_ExNetParams.m_ModelPath))
196
197 ("i,input-name",
198 "Identifier of the input tensors in the network separated by comma.",
199 cxxopts::value<std::string>())
200
201 ("o,output-name",
202 "Identifier of the output tensors in the network separated by comma.",
203 cxxopts::value<std::string>());
204
205 m_CxxOptions.add_options("b) General")
206 ("b,dynamic-backends-path",
207 "Path where to load any available dynamic backend from. "
208 "If left empty (the default), dynamic backends will not be used.",
209 cxxopts::value<std::string>(m_RuntimeOptions.m_DynamicBackendsPath))
210
Sadik Armagana04a9d72021-04-27 10:02:10 +0100211 ("n,concurrent",
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100212 "This option is for Arm NN internal asynchronous testing purposes. "
Jan Eilersf17fcd52021-07-26 22:20:00 +0100213 "False by default. If set to true will use std::launch::async or the Arm NN thread pool, "
214 "if 'thread-pool-size' is greater than 0, for asynchronous execution.",
Sadik Armagana04a9d72021-04-27 10:02:10 +0100215 cxxopts::value<bool>(m_ExNetParams.m_Concurrent)->default_value("false")->implicit_value("true"))
216
Jan Eilers45274902020-10-15 18:34:43 +0100217 ("d,input-tensor-data",
218 "Path to files containing the input data as a flat array separated by whitespace. "
Jan Eilersf17fcd52021-07-26 22:20:00 +0100219 "Several paths can be passed by separating them with a comma if the network has multiple inputs "
220 "or you wish to run the model multiple times with different input data using the 'iterations' option. "
221 "If not specified, the network will be run with dummy data (useful for profiling).",
Jan Eilers45274902020-10-15 18:34:43 +0100222 cxxopts::value<std::string>()->default_value(""))
223
224 ("h,help", "Display usage information")
225
226 ("infer-output-shape",
227 "Infers output tensor shape from input tensor shape and validate where applicable (where supported by "
228 "parser)",
229 cxxopts::value<bool>(m_ExNetParams.m_InferOutputShape)->default_value("false")->implicit_value("true"))
230
231 ("iterations",
Jan Eilersf17fcd52021-07-26 22:20:00 +0100232 "Number of iterations to run the network for, default is set to 1. "
233 "If you wish to run the model with different input data for every execution you can do so by "
234 "supplying more input file paths to the 'input-tensor-data' option. "
235 "Note: The number of input files provided must be divisible by the number of inputs of the model. "
236 "e.g. Your model has 2 inputs and you supply 4 input files. If you set 'iterations' to 6 the first "
237 "run will consume the first two inputs, the second the next two and the last will begin from the "
238 "start and use the first two inputs again. "
239 "Note: If the 'concurrent' option is enabled all iterations will be run asynchronously.",
Jan Eilers45274902020-10-15 18:34:43 +0100240 cxxopts::value<size_t>(m_ExNetParams.m_Iterations)->default_value("1"))
241
242 ("l,dequantize-output",
243 "If this option is enabled, all quantized outputs will be dequantized to float. "
244 "If unset, default to not get dequantized. "
Colm Donelan3cff15a2021-10-12 15:06:19 +0100245 "Accepted values (true or false)"
246 " (Not available when executing ArmNNTfLiteDelegate or TfliteInterpreter)",
Jan Eilers45274902020-10-15 18:34:43 +0100247 cxxopts::value<bool>(m_ExNetParams.m_DequantizeOutput)->default_value("false")->implicit_value("true"))
248
249 ("p,print-intermediate-layers",
250 "If this option is enabled, the output of every graph layer will be printed.",
251 cxxopts::value<bool>(m_ExNetParams.m_PrintIntermediate)->default_value("false")
252 ->implicit_value("true"))
253
254 ("parse-unsupported",
255 "Add unsupported operators as stand-in layers (where supported by parser)",
256 cxxopts::value<bool>(m_ExNetParams.m_ParseUnsupported)->default_value("false")->implicit_value("true"))
257
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100258 ("N,do-not-print-output",
Jan Eilers284b5d12021-09-07 12:46:15 +0100259 "The default behaviour of ExecuteNetwork is to print the resulting outputs on the console. "
260 "This behaviour can be changed by adding this flag to your command.",
261 cxxopts::value<bool>(m_ExNetParams.m_DontPrintOutputs)->default_value("false")->implicit_value("true"))
262
Jan Eilers45274902020-10-15 18:34:43 +0100263 ("q,quantize-input",
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100264 "If this option is enabled, all float inputs will be quantized as appropriate for the model's inputs. "
Colm Donelan3cff15a2021-10-12 15:06:19 +0100265 "If unset, default to not quantized. Accepted values (true or false)"
266 " (Not available when executing ArmNNTfLiteDelegate or TfliteInterpreter)",
Jan Eilers45274902020-10-15 18:34:43 +0100267 cxxopts::value<bool>(m_ExNetParams.m_QuantizeInput)->default_value("false")->implicit_value("true"))
Jan Eilers45274902020-10-15 18:34:43 +0100268 ("r,threshold-time",
269 "Threshold time is the maximum allowed time for inference measured in milliseconds. If the actual "
270 "inference time is greater than the threshold time, the test will fail. By default, no threshold "
271 "time is used.",
272 cxxopts::value<double>(m_ExNetParams.m_ThresholdTime)->default_value("0.0"))
273
274 ("s,input-tensor-shape",
275 "The shape of the input tensors in the network as a flat array of integers separated by comma."
276 "Several shapes can be passed by separating them with a colon (:).",
277 cxxopts::value<std::string>())
278
279 ("v,visualize-optimized-model",
280 "Enables built optimized model visualizer. If unset, defaults to off.",
281 cxxopts::value<bool>(m_ExNetParams.m_EnableLayerDetails)->default_value("false")
282 ->implicit_value("true"))
283
284 ("w,write-outputs-to-file",
285 "Comma-separated list of output file paths keyed with the binding-id of the output slot. "
286 "If left empty (the default), the output tensors will not be written to a file.",
287 cxxopts::value<std::string>())
288
289 ("x,subgraph-number",
Colm Donelan3cff15a2021-10-12 15:06:19 +0100290 "Id of the subgraph to be executed. Defaults to 0."
291 " (Not available when executing ArmNNTfLiteDelegate or TfliteInterpreter)",
Jan Eilers45274902020-10-15 18:34:43 +0100292 cxxopts::value<size_t>(m_ExNetParams.m_SubgraphId)->default_value("0"))
293
294 ("y,input-type",
295 "The type of the input tensors in the network separated by comma. "
296 "If unset, defaults to \"float\" for all defined inputs. "
David Monahan67cc5fc2021-11-03 12:56:41 +0000297 "Accepted values (float, int, qasymms8 or qasymmu8).",
Jan Eilers45274902020-10-15 18:34:43 +0100298 cxxopts::value<std::string>())
299
300 ("z,output-type",
301 "The type of the output tensors in the network separated by comma. "
302 "If unset, defaults to \"float\" for all defined outputs. "
David Monahan67cc5fc2021-11-03 12:56:41 +0000303 "Accepted values (float, int, qasymms8 or qasymmu8).",
Finn Williamsf806c4d2021-02-22 15:13:12 +0000304 cxxopts::value<std::string>())
305
306 ("T,tflite-executor",
307 "Set the executor for the tflite model: parser, delegate, tflite"
308 "parser is the ArmNNTfLiteParser, "
309 "delegate is the ArmNNTfLiteDelegate, "
310 "tflite is the TfliteInterpreter",
311 cxxopts::value<std::string>()->default_value("parser"))
312
313 ("D,armnn-tflite-delegate",
314 "Enable Arm NN TfLite delegate. "
Jan Eilersf17fcd52021-07-26 22:20:00 +0100315 "DEPRECATED: This option is deprecated please use tflite-executor instead",
Sadik Armagana04a9d72021-04-27 10:02:10 +0100316 cxxopts::value<bool>(m_ExNetParams.m_EnableDelegate)->default_value("false")->implicit_value("true"))
317
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100318 ("simultaneous-iterations",
319 "Number of simultaneous iterations to async-run the network for, default is set to 1 (disabled). "
Jan Eilersf17fcd52021-07-26 22:20:00 +0100320 "When thread-pool-size is set the Arm NN thread pool is used. Otherwise std::launch::async is used."
321 "DEPRECATED: This option is deprecated and will be removed soon. "
322 "Please use the option 'iterations' combined with 'concurrent' instead.",
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100323 cxxopts::value<size_t>(m_ExNetParams.m_SimultaneousIterations)->default_value("1"))
324
325 ("thread-pool-size",
326 "Number of Arm NN threads to use when running the network asynchronously via the Arm NN thread pool. "
Jan Eilersf17fcd52021-07-26 22:20:00 +0100327 "The default is set to 0 which equals disabled. If 'thread-pool-size' is greater than 0 the "
328 "'concurrent' option is automatically set to true.",
Kevin May94dd4db2021-05-26 16:01:08 +0100329 cxxopts::value<size_t>(m_ExNetParams.m_ThreadPoolSize)->default_value("0"));
Jan Eilers45274902020-10-15 18:34:43 +0100330
331 m_CxxOptions.add_options("c) Optimization")
332 ("bf16-turbo-mode",
333 "If this option is enabled, FP32 layers, "
334 "weights and biases will be converted to BFloat16 where the backend supports it",
335 cxxopts::value<bool>(m_ExNetParams.m_EnableBf16TurboMode)
336 ->default_value("false")->implicit_value("true"))
337
338 ("enable-fast-math",
339 "Enables fast_math options in backends that support it. Using the fast_math flag can lead to "
340 "performance improvements but may result in reduced or different precision.",
341 cxxopts::value<bool>(m_ExNetParams.m_EnableFastMath)->default_value("false")->implicit_value("true"))
342
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000343 ("number-of-threads",
344 "Assign the number of threads used by the CpuAcc backend. "
345 "Input value must be between 1 and 64. "
346 "Default is set to 0 (Backend will decide number of threads to use).",
347 cxxopts::value<unsigned int>(m_ExNetParams.m_NumberOfThreads)->default_value("0"))
348
Matthew Sloyan42432112021-01-08 10:30:51 +0000349 ("save-cached-network",
Matthew Sloyan9d7a3322021-01-12 16:19:43 +0000350 "Enables saving of the cached network to a file given with the cached-network-filepath option. "
Matthew Sloyan42432112021-01-08 10:30:51 +0000351 "See also --cached-network-filepath",
352 cxxopts::value<bool>(m_ExNetParams.m_SaveCachedNetwork)
353 ->default_value("false")->implicit_value("true"))
354
355 ("cached-network-filepath",
Matthew Sloyan9d7a3322021-01-12 16:19:43 +0000356 "If non-empty, the given file will be used to load/save the cached network. "
357 "If save-cached-network is given then the cached network will be saved to the given file. "
358 "To save the cached network a file must already exist. "
359 "If save-cached-network is not given then the cached network will be loaded from the given file. "
360 "This will remove initial compilation time of kernels and speed up the first execution.",
Matthew Sloyan42432112021-01-08 10:30:51 +0000361 cxxopts::value<std::string>(m_ExNetParams.m_CachedNetworkFilePath)->default_value(""))
362
Jan Eilers45274902020-10-15 18:34:43 +0100363 ("fp16-turbo-mode",
364 "If this option is enabled, FP32 layers, "
365 "weights and biases will be converted to FP16 where the backend supports it",
366 cxxopts::value<bool>(m_ExNetParams.m_EnableFp16TurboMode)
367 ->default_value("false")->implicit_value("true"))
368
369 ("tuning-level",
370 "Sets the tuning level which enables a tuning run which will update/create a tuning file. "
371 "Available options are: 1 (Rapid), 2 (Normal), 3 (Exhaustive). "
372 "Requires tuning-path to be set, default is set to 0 (No tuning run)",
373 cxxopts::value<int>(m_ExNetParams.m_TuningLevel)->default_value("0"))
374
375 ("tuning-path",
376 "Path to tuning file. Enables use of CL tuning",
Finn Williams40646322021-02-11 16:16:42 +0000377 cxxopts::value<std::string>(m_ExNetParams.m_TuningPath))
378
379 ("MLGOTuningFilePath",
380 "Path to tuning file. Enables use of CL MLGO tuning",
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100381 cxxopts::value<std::string>(m_ExNetParams.m_MLGOTuningFilePath))
382
383 ("R, reuse-buffers",
384 "If enabled then the IO buffers will be reused for each inference",
385 cxxopts::value<bool>(m_ExNetParams.m_ReuseBuffers)->default_value("false")->implicit_value("true"));
Jan Eilers45274902020-10-15 18:34:43 +0100386
387 m_CxxOptions.add_options("d) Profiling")
388 ("a,enable-external-profiling",
389 "If enabled external profiling will be switched on",
390 cxxopts::value<bool>(m_RuntimeOptions.m_ProfilingOptions.m_EnableProfiling)
391 ->default_value("false")->implicit_value("true"))
392
393 ("e,event-based-profiling",
394 "Enables built in profiler. If unset, defaults to off.",
395 cxxopts::value<bool>(m_ExNetParams.m_EnableProfiling)->default_value("false")->implicit_value("true"))
396
397 ("g,file-only-external-profiling",
398 "If enabled then the 'file-only' test mode of external profiling will be enabled",
399 cxxopts::value<bool>(m_RuntimeOptions.m_ProfilingOptions.m_FileOnly)
400 ->default_value("false")->implicit_value("true"))
401
402 ("file-format",
403 "If profiling is enabled specifies the output file format",
404 cxxopts::value<std::string>(m_RuntimeOptions.m_ProfilingOptions.m_FileFormat)->default_value("binary"))
405
406 ("j,outgoing-capture-file",
407 "If specified the outgoing external profiling packets will be captured in this binary file",
408 cxxopts::value<std::string>(m_RuntimeOptions.m_ProfilingOptions.m_OutgoingCaptureFile))
409
410 ("k,incoming-capture-file",
411 "If specified the incoming external profiling packets will be captured in this binary file",
412 cxxopts::value<std::string>(m_RuntimeOptions.m_ProfilingOptions.m_IncomingCaptureFile))
413
414 ("timeline-profiling",
415 "If enabled timeline profiling will be switched on, requires external profiling",
416 cxxopts::value<bool>(m_RuntimeOptions.m_ProfilingOptions.m_TimelineEnabled)
417 ->default_value("false")->implicit_value("true"))
418
419 ("u,counter-capture-period",
420 "If profiling is enabled in 'file-only' mode this is the capture period that will be used in the test",
Keith Davisf4874862021-08-09 16:49:18 +0100421 cxxopts::value<uint32_t>(m_RuntimeOptions.m_ProfilingOptions.m_CapturePeriod)->default_value("150"))
422
423 ("output-network-details",
Keith Davis4914d0c2021-08-18 17:14:05 +0100424 "Outputs layer tensor infos and descriptors to std out along with profiling events. Defaults to off.",
Keith Davisf4874862021-08-09 16:49:18 +0100425 cxxopts::value<bool>(m_ExNetParams.m_OutputDetailsToStdOut)->default_value("false")
Keith Davis4914d0c2021-08-18 17:14:05 +0100426 ->implicit_value("true"))
427 ("output-network-details-only",
428 "Outputs layer tensor infos and descriptors to std out without profiling events. Defaults to off.",
429 cxxopts::value<bool>(m_ExNetParams.m_OutputDetailsOnlyToStdOut)->default_value("false")
Jim Flynn15425812022-02-15 16:53:13 +0000430 ->implicit_value("true"))
Keith Davis4914d0c2021-08-18 17:14:05 +0100431
Jim Flynn15425812022-02-15 16:53:13 +0000432 ("import-inputs-if-aligned",
433 "In & Out tensors will be imported per inference if the memory alignment allows. Defaults to false.",
434 cxxopts::value<bool>(m_ExNetParams.m_ImportInputsIfAligned)->default_value("false")
435 ->implicit_value("true"));
Jan Eilers45274902020-10-15 18:34:43 +0100436 }
437 catch (const std::exception& e)
438 {
439 ARMNN_ASSERT_MSG(false, "Caught unexpected exception");
440 ARMNN_LOG(fatal) << "Fatal internal error: " << e.what();
441 exit(EXIT_FAILURE);
442 }
443}
444
445ProgramOptions::ProgramOptions(int ac, const char* av[]): ProgramOptions()
446{
447 ParseOptions(ac, av);
448}
449
450void ProgramOptions::ParseOptions(int ac, const char* av[])
451{
452 // Parses the command-line.
453 m_CxxResult = m_CxxOptions.parse(ac, av);
454
455 if (m_CxxResult.count("help") || ac <= 1)
456 {
457 std::cout << m_CxxOptions.help() << std::endl;
458 exit(EXIT_SUCCESS);
459 }
460
461 CheckRequiredOptions(m_CxxResult);
462 CheckOptionDependencies(m_CxxResult);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100463 CheckForDeprecatedOptions(m_CxxResult);
Jan Eilers45274902020-10-15 18:34:43 +0100464
465 // Some options can't be assigned directly because they need some post-processing:
Jan Eilers3dda41d2020-11-11 11:44:14 +0000466 auto computeDevices = GetOptionValue<std::vector<std::string>>("compute", m_CxxResult);
467 m_ExNetParams.m_ComputeDevices = GetBackendIDs(computeDevices);
Jan Eilers45274902020-10-15 18:34:43 +0100468 m_ExNetParams.m_ModelFormat =
469 armnn::stringUtils::StringTrimCopy(GetOptionValue<std::string>("model-format", m_CxxResult));
470 m_ExNetParams.m_InputNames =
471 ParseStringList(GetOptionValue<std::string>("input-name", m_CxxResult), ",");
472 m_ExNetParams.m_InputTensorDataFilePaths =
473 ParseStringList(GetOptionValue<std::string>("input-tensor-data", m_CxxResult), ",");
474 m_ExNetParams.m_OutputNames =
475 ParseStringList(GetOptionValue<std::string>("output-name", m_CxxResult), ",");
476 m_ExNetParams.m_InputTypes =
477 ParseStringList(GetOptionValue<std::string>("input-type", m_CxxResult), ",");
478 m_ExNetParams.m_OutputTypes =
479 ParseStringList(GetOptionValue<std::string>("output-type", m_CxxResult), ",");
480 m_ExNetParams.m_OutputTensorFiles =
481 ParseStringList(GetOptionValue<std::string>("write-outputs-to-file", m_CxxResult), ",");
482 m_ExNetParams.m_GenerateTensorData =
483 m_ExNetParams.m_InputTensorDataFilePaths.empty();
Francis Murtaghbf18a262020-10-27 15:20:40 +0000484 m_ExNetParams.m_DynamicBackendsPath = m_RuntimeOptions.m_DynamicBackendsPath;
Jan Eilers45274902020-10-15 18:34:43 +0100485
Sadik Armagan8c7a28b2021-04-01 17:27:21 +0100486 m_RuntimeOptions.m_EnableGpuProfiling = m_ExNetParams.m_EnableProfiling;
Finn Williamsf806c4d2021-02-22 15:13:12 +0000487
488 std::string tfliteExecutor = GetOptionValue<std::string>("tflite-executor", m_CxxResult);
489
490 if (tfliteExecutor.size() == 0 || tfliteExecutor == "parser")
491 {
492 m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser;
493 }
494 else if (tfliteExecutor == "delegate")
495 {
496 m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate;
497 }
498 else if (tfliteExecutor == "tflite")
499 {
500 m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter;
501 }
502 else
503 {
504 ARMNN_LOG(info) << fmt::format("Invalid tflite-executor option '{}'.", tfliteExecutor);
505 throw armnn::InvalidArgumentException ("Invalid tflite-executor option");
506 }
507
Jan Eilersf17fcd52021-07-26 22:20:00 +0100508 // For backwards compatibility when deprecated options are used
Finn Williamsf806c4d2021-02-22 15:13:12 +0000509 if (m_ExNetParams.m_EnableDelegate)
510 {
511 m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100512 }
513 if (m_ExNetParams.m_SimultaneousIterations > 1)
514 {
515 m_ExNetParams.m_Iterations = m_ExNetParams.m_SimultaneousIterations;
516 m_ExNetParams.m_Concurrent = true;
Finn Williamsf806c4d2021-02-22 15:13:12 +0000517 }
518
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100519 // Set concurrent to true if the user expects to run inferences asynchronously
Jan Eilersf17fcd52021-07-26 22:20:00 +0100520 if (m_ExNetParams.m_ThreadPoolSize > 0)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100521 {
522 m_ExNetParams.m_Concurrent = true;
523 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000524
Jan Eilers45274902020-10-15 18:34:43 +0100525 // Parse input tensor shape from the string we got from the command-line.
526 std::vector<std::string> inputTensorShapesVector =
527 ParseStringList(GetOptionValue<std::string>("input-tensor-shape", m_CxxResult), ":");
528
529 if (!inputTensorShapesVector.empty())
530 {
531 m_ExNetParams.m_InputTensorShapes.reserve(inputTensorShapesVector.size());
532
533 for(const std::string& shape : inputTensorShapesVector)
534 {
535 std::stringstream ss(shape);
536 std::vector<unsigned int> dims = ParseArray(ss);
537
538 m_ExNetParams.m_InputTensorShapes.push_back(
539 std::make_unique<armnn::TensorShape>(static_cast<unsigned int>(dims.size()), dims.data()));
540 }
541 }
542
543 // We have to validate ExecuteNetworkParams first so that the tuning path and level is validated
544 ValidateExecuteNetworkParams();
545
546 // Parse CL tuning parameters to runtime options
547 if (!m_ExNetParams.m_TuningPath.empty())
548 {
549 m_RuntimeOptions.m_BackendOptions.emplace_back(
550 armnn::BackendOptions
551 {
552 "GpuAcc",
553 {
554 {"TuningLevel", m_ExNetParams.m_TuningLevel},
555 {"TuningFile", m_ExNetParams.m_TuningPath.c_str()},
Finn Williams40646322021-02-11 16:16:42 +0000556 {"KernelProfilingEnabled", m_ExNetParams.m_EnableProfiling},
557 {"MLGOTuningFilePath", m_ExNetParams.m_MLGOTuningFilePath}
Jan Eilers45274902020-10-15 18:34:43 +0100558 }
559 }
560 );
561 }
562
563 ValidateRuntimeOptions();
564}
565