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