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> |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 7 | #include <armnn/utility/Timer.hpp> |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 8 | |
| 9 | #if defined(ARMNN_SERIALIZER) |
| 10 | #include "armnnDeserializer/IDeserializer.hpp" |
| 11 | #endif |
| 12 | #if defined(ARMNN_CAFFE_PARSER) |
| 13 | #include "armnnCaffeParser/ICaffeParser.hpp" |
| 14 | #endif |
| 15 | #if defined(ARMNN_TF_PARSER) |
| 16 | #include "armnnTfParser/ITfParser.hpp" |
| 17 | #endif |
| 18 | #if defined(ARMNN_TF_LITE_PARSER) |
| 19 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 20 | #endif |
| 21 | #if defined(ARMNN_ONNX_PARSER) |
| 22 | #include "armnnOnnxParser/IOnnxParser.hpp" |
| 23 | #endif |
| 24 | #include "CsvReader.hpp" |
| 25 | #include "../InferenceTest.hpp" |
| 26 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 27 | #include <Profiling.hpp> |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 28 | #include <ResolveType.hpp> |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 29 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 30 | #include <boost/program_options.hpp> |
| 31 | #include <boost/variant.hpp> |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <fstream> |
| 35 | #include <functional> |
| 36 | #include <future> |
| 37 | #include <algorithm> |
| 38 | #include <iterator> |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | |
| 43 | // Configure boost::program_options for command-line parsing and validation. |
| 44 | namespace po = boost::program_options; |
| 45 | |
| 46 | template<typename T, typename TParseElementFunc> |
| 47 | std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char * chars = "\t ,:") |
| 48 | { |
| 49 | std::vector<T> result; |
| 50 | // Processes line-by-line. |
| 51 | std::string line; |
| 52 | while (std::getline(stream, line)) |
| 53 | { |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 54 | std::vector<std::string> tokens = armnn::stringUtils::StringTokenizer(line, chars); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 55 | for (const std::string& token : tokens) |
| 56 | { |
| 57 | if (!token.empty()) // See https://stackoverflow.com/questions/10437406/ |
| 58 | { |
| 59 | try |
| 60 | { |
| 61 | result.push_back(parseElementFunc(token)); |
| 62 | } |
| 63 | catch (const std::exception&) |
| 64 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 65 | ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | bool CheckOption(const po::variables_map& vm, |
| 75 | const char* option) |
| 76 | { |
| 77 | // Check that the given option is valid. |
| 78 | if (option == nullptr) |
| 79 | { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | // Check whether 'option' is provided. |
| 84 | return vm.find(option) != vm.end(); |
| 85 | } |
| 86 | |
| 87 | void CheckOptionDependency(const po::variables_map& vm, |
| 88 | const char* option, |
| 89 | const char* required) |
| 90 | { |
| 91 | // Check that the given options are valid. |
| 92 | if (option == nullptr || required == nullptr) |
| 93 | { |
| 94 | throw po::error("Invalid option to check dependency for"); |
| 95 | } |
| 96 | |
| 97 | // Check that if 'option' is provided, 'required' is also provided. |
| 98 | if (CheckOption(vm, option) && !vm[option].defaulted()) |
| 99 | { |
| 100 | if (CheckOption(vm, required) == 0 || vm[required].defaulted()) |
| 101 | { |
| 102 | throw po::error(std::string("Option '") + option + "' requires option '" + required + "'."); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void CheckOptionDependencies(const po::variables_map& vm) |
| 108 | { |
| 109 | CheckOptionDependency(vm, "model-path", "model-format"); |
| 110 | CheckOptionDependency(vm, "model-path", "input-name"); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 111 | CheckOptionDependency(vm, "model-path", "output-name"); |
| 112 | CheckOptionDependency(vm, "input-tensor-shape", "model-path"); |
| 113 | } |
| 114 | |
| 115 | template<armnn::DataType NonQuantizedType> |
| 116 | auto ParseDataArray(std::istream & stream); |
| 117 | |
| 118 | template<armnn::DataType QuantizedType> |
| 119 | auto ParseDataArray(std::istream& stream, |
| 120 | const float& quantizationScale, |
| 121 | const int32_t& quantizationOffset); |
| 122 | |
| 123 | template<> |
| 124 | auto ParseDataArray<armnn::DataType::Float32>(std::istream & stream) |
| 125 | { |
| 126 | return ParseArrayImpl<float>(stream, [](const std::string& s) { return std::stof(s); }); |
| 127 | } |
| 128 | |
| 129 | template<> |
| 130 | auto ParseDataArray<armnn::DataType::Signed32>(std::istream & stream) |
| 131 | { |
| 132 | return ParseArrayImpl<int>(stream, [](const std::string & s) { return std::stoi(s); }); |
| 133 | } |
| 134 | |
| 135 | template<> |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 136 | auto ParseDataArray<armnn::DataType::QAsymmU8>(std::istream& stream) |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 137 | { |
| 138 | return ParseArrayImpl<uint8_t>(stream, |
| 139 | [](const std::string& s) { return boost::numeric_cast<uint8_t>(std::stoi(s)); }); |
| 140 | } |
| 141 | |
| 142 | template<> |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 143 | auto ParseDataArray<armnn::DataType::QAsymmU8>(std::istream& stream, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 144 | const float& quantizationScale, |
| 145 | const int32_t& quantizationOffset) |
| 146 | { |
| 147 | return ParseArrayImpl<uint8_t>(stream, |
| 148 | [&quantizationScale, &quantizationOffset](const std::string & s) |
| 149 | { |
| 150 | return boost::numeric_cast<uint8_t>( |
Rob Hughes | 93667b1 | 2019-09-23 16:24:05 +0100 | [diff] [blame] | 151 | armnn::Quantize<uint8_t>(std::stof(s), |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 152 | quantizationScale, |
| 153 | quantizationOffset)); |
| 154 | }); |
| 155 | } |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 156 | std::vector<unsigned int> ParseArray(std::istream& stream) |
| 157 | { |
| 158 | return ParseArrayImpl<unsigned int>(stream, |
| 159 | [](const std::string& s) { return boost::numeric_cast<unsigned int>(std::stoi(s)); }); |
| 160 | } |
| 161 | |
| 162 | std::vector<std::string> ParseStringList(const std::string & inputString, const char * delimiter) |
| 163 | { |
| 164 | std::stringstream stream(inputString); |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 165 | return ParseArrayImpl<std::string>(stream, [](const std::string& s) { |
| 166 | return armnn::stringUtils::StringTrimCopy(s); }, delimiter); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void RemoveDuplicateDevices(std::vector<armnn::BackendId>& computeDevices) |
| 170 | { |
| 171 | // Mark the duplicate devices as 'Undefined'. |
| 172 | for (auto i = computeDevices.begin(); i != computeDevices.end(); ++i) |
| 173 | { |
| 174 | for (auto j = std::next(i); j != computeDevices.end(); ++j) |
| 175 | { |
| 176 | if (*j == *i) |
| 177 | { |
| 178 | *j = armnn::Compute::Undefined; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Remove 'Undefined' devices. |
| 184 | computeDevices.erase(std::remove(computeDevices.begin(), computeDevices.end(), armnn::Compute::Undefined), |
| 185 | computeDevices.end()); |
| 186 | } |
| 187 | |
| 188 | struct TensorPrinter : public boost::static_visitor<> |
| 189 | { |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 190 | TensorPrinter(const std::string& binding, |
| 191 | const armnn::TensorInfo& info, |
| 192 | const std::string& outputTensorFile, |
| 193 | bool dequantizeOutput) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 194 | : m_OutputBinding(binding) |
| 195 | , m_Scale(info.GetQuantizationScale()) |
| 196 | , m_Offset(info.GetQuantizationOffset()) |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 197 | , m_OutputTensorFile(outputTensorFile) |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 198 | , m_DequantizeOutput(dequantizeOutput) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 199 | {} |
| 200 | |
| 201 | void operator()(const std::vector<float>& values) |
| 202 | { |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 203 | ForEachValue(values, [](float value) |
| 204 | { |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 205 | printf("%f ", value); |
| 206 | }); |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 207 | WriteToFile(values); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void operator()(const std::vector<uint8_t>& values) |
| 211 | { |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 212 | if(m_DequantizeOutput) |
| 213 | { |
| 214 | auto& scale = m_Scale; |
| 215 | auto& offset = m_Offset; |
| 216 | std::vector<float> dequantizedValues; |
| 217 | ForEachValue(values, [&scale, &offset, &dequantizedValues](uint8_t value) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 218 | { |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 219 | auto dequantizedValue = armnn::Dequantize(value, scale, offset); |
| 220 | printf("%f ", dequantizedValue); |
| 221 | dequantizedValues.push_back(dequantizedValue); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 222 | }); |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 223 | WriteToFile(dequantizedValues); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | const std::vector<int> intValues(values.begin(), values.end()); |
| 228 | operator()(intValues); |
| 229 | } |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void operator()(const std::vector<int>& values) |
| 233 | { |
| 234 | ForEachValue(values, [](int value) |
| 235 | { |
| 236 | printf("%d ", value); |
| 237 | }); |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 238 | WriteToFile(values); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private: |
| 242 | template<typename Container, typename Delegate> |
| 243 | void ForEachValue(const Container& c, Delegate delegate) |
| 244 | { |
| 245 | std::cout << m_OutputBinding << ": "; |
| 246 | for (const auto& value : c) |
| 247 | { |
| 248 | delegate(value); |
| 249 | } |
| 250 | printf("\n"); |
| 251 | } |
| 252 | |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 253 | template<typename T> |
| 254 | void WriteToFile(const std::vector<T>& values) |
| 255 | { |
| 256 | if (!m_OutputTensorFile.empty()) |
| 257 | { |
| 258 | std::ofstream outputTensorFile; |
| 259 | outputTensorFile.open(m_OutputTensorFile, std::ofstream::out | std::ofstream::trunc); |
| 260 | if (outputTensorFile.is_open()) |
| 261 | { |
| 262 | outputTensorFile << m_OutputBinding << ": "; |
| 263 | std::copy(values.begin(), values.end(), std::ostream_iterator<T>(outputTensorFile, " ")); |
| 264 | } |
| 265 | else |
| 266 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 267 | ARMNN_LOG(info) << "Output Tensor File: " << m_OutputTensorFile << " could not be opened!"; |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 268 | } |
| 269 | outputTensorFile.close(); |
| 270 | } |
| 271 | } |
| 272 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 273 | std::string m_OutputBinding; |
| 274 | float m_Scale=0.0f; |
| 275 | int m_Offset=0; |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 276 | std::string m_OutputTensorFile; |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 277 | bool m_DequantizeOutput = false; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 281 | |
| 282 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 283 | std::vector<T> GenerateDummyTensorData(unsigned int numElements) |
| 284 | { |
| 285 | return std::vector<T>(numElements, static_cast<T>(0)); |
| 286 | } |
| 287 | |
| 288 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
| 289 | using QuantizationParams = std::pair<float, int32_t>; |
| 290 | |
| 291 | void PopulateTensorWithData(TContainer& tensorData, |
| 292 | unsigned int numElements, |
| 293 | const std::string& dataTypeStr, |
| 294 | const armnn::Optional<QuantizationParams>& qParams, |
| 295 | const armnn::Optional<std::string>& dataFile) |
| 296 | { |
| 297 | const bool readFromFile = dataFile.has_value() && !dataFile.value().empty(); |
| 298 | const bool quantizeData = qParams.has_value(); |
| 299 | |
| 300 | std::ifstream inputTensorFile; |
| 301 | if (readFromFile) |
| 302 | { |
| 303 | inputTensorFile = std::ifstream(dataFile.value()); |
| 304 | } |
| 305 | |
| 306 | if (dataTypeStr.compare("float") == 0) |
| 307 | { |
| 308 | if (quantizeData) |
| 309 | { |
| 310 | const float qScale = qParams.value().first; |
| 311 | const int qOffset = qParams.value().second; |
| 312 | |
| 313 | tensorData = readFromFile ? |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 314 | ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile, qScale, qOffset) : |
| 315 | GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements); |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | { |
| 319 | tensorData = readFromFile ? |
| 320 | ParseDataArray<armnn::DataType::Float32>(inputTensorFile) : |
| 321 | GenerateDummyTensorData<armnn::DataType::Float32>(numElements); |
| 322 | } |
| 323 | } |
| 324 | else if (dataTypeStr.compare("int") == 0) |
| 325 | { |
| 326 | tensorData = readFromFile ? |
| 327 | ParseDataArray<armnn::DataType::Signed32>(inputTensorFile) : |
| 328 | GenerateDummyTensorData<armnn::DataType::Signed32>(numElements); |
| 329 | } |
| 330 | else if (dataTypeStr.compare("qasymm8") == 0) |
| 331 | { |
| 332 | tensorData = readFromFile ? |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 333 | ParseDataArray<armnn::DataType::QAsymmU8>(inputTensorFile) : |
| 334 | GenerateDummyTensorData<armnn::DataType::QAsymmU8>(numElements); |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | { |
| 338 | std::string errorMessage = "Unsupported tensor data type " + dataTypeStr; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 339 | ARMNN_LOG(fatal) << errorMessage; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 340 | |
| 341 | inputTensorFile.close(); |
| 342 | throw armnn::Exception(errorMessage); |
| 343 | } |
| 344 | |
| 345 | inputTensorFile.close(); |
| 346 | } |
| 347 | |
| 348 | } // anonymous namespace |
| 349 | |
| 350 | bool generateTensorData = true; |
| 351 | |
| 352 | struct ExecuteNetworkParams |
| 353 | { |
| 354 | using TensorShapePtr = std::unique_ptr<armnn::TensorShape>; |
| 355 | |
| 356 | const char* m_ModelPath; |
| 357 | bool m_IsModelBinary; |
| 358 | std::vector<armnn::BackendId> m_ComputeDevices; |
| 359 | std::string m_DynamicBackendsPath; |
| 360 | std::vector<string> m_InputNames; |
| 361 | std::vector<TensorShapePtr> m_InputTensorShapes; |
| 362 | std::vector<string> m_InputTensorDataFilePaths; |
| 363 | std::vector<string> m_InputTypes; |
| 364 | bool m_QuantizeInput; |
| 365 | std::vector<string> m_OutputTypes; |
| 366 | std::vector<string> m_OutputNames; |
| 367 | std::vector<string> m_OutputTensorFiles; |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 368 | bool m_DequantizeOutput; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 369 | bool m_EnableProfiling; |
| 370 | bool m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 371 | bool m_EnableBf16TurboMode; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 372 | double m_ThresholdTime; |
| 373 | bool m_PrintIntermediate; |
| 374 | size_t m_SubgraphId; |
| 375 | bool m_EnableLayerDetails = false; |
| 376 | bool m_GenerateTensorData; |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 377 | bool m_ParseUnsupported = false; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 378 | }; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 379 | |
| 380 | template<typename TParser, typename TDataType> |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 381 | int MainImpl(const ExecuteNetworkParams& params, |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 382 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr, |
| 383 | size_t iterations = 1) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 384 | { |
| 385 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
| 386 | |
| 387 | std::vector<TContainer> inputDataContainers; |
| 388 | |
| 389 | try |
| 390 | { |
| 391 | // Creates an InferenceModel, which will parse the model and load it into an IRuntime. |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 392 | typename InferenceModel<TParser, TDataType>::Params inferenceModelParams; |
| 393 | inferenceModelParams.m_ModelPath = params.m_ModelPath; |
| 394 | inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary; |
| 395 | inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices; |
| 396 | inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath; |
| 397 | inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate; |
| 398 | inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails; |
Derek Lamberti | 167c082 | 2019-12-03 09:37:32 +0000 | [diff] [blame] | 399 | inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 400 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 401 | for(const std::string& inputName: params.m_InputNames) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 402 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 403 | inferenceModelParams.m_InputBindings.push_back(inputName); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 406 | for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 407 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 408 | inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 411 | for(const std::string& outputName: params.m_OutputNames) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 412 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 413 | inferenceModelParams.m_OutputBindings.push_back(outputName); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 416 | inferenceModelParams.m_SubgraphId = params.m_SubgraphId; |
| 417 | inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 418 | inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 419 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 420 | InferenceModel<TParser, TDataType> model(inferenceModelParams, |
| 421 | params.m_EnableProfiling, |
| 422 | params.m_DynamicBackendsPath, |
| 423 | runtime); |
| 424 | |
| 425 | const size_t numInputs = inferenceModelParams.m_InputBindings.size(); |
| 426 | for(unsigned int i = 0; i < numInputs; ++i) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 427 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 428 | armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ? |
| 429 | armnn::MakeOptional<QuantizationParams>(model.GetInputQuantizationParams()) : |
| 430 | armnn::EmptyOptional(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 431 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 432 | armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ? |
| 433 | armnn::EmptyOptional() : |
| 434 | armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[i]); |
| 435 | |
| 436 | unsigned int numElements = model.GetInputSize(i); |
| 437 | if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i]) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 438 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 439 | // If the user has provided a tensor shape for the current input, |
| 440 | // override numElements |
| 441 | numElements = params.m_InputTensorShapes[i]->GetNumElements(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 444 | TContainer tensorData; |
| 445 | PopulateTensorWithData(tensorData, |
| 446 | numElements, |
| 447 | params.m_InputTypes[i], |
| 448 | qParams, |
| 449 | dataFile); |
| 450 | |
| 451 | inputDataContainers.push_back(tensorData); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 454 | const size_t numOutputs = inferenceModelParams.m_OutputBindings.size(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 455 | std::vector<TContainer> outputDataContainers; |
| 456 | |
| 457 | for (unsigned int i = 0; i < numOutputs; ++i) |
| 458 | { |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 459 | if (params.m_OutputTypes[i].compare("float") == 0) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 460 | { |
| 461 | outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i))); |
| 462 | } |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 463 | else if (params.m_OutputTypes[i].compare("int") == 0) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 464 | { |
| 465 | outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i))); |
| 466 | } |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 467 | else if (params.m_OutputTypes[i].compare("qasymm8") == 0) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 468 | { |
| 469 | outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i))); |
| 470 | } |
| 471 | else |
| 472 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 473 | ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". "; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 474 | return EXIT_FAILURE; |
| 475 | } |
| 476 | } |
| 477 | |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 478 | for (size_t x = 0; x < iterations; x++) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 479 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 480 | // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds) |
| 481 | auto inference_duration = model.Run(inputDataContainers, outputDataContainers); |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 482 | |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 483 | if (params.m_GenerateTensorData) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 484 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 485 | ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful"; |
| 486 | } |
| 487 | |
| 488 | // Print output tensors |
| 489 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 490 | for (size_t i = 0; i < numOutputs; i++) |
| 491 | { |
| 492 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 493 | auto outputTensorFile = params.m_OutputTensorFiles.empty() ? "" : params.m_OutputTensorFiles[i]; |
| 494 | |
| 495 | TensorPrinter printer(inferenceModelParams.m_OutputBindings[i], |
| 496 | infoOut, |
| 497 | outputTensorFile, |
| 498 | params.m_DequantizeOutput); |
| 499 | boost::apply_visitor(printer, outputDataContainers[i]); |
| 500 | } |
| 501 | |
| 502 | ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2) |
| 503 | << std::fixed << inference_duration.count() << " ms\n"; |
| 504 | |
| 505 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 506 | if (params.m_ThresholdTime != 0.0) |
| 507 | { |
| 508 | ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2) |
| 509 | << std::fixed << params.m_ThresholdTime << " ms"; |
| 510 | auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count(); |
| 511 | ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 512 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 513 | |
| 514 | if (thresholdMinusInference < 0) |
| 515 | { |
| 516 | std::string errorMessage = "Elapsed inference time is greater than provided threshold time."; |
| 517 | ARMNN_LOG(fatal) << errorMessage; |
| 518 | } |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 519 | } |
| 520 | } |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 521 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 522 | catch (const armnn::Exception& e) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 523 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 524 | ARMNN_LOG(fatal) << "Armnn Error: " << e.what(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 525 | return EXIT_FAILURE; |
| 526 | } |
| 527 | |
| 528 | return EXIT_SUCCESS; |
| 529 | } |
| 530 | |
| 531 | // This will run a test |
| 532 | int RunTest(const std::string& format, |
| 533 | const std::string& inputTensorShapesStr, |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 534 | const vector<armnn::BackendId>& computeDevices, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 535 | const std::string& dynamicBackendsPath, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 536 | const std::string& path, |
| 537 | const std::string& inputNames, |
| 538 | const std::string& inputTensorDataFilePaths, |
| 539 | const std::string& inputTypes, |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 540 | bool quantizeInput, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 541 | const std::string& outputTypes, |
| 542 | const std::string& outputNames, |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 543 | const std::string& outputTensorFiles, |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 544 | bool dequantizeOuput, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 545 | bool enableProfiling, |
| 546 | bool enableFp16TurboMode, |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 547 | bool enableBf16TurboMode, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 548 | const double& thresholdTime, |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 549 | bool printIntermediate, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 550 | const size_t subgraphId, |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 551 | bool enableLayerDetails = false, |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 552 | bool parseUnsupported = false, |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 553 | const size_t iterations = 1, |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 554 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 555 | { |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 556 | std::string modelFormat = armnn::stringUtils::StringTrimCopy(format); |
| 557 | std::string modelPath = armnn::stringUtils::StringTrimCopy(path); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 558 | std::vector<std::string> inputNamesVector = ParseStringList(inputNames, ","); |
Francis Murtagh | 1555cbd | 2019-10-08 14:47:46 +0100 | [diff] [blame] | 559 | std::vector<std::string> inputTensorShapesVector = ParseStringList(inputTensorShapesStr, ":"); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 560 | std::vector<std::string> inputTensorDataFilePathsVector = ParseStringList( |
| 561 | inputTensorDataFilePaths, ","); |
| 562 | std::vector<std::string> outputNamesVector = ParseStringList(outputNames, ","); |
| 563 | std::vector<std::string> inputTypesVector = ParseStringList(inputTypes, ","); |
| 564 | std::vector<std::string> outputTypesVector = ParseStringList(outputTypes, ","); |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 565 | std::vector<std::string> outputTensorFilesVector = ParseStringList(outputTensorFiles, ","); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 566 | |
| 567 | // Parse model binary flag from the model-format string we got from the command-line |
| 568 | bool isModelBinary; |
| 569 | if (modelFormat.find("bin") != std::string::npos) |
| 570 | { |
| 571 | isModelBinary = true; |
| 572 | } |
| 573 | else if (modelFormat.find("txt") != std::string::npos || modelFormat.find("text") != std::string::npos) |
| 574 | { |
| 575 | isModelBinary = false; |
| 576 | } |
| 577 | else |
| 578 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 579 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat << "'. Please include 'binary' or 'text'"; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 580 | return EXIT_FAILURE; |
| 581 | } |
| 582 | |
| 583 | if ((inputTensorShapesVector.size() != 0) && (inputTensorShapesVector.size() != inputNamesVector.size())) |
| 584 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 585 | ARMNN_LOG(fatal) << "input-name and input-tensor-shape must have the same amount of elements."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 586 | return EXIT_FAILURE; |
| 587 | } |
| 588 | |
| 589 | if ((inputTensorDataFilePathsVector.size() != 0) && |
| 590 | (inputTensorDataFilePathsVector.size() != inputNamesVector.size())) |
| 591 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 592 | ARMNN_LOG(fatal) << "input-name and input-tensor-data must have the same amount of elements."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 593 | return EXIT_FAILURE; |
| 594 | } |
| 595 | |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 596 | if ((outputTensorFilesVector.size() != 0) && |
| 597 | (outputTensorFilesVector.size() != outputNamesVector.size())) |
| 598 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 599 | ARMNN_LOG(fatal) << "output-name and write-outputs-to-file must have the same amount of elements."; |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 600 | return EXIT_FAILURE; |
| 601 | } |
| 602 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 603 | if (inputTypesVector.size() == 0) |
| 604 | { |
| 605 | //Defaults the value of all inputs to "float" |
| 606 | inputTypesVector.assign(inputNamesVector.size(), "float"); |
| 607 | } |
Matteo Martincigh | 08b5186 | 2019-08-29 16:26:10 +0100 | [diff] [blame] | 608 | else if ((inputTypesVector.size() != 0) && (inputTypesVector.size() != inputNamesVector.size())) |
| 609 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 610 | ARMNN_LOG(fatal) << "input-name and input-type must have the same amount of elements."; |
Matteo Martincigh | 08b5186 | 2019-08-29 16:26:10 +0100 | [diff] [blame] | 611 | return EXIT_FAILURE; |
| 612 | } |
| 613 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 614 | if (outputTypesVector.size() == 0) |
| 615 | { |
| 616 | //Defaults the value of all outputs to "float" |
| 617 | outputTypesVector.assign(outputNamesVector.size(), "float"); |
| 618 | } |
Matteo Martincigh | 08b5186 | 2019-08-29 16:26:10 +0100 | [diff] [blame] | 619 | else if ((outputTypesVector.size() != 0) && (outputTypesVector.size() != outputNamesVector.size())) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 620 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 621 | ARMNN_LOG(fatal) << "output-name and output-type must have the same amount of elements."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 622 | return EXIT_FAILURE; |
| 623 | } |
| 624 | |
| 625 | // Parse input tensor shape from the string we got from the command-line. |
| 626 | std::vector<std::unique_ptr<armnn::TensorShape>> inputTensorShapes; |
| 627 | |
| 628 | if (!inputTensorShapesVector.empty()) |
| 629 | { |
| 630 | inputTensorShapes.reserve(inputTensorShapesVector.size()); |
| 631 | |
| 632 | for(const std::string& shape : inputTensorShapesVector) |
| 633 | { |
| 634 | std::stringstream ss(shape); |
| 635 | std::vector<unsigned int> dims = ParseArray(ss); |
| 636 | |
| 637 | try |
| 638 | { |
| 639 | // Coverity fix: An exception of type armnn::InvalidArgumentException is thrown and never caught. |
Rob Hughes | bb46dde | 2020-05-20 15:27:37 +0100 | [diff] [blame] | 640 | inputTensorShapes.push_back( |
| 641 | std::make_unique<armnn::TensorShape>(static_cast<unsigned int>(dims.size()), dims.data())); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 642 | } |
| 643 | catch (const armnn::InvalidArgumentException& e) |
| 644 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 645 | ARMNN_LOG(fatal) << "Cannot create tensor shape: " << e.what(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 646 | return EXIT_FAILURE; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Check that threshold time is not less than zero |
| 652 | if (thresholdTime < 0) |
| 653 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 654 | ARMNN_LOG(fatal) << "Threshold time supplied as a command line argument is less than zero."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 655 | return EXIT_FAILURE; |
| 656 | } |
| 657 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 658 | ExecuteNetworkParams params; |
| 659 | params.m_ModelPath = modelPath.c_str(); |
| 660 | params.m_IsModelBinary = isModelBinary; |
| 661 | params.m_ComputeDevices = computeDevices; |
| 662 | params.m_DynamicBackendsPath = dynamicBackendsPath; |
| 663 | params.m_InputNames = inputNamesVector; |
| 664 | params.m_InputTensorShapes = std::move(inputTensorShapes); |
| 665 | params.m_InputTensorDataFilePaths = inputTensorDataFilePathsVector; |
| 666 | params.m_InputTypes = inputTypesVector; |
| 667 | params.m_QuantizeInput = quantizeInput; |
| 668 | params.m_OutputTypes = outputTypesVector; |
| 669 | params.m_OutputNames = outputNamesVector; |
| 670 | params.m_OutputTensorFiles = outputTensorFilesVector; |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 671 | params.m_DequantizeOutput = dequantizeOuput; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 672 | params.m_EnableProfiling = enableProfiling; |
| 673 | params.m_EnableFp16TurboMode = enableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 674 | params.m_EnableBf16TurboMode = enableBf16TurboMode; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 675 | params.m_ThresholdTime = thresholdTime; |
| 676 | params.m_PrintIntermediate = printIntermediate; |
| 677 | params.m_SubgraphId = subgraphId; |
| 678 | params.m_EnableLayerDetails = enableLayerDetails; |
| 679 | params.m_GenerateTensorData = inputTensorDataFilePathsVector.empty(); |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 680 | params.m_ParseUnsupported = parseUnsupported; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 681 | |
| 682 | // Warn if ExecuteNetwork will generate dummy input data |
| 683 | if (params.m_GenerateTensorData) |
| 684 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 685 | ARMNN_LOG(warning) << "No input files provided, input tensors will be filled with 0s."; |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 688 | // Forward to implementation based on the parser type |
| 689 | if (modelFormat.find("armnn") != std::string::npos) |
| 690 | { |
| 691 | #if defined(ARMNN_SERIALIZER) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 692 | return MainImpl<armnnDeserializer::IDeserializer, float>(params, runtime, iterations); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 693 | #else |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 694 | ARMNN_LOG(fatal) << "Not built with serialization support."; |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 695 | return EXIT_FAILURE; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 696 | #endif |
| 697 | } |
| 698 | else if (modelFormat.find("caffe") != std::string::npos) |
| 699 | { |
| 700 | #if defined(ARMNN_CAFFE_PARSER) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 701 | return MainImpl<armnnCaffeParser::ICaffeParser, float>(params, runtime, iterations); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 702 | #else |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 703 | ARMNN_LOG(fatal) << "Not built with Caffe parser support."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 704 | return EXIT_FAILURE; |
| 705 | #endif |
| 706 | } |
| 707 | else if (modelFormat.find("onnx") != std::string::npos) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 708 | { |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 709 | #if defined(ARMNN_ONNX_PARSER) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 710 | return MainImpl<armnnOnnxParser::IOnnxParser, float>(params, runtime, iterations); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 711 | #else |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 712 | ARMNN_LOG(fatal) << "Not built with Onnx parser support."; |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 713 | return EXIT_FAILURE; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 714 | #endif |
| 715 | } |
| 716 | else if (modelFormat.find("tensorflow") != std::string::npos) |
| 717 | { |
| 718 | #if defined(ARMNN_TF_PARSER) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 719 | return MainImpl<armnnTfParser::ITfParser, float>(params, runtime, iterations); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 720 | #else |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 721 | ARMNN_LOG(fatal) << "Not built with Tensorflow parser support."; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 722 | return EXIT_FAILURE; |
| 723 | #endif |
| 724 | } |
| 725 | else if(modelFormat.find("tflite") != std::string::npos) |
| 726 | { |
| 727 | #if defined(ARMNN_TF_LITE_PARSER) |
| 728 | if (! isModelBinary) |
| 729 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 730 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat |
| 731 | << "'. Only 'binary' format supported for tflite files"; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 732 | return EXIT_FAILURE; |
| 733 | } |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 734 | return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(params, runtime, iterations); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 735 | #else |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 736 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat |
| 737 | << "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 738 | return EXIT_FAILURE; |
| 739 | #endif |
| 740 | } |
| 741 | else |
| 742 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 743 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat |
| 744 | << "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 745 | return EXIT_FAILURE; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | int RunCsvTest(const armnnUtils::CsvRow &csvRow, const std::shared_ptr<armnn::IRuntime>& runtime, |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 750 | const bool enableProfiling, const bool enableFp16TurboMode, const bool enableBf16TurboMode, |
| 751 | const double& thresholdTime, const bool printIntermediate, bool enableLayerDetails = false, |
| 752 | bool parseUnuspported = false) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 753 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 754 | IgnoreUnused(runtime); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 755 | std::string modelFormat; |
| 756 | std::string modelPath; |
| 757 | std::string inputNames; |
| 758 | std::string inputTensorShapes; |
| 759 | std::string inputTensorDataFilePaths; |
| 760 | std::string outputNames; |
| 761 | std::string inputTypes; |
| 762 | std::string outputTypes; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 763 | std::string dynamicBackendsPath; |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 764 | std::string outputTensorFiles; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 765 | |
| 766 | size_t subgraphId = 0; |
| 767 | |
| 768 | const std::string backendsMessage = std::string("The preferred order of devices to run layers on by default. ") |
| 769 | + std::string("Possible choices: ") |
| 770 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 771 | |
| 772 | po::options_description desc("Options"); |
| 773 | try |
| 774 | { |
| 775 | desc.add_options() |
| 776 | ("model-format,f", po::value(&modelFormat), |
| 777 | "armnn-binary, caffe-binary, caffe-text, tflite-binary, onnx-binary, onnx-text, tensorflow-binary or " |
| 778 | "tensorflow-text.") |
| 779 | ("model-path,m", po::value(&modelPath), "Path to model file, e.g. .armnn, .caffemodel, .prototxt, " |
| 780 | ".tflite, .onnx") |
| 781 | ("compute,c", po::value<std::vector<armnn::BackendId>>()->multitoken(), |
| 782 | backendsMessage.c_str()) |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 783 | ("dynamic-backends-path,b", po::value(&dynamicBackendsPath), |
| 784 | "Path where to load any available dynamic backend from. " |
| 785 | "If left empty (the default), dynamic backends will not be used.") |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 786 | ("input-name,i", po::value(&inputNames), "Identifier of the input tensors in the network separated by comma.") |
| 787 | ("subgraph-number,n", po::value<size_t>(&subgraphId)->default_value(0), "Id of the subgraph to be " |
| 788 | "executed. Defaults to 0.") |
| 789 | ("input-tensor-shape,s", po::value(&inputTensorShapes), |
| 790 | "The shape of the input tensors in the network as a flat array of integers separated by comma. " |
| 791 | "Several shapes can be passed separating them by semicolon. " |
| 792 | "This parameter is optional, depending on the network.") |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 793 | ("input-tensor-data,d", po::value(&inputTensorDataFilePaths)->default_value(""), |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 794 | "Path to files containing the input data as a flat array separated by whitespace. " |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 795 | "Several paths can be passed separating them by comma. If not specified, the network will be run with dummy " |
| 796 | "data (useful for profiling).") |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 797 | ("input-type,y",po::value(&inputTypes), "The type of the input tensors in the network separated by comma. " |
| 798 | "If unset, defaults to \"float\" for all defined inputs. " |
| 799 | "Accepted values (float, int or qasymm8).") |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 800 | ("quantize-input,q",po::bool_switch()->default_value(false), |
| 801 | "If this option is enabled, all float inputs will be quantized to qasymm8. " |
| 802 | "If unset, default to not quantized. " |
| 803 | "Accepted values (true or false)") |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 804 | ("output-type,z",po::value(&outputTypes), "The type of the output tensors in the network separated by comma. " |
| 805 | "If unset, defaults to \"float\" for all defined outputs. " |
| 806 | "Accepted values (float, int or qasymm8).") |
| 807 | ("output-name,o", po::value(&outputNames), |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 808 | "Identifier of the output tensors in the network separated by comma.") |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 809 | ("dequantize-output,l",po::bool_switch()->default_value(false), |
| 810 | "If this option is enabled, all quantized outputs will be dequantized to float. " |
| 811 | "If unset, default to not get dequantized. " |
| 812 | "Accepted values (true or false)") |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 813 | ("write-outputs-to-file,w", po::value(&outputTensorFiles), |
| 814 | "Comma-separated list of output file paths keyed with the binding-id of the output slot. " |
| 815 | "If left empty (the default), the output tensors will not be written to a file."); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 816 | } |
| 817 | catch (const std::exception& e) |
| 818 | { |
| 819 | // Coverity points out that default_value(...) can throw a bad_lexical_cast, |
| 820 | // and that desc.add_options() can throw boost::io::too_few_args. |
| 821 | // They really won't in any of these cases. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 822 | ARMNN_ASSERT_MSG(false, "Caught unexpected exception"); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 823 | ARMNN_LOG(fatal) << "Fatal internal error: " << e.what(); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 824 | return EXIT_FAILURE; |
| 825 | } |
| 826 | |
| 827 | std::vector<const char*> clOptions; |
| 828 | clOptions.reserve(csvRow.values.size()); |
| 829 | for (const std::string& value : csvRow.values) |
| 830 | { |
| 831 | clOptions.push_back(value.c_str()); |
| 832 | } |
| 833 | |
| 834 | po::variables_map vm; |
| 835 | try |
| 836 | { |
| 837 | po::store(po::parse_command_line(static_cast<int>(clOptions.size()), clOptions.data(), desc), vm); |
| 838 | |
| 839 | po::notify(vm); |
| 840 | |
| 841 | CheckOptionDependencies(vm); |
| 842 | } |
| 843 | catch (const po::error& e) |
| 844 | { |
| 845 | std::cerr << e.what() << std::endl << std::endl; |
| 846 | std::cerr << desc << std::endl; |
| 847 | return EXIT_FAILURE; |
| 848 | } |
| 849 | |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 850 | // Get the value of the switch arguments. |
| 851 | bool quantizeInput = vm["quantize-input"].as<bool>(); |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 852 | bool dequantizeOutput = vm["dequantize-output"].as<bool>(); |
Narumol Prangnawarat | 610256f | 2019-06-26 15:10:46 +0100 | [diff] [blame] | 853 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 854 | // Get the preferred order of compute devices. |
| 855 | std::vector<armnn::BackendId> computeDevices = vm["compute"].as<std::vector<armnn::BackendId>>(); |
| 856 | |
| 857 | // Remove duplicates from the list of compute devices. |
| 858 | RemoveDuplicateDevices(computeDevices); |
| 859 | |
| 860 | // Check that the specified compute devices are valid. |
| 861 | std::string invalidBackends; |
| 862 | if (!CheckRequestedBackendsAreValid(computeDevices, armnn::Optional<std::string&>(invalidBackends))) |
| 863 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 864 | ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: " |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 865 | << invalidBackends; |
| 866 | return EXIT_FAILURE; |
| 867 | } |
| 868 | |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 869 | return RunTest(modelFormat, inputTensorShapes, computeDevices, dynamicBackendsPath, modelPath, inputNames, |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 870 | inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames, outputTensorFiles, |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 871 | dequantizeOutput, enableProfiling, enableFp16TurboMode, enableBf16TurboMode, |
| 872 | thresholdTime, printIntermediate, subgraphId, enableLayerDetails, parseUnuspported); |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 873 | } |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 874 | |
| 875 | #if defined(ARMCOMPUTECL_ENABLED) |
| 876 | int RunCLTuning(const std::string& tuningPath, |
| 877 | const int tuningLevel, |
| 878 | const std::string& modelFormat, |
| 879 | const std::string& inputTensorShapes, |
| 880 | const vector<armnn::BackendId>& computeDevices, |
| 881 | const std::string& dynamicBackendsPath, |
| 882 | const std::string& modelPath, |
| 883 | const std::string& inputNames, |
| 884 | const std::string& inputTensorDataFilePaths, |
| 885 | const std::string& inputTypes, |
| 886 | bool quantizeInput, |
| 887 | const std::string& outputTypes, |
| 888 | const std::string& outputNames, |
| 889 | const std::string& outputTensorFiles, |
| 890 | bool dequantizeOutput, |
| 891 | bool enableProfiling, |
| 892 | bool enableFp16TurboMode, |
| 893 | bool enableBf16TurboMode, |
| 894 | const double& thresholdTime, |
| 895 | bool printIntermediate, |
| 896 | const size_t subgraphId, |
| 897 | bool enableLayerDetails = false, |
| 898 | bool parseUnsupported = false) |
| 899 | { |
| 900 | armnn::IRuntime::CreationOptions options; |
| 901 | options.m_BackendOptions.emplace_back( |
| 902 | armnn::BackendOptions |
| 903 | { |
| 904 | "GpuAcc", |
| 905 | { |
| 906 | {"TuningLevel", tuningLevel}, |
| 907 | {"TuningFile", tuningPath.c_str()}, |
| 908 | {"KernelProfilingEnabled", enableProfiling} |
| 909 | } |
| 910 | } |
| 911 | ); |
| 912 | |
| 913 | std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(options)); |
| 914 | const auto start_time = armnn::GetTimeNow(); |
| 915 | |
| 916 | ARMNN_LOG(info) << "Tuning run...\n"; |
| 917 | int state = RunTest(modelFormat, inputTensorShapes, computeDevices, dynamicBackendsPath, modelPath, inputNames, |
| 918 | inputTensorDataFilePaths, inputTypes, quantizeInput, outputTypes, outputNames, |
| 919 | outputTensorFiles, dequantizeOutput, enableProfiling, enableFp16TurboMode, enableBf16TurboMode, |
| 920 | thresholdTime, printIntermediate, subgraphId, enableLayerDetails, parseUnsupported, 1, runtime); |
| 921 | |
| 922 | ARMNN_LOG(info) << "Tuning time: " << std::setprecision(2) |
| 923 | << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n"; |
| 924 | |
| 925 | return state; |
| 926 | } |
| 927 | #endif |