Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 1 | // |
| 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 | |
| 42 | namespace |
| 43 | { |
| 44 | |
| 45 | // Configure boost::program_options for command-line parsing and validation. |
| 46 | namespace po = boost::program_options; |
| 47 | |
| 48 | template<typename T, typename TParseElementFunc> |
| 49 | std::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 | |
| 86 | bool 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 | |
| 99 | void 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 | |
| 119 | void 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 | |
| 128 | template<armnn::DataType NonQuantizedType> |
| 129 | auto ParseDataArray(std::istream & stream); |
| 130 | |
| 131 | template<armnn::DataType QuantizedType> |
| 132 | auto ParseDataArray(std::istream& stream, |
| 133 | const float& quantizationScale, |
| 134 | const int32_t& quantizationOffset); |
| 135 | |
| 136 | template<> |
| 137 | auto ParseDataArray<armnn::DataType::Float32>(std::istream & stream) |
| 138 | { |
| 139 | return ParseArrayImpl<float>(stream, [](const std::string& s) { return std::stof(s); }); |
| 140 | } |
| 141 | |
| 142 | template<> |
| 143 | auto ParseDataArray<armnn::DataType::Signed32>(std::istream & stream) |
| 144 | { |
| 145 | return ParseArrayImpl<int>(stream, [](const std::string & s) { return std::stoi(s); }); |
| 146 | } |
| 147 | |
| 148 | template<> |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 149 | auto 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 | |
| 155 | template<> |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 156 | auto 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 Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 169 | std::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 | |
| 175 | std::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 | |
| 181 | void 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 | |
| 200 | struct 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 | |
| 233 | private: |
| 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 | |
| 253 | template<typename TParser, typename TDataType> |
| 254 | int MainImpl(const char* modelPath, |
| 255 | bool isModelBinary, |
| 256 | const std::vector<armnn::BackendId>& computeDevices, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 257 | const std::string& dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 258 | const std::vector<string>& inputNames, |
| 259 | const std::vector<std::unique_ptr<armnn::TensorShape>>& inputTensorShapes, |
| 260 | const std::vector<string>& inputTensorDataFilePaths, |
| 261 | const std::vector<string>& inputTypes, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 262 | bool quantizeInput, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 263 | const std::vector<string>& outputTypes, |
| 264 | const std::vector<string>& outputNames, |
| 265 | bool enableProfiling, |
| 266 | bool enableFp16TurboMode, |
| 267 | const double& thresholdTime, |
| 268 | const size_t subgraphId, |
| 269 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 270 | { |
| 271 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
| 272 | |
| 273 | std::vector<TContainer> inputDataContainers; |
| 274 | |
| 275 | try |
| 276 | { |
| 277 | // Creates an InferenceModel, which will parse the model and load it into an IRuntime. |
| 278 | typename InferenceModel<TParser, TDataType>::Params params; |
| 279 | params.m_ModelPath = modelPath; |
| 280 | params.m_IsModelBinary = isModelBinary; |
| 281 | params.m_ComputeDevices = computeDevices; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 282 | params.m_DynamicBackendsPath = dynamicBackendsPath; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 283 | |
| 284 | for(const std::string& inputName: inputNames) |
| 285 | { |
| 286 | params.m_InputBindings.push_back(inputName); |
| 287 | } |
| 288 | |
| 289 | for(unsigned int i = 0; i < inputTensorShapes.size(); ++i) |
| 290 | { |
| 291 | params.m_InputShapes.push_back(*inputTensorShapes[i]); |
| 292 | } |
| 293 | |
| 294 | for(const std::string& outputName: outputNames) |
| 295 | { |
| 296 | params.m_OutputBindings.push_back(outputName); |
| 297 | } |
| 298 | |
| 299 | params.m_SubgraphId = subgraphId; |
| 300 | params.m_EnableFp16TurboMode = enableFp16TurboMode; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 301 | InferenceModel<TParser, TDataType> model(params, enableProfiling, dynamicBackendsPath, runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 302 | |
| 303 | for(unsigned int i = 0; i < inputTensorDataFilePaths.size(); ++i) |
| 304 | { |
| 305 | std::ifstream inputTensorFile(inputTensorDataFilePaths[i]); |
| 306 | |
| 307 | if (inputTypes[i].compare("float") == 0) |
| 308 | { |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 309 | if (quantizeInput) |
| 310 | { |
| 311 | auto inputBinding = model.GetInputBindingInfo(); |
| 312 | inputDataContainers.push_back( |
| 313 | ParseDataArray<armnn::DataType::QuantisedAsymm8>(inputTensorFile, |
| 314 | inputBinding.second.GetQuantizationScale(), |
| 315 | inputBinding.second.GetQuantizationOffset())); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | inputDataContainers.push_back( |
| 320 | ParseDataArray<armnn::DataType::Float32>(inputTensorFile)); |
| 321 | } |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 322 | } |
| 323 | else if (inputTypes[i].compare("int") == 0) |
| 324 | { |
| 325 | inputDataContainers.push_back( |
| 326 | ParseDataArray<armnn::DataType::Signed32>(inputTensorFile)); |
| 327 | } |
| 328 | else if (inputTypes[i].compare("qasymm8") == 0) |
| 329 | { |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 330 | inputDataContainers.push_back( |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 331 | ParseDataArray<armnn::DataType::QuantisedAsymm8>(inputTensorFile)); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | { |
| 335 | BOOST_LOG_TRIVIAL(fatal) << "Unsupported tensor data type \"" << inputTypes[i] << "\". "; |
| 336 | return EXIT_FAILURE; |
| 337 | } |
| 338 | |
| 339 | inputTensorFile.close(); |
| 340 | } |
| 341 | |
| 342 | const size_t numOutputs = params.m_OutputBindings.size(); |
| 343 | std::vector<TContainer> outputDataContainers; |
| 344 | |
| 345 | for (unsigned int i = 0; i < numOutputs; ++i) |
| 346 | { |
| 347 | if (outputTypes[i].compare("float") == 0) |
| 348 | { |
| 349 | outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i))); |
| 350 | } |
| 351 | else if (outputTypes[i].compare("int") == 0) |
| 352 | { |
| 353 | outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i))); |
| 354 | } |
| 355 | else if (outputTypes[i].compare("qasymm8") == 0) |
| 356 | { |
| 357 | outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i))); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | BOOST_LOG_TRIVIAL(fatal) << "Unsupported tensor data type \"" << outputTypes[i] << "\". "; |
| 362 | return EXIT_FAILURE; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds) |
| 367 | auto inference_duration = model.Run(inputDataContainers, outputDataContainers); |
| 368 | |
| 369 | // Print output tensors |
| 370 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 371 | for (size_t i = 0; i < numOutputs; i++) |
| 372 | { |
| 373 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 374 | TensorPrinter printer(params.m_OutputBindings[i], infoOut); |
| 375 | boost::apply_visitor(printer, outputDataContainers[i]); |
| 376 | } |
| 377 | |
| 378 | BOOST_LOG_TRIVIAL(info) << "\nInference time: " << std::setprecision(2) |
| 379 | << std::fixed << inference_duration.count() << " ms"; |
| 380 | |
| 381 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 382 | if (thresholdTime != 0.0) |
| 383 | { |
| 384 | BOOST_LOG_TRIVIAL(info) << "Threshold time: " << std::setprecision(2) |
| 385 | << std::fixed << thresholdTime << " ms"; |
| 386 | auto thresholdMinusInference = thresholdTime - inference_duration.count(); |
| 387 | BOOST_LOG_TRIVIAL(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 388 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 389 | |
| 390 | if (thresholdMinusInference < 0) |
| 391 | { |
| 392 | BOOST_LOG_TRIVIAL(fatal) << "Elapsed inference time is greater than provided threshold time.\n"; |
| 393 | return EXIT_FAILURE; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | } |
| 399 | catch (armnn::Exception const& e) |
| 400 | { |
| 401 | BOOST_LOG_TRIVIAL(fatal) << "Armnn Error: " << e.what(); |
| 402 | return EXIT_FAILURE; |
| 403 | } |
| 404 | |
| 405 | return EXIT_SUCCESS; |
| 406 | } |
| 407 | |
| 408 | // This will run a test |
| 409 | int RunTest(const std::string& format, |
| 410 | const std::string& inputTensorShapesStr, |
| 411 | const vector<armnn::BackendId>& computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 412 | const std::string& dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 413 | const std::string& path, |
| 414 | const std::string& inputNames, |
| 415 | const std::string& inputTensorDataFilePaths, |
| 416 | const std::string& inputTypes, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 417 | bool quantizeInput, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 418 | const std::string& outputTypes, |
| 419 | const std::string& outputNames, |
| 420 | bool enableProfiling, |
| 421 | bool enableFp16TurboMode, |
| 422 | const double& thresholdTime, |
| 423 | const size_t subgraphId, |
| 424 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 425 | { |
| 426 | std::string modelFormat = boost::trim_copy(format); |
| 427 | std::string modelPath = boost::trim_copy(path); |
| 428 | std::vector<std::string> inputNamesVector = ParseStringList(inputNames, ","); |
| 429 | std::vector<std::string> inputTensorShapesVector = ParseStringList(inputTensorShapesStr, ";"); |
| 430 | std::vector<std::string> inputTensorDataFilePathsVector = ParseStringList( |
| 431 | inputTensorDataFilePaths, ","); |
| 432 | std::vector<std::string> outputNamesVector = ParseStringList(outputNames, ","); |
| 433 | std::vector<std::string> inputTypesVector = ParseStringList(inputTypes, ","); |
| 434 | std::vector<std::string> outputTypesVector = ParseStringList(outputTypes, ","); |
| 435 | |
| 436 | // Parse model binary flag from the model-format string we got from the command-line |
| 437 | bool isModelBinary; |
| 438 | if (modelFormat.find("bin") != std::string::npos) |
| 439 | { |
| 440 | isModelBinary = true; |
| 441 | } |
| 442 | else if (modelFormat.find("txt") != std::string::npos || modelFormat.find("text") != std::string::npos) |
| 443 | { |
| 444 | isModelBinary = false; |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Please include 'binary' or 'text'"; |
| 449 | return EXIT_FAILURE; |
| 450 | } |
| 451 | |
| 452 | if ((inputTensorShapesVector.size() != 0) && (inputTensorShapesVector.size() != inputNamesVector.size())) |
| 453 | { |
| 454 | BOOST_LOG_TRIVIAL(fatal) << "input-name and input-tensor-shape must have the same amount of elements."; |
| 455 | return EXIT_FAILURE; |
| 456 | } |
| 457 | |
| 458 | if ((inputTensorDataFilePathsVector.size() != 0) && |
| 459 | (inputTensorDataFilePathsVector.size() != inputNamesVector.size())) |
| 460 | { |
| 461 | BOOST_LOG_TRIVIAL(fatal) << "input-name and input-tensor-data must have the same amount of elements."; |
| 462 | return EXIT_FAILURE; |
| 463 | } |
| 464 | |
| 465 | if (inputTypesVector.size() == 0) |
| 466 | { |
| 467 | //Defaults the value of all inputs to "float" |
| 468 | inputTypesVector.assign(inputNamesVector.size(), "float"); |
| 469 | } |
| 470 | if (outputTypesVector.size() == 0) |
| 471 | { |
| 472 | //Defaults the value of all outputs to "float" |
| 473 | outputTypesVector.assign(outputNamesVector.size(), "float"); |
| 474 | } |
| 475 | else if ((inputTypesVector.size() != 0) && (inputTypesVector.size() != inputNamesVector.size())) |
| 476 | { |
| 477 | BOOST_LOG_TRIVIAL(fatal) << "input-name and input-type must have the same amount of elements."; |
| 478 | return EXIT_FAILURE; |
| 479 | } |
| 480 | |
| 481 | // Parse input tensor shape from the string we got from the command-line. |
| 482 | std::vector<std::unique_ptr<armnn::TensorShape>> inputTensorShapes; |
| 483 | |
| 484 | if (!inputTensorShapesVector.empty()) |
| 485 | { |
| 486 | inputTensorShapes.reserve(inputTensorShapesVector.size()); |
| 487 | |
| 488 | for(const std::string& shape : inputTensorShapesVector) |
| 489 | { |
| 490 | std::stringstream ss(shape); |
| 491 | std::vector<unsigned int> dims = ParseArray(ss); |
| 492 | |
| 493 | try |
| 494 | { |
| 495 | // Coverity fix: An exception of type armnn::InvalidArgumentException is thrown and never caught. |
| 496 | inputTensorShapes.push_back(std::make_unique<armnn::TensorShape>(dims.size(), dims.data())); |
| 497 | } |
| 498 | catch (const armnn::InvalidArgumentException& e) |
| 499 | { |
| 500 | BOOST_LOG_TRIVIAL(fatal) << "Cannot create tensor shape: " << e.what(); |
| 501 | return EXIT_FAILURE; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Check that threshold time is not less than zero |
| 507 | if (thresholdTime < 0) |
| 508 | { |
| 509 | BOOST_LOG_TRIVIAL(fatal) << "Threshold time supplied as a commoand line argument is less than zero."; |
| 510 | return EXIT_FAILURE; |
| 511 | } |
| 512 | |
| 513 | // Forward to implementation based on the parser type |
| 514 | if (modelFormat.find("armnn") != std::string::npos) |
| 515 | { |
| 516 | #if defined(ARMNN_SERIALIZER) |
| 517 | return MainImpl<armnnDeserializer::IDeserializer, float>( |
| 518 | modelPath.c_str(), isModelBinary, computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 519 | dynamicBackendsPath, inputNamesVector, inputTensorShapes, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 520 | inputTensorDataFilePathsVector, inputTypesVector, quantizeInput, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 521 | outputTypesVector, outputNamesVector, enableProfiling, |
| 522 | enableFp16TurboMode, thresholdTime, subgraphId, runtime); |
| 523 | #else |
| 524 | BOOST_LOG_TRIVIAL(fatal) << "Not built with serialization support."; |
| 525 | return EXIT_FAILURE; |
| 526 | #endif |
| 527 | } |
| 528 | else if (modelFormat.find("caffe") != std::string::npos) |
| 529 | { |
| 530 | #if defined(ARMNN_CAFFE_PARSER) |
| 531 | return MainImpl<armnnCaffeParser::ICaffeParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 532 | dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 533 | inputNamesVector, inputTensorShapes, |
| 534 | inputTensorDataFilePathsVector, inputTypesVector, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 535 | quantizeInput, outputTypesVector, outputNamesVector, |
| 536 | enableProfiling, enableFp16TurboMode, thresholdTime, |
| 537 | subgraphId, runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 538 | #else |
| 539 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Caffe parser support."; |
| 540 | return EXIT_FAILURE; |
| 541 | #endif |
| 542 | } |
| 543 | else if (modelFormat.find("onnx") != std::string::npos) |
| 544 | { |
| 545 | #if defined(ARMNN_ONNX_PARSER) |
| 546 | return MainImpl<armnnOnnxParser::IOnnxParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 547 | dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 548 | inputNamesVector, inputTensorShapes, |
| 549 | inputTensorDataFilePathsVector, inputTypesVector, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 550 | quantizeInput, outputTypesVector, outputNamesVector, |
| 551 | enableProfiling, enableFp16TurboMode, thresholdTime, |
| 552 | subgraphId, runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 553 | #else |
| 554 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Onnx parser support."; |
| 555 | return EXIT_FAILURE; |
| 556 | #endif |
| 557 | } |
| 558 | else if (modelFormat.find("tensorflow") != std::string::npos) |
| 559 | { |
| 560 | #if defined(ARMNN_TF_PARSER) |
| 561 | return MainImpl<armnnTfParser::ITfParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 562 | dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 563 | inputNamesVector, inputTensorShapes, |
| 564 | inputTensorDataFilePathsVector, inputTypesVector, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 565 | quantizeInput, outputTypesVector, outputNamesVector, |
| 566 | enableProfiling, enableFp16TurboMode, thresholdTime, |
| 567 | subgraphId, runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 568 | #else |
| 569 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Tensorflow parser support."; |
| 570 | return EXIT_FAILURE; |
| 571 | #endif |
| 572 | } |
| 573 | else if(modelFormat.find("tflite") != std::string::npos) |
| 574 | { |
| 575 | #if defined(ARMNN_TF_LITE_PARSER) |
| 576 | if (! isModelBinary) |
| 577 | { |
| 578 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Only 'binary' format supported \ |
| 579 | for tflite files"; |
| 580 | return EXIT_FAILURE; |
| 581 | } |
| 582 | return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 583 | dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 584 | inputNamesVector, inputTensorShapes, |
| 585 | inputTensorDataFilePathsVector, inputTypesVector, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 586 | quantizeInput, outputTypesVector, outputNamesVector, |
| 587 | enableProfiling, enableFp16TurboMode, thresholdTime, |
| 588 | subgraphId, runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 589 | #else |
| 590 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << |
| 591 | "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
| 592 | return EXIT_FAILURE; |
| 593 | #endif |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << |
| 598 | "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
| 599 | return EXIT_FAILURE; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | int RunCsvTest(const armnnUtils::CsvRow &csvRow, const std::shared_ptr<armnn::IRuntime>& runtime, |
| 604 | const bool enableProfiling, const bool enableFp16TurboMode, const double& thresholdTime) |
| 605 | { |
| 606 | std::string modelFormat; |
| 607 | std::string modelPath; |
| 608 | std::string inputNames; |
| 609 | std::string inputTensorShapes; |
| 610 | std::string inputTensorDataFilePaths; |
| 611 | std::string outputNames; |
| 612 | std::string inputTypes; |
| 613 | std::string outputTypes; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 614 | std::string dynamicBackendsPath; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 615 | |
| 616 | size_t subgraphId = 0; |
| 617 | |
| 618 | const std::string backendsMessage = std::string("The preferred order of devices to run layers on by default. ") |
| 619 | + std::string("Possible choices: ") |
| 620 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 621 | |
| 622 | po::options_description desc("Options"); |
| 623 | try |
| 624 | { |
| 625 | desc.add_options() |
| 626 | ("model-format,f", po::value(&modelFormat), |
| 627 | "armnn-binary, caffe-binary, caffe-text, tflite-binary, onnx-binary, onnx-text, tensorflow-binary or " |
| 628 | "tensorflow-text.") |
| 629 | ("model-path,m", po::value(&modelPath), "Path to model file, e.g. .armnn, .caffemodel, .prototxt, " |
| 630 | ".tflite, .onnx") |
| 631 | ("compute,c", po::value<std::vector<armnn::BackendId>>()->multitoken(), |
| 632 | backendsMessage.c_str()) |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 633 | ("dynamic-backends-path,b", po::value(&dynamicBackendsPath), |
| 634 | "Path where to load any available dynamic backend from. " |
| 635 | "If left empty (the default), dynamic backends will not be used.") |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 636 | ("input-name,i", po::value(&inputNames), "Identifier of the input tensors in the network separated by comma.") |
| 637 | ("subgraph-number,n", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be " |
| 638 | "executed. Defaults to 0.") |
| 639 | ("input-tensor-shape,s", po::value(&inputTensorShapes), |
| 640 | "The shape of the input tensors in the network as a flat array of integers separated by comma. " |
| 641 | "Several shapes can be passed separating them by semicolon. " |
| 642 | "This parameter is optional, depending on the network.") |
| 643 | ("input-tensor-data,d", po::value(&inputTensorDataFilePaths), |
| 644 | "Path to files containing the input data as a flat array separated by whitespace. " |
| 645 | "Several paths can be passed separating them by comma.") |
| 646 | ("input-type,y",po::value(&inputTypes), "The type of the input tensors in the network separated by comma. " |
| 647 | "If unset, defaults to \"float\" for all defined inputs. " |
| 648 | "Accepted values (float, int or qasymm8).") |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 649 | ("quantize-input,q",po::bool_switch()->default_value(false), |
| 650 | "If this option is enabled, all float inputs will be quantized to qasymm8. " |
| 651 | "If unset, default to not quantized. " |
| 652 | "Accepted values (true or false)") |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 653 | ("output-type,z",po::value(&outputTypes), "The type of the output tensors in the network separated by comma. " |
| 654 | "If unset, defaults to \"float\" for all defined outputs. " |
| 655 | "Accepted values (float, int or qasymm8).") |
| 656 | ("output-name,o", po::value(&outputNames), |
| 657 | "Identifier of the output tensors in the network separated by comma."); |
| 658 | } |
| 659 | catch (const std::exception& e) |
| 660 | { |
| 661 | // Coverity points out that default_value(...) can throw a bad_lexical_cast, |
| 662 | // and that desc.add_options() can throw boost::io::too_few_args. |
| 663 | // They really won't in any of these cases. |
| 664 | BOOST_ASSERT_MSG(false, "Caught unexpected exception"); |
| 665 | BOOST_LOG_TRIVIAL(fatal) << "Fatal internal error: " << e.what(); |
| 666 | return EXIT_FAILURE; |
| 667 | } |
| 668 | |
| 669 | std::vector<const char*> clOptions; |
| 670 | clOptions.reserve(csvRow.values.size()); |
| 671 | for (const std::string& value : csvRow.values) |
| 672 | { |
| 673 | clOptions.push_back(value.c_str()); |
| 674 | } |
| 675 | |
| 676 | po::variables_map vm; |
| 677 | try |
| 678 | { |
| 679 | po::store(po::parse_command_line(static_cast<int>(clOptions.size()), clOptions.data(), desc), vm); |
| 680 | |
| 681 | po::notify(vm); |
| 682 | |
| 683 | CheckOptionDependencies(vm); |
| 684 | } |
| 685 | catch (const po::error& e) |
| 686 | { |
| 687 | std::cerr << e.what() << std::endl << std::endl; |
| 688 | std::cerr << desc << std::endl; |
| 689 | return EXIT_FAILURE; |
| 690 | } |
| 691 | |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 692 | // Get the value of the switch arguments. |
| 693 | bool quantizeInput = vm["quantize-input"].as<bool>(); |
| 694 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 695 | // Get the preferred order of compute devices. |
| 696 | std::vector<armnn::BackendId> computeDevices = vm["compute"].as<std::vector<armnn::BackendId>>(); |
| 697 | |
| 698 | // Remove duplicates from the list of compute devices. |
| 699 | RemoveDuplicateDevices(computeDevices); |
| 700 | |
| 701 | // Check that the specified compute devices are valid. |
| 702 | std::string invalidBackends; |
| 703 | if (!CheckRequestedBackendsAreValid(computeDevices, armnn::Optional<std::string&>(invalidBackends))) |
| 704 | { |
| 705 | BOOST_LOG_TRIVIAL(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 706 | << invalidBackends; |
| 707 | return EXIT_FAILURE; |
| 708 | } |
| 709 | |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 710 | return RunTest(modelFormat, inputTensorShapes, computeDevices, dynamicBackendsPath, modelPath, inputNames, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 711 | inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 712 | enableProfiling, enableFp16TurboMode, thresholdTime, subgraphId); |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame^] | 713 | } |