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