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