blob: 440dcf9aa837fc7d3f639c379b37ce7ba02014e4 [file] [log] [blame]
Francis Murtaghbee4bc92019-06-18 12:30:37 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include <armnn/ArmNN.hpp>
6#include <armnn/TypesUtils.hpp>
7
8#if defined(ARMNN_SERIALIZER)
9#include "armnnDeserializer/IDeserializer.hpp"
10#endif
11#if defined(ARMNN_CAFFE_PARSER)
12#include "armnnCaffeParser/ICaffeParser.hpp"
13#endif
14#if defined(ARMNN_TF_PARSER)
15#include "armnnTfParser/ITfParser.hpp"
16#endif
17#if defined(ARMNN_TF_LITE_PARSER)
18#include "armnnTfLiteParser/ITfLiteParser.hpp"
19#endif
20#if defined(ARMNN_ONNX_PARSER)
21#include "armnnOnnxParser/IOnnxParser.hpp"
22#endif
23#include "CsvReader.hpp"
24#include "../InferenceTest.hpp"
25
26#include <Logging.hpp>
27#include <Profiling.hpp>
28
29#include <boost/algorithm/string/trim.hpp>
30#include <boost/algorithm/string/split.hpp>
31#include <boost/algorithm/string/classification.hpp>
32#include <boost/program_options.hpp>
33#include <boost/variant.hpp>
34
35#include <iostream>
36#include <fstream>
37#include <functional>
38#include <future>
39#include <algorithm>
40#include <iterator>
41
42namespace
43{
44
45// Configure boost::program_options for command-line parsing and validation.
46namespace po = boost::program_options;
47
48template<typename T, typename TParseElementFunc>
49std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char * chars = "\t ,:")
50{
51 std::vector<T> result;
52 // Processes line-by-line.
53 std::string line;
54 while (std::getline(stream, line))
55 {
56 std::vector<std::string> tokens;
57 try
58 {
59 // Coverity fix: boost::split() may throw an exception of type boost::bad_function_call.
60 boost::split(tokens, line, boost::algorithm::is_any_of(chars), boost::token_compress_on);
61 }
62 catch (const std::exception& e)
63 {
64 BOOST_LOG_TRIVIAL(error) << "An error occurred when splitting tokens: " << e.what();
65 continue;
66 }
67 for (const std::string& token : tokens)
68 {
69 if (!token.empty()) // See https://stackoverflow.com/questions/10437406/
70 {
71 try
72 {
73 result.push_back(parseElementFunc(token));
74 }
75 catch (const std::exception&)
76 {
77 BOOST_LOG_TRIVIAL(error) << "'" << token << "' is not a valid number. It has been ignored.";
78 }
79 }
80 }
81 }
82
83 return result;
84}
85
86bool CheckOption(const po::variables_map& vm,
87 const char* option)
88{
89 // Check that the given option is valid.
90 if (option == nullptr)
91 {
92 return false;
93 }
94
95 // Check whether 'option' is provided.
96 return vm.find(option) != vm.end();
97}
98
99void CheckOptionDependency(const po::variables_map& vm,
100 const char* option,
101 const char* required)
102{
103 // Check that the given options are valid.
104 if (option == nullptr || required == nullptr)
105 {
106 throw po::error("Invalid option to check dependency for");
107 }
108
109 // Check that if 'option' is provided, 'required' is also provided.
110 if (CheckOption(vm, option) && !vm[option].defaulted())
111 {
112 if (CheckOption(vm, required) == 0 || vm[required].defaulted())
113 {
114 throw po::error(std::string("Option '") + option + "' requires option '" + required + "'.");
115 }
116 }
117}
118
119void CheckOptionDependencies(const po::variables_map& vm)
120{
121 CheckOptionDependency(vm, "model-path", "model-format");
122 CheckOptionDependency(vm, "model-path", "input-name");
123 CheckOptionDependency(vm, "model-path", "input-tensor-data");
124 CheckOptionDependency(vm, "model-path", "output-name");
125 CheckOptionDependency(vm, "input-tensor-shape", "model-path");
126}
127
128template<armnn::DataType NonQuantizedType>
129auto ParseDataArray(std::istream & stream);
130
131template<armnn::DataType QuantizedType>
132auto ParseDataArray(std::istream& stream,
133 const float& quantizationScale,
134 const int32_t& quantizationOffset);
135
136template<>
137auto ParseDataArray<armnn::DataType::Float32>(std::istream & stream)
138{
139 return ParseArrayImpl<float>(stream, [](const std::string& s) { return std::stof(s); });
140}
141
142template<>
143auto ParseDataArray<armnn::DataType::Signed32>(std::istream & stream)
144{
145 return ParseArrayImpl<int>(stream, [](const std::string & s) { return std::stoi(s); });
146}
147
148template<>
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100149auto ParseDataArray<armnn::DataType::QuantisedAsymm8>(std::istream& stream)
150{
151 return ParseArrayImpl<uint8_t>(stream,
152 [](const std::string& s) { return boost::numeric_cast<uint8_t>(std::stoi(s)); });
153}
154
155template<>
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100156auto ParseDataArray<armnn::DataType::QuantisedAsymm8>(std::istream& stream,
157 const float& quantizationScale,
158 const int32_t& quantizationOffset)
159{
160 return ParseArrayImpl<uint8_t>(stream,
161 [&quantizationScale, &quantizationOffset](const std::string & s)
162 {
163 return boost::numeric_cast<uint8_t>(
164 armnn::Quantize<u_int8_t>(std::stof(s),
165 quantizationScale,
166 quantizationOffset));
167 });
168}
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100169std::vector<unsigned int> ParseArray(std::istream& stream)
170{
171 return ParseArrayImpl<unsigned int>(stream,
172 [](const std::string& s) { return boost::numeric_cast<unsigned int>(std::stoi(s)); });
173}
174
175std::vector<std::string> ParseStringList(const std::string & inputString, const char * delimiter)
176{
177 std::stringstream stream(inputString);
178 return ParseArrayImpl<std::string>(stream, [](const std::string& s) { return boost::trim_copy(s); }, delimiter);
179}
180
181void RemoveDuplicateDevices(std::vector<armnn::BackendId>& computeDevices)
182{
183 // Mark the duplicate devices as 'Undefined'.
184 for (auto i = computeDevices.begin(); i != computeDevices.end(); ++i)
185 {
186 for (auto j = std::next(i); j != computeDevices.end(); ++j)
187 {
188 if (*j == *i)
189 {
190 *j = armnn::Compute::Undefined;
191 }
192 }
193 }
194
195 // Remove 'Undefined' devices.
196 computeDevices.erase(std::remove(computeDevices.begin(), computeDevices.end(), armnn::Compute::Undefined),
197 computeDevices.end());
198}
199
200struct TensorPrinter : public boost::static_visitor<>
201{
202 TensorPrinter(const std::string& binding, const armnn::TensorInfo& info)
203 : m_OutputBinding(binding)
204 , m_Scale(info.GetQuantizationScale())
205 , m_Offset(info.GetQuantizationOffset())
206 {}
207
208 void operator()(const std::vector<float>& values)
209 {
210 ForEachValue(values, [](float value){
211 printf("%f ", value);
212 });
213 }
214
215 void operator()(const std::vector<uint8_t>& values)
216 {
217 auto& scale = m_Scale;
218 auto& offset = m_Offset;
219 ForEachValue(values, [&scale, &offset](uint8_t value)
220 {
221 printf("%f ", armnn::Dequantize(value, scale, offset));
222 });
223 }
224
225 void operator()(const std::vector<int>& values)
226 {
227 ForEachValue(values, [](int value)
228 {
229 printf("%d ", value);
230 });
231 }
232
233private:
234 template<typename Container, typename Delegate>
235 void ForEachValue(const Container& c, Delegate delegate)
236 {
237 std::cout << m_OutputBinding << ": ";
238 for (const auto& value : c)
239 {
240 delegate(value);
241 }
242 printf("\n");
243 }
244
245 std::string m_OutputBinding;
246 float m_Scale=0.0f;
247 int m_Offset=0;
248};
249
250
251} // namespace
252
253template<typename TParser, typename TDataType>
254int MainImpl(const char* modelPath,
255 bool isModelBinary,
256 const std::vector<armnn::BackendId>& computeDevices,
257 const std::vector<string>& inputNames,
258 const std::vector<std::unique_ptr<armnn::TensorShape>>& inputTensorShapes,
259 const std::vector<string>& inputTensorDataFilePaths,
260 const std::vector<string>& inputTypes,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100261 bool quantizeInput,
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100262 const std::vector<string>& outputTypes,
263 const std::vector<string>& outputNames,
264 bool enableProfiling,
265 bool enableFp16TurboMode,
266 const double& thresholdTime,
267 const size_t subgraphId,
268 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
269{
270 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
271
272 std::vector<TContainer> inputDataContainers;
273
274 try
275 {
276 // Creates an InferenceModel, which will parse the model and load it into an IRuntime.
277 typename InferenceModel<TParser, TDataType>::Params params;
278 params.m_ModelPath = modelPath;
279 params.m_IsModelBinary = isModelBinary;
280 params.m_ComputeDevices = computeDevices;
281
282 for(const std::string& inputName: inputNames)
283 {
284 params.m_InputBindings.push_back(inputName);
285 }
286
287 for(unsigned int i = 0; i < inputTensorShapes.size(); ++i)
288 {
289 params.m_InputShapes.push_back(*inputTensorShapes[i]);
290 }
291
292 for(const std::string& outputName: outputNames)
293 {
294 params.m_OutputBindings.push_back(outputName);
295 }
296
297 params.m_SubgraphId = subgraphId;
298 params.m_EnableFp16TurboMode = enableFp16TurboMode;
299 InferenceModel<TParser, TDataType> model(params, enableProfiling, runtime);
300
301 for(unsigned int i = 0; i < inputTensorDataFilePaths.size(); ++i)
302 {
303 std::ifstream inputTensorFile(inputTensorDataFilePaths[i]);
304
305 if (inputTypes[i].compare("float") == 0)
306 {
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100307 if (quantizeInput)
308 {
309 auto inputBinding = model.GetInputBindingInfo();
310 inputDataContainers.push_back(
311 ParseDataArray<armnn::DataType::QuantisedAsymm8>(inputTensorFile,
312 inputBinding.second.GetQuantizationScale(),
313 inputBinding.second.GetQuantizationOffset()));
314 }
315 else
316 {
317 inputDataContainers.push_back(
318 ParseDataArray<armnn::DataType::Float32>(inputTensorFile));
319 }
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100320 }
321 else if (inputTypes[i].compare("int") == 0)
322 {
323 inputDataContainers.push_back(
324 ParseDataArray<armnn::DataType::Signed32>(inputTensorFile));
325 }
326 else if (inputTypes[i].compare("qasymm8") == 0)
327 {
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100328 inputDataContainers.push_back(
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100329 ParseDataArray<armnn::DataType::QuantisedAsymm8>(inputTensorFile));
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100330 }
331 else
332 {
333 BOOST_LOG_TRIVIAL(fatal) << "Unsupported tensor data type \"" << inputTypes[i] << "\". ";
334 return EXIT_FAILURE;
335 }
336
337 inputTensorFile.close();
338 }
339
340 const size_t numOutputs = params.m_OutputBindings.size();
341 std::vector<TContainer> outputDataContainers;
342
343 for (unsigned int i = 0; i < numOutputs; ++i)
344 {
345 if (outputTypes[i].compare("float") == 0)
346 {
347 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
348 }
349 else if (outputTypes[i].compare("int") == 0)
350 {
351 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
352 }
353 else if (outputTypes[i].compare("qasymm8") == 0)
354 {
355 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
356 }
357 else
358 {
359 BOOST_LOG_TRIVIAL(fatal) << "Unsupported tensor data type \"" << outputTypes[i] << "\". ";
360 return EXIT_FAILURE;
361 }
362 }
363
364 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
365 auto inference_duration = model.Run(inputDataContainers, outputDataContainers);
366
367 // Print output tensors
368 const auto& infosOut = model.GetOutputBindingInfos();
369 for (size_t i = 0; i < numOutputs; i++)
370 {
371 const armnn::TensorInfo& infoOut = infosOut[i].second;
372 TensorPrinter printer(params.m_OutputBindings[i], infoOut);
373 boost::apply_visitor(printer, outputDataContainers[i]);
374 }
375
376 BOOST_LOG_TRIVIAL(info) << "\nInference time: " << std::setprecision(2)
377 << std::fixed << inference_duration.count() << " ms";
378
379 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
380 if (thresholdTime != 0.0)
381 {
382 BOOST_LOG_TRIVIAL(info) << "Threshold time: " << std::setprecision(2)
383 << std::fixed << thresholdTime << " ms";
384 auto thresholdMinusInference = thresholdTime - inference_duration.count();
385 BOOST_LOG_TRIVIAL(info) << "Threshold time - Inference time: " << std::setprecision(2)
386 << std::fixed << thresholdMinusInference << " ms" << "\n";
387
388 if (thresholdMinusInference < 0)
389 {
390 BOOST_LOG_TRIVIAL(fatal) << "Elapsed inference time is greater than provided threshold time.\n";
391 return EXIT_FAILURE;
392 }
393 }
394
395
396 }
397 catch (armnn::Exception const& e)
398 {
399 BOOST_LOG_TRIVIAL(fatal) << "Armnn Error: " << e.what();
400 return EXIT_FAILURE;
401 }
402
403 return EXIT_SUCCESS;
404}
405
406// This will run a test
407int RunTest(const std::string& format,
408 const std::string& inputTensorShapesStr,
409 const vector<armnn::BackendId>& computeDevice,
410 const std::string& path,
411 const std::string& inputNames,
412 const std::string& inputTensorDataFilePaths,
413 const std::string& inputTypes,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100414 bool quantizeInput,
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100415 const std::string& outputTypes,
416 const std::string& outputNames,
417 bool enableProfiling,
418 bool enableFp16TurboMode,
419 const double& thresholdTime,
420 const size_t subgraphId,
421 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
422{
423 std::string modelFormat = boost::trim_copy(format);
424 std::string modelPath = boost::trim_copy(path);
425 std::vector<std::string> inputNamesVector = ParseStringList(inputNames, ",");
426 std::vector<std::string> inputTensorShapesVector = ParseStringList(inputTensorShapesStr, ";");
427 std::vector<std::string> inputTensorDataFilePathsVector = ParseStringList(
428 inputTensorDataFilePaths, ",");
429 std::vector<std::string> outputNamesVector = ParseStringList(outputNames, ",");
430 std::vector<std::string> inputTypesVector = ParseStringList(inputTypes, ",");
431 std::vector<std::string> outputTypesVector = ParseStringList(outputTypes, ",");
432
433 // Parse model binary flag from the model-format string we got from the command-line
434 bool isModelBinary;
435 if (modelFormat.find("bin") != std::string::npos)
436 {
437 isModelBinary = true;
438 }
439 else if (modelFormat.find("txt") != std::string::npos || modelFormat.find("text") != std::string::npos)
440 {
441 isModelBinary = false;
442 }
443 else
444 {
445 BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Please include 'binary' or 'text'";
446 return EXIT_FAILURE;
447 }
448
449 if ((inputTensorShapesVector.size() != 0) && (inputTensorShapesVector.size() != inputNamesVector.size()))
450 {
451 BOOST_LOG_TRIVIAL(fatal) << "input-name and input-tensor-shape must have the same amount of elements.";
452 return EXIT_FAILURE;
453 }
454
455 if ((inputTensorDataFilePathsVector.size() != 0) &&
456 (inputTensorDataFilePathsVector.size() != inputNamesVector.size()))
457 {
458 BOOST_LOG_TRIVIAL(fatal) << "input-name and input-tensor-data must have the same amount of elements.";
459 return EXIT_FAILURE;
460 }
461
462 if (inputTypesVector.size() == 0)
463 {
464 //Defaults the value of all inputs to "float"
465 inputTypesVector.assign(inputNamesVector.size(), "float");
466 }
467 if (outputTypesVector.size() == 0)
468 {
469 //Defaults the value of all outputs to "float"
470 outputTypesVector.assign(outputNamesVector.size(), "float");
471 }
472 else if ((inputTypesVector.size() != 0) && (inputTypesVector.size() != inputNamesVector.size()))
473 {
474 BOOST_LOG_TRIVIAL(fatal) << "input-name and input-type must have the same amount of elements.";
475 return EXIT_FAILURE;
476 }
477
478 // Parse input tensor shape from the string we got from the command-line.
479 std::vector<std::unique_ptr<armnn::TensorShape>> inputTensorShapes;
480
481 if (!inputTensorShapesVector.empty())
482 {
483 inputTensorShapes.reserve(inputTensorShapesVector.size());
484
485 for(const std::string& shape : inputTensorShapesVector)
486 {
487 std::stringstream ss(shape);
488 std::vector<unsigned int> dims = ParseArray(ss);
489
490 try
491 {
492 // Coverity fix: An exception of type armnn::InvalidArgumentException is thrown and never caught.
493 inputTensorShapes.push_back(std::make_unique<armnn::TensorShape>(dims.size(), dims.data()));
494 }
495 catch (const armnn::InvalidArgumentException& e)
496 {
497 BOOST_LOG_TRIVIAL(fatal) << "Cannot create tensor shape: " << e.what();
498 return EXIT_FAILURE;
499 }
500 }
501 }
502
503 // Check that threshold time is not less than zero
504 if (thresholdTime < 0)
505 {
506 BOOST_LOG_TRIVIAL(fatal) << "Threshold time supplied as a commoand line argument is less than zero.";
507 return EXIT_FAILURE;
508 }
509
510 // Forward to implementation based on the parser type
511 if (modelFormat.find("armnn") != std::string::npos)
512 {
513#if defined(ARMNN_SERIALIZER)
514 return MainImpl<armnnDeserializer::IDeserializer, float>(
515 modelPath.c_str(), isModelBinary, computeDevice,
516 inputNamesVector, inputTensorShapes,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100517 inputTensorDataFilePathsVector, inputTypesVector, quantizeInput,
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100518 outputTypesVector, outputNamesVector, enableProfiling,
519 enableFp16TurboMode, thresholdTime, subgraphId, runtime);
520#else
521 BOOST_LOG_TRIVIAL(fatal) << "Not built with serialization support.";
522 return EXIT_FAILURE;
523#endif
524 }
525 else if (modelFormat.find("caffe") != std::string::npos)
526 {
527#if defined(ARMNN_CAFFE_PARSER)
528 return MainImpl<armnnCaffeParser::ICaffeParser, float>(modelPath.c_str(), isModelBinary, computeDevice,
529 inputNamesVector, inputTensorShapes,
530 inputTensorDataFilePathsVector, inputTypesVector,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100531 quantizeInput, outputTypesVector, outputNamesVector,
532 enableProfiling, enableFp16TurboMode, thresholdTime,
533 subgraphId, runtime);
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100534#else
535 BOOST_LOG_TRIVIAL(fatal) << "Not built with Caffe parser support.";
536 return EXIT_FAILURE;
537#endif
538 }
539 else if (modelFormat.find("onnx") != std::string::npos)
540{
541#if defined(ARMNN_ONNX_PARSER)
542 return MainImpl<armnnOnnxParser::IOnnxParser, float>(modelPath.c_str(), isModelBinary, computeDevice,
543 inputNamesVector, inputTensorShapes,
544 inputTensorDataFilePathsVector, inputTypesVector,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100545 quantizeInput, outputTypesVector, outputNamesVector,
546 enableProfiling, enableFp16TurboMode, thresholdTime,
547 subgraphId, runtime);
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100548#else
549 BOOST_LOG_TRIVIAL(fatal) << "Not built with Onnx parser support.";
550 return EXIT_FAILURE;
551#endif
552 }
553 else if (modelFormat.find("tensorflow") != std::string::npos)
554 {
555#if defined(ARMNN_TF_PARSER)
556 return MainImpl<armnnTfParser::ITfParser, float>(modelPath.c_str(), isModelBinary, computeDevice,
557 inputNamesVector, inputTensorShapes,
558 inputTensorDataFilePathsVector, inputTypesVector,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100559 quantizeInput, outputTypesVector, outputNamesVector,
560 enableProfiling, enableFp16TurboMode, thresholdTime,
561 subgraphId, runtime);
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100562#else
563 BOOST_LOG_TRIVIAL(fatal) << "Not built with Tensorflow parser support.";
564 return EXIT_FAILURE;
565#endif
566 }
567 else if(modelFormat.find("tflite") != std::string::npos)
568 {
569#if defined(ARMNN_TF_LITE_PARSER)
570 if (! isModelBinary)
571 {
572 BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Only 'binary' format supported \
573 for tflite files";
574 return EXIT_FAILURE;
575 }
576 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(modelPath.c_str(), isModelBinary, computeDevice,
577 inputNamesVector, inputTensorShapes,
578 inputTensorDataFilePathsVector, inputTypesVector,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100579 quantizeInput, outputTypesVector, outputNamesVector,
580 enableProfiling, enableFp16TurboMode, thresholdTime,
581 subgraphId, runtime);
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100582#else
583 BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat <<
584 "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'";
585 return EXIT_FAILURE;
586#endif
587 }
588 else
589 {
590 BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat <<
591 "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'";
592 return EXIT_FAILURE;
593 }
594}
595
596int RunCsvTest(const armnnUtils::CsvRow &csvRow, const std::shared_ptr<armnn::IRuntime>& runtime,
597 const bool enableProfiling, const bool enableFp16TurboMode, const double& thresholdTime)
598{
599 std::string modelFormat;
600 std::string modelPath;
601 std::string inputNames;
602 std::string inputTensorShapes;
603 std::string inputTensorDataFilePaths;
604 std::string outputNames;
605 std::string inputTypes;
606 std::string outputTypes;
607
608 size_t subgraphId = 0;
609
610 const std::string backendsMessage = std::string("The preferred order of devices to run layers on by default. ")
611 + std::string("Possible choices: ")
612 + armnn::BackendRegistryInstance().GetBackendIdsAsString();
613
614 po::options_description desc("Options");
615 try
616 {
617 desc.add_options()
618 ("model-format,f", po::value(&modelFormat),
619 "armnn-binary, caffe-binary, caffe-text, tflite-binary, onnx-binary, onnx-text, tensorflow-binary or "
620 "tensorflow-text.")
621 ("model-path,m", po::value(&modelPath), "Path to model file, e.g. .armnn, .caffemodel, .prototxt, "
622 ".tflite, .onnx")
623 ("compute,c", po::value<std::vector<armnn::BackendId>>()->multitoken(),
624 backendsMessage.c_str())
625 ("input-name,i", po::value(&inputNames), "Identifier of the input tensors in the network separated by comma.")
626 ("subgraph-number,n", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be "
627 "executed. Defaults to 0.")
628 ("input-tensor-shape,s", po::value(&inputTensorShapes),
629 "The shape of the input tensors in the network as a flat array of integers separated by comma. "
630 "Several shapes can be passed separating them by semicolon. "
631 "This parameter is optional, depending on the network.")
632 ("input-tensor-data,d", po::value(&inputTensorDataFilePaths),
633 "Path to files containing the input data as a flat array separated by whitespace. "
634 "Several paths can be passed separating them by comma.")
635 ("input-type,y",po::value(&inputTypes), "The type of the input tensors in the network separated by comma. "
636 "If unset, defaults to \"float\" for all defined inputs. "
637 "Accepted values (float, int or qasymm8).")
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100638 ("quantize-input,q",po::bool_switch()->default_value(false),
639 "If this option is enabled, all float inputs will be quantized to qasymm8. "
640 "If unset, default to not quantized. "
641 "Accepted values (true or false)")
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100642 ("output-type,z",po::value(&outputTypes), "The type of the output tensors in the network separated by comma. "
643 "If unset, defaults to \"float\" for all defined outputs. "
644 "Accepted values (float, int or qasymm8).")
645 ("output-name,o", po::value(&outputNames),
646 "Identifier of the output tensors in the network separated by comma.");
647 }
648 catch (const std::exception& e)
649 {
650 // Coverity points out that default_value(...) can throw a bad_lexical_cast,
651 // and that desc.add_options() can throw boost::io::too_few_args.
652 // They really won't in any of these cases.
653 BOOST_ASSERT_MSG(false, "Caught unexpected exception");
654 BOOST_LOG_TRIVIAL(fatal) << "Fatal internal error: " << e.what();
655 return EXIT_FAILURE;
656 }
657
658 std::vector<const char*> clOptions;
659 clOptions.reserve(csvRow.values.size());
660 for (const std::string& value : csvRow.values)
661 {
662 clOptions.push_back(value.c_str());
663 }
664
665 po::variables_map vm;
666 try
667 {
668 po::store(po::parse_command_line(static_cast<int>(clOptions.size()), clOptions.data(), desc), vm);
669
670 po::notify(vm);
671
672 CheckOptionDependencies(vm);
673 }
674 catch (const po::error& e)
675 {
676 std::cerr << e.what() << std::endl << std::endl;
677 std::cerr << desc << std::endl;
678 return EXIT_FAILURE;
679 }
680
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100681 // Get the value of the switch arguments.
682 bool quantizeInput = vm["quantize-input"].as<bool>();
683
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100684 // Get the preferred order of compute devices.
685 std::vector<armnn::BackendId> computeDevices = vm["compute"].as<std::vector<armnn::BackendId>>();
686
687 // Remove duplicates from the list of compute devices.
688 RemoveDuplicateDevices(computeDevices);
689
690 // Check that the specified compute devices are valid.
691 std::string invalidBackends;
692 if (!CheckRequestedBackendsAreValid(computeDevices, armnn::Optional<std::string&>(invalidBackends)))
693 {
694 BOOST_LOG_TRIVIAL(fatal) << "The list of preferred devices contains invalid backend IDs: "
695 << invalidBackends;
696 return EXIT_FAILURE;
697 }
698
699 return RunTest(modelFormat, inputTensorShapes, computeDevices, modelPath, inputNames,
Narumol Prangnawarat610256f2019-06-26 15:10:46 +0100700 inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames,
Francis Murtaghbee4bc92019-06-18 12:30:37 +0100701 enableProfiling, enableFp16TurboMode, thresholdTime, subgraphId);
702}