Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 5 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 6 | #include "NetworkExecutionUtils/NetworkExecutionUtils.hpp" |
| 7 | #include "ExecuteNetworkProgramOptions.hpp" |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 8 | #include <armnn/IAsyncExecutionCallback.hpp> |
| 9 | #include <AsyncExecutionCallback.hpp> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 10 | |
| 11 | #include <armnn/Logging.hpp> |
Rob Hughes | 9542f90 | 2021-07-14 09:48:54 +0100 | [diff] [blame] | 12 | #include <armnnUtils/Filesystem.hpp> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 13 | #include <InferenceTest.hpp> |
| 14 | |
| 15 | #if defined(ARMNN_SERIALIZER) |
| 16 | #include "armnnDeserializer/IDeserializer.hpp" |
| 17 | #endif |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 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 |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 24 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 25 | #include <armnn_delegate.hpp> |
| 26 | #include <DelegateOptions.hpp> |
| 27 | |
| 28 | #include <tensorflow/lite/builtin_ops.h> |
| 29 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 30 | #include <tensorflow/lite/c/common.h> |
| 31 | #include <tensorflow/lite/optional_debug_tools.h> |
| 32 | #include <tensorflow/lite/kernels/builtin_op_kernels.h> |
| 33 | #include <tensorflow/lite/interpreter.h> |
| 34 | #include <tensorflow/lite/kernels/register.h> |
| 35 | #endif |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 36 | |
| 37 | #include <future> |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 38 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 39 | int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params, |
| 40 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 41 | { |
| 42 | using namespace tflite; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 43 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 44 | std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(params.m_ModelPath.c_str()); |
| 45 | |
| 46 | auto tfLiteInterpreter = std::make_unique<Interpreter>(); |
| 47 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 48 | |
| 49 | tflite::InterpreterBuilder builder(*model, resolver); |
| 50 | builder(&tfLiteInterpreter); |
| 51 | tfLiteInterpreter->AllocateTensors(); |
| 52 | |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 53 | int status = 0; |
| 54 | if (params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate) |
Sadik Armagan | 19a1c03 | 2021-01-20 12:17:00 +0000 | [diff] [blame] | 55 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 56 | // Create the Armnn Delegate |
| 57 | armnnDelegate::DelegateOptions delegateOptions(params.m_ComputeDevices); |
| 58 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 59 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 60 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 61 | // Register armnn_delegate to TfLiteInterpreter |
| 62 | status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate)); |
| 63 | if (status == kTfLiteError) |
| 64 | { |
| 65 | ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!"; |
| 66 | return EXIT_FAILURE; |
| 67 | } |
Sadik Armagan | 19a1c03 | 2021-01-20 12:17:00 +0000 | [diff] [blame] | 68 | } |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 69 | else |
| 70 | { |
| 71 | std::cout << "Running on TfLite without ArmNN delegate\n"; |
| 72 | } |
| 73 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 74 | |
| 75 | std::vector<std::string> inputBindings; |
| 76 | for (const std::string& inputName: params.m_InputNames) |
| 77 | { |
| 78 | inputBindings.push_back(inputName); |
| 79 | } |
| 80 | |
| 81 | armnn::Optional<std::string> dataFile = params.m_GenerateTensorData |
| 82 | ? armnn::EmptyOptional() |
| 83 | : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]); |
| 84 | |
| 85 | const size_t numInputs = inputBindings.size(); |
| 86 | |
| 87 | for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex) |
| 88 | { |
| 89 | int input = tfLiteInterpreter->inputs()[inputIndex]; |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 90 | TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims; |
| 91 | |
| 92 | long inputSize = 1; |
| 93 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim) |
| 94 | { |
| 95 | inputSize *= inputDims->data[dim]; |
| 96 | } |
| 97 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 98 | if (params.m_InputTypes[inputIndex].compare("float") == 0) |
| 99 | { |
| 100 | auto inputData = tfLiteInterpreter->typed_tensor<float>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 101 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 102 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 103 | { |
| 104 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 105 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 106 | return EXIT_FAILURE; |
| 107 | } |
| 108 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 109 | std::vector<float> tensorData; |
| 110 | PopulateTensorWithDataGeneric<float>(tensorData, |
| 111 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 112 | dataFile, |
| 113 | [](const std::string& s) |
| 114 | { return std::stof(s); }); |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 115 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 116 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
| 117 | } |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 118 | else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0) |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 119 | { |
| 120 | auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 121 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 122 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 123 | { |
| 124 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 125 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 126 | return EXIT_FAILURE; |
| 127 | } |
| 128 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 129 | std::vector<int8_t> tensorData; |
| 130 | PopulateTensorWithDataGeneric<int8_t>(tensorData, |
| 131 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 132 | dataFile, |
| 133 | [](const std::string& s) |
| 134 | { return armnn::numeric_cast<int8_t>(std::stoi(s)); }); |
| 135 | |
| 136 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 137 | } |
| 138 | else if (params.m_InputTypes[inputIndex].compare("int") == 0) |
| 139 | { |
| 140 | auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 141 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 142 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 143 | { |
| 144 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 145 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 146 | return EXIT_FAILURE; |
| 147 | } |
| 148 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 149 | std::vector<int32_t> tensorData; |
| 150 | PopulateTensorWithDataGeneric<int32_t>(tensorData, |
| 151 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 152 | dataFile, |
| 153 | [](const std::string& s) |
| 154 | { return std::stoi(s); }); |
| 155 | |
| 156 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 157 | } |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 158 | else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 || |
| 159 | params.m_InputTypes[inputIndex].compare("qasymmu8") == 0) |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 160 | { |
| 161 | auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 162 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 163 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 164 | { |
| 165 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 166 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 167 | return EXIT_FAILURE; |
| 168 | } |
| 169 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 170 | std::vector<uint8_t> tensorData; |
| 171 | PopulateTensorWithDataGeneric<uint8_t>(tensorData, |
| 172 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 173 | dataFile, |
| 174 | [](const std::string& s) |
| 175 | { return armnn::numeric_cast<uint8_t>(std::stoi(s)); }); |
| 176 | |
| 177 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 178 | } |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 179 | else if (params.m_InputTypes[inputIndex].compare("qasymms8") == 0) |
| 180 | { |
| 181 | auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input); |
| 182 | |
| 183 | if(inputData == NULL) |
| 184 | { |
| 185 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 186 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 187 | return EXIT_FAILURE; |
| 188 | } |
| 189 | |
| 190 | std::vector<int8_t> tensorData; |
| 191 | PopulateTensorWithDataGeneric<int8_t>(tensorData, |
| 192 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 193 | dataFile, |
| 194 | [](const std::string& s) |
| 195 | { return armnn::numeric_cast<int8_t>(std::stoi(s)); }); |
| 196 | |
| 197 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
| 198 | } |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 199 | else |
| 200 | { |
| 201 | ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". "; |
| 202 | return EXIT_FAILURE; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | for (size_t x = 0; x < params.m_Iterations; x++) |
| 207 | { |
| 208 | // Run the inference |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 209 | status = tfLiteInterpreter->Invoke(); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 210 | |
| 211 | // Print out the output |
| 212 | for (unsigned int outputIndex = 0; outputIndex < params.m_OutputNames.size(); ++outputIndex) |
| 213 | { |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 214 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex]; |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 215 | TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 216 | |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 217 | long outputSize = 1; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 218 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim) |
| 219 | { |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 220 | outputSize *= outputDims->data[dim]; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | std::cout << params.m_OutputNames[outputIndex] << ": "; |
| 224 | if (params.m_OutputTypes[outputIndex].compare("float") == 0) |
| 225 | { |
| 226 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 227 | if(tfLiteDelageOutputData == NULL) |
| 228 | { |
| 229 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 230 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 231 | return EXIT_FAILURE; |
| 232 | } |
| 233 | |
| 234 | for (int i = 0; i < outputSize; ++i) |
| 235 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 236 | printf("%f ", tfLiteDelageOutputData[i]); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | else if (params.m_OutputTypes[outputIndex].compare("int") == 0) |
| 240 | { |
| 241 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 242 | if(tfLiteDelageOutputData == NULL) |
| 243 | { |
| 244 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 245 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 246 | return EXIT_FAILURE; |
| 247 | } |
| 248 | |
| 249 | for (int i = 0; i < outputSize; ++i) |
| 250 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 251 | printf("%d ", tfLiteDelageOutputData[i]); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 254 | else if (params.m_OutputTypes[outputIndex].compare("qsymms8") == 0) |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 255 | { |
| 256 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId); |
| 257 | if(tfLiteDelageOutputData == NULL) |
| 258 | { |
| 259 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 260 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 261 | return EXIT_FAILURE; |
| 262 | } |
| 263 | |
| 264 | for (int i = 0; i < outputSize; ++i) |
| 265 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 266 | printf("%d ", tfLiteDelageOutputData[i]); |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 269 | else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0 || |
| 270 | params.m_OutputTypes[outputIndex].compare("qasymmu8") == 0) |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 271 | { |
| 272 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 273 | if(tfLiteDelageOutputData == NULL) |
| 274 | { |
| 275 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 276 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 277 | return EXIT_FAILURE; |
| 278 | } |
| 279 | |
| 280 | for (int i = 0; i < outputSize; ++i) |
| 281 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 282 | printf("%u ", tfLiteDelageOutputData[i]); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 288 | "\"" << params.m_OutputTypes[outputIndex] << |
| 289 | "\" may be incorrect. Output type can be specified with -z argument"; |
| 290 | return EXIT_FAILURE; |
| 291 | } |
| 292 | std::cout << std::endl; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return status; |
| 297 | } |
| 298 | #endif |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 299 | template<typename TParser, typename TDataType> |
| 300 | int MainImpl(const ExecuteNetworkParams& params, |
| 301 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 302 | { |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 303 | using namespace std::chrono; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 304 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 305 | std::vector<std::vector<TContainer>> inputs; |
| 306 | std::vector<std::vector<TContainer>> outputs; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 307 | |
| 308 | try |
| 309 | { |
| 310 | // Creates an InferenceModel, which will parse the model and load it into an IRuntime. |
| 311 | typename InferenceModel<TParser, TDataType>::Params inferenceModelParams; |
| 312 | inferenceModelParams.m_ModelPath = params.m_ModelPath; |
| 313 | inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary; |
| 314 | inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices; |
| 315 | inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath; |
| 316 | inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate; |
| 317 | inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails; |
| 318 | inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported; |
| 319 | inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape; |
| 320 | inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath; |
Matthew Sloyan | 4243211 | 2021-01-08 10:30:51 +0000 | [diff] [blame] | 321 | inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork; |
| 322 | inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath; |
Matthew Sloyan | 0a7dc6b | 2021-02-10 16:50:53 +0000 | [diff] [blame] | 323 | inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads; |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 324 | inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath; |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 325 | inferenceModelParams.m_AsyncEnabled = params.m_Concurrent; |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 326 | inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 327 | |
| 328 | for(const std::string& inputName: params.m_InputNames) |
| 329 | { |
| 330 | inferenceModelParams.m_InputBindings.push_back(inputName); |
| 331 | } |
| 332 | |
| 333 | for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i) |
| 334 | { |
| 335 | inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]); |
| 336 | } |
| 337 | |
| 338 | for(const std::string& outputName: params.m_OutputNames) |
| 339 | { |
| 340 | inferenceModelParams.m_OutputBindings.push_back(outputName); |
| 341 | } |
| 342 | |
| 343 | inferenceModelParams.m_SubgraphId = params.m_SubgraphId; |
| 344 | inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode; |
| 345 | inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode; |
| 346 | |
| 347 | InferenceModel<TParser, TDataType> model(inferenceModelParams, |
| 348 | params.m_EnableProfiling, |
| 349 | params.m_DynamicBackendsPath, |
| 350 | runtime); |
| 351 | |
| 352 | const size_t numInputs = inferenceModelParams.m_InputBindings.size(); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 353 | |
| 354 | armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ? |
| 355 | armnn::MakeOptional<QuantizationParams>( |
| 356 | model.GetInputQuantizationParams()) : |
| 357 | armnn::EmptyOptional(); |
| 358 | |
| 359 | for(unsigned int j = 0; j < params.m_SimultaneousIterations ; ++j) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 360 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 361 | std::vector<TContainer> inputDataContainers; |
| 362 | for(unsigned int i = 0; i < numInputs; ++i) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 363 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 364 | armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ? |
| 365 | armnn::EmptyOptional() : |
| 366 | armnn::MakeOptional<std::string>( |
| 367 | params.m_InputTensorDataFilePaths[(j * numInputs) + i]); |
| 368 | |
| 369 | unsigned int numElements = model.GetInputSize(i); |
| 370 | if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i]) |
| 371 | { |
| 372 | // If the user has provided a tensor shape for the current input, |
| 373 | // override numElements |
| 374 | numElements = params.m_InputTensorShapes[i]->GetNumElements(); |
| 375 | } |
| 376 | |
| 377 | TContainer tensorData; |
| 378 | PopulateTensorWithData(tensorData, |
| 379 | numElements, |
| 380 | params.m_InputTypes[i], |
| 381 | qParams, |
| 382 | dataFile); |
| 383 | |
| 384 | inputDataContainers.push_back(tensorData); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 385 | } |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 386 | inputs.push_back(inputDataContainers); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | const size_t numOutputs = inferenceModelParams.m_OutputBindings.size(); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 390 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 391 | for (unsigned int j = 0; j < params.m_SimultaneousIterations; ++j) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 392 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 393 | std::vector <TContainer> outputDataContainers; |
| 394 | for (unsigned int i = 0; i < numOutputs; ++i) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 395 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 396 | if (params.m_OutputTypes[i].compare("float") == 0) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 397 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 398 | outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i))); |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 399 | } |
| 400 | else if (params.m_OutputTypes[i].compare("int") == 0) |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 401 | { |
| 402 | outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i))); |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 403 | } |
| 404 | else if (params.m_OutputTypes[i].compare("qasymm8") == 0 || |
| 405 | params.m_OutputTypes[i].compare("qasymmu8") == 0) |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 406 | { |
| 407 | outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i))); |
Mike Kelly | d7ed6d4 | 2021-07-21 09:42:43 +0100 | [diff] [blame] | 408 | } |
| 409 | else if (params.m_OutputTypes[i].compare("qasymms8") == 0) |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 410 | { |
| 411 | outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i))); |
| 412 | } else |
| 413 | { |
| 414 | ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". "; |
| 415 | return EXIT_FAILURE; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 418 | outputs.push_back(outputDataContainers); |
| 419 | } |
| 420 | |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 421 | // Synchronous execution |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 422 | if (!params.m_Concurrent) |
| 423 | { |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 424 | for (size_t x = 0; x < params.m_Iterations; x++) |
| 425 | { |
| 426 | // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds) |
| 427 | auto inference_duration = model.Run(inputs[0], outputs[0]); |
| 428 | |
| 429 | if (params.m_GenerateTensorData) |
| 430 | { |
| 431 | ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful"; |
| 432 | } |
| 433 | |
| 434 | // Print output tensors |
| 435 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 436 | for (size_t i = 0; i < numOutputs; i++) |
| 437 | { |
| 438 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 439 | auto outputTensorFile = params.m_OutputTensorFiles.empty() ? "" : params.m_OutputTensorFiles[i]; |
| 440 | |
| 441 | TensorPrinter printer(inferenceModelParams.m_OutputBindings[i], |
| 442 | infoOut, |
| 443 | outputTensorFile, |
| 444 | params.m_DequantizeOutput); |
| 445 | mapbox::util::apply_visitor(printer, outputs[0][i]); |
| 446 | } |
| 447 | |
| 448 | ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2) |
| 449 | << std::fixed << inference_duration.count() << " ms\n"; |
| 450 | |
| 451 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 452 | if (params.m_ThresholdTime != 0.0) |
| 453 | { |
| 454 | ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2) |
| 455 | << std::fixed << params.m_ThresholdTime << " ms"; |
| 456 | auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count(); |
| 457 | ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 458 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 459 | |
| 460 | if (thresholdMinusInference < 0) |
| 461 | { |
| 462 | std::string errorMessage = "Elapsed inference time is greater than provided threshold time."; |
| 463 | ARMNN_LOG(fatal) << errorMessage; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 468 | // Asynchronous execution using the Arm NN thread pool |
Kevin May | 94dd4db | 2021-05-26 16:01:08 +0100 | [diff] [blame] | 469 | else if (params.m_ThreadPoolSize >= 1) |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 470 | { |
| 471 | try |
| 472 | { |
| 473 | ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n"; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 474 | armnn::AsyncCallbackManager callbackManager; |
| 475 | std::unordered_map<armnn::InferenceId, std::vector<TContainer>&> inferenceOutputMap; |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 476 | |
| 477 | // Declare the latest and earliest inference times here to be used when calculating overall time |
| 478 | std::chrono::high_resolution_clock::time_point earliestStartTime; |
| 479 | std::chrono::high_resolution_clock::time_point latestEndTime = |
| 480 | std::chrono::high_resolution_clock::now(); |
| 481 | |
| 482 | // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the |
| 483 | // LoadedNetwork with each scheduled inference having a specific priority |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 484 | for (size_t i = 0; i < params.m_SimultaneousIterations; ++i) |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 485 | { |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 486 | std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback(); |
| 487 | inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]}); |
| 488 | model.RunAsync(inputs[i], outputs[i], cb); |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // Check the results |
| 492 | unsigned int j = 0; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 493 | for (size_t iteration = 0; iteration < params.m_SimultaneousIterations; ++iteration) |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 494 | { |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 495 | auto cb = callbackManager.GetNotifiedCallback(); |
| 496 | |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 497 | // Get the results |
| 498 | auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime()); |
| 499 | auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime()); |
| 500 | auto inferenceDuration = endTime - startTime; |
| 501 | |
| 502 | if (latestEndTime < cb->GetEndTime()) |
| 503 | { |
| 504 | latestEndTime = cb->GetEndTime(); |
| 505 | } |
| 506 | |
| 507 | if (earliestStartTime.time_since_epoch().count() == 0) |
| 508 | { |
| 509 | earliestStartTime = cb->GetStartTime(); |
| 510 | } |
| 511 | else if (earliestStartTime > cb->GetStartTime()) |
| 512 | { |
| 513 | earliestStartTime = cb->GetStartTime(); |
| 514 | } |
| 515 | |
| 516 | if (params.m_GenerateTensorData) |
| 517 | { |
| 518 | ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful"; |
| 519 | } |
| 520 | |
| 521 | // Print output tensors |
| 522 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 523 | for (size_t i = 0; i < numOutputs; i++) |
| 524 | { |
| 525 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 526 | auto outputTensorFile = params.m_OutputTensorFiles.empty() |
| 527 | ? "" |
| 528 | : params.m_OutputTensorFiles[(j * numOutputs) + i]; |
| 529 | |
| 530 | TensorPrinter printer(inferenceModelParams.m_OutputBindings[i], |
| 531 | infoOut, |
| 532 | outputTensorFile, |
| 533 | params.m_DequantizeOutput); |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 534 | mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]); |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2) |
| 538 | << std::fixed << inferenceDuration.count() << " ms\n"; |
| 539 | |
| 540 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 541 | if (params.m_ThresholdTime != 0.0) |
| 542 | { |
| 543 | ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2) |
| 544 | << std::fixed << params.m_ThresholdTime << " ms"; |
| 545 | auto thresholdMinusInference = |
| 546 | params.m_ThresholdTime - duration<double, std::milli>(inferenceDuration).count(); |
| 547 | ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 548 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 549 | |
| 550 | if (thresholdMinusInference < 0) |
| 551 | { |
| 552 | ARMNN_LOG(fatal) << "Elapsed inference time is greater than provided threshold time. \n"; |
| 553 | } |
| 554 | } |
| 555 | ++j; |
| 556 | } |
| 557 | //print duration difference between overallStartTime and overallEndTime |
| 558 | auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime); |
| 559 | auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime); |
| 560 | auto totalInferenceDuration = overallEndTime - overallStartTime; |
| 561 | ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2) |
| 562 | << std::fixed << totalInferenceDuration.count() << " ms\n"; |
| 563 | } |
| 564 | catch (const armnn::Exception& e) |
| 565 | { |
| 566 | ARMNN_LOG(fatal) << "Armnn Error: " << e.what(); |
| 567 | return EXIT_FAILURE; |
| 568 | } |
| 569 | } |
| 570 | // Asynchronous execution using std::launch::async |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 571 | else |
| 572 | { |
| 573 | try |
| 574 | { |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 575 | ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n"; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 576 | std::vector<std::future<std::tuple<unsigned int, |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 577 | std::chrono::duration<double, std::milli>>>> inferenceResults; |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 578 | inferenceResults.reserve(params.m_SimultaneousIterations); |
| 579 | |
| 580 | // Create WorkingMemHandles for each inference |
| 581 | std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles; |
| 582 | workingMemHandles.reserve(params.m_SimultaneousIterations); |
| 583 | for (unsigned int i = 0; i < params.m_SimultaneousIterations; ++i) |
| 584 | { |
| 585 | workingMemHandles.push_back(model.CreateWorkingMemHandle()); |
| 586 | } |
| 587 | |
| 588 | // Run each inference in its own thread |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 589 | // start a timer |
| 590 | const auto start_time = armnn::GetTimeNow(); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 591 | for (unsigned int i = 0; i < params.m_SimultaneousIterations; ++i) |
| 592 | { |
| 593 | armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get(); |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 594 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 595 | inferenceResults.push_back(std::async( |
| 596 | std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() { |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 597 | return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 598 | } |
| 599 | )); |
| 600 | } |
| 601 | |
| 602 | // Check the results |
| 603 | for (unsigned int j = 0; j < inferenceResults.size(); ++j) |
| 604 | { |
| 605 | // Get the results |
| 606 | auto inferenceResult = inferenceResults[j].get(); |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 607 | auto inferenceDuration = std::get<1>(inferenceResult); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 608 | auto inferenceID = std::get<0>(inferenceResult); |
| 609 | |
| 610 | if (params.m_GenerateTensorData) |
| 611 | { |
| 612 | ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful"; |
| 613 | } |
| 614 | |
| 615 | // Print output tensors |
| 616 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 617 | for (size_t i = 0; i < numOutputs; i++) |
| 618 | { |
| 619 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 620 | auto outputTensorFile = params.m_OutputTensorFiles.empty() |
| 621 | ? "" |
| 622 | : params.m_OutputTensorFiles[(j * numOutputs) + i]; |
| 623 | |
| 624 | TensorPrinter printer(inferenceModelParams.m_OutputBindings[i], |
| 625 | infoOut, |
| 626 | outputTensorFile, |
| 627 | params.m_DequantizeOutput); |
| 628 | mapbox::util::apply_visitor(printer, outputs[j][i]); |
| 629 | } |
| 630 | |
| 631 | ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2) |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 632 | << std::fixed << inferenceDuration.count() << " ms\n"; |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 633 | |
| 634 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 635 | if (params.m_ThresholdTime != 0.0) |
| 636 | { |
| 637 | ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2) |
| 638 | << std::fixed << params.m_ThresholdTime << " ms"; |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 639 | auto thresholdMinusInference = params.m_ThresholdTime - inferenceDuration.count(); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 640 | ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 641 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 642 | |
| 643 | if (thresholdMinusInference < 0) |
| 644 | { |
| 645 | ARMNN_LOG(fatal) << "Elapsed inference time is greater than provided threshold time. \n"; |
| 646 | } |
| 647 | } |
| 648 | ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n"; |
| 649 | |
| 650 | } |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 651 | // finish timer |
| 652 | const auto duration = armnn::GetTimeDuration(start_time); |
| 653 | ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2) |
| 654 | << std::fixed << duration.count() << " ms\n"; |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 655 | } |
| 656 | catch (const armnn::Exception& e) |
| 657 | { |
| 658 | ARMNN_LOG(fatal) << "Armnn Error: " << e.what(); |
| 659 | return EXIT_FAILURE; |
| 660 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | catch (const armnn::Exception& e) |
| 664 | { |
| 665 | ARMNN_LOG(fatal) << "Armnn Error: " << e.what(); |
| 666 | return EXIT_FAILURE; |
| 667 | } |
| 668 | |
| 669 | return EXIT_SUCCESS; |
| 670 | } |
| 671 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 672 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 673 | // MAIN |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 674 | int main(int argc, const char* argv[]) |
| 675 | { |
| 676 | // Configures logging for both the ARMNN library and this test program. |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 677 | #ifdef NDEBUG |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 678 | armnn::LogSeverity level = armnn::LogSeverity::Info; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 679 | #else |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 680 | armnn::LogSeverity level = armnn::LogSeverity::Debug; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 681 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 682 | armnn::ConfigureLogging(true, true, level); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 683 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 684 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 685 | // Get ExecuteNetwork parameters and runtime options from command line |
| 686 | ProgramOptions ProgramOptions(argc, argv); |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 687 | |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 688 | // Create runtime |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 689 | std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions)); |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 690 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 691 | std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat; |
| 692 | |
| 693 | // Forward to implementation based on the parser type |
| 694 | if (modelFormat.find("armnn") != std::string::npos) |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 695 | { |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 696 | #if defined(ARMNN_SERIALIZER) |
| 697 | return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime); |
| 698 | #else |
| 699 | ARMNN_LOG(fatal) << "Not built with serialization support."; |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 700 | return EXIT_FAILURE; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 701 | #endif |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 702 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 703 | else if (modelFormat.find("onnx") != std::string::npos) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 704 | { |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 705 | #if defined(ARMNN_ONNX_PARSER) |
| 706 | return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 707 | #else |
| 708 | ARMNN_LOG(fatal) << "Not built with Onnx parser support."; |
| 709 | return EXIT_FAILURE; |
| 710 | #endif |
| 711 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 712 | else if(modelFormat.find("tflite") != std::string::npos) |
| 713 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 714 | if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser) |
| 715 | { |
| 716 | #if defined(ARMNN_TF_LITE_PARSER) |
| 717 | return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 718 | #else |
| 719 | ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support."; |
| 720 | return EXIT_FAILURE; |
| 721 | #endif |
| 722 | } |
| 723 | else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == |
| 724 | ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate || |
| 725 | ProgramOptions.m_ExNetParams.m_TfLiteExecutor == |
| 726 | ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter) |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 727 | { |
| 728 | #if defined(ARMNN_TF_LITE_DELEGATE) |
| 729 | return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, runtime); |
| 730 | #else |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 731 | ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support."; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 732 | return EXIT_FAILURE; |
| 733 | #endif |
| 734 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 735 | } |
| 736 | else |
| 737 | { |
| 738 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat |
Nikhil Raj | 5d955cf | 2021-04-19 16:59:48 +0100 | [diff] [blame] | 739 | << "'. Please include 'tflite' or 'onnx'"; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 740 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 741 | } |
| 742 | } |