telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 5 | #include <armnn/ArmNN.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 6 | #include <armnn/TypesUtils.hpp> |
| 7 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #if defined(ARMNN_CAFFE_PARSER) |
| 9 | #include "armnnCaffeParser/ICaffeParser.hpp" |
| 10 | #endif |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 11 | #if defined(ARMNN_TF_PARSER) |
| 12 | #include "armnnTfParser/ITfParser.hpp" |
| 13 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | #if defined(ARMNN_TF_LITE_PARSER) |
| 15 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 16 | #endif |
| 17 | #if defined(ARMNN_ONNX_PARSER) |
| 18 | #include "armnnOnnxParser/IOnnxParser.hpp" |
| 19 | #endif |
| 20 | #include "CsvReader.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | #include "../InferenceTest.hpp" |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | #include <Logging.hpp> |
| 24 | #include <Profiling.hpp> |
| 25 | |
| 26 | #include <boost/algorithm/string/trim.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | #include <boost/algorithm/string/split.hpp> |
| 28 | #include <boost/algorithm/string/classification.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | #include <boost/program_options.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | |
| 31 | #include <iostream> |
| 32 | #include <fstream> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 33 | #include <functional> |
| 34 | #include <future> |
| 35 | #include <algorithm> |
| 36 | #include <iterator> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | |
| 38 | namespace |
| 39 | { |
| 40 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | // Configure boost::program_options for command-line parsing and validation. |
| 42 | namespace po = boost::program_options; |
| 43 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | template<typename T, typename TParseElementFunc> |
| 45 | std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc) |
| 46 | { |
| 47 | std::vector<T> result; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | // Processes line-by-line. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | std::string line; |
| 50 | while (std::getline(stream, line)) |
| 51 | { |
| 52 | std::vector<std::string> tokens; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 53 | try |
| 54 | { |
| 55 | // Coverity fix: boost::split() may throw an exception of type boost::bad_function_call. |
| 56 | boost::split(tokens, line, boost::algorithm::is_any_of("\t ,;:"), boost::token_compress_on); |
| 57 | } |
| 58 | catch (const std::exception& e) |
| 59 | { |
| 60 | BOOST_LOG_TRIVIAL(error) << "An error occurred when splitting tokens: " << e.what(); |
| 61 | continue; |
| 62 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | for (const std::string& token : tokens) |
| 64 | { |
| 65 | if (!token.empty()) // See https://stackoverflow.com/questions/10437406/ |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | result.push_back(parseElementFunc(token)); |
| 70 | } |
| 71 | catch (const std::exception&) |
| 72 | { |
| 73 | BOOST_LOG_TRIVIAL(error) << "'" << token << "' is not a valid number. It has been ignored."; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 82 | bool CheckOption(const po::variables_map& vm, |
| 83 | const char* option) |
| 84 | { |
| 85 | // Check that the given option is valid. |
| 86 | if (option == nullptr) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // Check whether 'option' is provided. |
| 92 | return vm.find(option) != vm.end(); |
| 93 | } |
| 94 | |
| 95 | void CheckOptionDependency(const po::variables_map& vm, |
| 96 | const char* option, |
| 97 | const char* required) |
| 98 | { |
| 99 | // Check that the given options are valid. |
| 100 | if (option == nullptr || required == nullptr) |
| 101 | { |
| 102 | throw po::error("Invalid option to check dependency for"); |
| 103 | } |
| 104 | |
| 105 | // Check that if 'option' is provided, 'required' is also provided. |
| 106 | if (CheckOption(vm, option) && !vm[option].defaulted()) |
| 107 | { |
| 108 | if (CheckOption(vm, required) == 0 || vm[required].defaulted()) |
| 109 | { |
| 110 | throw po::error(std::string("Option '") + option + "' requires option '" + required + "'."); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void CheckOptionDependencies(const po::variables_map& vm) |
| 116 | { |
| 117 | CheckOptionDependency(vm, "model-path", "model-format"); |
| 118 | CheckOptionDependency(vm, "model-path", "input-name"); |
| 119 | CheckOptionDependency(vm, "model-path", "input-tensor-data"); |
| 120 | CheckOptionDependency(vm, "model-path", "output-name"); |
| 121 | CheckOptionDependency(vm, "input-tensor-shape", "model-path"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template<typename T> |
| 125 | std::vector<T> ParseArray(std::istream& stream); |
| 126 | |
| 127 | template<> |
| 128 | std::vector<float> ParseArray(std::istream& stream) |
| 129 | { |
| 130 | return ParseArrayImpl<float>(stream, [](const std::string& s) { return std::stof(s); }); |
| 131 | } |
| 132 | |
| 133 | template<> |
| 134 | std::vector<unsigned int> ParseArray(std::istream& stream) |
| 135 | { |
| 136 | return ParseArrayImpl<unsigned int>(stream, |
| 137 | [](const std::string& s) { return boost::numeric_cast<unsigned int>(std::stoi(s)); }); |
| 138 | } |
| 139 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 140 | void PrintOutputData(const std::string& outputLayerName, const std::vector<float>& data) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 142 | std::cout << outputLayerName << ": "; |
Aron Virginas-Tar | 93f5f97 | 2019-01-31 13:12:34 +0000 | [diff] [blame^] | 143 | for (size_t i = 0; i < data.size(); i++) |
| 144 | { |
| 145 | printf("%f ", data[i]); |
| 146 | } |
| 147 | printf("\n"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | } |
| 149 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 150 | void RemoveDuplicateDevices(std::vector<armnn::BackendId>& computeDevices) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 151 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 152 | // Mark the duplicate devices as 'Undefined'. |
| 153 | for (auto i = computeDevices.begin(); i != computeDevices.end(); ++i) |
| 154 | { |
| 155 | for (auto j = std::next(i); j != computeDevices.end(); ++j) |
| 156 | { |
| 157 | if (*j == *i) |
| 158 | { |
| 159 | *j = armnn::Compute::Undefined; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Remove 'Undefined' devices. |
| 165 | computeDevices.erase(std::remove(computeDevices.begin(), computeDevices.end(), armnn::Compute::Undefined), |
| 166 | computeDevices.end()); |
| 167 | } |
| 168 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 169 | } // namespace |
| 170 | |
| 171 | template<typename TParser, typename TDataType> |
| 172 | int MainImpl(const char* modelPath, |
| 173 | bool isModelBinary, |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 174 | const std::vector<armnn::BackendId>& computeDevice, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 175 | const char* inputName, |
| 176 | const armnn::TensorShape* inputTensorShape, |
| 177 | const char* inputTensorDataFilePath, |
| 178 | const char* outputName, |
| 179 | bool enableProfiling, |
| 180 | const size_t subgraphId, |
| 181 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 182 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 183 | using TContainer = std::vector<TDataType>; |
| 184 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 185 | // Loads input tensor. |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 186 | TContainer inputDataContainer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 187 | { |
| 188 | std::ifstream inputTensorFile(inputTensorDataFilePath); |
| 189 | if (!inputTensorFile.good()) |
| 190 | { |
| 191 | BOOST_LOG_TRIVIAL(fatal) << "Failed to load input tensor data file from " << inputTensorDataFilePath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 192 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 193 | } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 194 | inputDataContainer = ParseArray<TDataType>(inputTensorFile); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | try |
| 198 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | // Creates an InferenceModel, which will parse the model and load it into an IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 200 | typename InferenceModel<TParser, TDataType>::Params params; |
| 201 | params.m_ModelPath = modelPath; |
| 202 | params.m_IsModelBinary = isModelBinary; |
| 203 | params.m_ComputeDevice = computeDevice; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 204 | params.m_InputBindings = { inputName }; |
| 205 | params.m_InputShapes = { *inputTensorShape }; |
| 206 | params.m_OutputBindings = { outputName }; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 207 | params.m_EnableProfiling = enableProfiling; |
| 208 | params.m_SubgraphId = subgraphId; |
| 209 | InferenceModel<TParser, TDataType> model(params, runtime); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 210 | |
Aron Virginas-Tar | 9b93747 | 2019-01-30 17:41:47 +0000 | [diff] [blame] | 211 | const size_t numOutputs = params.m_OutputBindings.size(); |
| 212 | const size_t containerSize = model.GetOutputSize(); |
| 213 | |
Aron Virginas-Tar | 93f5f97 | 2019-01-31 13:12:34 +0000 | [diff] [blame^] | 214 | // Set up input data container |
| 215 | std::vector<TContainer> inputData(1, std::move(inputDataContainer)); |
| 216 | |
| 217 | // Set up output data container |
| 218 | std::vector<TContainer> outputData(numOutputs, TContainer(containerSize)); |
Aron Virginas-Tar | 9b93747 | 2019-01-30 17:41:47 +0000 | [diff] [blame] | 219 | |
| 220 | // Execute model |
Aron Virginas-Tar | 93f5f97 | 2019-01-31 13:12:34 +0000 | [diff] [blame^] | 221 | model.Run(inputData, outputData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 222 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 223 | // Print output tensors |
| 224 | for (size_t i = 0; i < numOutputs; i++) |
| 225 | { |
Aron Virginas-Tar | 93f5f97 | 2019-01-31 13:12:34 +0000 | [diff] [blame^] | 226 | PrintOutputData(params.m_OutputBindings[i], outputData[i]); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 227 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 228 | } |
| 229 | catch (armnn::Exception const& e) |
| 230 | { |
| 231 | BOOST_LOG_TRIVIAL(fatal) << "Armnn Error: " << e.what(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 232 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 233 | } |
| 234 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 235 | return EXIT_SUCCESS; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 236 | } |
| 237 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 238 | // This will run a test |
| 239 | int RunTest(const std::string& modelFormat, |
| 240 | const std::string& inputTensorShapeStr, |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 241 | const vector<armnn::BackendId>& computeDevice, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 242 | const std::string& modelPath, |
| 243 | const std::string& inputName, |
| 244 | const std::string& inputTensorDataFilePath, |
| 245 | const std::string& outputName, |
| 246 | bool enableProfiling, |
| 247 | const size_t subgraphId, |
| 248 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 249 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 250 | // Parse model binary flag from the model-format string we got from the command-line |
| 251 | bool isModelBinary; |
| 252 | if (modelFormat.find("bin") != std::string::npos) |
| 253 | { |
| 254 | isModelBinary = true; |
| 255 | } |
| 256 | else if (modelFormat.find("txt") != std::string::npos || modelFormat.find("text") != std::string::npos) |
| 257 | { |
| 258 | isModelBinary = false; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Please include 'binary' or 'text'"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 263 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Parse input tensor shape from the string we got from the command-line. |
| 267 | std::unique_ptr<armnn::TensorShape> inputTensorShape; |
| 268 | if (!inputTensorShapeStr.empty()) |
| 269 | { |
| 270 | std::stringstream ss(inputTensorShapeStr); |
| 271 | std::vector<unsigned int> dims = ParseArray<unsigned int>(ss); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 272 | |
| 273 | try |
| 274 | { |
| 275 | // Coverity fix: An exception of type armnn::InvalidArgumentException is thrown and never caught. |
| 276 | inputTensorShape = std::make_unique<armnn::TensorShape>(dims.size(), dims.data()); |
| 277 | } |
| 278 | catch (const armnn::InvalidArgumentException& e) |
| 279 | { |
| 280 | BOOST_LOG_TRIVIAL(fatal) << "Cannot create tensor shape: " << e.what(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 281 | return EXIT_FAILURE; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 282 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Forward to implementation based on the parser type |
| 286 | if (modelFormat.find("caffe") != std::string::npos) |
| 287 | { |
| 288 | #if defined(ARMNN_CAFFE_PARSER) |
| 289 | return MainImpl<armnnCaffeParser::ICaffeParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 290 | inputName.c_str(), inputTensorShape.get(), |
| 291 | inputTensorDataFilePath.c_str(), outputName.c_str(), |
| 292 | enableProfiling, subgraphId, runtime); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 293 | #else |
| 294 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Caffe parser support."; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 295 | return EXIT_FAILURE; |
| 296 | #endif |
| 297 | } |
| 298 | else if (modelFormat.find("onnx") != std::string::npos) |
| 299 | { |
| 300 | #if defined(ARMNN_ONNX_PARSER) |
| 301 | return MainImpl<armnnOnnxParser::IOnnxParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
| 302 | inputName.c_str(), inputTensorShape.get(), |
| 303 | inputTensorDataFilePath.c_str(), outputName.c_str(), |
| 304 | enableProfiling, subgraphId, runtime); |
| 305 | #else |
| 306 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Onnx parser support."; |
| 307 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 308 | #endif |
| 309 | } |
| 310 | else if (modelFormat.find("tensorflow") != std::string::npos) |
| 311 | { |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 312 | #if defined(ARMNN_TF_PARSER) |
| 313 | return MainImpl<armnnTfParser::ITfParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 314 | inputName.c_str(), inputTensorShape.get(), |
| 315 | inputTensorDataFilePath.c_str(), outputName.c_str(), |
| 316 | enableProfiling, subgraphId, runtime); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 317 | #else |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 318 | BOOST_LOG_TRIVIAL(fatal) << "Not built with Tensorflow parser support."; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 319 | return EXIT_FAILURE; |
| 320 | #endif |
| 321 | } |
| 322 | else if(modelFormat.find("tflite") != std::string::npos) |
| 323 | { |
| 324 | #if defined(ARMNN_TF_LITE_PARSER) |
| 325 | if (! isModelBinary) |
| 326 | { |
| 327 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << "'. Only 'binary' format supported \ |
| 328 | for tflite files"; |
| 329 | return EXIT_FAILURE; |
| 330 | } |
| 331 | return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(modelPath.c_str(), isModelBinary, computeDevice, |
| 332 | inputName.c_str(), inputTensorShape.get(), |
| 333 | inputTensorDataFilePath.c_str(), outputName.c_str(), |
| 334 | enableProfiling, subgraphId, runtime); |
| 335 | #else |
| 336 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << |
| 337 | "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
| 338 | return EXIT_FAILURE; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 339 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 340 | } |
| 341 | else |
| 342 | { |
| 343 | BOOST_LOG_TRIVIAL(fatal) << "Unknown model format: '" << modelFormat << |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 344 | "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
| 345 | return EXIT_FAILURE; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | int RunCsvTest(const armnnUtils::CsvRow &csvRow, |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 350 | const std::shared_ptr<armnn::IRuntime>& runtime, const bool enableProfiling) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 351 | { |
| 352 | std::string modelFormat; |
| 353 | std::string modelPath; |
| 354 | std::string inputName; |
| 355 | std::string inputTensorShapeStr; |
| 356 | std::string inputTensorDataFilePath; |
| 357 | std::string outputName; |
| 358 | |
| 359 | size_t subgraphId = 0; |
| 360 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 361 | const std::string backendsMessage = std::string("The preferred order of devices to run layers on by default. ") |
| 362 | + std::string("Possible choices: ") |
| 363 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 364 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 365 | po::options_description desc("Options"); |
| 366 | try |
| 367 | { |
| 368 | desc.add_options() |
| 369 | ("model-format,f", po::value(&modelFormat), |
| 370 | "caffe-binary, caffe-text, tflite-binary, onnx-binary, onnx-text, tensorflow-binary or tensorflow-text.") |
| 371 | ("model-path,m", po::value(&modelPath), "Path to model file, e.g. .caffemodel, .prototxt, .tflite," |
| 372 | " .onnx") |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 373 | ("compute,c", po::value<std::vector<armnn::BackendId>>()->multitoken(), |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 374 | backendsMessage.c_str()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 375 | ("input-name,i", po::value(&inputName), "Identifier of the input tensor in the network.") |
| 376 | ("subgraph-number,n", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be " |
| 377 | "executed. Defaults to 0") |
| 378 | ("input-tensor-shape,s", po::value(&inputTensorShapeStr), |
| 379 | "The shape of the input tensor in the network as a flat array of integers separated by whitespace. " |
| 380 | "This parameter is optional, depending on the network.") |
| 381 | ("input-tensor-data,d", po::value(&inputTensorDataFilePath), |
| 382 | "Path to a file containing the input data as a flat array separated by whitespace.") |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 383 | ("output-name,o", po::value(&outputName), "Identifier of the output tensor in the network."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 384 | } |
| 385 | catch (const std::exception& e) |
| 386 | { |
| 387 | // Coverity points out that default_value(...) can throw a bad_lexical_cast, |
| 388 | // and that desc.add_options() can throw boost::io::too_few_args. |
| 389 | // They really won't in any of these cases. |
| 390 | BOOST_ASSERT_MSG(false, "Caught unexpected exception"); |
| 391 | BOOST_LOG_TRIVIAL(fatal) << "Fatal internal error: " << e.what(); |
| 392 | return EXIT_FAILURE; |
| 393 | } |
| 394 | |
| 395 | std::vector<const char*> clOptions; |
| 396 | clOptions.reserve(csvRow.values.size()); |
| 397 | for (const std::string& value : csvRow.values) |
| 398 | { |
| 399 | clOptions.push_back(value.c_str()); |
| 400 | } |
| 401 | |
| 402 | po::variables_map vm; |
| 403 | try |
| 404 | { |
| 405 | po::store(po::parse_command_line(static_cast<int>(clOptions.size()), clOptions.data(), desc), vm); |
| 406 | |
| 407 | po::notify(vm); |
| 408 | |
| 409 | CheckOptionDependencies(vm); |
| 410 | } |
| 411 | catch (const po::error& e) |
| 412 | { |
| 413 | std::cerr << e.what() << std::endl << std::endl; |
| 414 | std::cerr << desc << std::endl; |
| 415 | return EXIT_FAILURE; |
| 416 | } |
| 417 | |
| 418 | // Remove leading and trailing whitespaces from the parsed arguments. |
| 419 | boost::trim(modelFormat); |
| 420 | boost::trim(modelPath); |
| 421 | boost::trim(inputName); |
| 422 | boost::trim(inputTensorShapeStr); |
| 423 | boost::trim(inputTensorDataFilePath); |
| 424 | boost::trim(outputName); |
| 425 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 426 | // Get the preferred order of compute devices. |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 427 | std::vector<armnn::BackendId> computeDevices = vm["compute"].as<std::vector<armnn::BackendId>>(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 428 | |
| 429 | // Remove duplicates from the list of compute devices. |
| 430 | RemoveDuplicateDevices(computeDevices); |
| 431 | |
| 432 | // Check that the specified compute devices are valid. |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 433 | std::string invalidBackends; |
| 434 | if (!CheckRequestedBackendsAreValid(computeDevices, armnn::Optional<std::string&>(invalidBackends))) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 435 | { |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 436 | BOOST_LOG_TRIVIAL(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 437 | << invalidBackends; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 438 | return EXIT_FAILURE; |
| 439 | } |
| 440 | |
| 441 | return RunTest(modelFormat, inputTensorShapeStr, computeDevices, |
| 442 | modelPath, inputName, inputTensorDataFilePath, outputName, enableProfiling, subgraphId, runtime); |
| 443 | } |
| 444 | |
| 445 | int main(int argc, const char* argv[]) |
| 446 | { |
| 447 | // Configures logging for both the ARMNN library and this test program. |
| 448 | #ifdef NDEBUG |
| 449 | armnn::LogSeverity level = armnn::LogSeverity::Info; |
| 450 | #else |
| 451 | armnn::LogSeverity level = armnn::LogSeverity::Debug; |
| 452 | #endif |
| 453 | armnn::ConfigureLogging(true, true, level); |
| 454 | armnnUtils::ConfigureLogging(boost::log::core::get().get(), true, true, level); |
| 455 | |
| 456 | std::string testCasesFile; |
| 457 | |
| 458 | std::string modelFormat; |
| 459 | std::string modelPath; |
| 460 | std::string inputName; |
| 461 | std::string inputTensorShapeStr; |
| 462 | std::string inputTensorDataFilePath; |
| 463 | std::string outputName; |
| 464 | |
| 465 | size_t subgraphId = 0; |
| 466 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 467 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 468 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 469 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 470 | po::options_description desc("Options"); |
| 471 | try |
| 472 | { |
| 473 | desc.add_options() |
| 474 | ("help", "Display usage information") |
| 475 | ("test-cases,t", po::value(&testCasesFile), "Path to a CSV file containing test cases to run. " |
| 476 | "If set, further parameters -- with the exception of compute device and concurrency -- will be ignored, " |
| 477 | "as they are expected to be defined in the file for each test in particular.") |
| 478 | ("concurrent,n", po::bool_switch()->default_value(false), |
| 479 | "Whether or not the test cases should be executed in parallel") |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 480 | ("model-format,f", po::value(&modelFormat)->required(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 481 | "caffe-binary, caffe-text, onnx-binary, onnx-text, tflite-binary, tensorflow-binary or tensorflow-text.") |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 482 | ("model-path,m", po::value(&modelPath)->required(), "Path to model file, e.g. .caffemodel, .prototxt," |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 483 | " .tflite, .onnx") |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 484 | ("compute,c", po::value<std::vector<std::string>>()->multitoken(), |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 485 | backendsMessage.c_str()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 486 | ("input-name,i", po::value(&inputName), "Identifier of the input tensor in the network.") |
| 487 | ("subgraph-number,x", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be executed." |
| 488 | "Defaults to 0") |
| 489 | ("input-tensor-shape,s", po::value(&inputTensorShapeStr), |
| 490 | "The shape of the input tensor in the network as a flat array of integers separated by whitespace. " |
| 491 | "This parameter is optional, depending on the network.") |
| 492 | ("input-tensor-data,d", po::value(&inputTensorDataFilePath), |
| 493 | "Path to a file containing the input data as a flat array separated by whitespace.") |
| 494 | ("output-name,o", po::value(&outputName), "Identifier of the output tensor in the network.") |
| 495 | ("event-based-profiling,e", po::bool_switch()->default_value(false), |
| 496 | "Enables built in profiler. If unset, defaults to off."); |
| 497 | } |
| 498 | catch (const std::exception& e) |
| 499 | { |
| 500 | // Coverity points out that default_value(...) can throw a bad_lexical_cast, |
| 501 | // and that desc.add_options() can throw boost::io::too_few_args. |
| 502 | // They really won't in any of these cases. |
| 503 | BOOST_ASSERT_MSG(false, "Caught unexpected exception"); |
| 504 | BOOST_LOG_TRIVIAL(fatal) << "Fatal internal error: " << e.what(); |
| 505 | return EXIT_FAILURE; |
| 506 | } |
| 507 | |
| 508 | // Parses the command-line. |
| 509 | po::variables_map vm; |
| 510 | try |
| 511 | { |
| 512 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 513 | |
| 514 | if (CheckOption(vm, "help") || argc <= 1) |
| 515 | { |
| 516 | std::cout << "Executes a neural network model using the provided input tensor. " << std::endl; |
| 517 | std::cout << "Prints the resulting output tensor." << std::endl; |
| 518 | std::cout << std::endl; |
| 519 | std::cout << desc << std::endl; |
| 520 | return EXIT_SUCCESS; |
| 521 | } |
| 522 | |
| 523 | po::notify(vm); |
| 524 | } |
| 525 | catch (const po::error& e) |
| 526 | { |
| 527 | std::cerr << e.what() << std::endl << std::endl; |
| 528 | std::cerr << desc << std::endl; |
| 529 | return EXIT_FAILURE; |
| 530 | } |
| 531 | |
| 532 | // Get the value of the switch arguments. |
| 533 | bool concurrent = vm["concurrent"].as<bool>(); |
| 534 | bool enableProfiling = vm["event-based-profiling"].as<bool>(); |
| 535 | |
| 536 | // Check whether we have to load test cases from a file. |
| 537 | if (CheckOption(vm, "test-cases")) |
| 538 | { |
| 539 | // Check that the file exists. |
| 540 | if (!boost::filesystem::exists(testCasesFile)) |
| 541 | { |
| 542 | BOOST_LOG_TRIVIAL(fatal) << "Given file \"" << testCasesFile << "\" does not exist"; |
| 543 | return EXIT_FAILURE; |
| 544 | } |
| 545 | |
| 546 | // Parse CSV file and extract test cases |
| 547 | armnnUtils::CsvReader reader; |
| 548 | std::vector<armnnUtils::CsvRow> testCases = reader.ParseFile(testCasesFile); |
| 549 | |
| 550 | // Check that there is at least one test case to run |
| 551 | if (testCases.empty()) |
| 552 | { |
| 553 | BOOST_LOG_TRIVIAL(fatal) << "Given file \"" << testCasesFile << "\" has no test cases"; |
| 554 | return EXIT_FAILURE; |
| 555 | } |
| 556 | |
| 557 | // Create runtime |
| 558 | armnn::IRuntime::CreationOptions options; |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 559 | options.m_EnableGpuProfiling = enableProfiling; |
| 560 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 561 | std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options)); |
| 562 | |
| 563 | const std::string executableName("ExecuteNetwork"); |
| 564 | |
| 565 | // Check whether we need to run the test cases concurrently |
| 566 | if (concurrent) |
| 567 | { |
| 568 | std::vector<std::future<int>> results; |
| 569 | results.reserve(testCases.size()); |
| 570 | |
| 571 | // Run each test case in its own thread |
| 572 | for (auto& testCase : testCases) |
| 573 | { |
| 574 | testCase.values.insert(testCase.values.begin(), executableName); |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 575 | results.push_back(std::async(std::launch::async, RunCsvTest, std::cref(testCase), std::cref(runtime), |
| 576 | enableProfiling)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Check results |
| 580 | for (auto& result : results) |
| 581 | { |
| 582 | if (result.get() != EXIT_SUCCESS) |
| 583 | { |
| 584 | return EXIT_FAILURE; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | // Run tests sequentially |
| 591 | for (auto& testCase : testCases) |
| 592 | { |
| 593 | testCase.values.insert(testCase.values.begin(), executableName); |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 594 | if (RunCsvTest(testCase, runtime, enableProfiling) != EXIT_SUCCESS) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 595 | { |
| 596 | return EXIT_FAILURE; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | return EXIT_SUCCESS; |
| 602 | } |
| 603 | else // Run single test |
| 604 | { |
Aron Virginas-Tar | 382e21c | 2019-01-22 14:10:39 +0000 | [diff] [blame] | 605 | // Get the preferred order of compute devices. If none are specified, default to using CpuRef |
| 606 | const std::string computeOption("compute"); |
| 607 | std::vector<std::string> computeDevicesAsStrings = CheckOption(vm, computeOption.c_str()) ? |
| 608 | vm[computeOption].as<std::vector<std::string>>() : |
| 609 | std::vector<std::string>({ "CpuRef" }); |
Matteo Martincigh | 067112f | 2018-10-29 11:01:09 +0000 | [diff] [blame] | 610 | std::vector<armnn::BackendId> computeDevices(computeDevicesAsStrings.begin(), computeDevicesAsStrings.end()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 611 | |
| 612 | // Remove duplicates from the list of compute devices. |
| 613 | RemoveDuplicateDevices(computeDevices); |
| 614 | |
| 615 | // Check that the specified compute devices are valid. |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 616 | std::string invalidBackends; |
| 617 | if (!CheckRequestedBackendsAreValid(computeDevices, armnn::Optional<std::string&>(invalidBackends))) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 618 | { |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 619 | BOOST_LOG_TRIVIAL(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 620 | << invalidBackends; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 621 | return EXIT_FAILURE; |
| 622 | } |
| 623 | |
| 624 | try |
| 625 | { |
| 626 | CheckOptionDependencies(vm); |
| 627 | } |
| 628 | catch (const po::error& e) |
| 629 | { |
| 630 | std::cerr << e.what() << std::endl << std::endl; |
| 631 | std::cerr << desc << std::endl; |
| 632 | return EXIT_FAILURE; |
| 633 | } |
| 634 | |
| 635 | return RunTest(modelFormat, inputTensorShapeStr, computeDevices, |
| 636 | modelPath, inputName, inputTensorDataFilePath, outputName, enableProfiling, subgraphId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 637 | } |
| 638 | } |