blob: f321a260091dd96029e4472438b753580d3d360a [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Sadik Armagana9c2ce12020-07-14 10:02:22 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
telsoa01c577f2c2018-08-31 09:22:23 +01005
Jan Eilers45274902020-10-15 18:34:43 +01006#include "NetworkExecutionUtils/NetworkExecutionUtils.hpp"
7#include "ExecuteNetworkProgramOptions.hpp"
Kevin Mayb4b3ac92021-05-21 16:42:21 +01008#include <armnn/IAsyncExecutionCallback.hpp>
9#include <AsyncExecutionCallback.hpp>
Jan Eilers45274902020-10-15 18:34:43 +010010
11#include <armnn/Logging.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010012#include <armnnUtils/Filesystem.hpp>
Francis Murtagh40d27412021-10-28 11:11:35 +010013#include <armnnUtils/TContainer.hpp>
Jan Eilers45274902020-10-15 18:34:43 +010014#include <InferenceTest.hpp>
15
16#if defined(ARMNN_SERIALIZER)
17#include "armnnDeserializer/IDeserializer.hpp"
18#endif
Jan Eilers45274902020-10-15 18:34:43 +010019#if defined(ARMNN_TF_LITE_PARSER)
20#include "armnnTfLiteParser/ITfLiteParser.hpp"
21#endif
22#if defined(ARMNN_ONNX_PARSER)
23#include "armnnOnnxParser/IOnnxParser.hpp"
24#endif
Sadik Armagan5d03e312020-11-17 16:43:56 +000025#if defined(ARMNN_TFLITE_DELEGATE)
26#include <armnn_delegate.hpp>
27#include <DelegateOptions.hpp>
28
29#include <tensorflow/lite/builtin_ops.h>
30#include <tensorflow/lite/c/builtin_op_data.h>
31#include <tensorflow/lite/c/common.h>
32#include <tensorflow/lite/optional_debug_tools.h>
33#include <tensorflow/lite/kernels/builtin_op_kernels.h>
34#include <tensorflow/lite/interpreter.h>
35#include <tensorflow/lite/kernels/register.h>
36#endif
Jan Eilers45274902020-10-15 18:34:43 +010037
38#include <future>
Colm Donelan3cff15a2021-10-12 15:06:19 +010039
40/**
41 * Given a measured duration and a threshold time tell the user whether we succeeded or not.
42 *
43 * @param duration the measured inference duration.
44 * @param thresholdTime the threshold time in milliseconds.
45 * @return false if the measured time exceeded the threshold.
46 */
47bool CheckInferenceTimeThreshold(const std::chrono::duration<double, std::milli>& duration,
48 const double& thresholdTime)
49{
Jan Eilers17d34da2021-12-08 16:15:12 +000050 ARMNN_LOG(info) << "Inference time: " << std::setprecision(2)
Colm Donelan3cff15a2021-10-12 15:06:19 +010051 << std::fixed << duration.count() << " ms\n";
52 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
53 if (thresholdTime != 0.0)
54 {
55 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
56 << std::fixed << thresholdTime << " ms";
57 auto thresholdMinusInference = thresholdTime - duration.count();
58 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
59 << std::fixed << thresholdMinusInference << " ms" << "\n";
60 if (thresholdMinusInference < 0)
61 {
62 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
63 ARMNN_LOG(fatal) << errorMessage;
64 return false;
65 }
66 }
67 return true;
68}
69
Sadik Armagan5d03e312020-11-17 16:43:56 +000070#if defined(ARMNN_TFLITE_DELEGATE)
Colm Donelan45142282021-10-21 23:39:52 +010071int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params, const armnn::IRuntime::CreationOptions runtimeOptions)
Sadik Armagan5d03e312020-11-17 16:43:56 +000072{
Tamas Nyiri00564232021-11-28 21:31:33 +000073 // Build model and corresponding interpreter
Sadik Armagan5d03e312020-11-17 16:43:56 +000074 using namespace tflite;
Jan Eilers45274902020-10-15 18:34:43 +010075
Sadik Armagan5d03e312020-11-17 16:43:56 +000076 std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(params.m_ModelPath.c_str());
77
78 auto tfLiteInterpreter = std::make_unique<Interpreter>();
79 tflite::ops::builtin::BuiltinOpResolver resolver;
80
81 tflite::InterpreterBuilder builder(*model, resolver);
82 builder(&tfLiteInterpreter);
83 tfLiteInterpreter->AllocateTensors();
84
Finn Williamsf806c4d2021-02-22 15:13:12 +000085 int status = 0;
Tamas Nyiri00564232021-11-28 21:31:33 +000086
87 // Create & populate Armnn Delegate, then register it to TfLiteInterpreter
Finn Williamsf806c4d2021-02-22 15:13:12 +000088 if (params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate)
Sadik Armagan19a1c032021-01-20 12:17:00 +000089 {
Finn Williamsf806c4d2021-02-22 15:13:12 +000090 // Create the Armnn Delegate
Colm Donelan3cff15a2021-10-12 15:06:19 +010091 // Populate a DelegateOptions from the ExecuteNetworkParams.
92 armnnDelegate::DelegateOptions delegateOptions = params.ToDelegateOptions();
93 delegateOptions.SetExternalProfilingParams(runtimeOptions.m_ProfilingOptions);
94
Finn Williamsf806c4d2021-02-22 15:13:12 +000095 std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)>
96 theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions),
97 armnnDelegate::TfLiteArmnnDelegateDelete);
98 // Register armnn_delegate to TfLiteInterpreter
99 status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate));
100 if (status == kTfLiteError)
101 {
102 ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!";
103 return EXIT_FAILURE;
104 }
Sadik Armagan19a1c032021-01-20 12:17:00 +0000105 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000106 else
107 {
108 std::cout << "Running on TfLite without ArmNN delegate\n";
109 }
110
Tamas Nyiri00564232021-11-28 21:31:33 +0000111 // Load (or generate) input data for inference
Sadik Armagan5d03e312020-11-17 16:43:56 +0000112 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData
113 ? armnn::EmptyOptional()
114 : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]);
115
Colm Donelan3cff15a2021-10-12 15:06:19 +0100116 const size_t numInputs = params.m_InputNames.size();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000117
Tamas Nyiri00564232021-11-28 21:31:33 +0000118 // Populate input tensor of interpreter
Sadik Armagan5d03e312020-11-17 16:43:56 +0000119 for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex)
120 {
121 int input = tfLiteInterpreter->inputs()[inputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000122 TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims;
123
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100124 unsigned int inputSize = 1;
125 if (params.m_InputTensorShapes.size() > 0)
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000126 {
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100127 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 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000135 }
136
Sadik Armagan5d03e312020-11-17 16:43:56 +0000137 if (params.m_InputTypes[inputIndex].compare("float") == 0)
138 {
139 auto inputData = tfLiteInterpreter->typed_tensor<float>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000140
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000141 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000142 {
143 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
144 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
145 return EXIT_FAILURE;
146 }
147
Finn Williams56870182020-11-20 13:57:53 +0000148 std::vector<float> tensorData;
149 PopulateTensorWithDataGeneric<float>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100150 inputSize,
151 dataFile,
152 [](const std::string& s)
153 { return std::stof(s); });
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000154
Finn Williams56870182020-11-20 13:57:53 +0000155 std::copy(tensorData.begin(), tensorData.end(), inputData);
156 }
Finn Williams35e7c1d2022-01-21 19:33:46 +0000157 else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0 ||
158 params.m_InputTypes[inputIndex].compare("qasymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000159 {
160 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000161
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000162 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000163 {
164 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
165 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
166 return EXIT_FAILURE;
167 }
168
Finn Williams56870182020-11-20 13:57:53 +0000169 std::vector<int8_t> tensorData;
170 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100171 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000172 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);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000177 }
178 else if (params.m_InputTypes[inputIndex].compare("int") == 0)
179 {
180 auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000181
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000182 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000183 {
184 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
185 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
186 return EXIT_FAILURE;
187 }
188
Finn Williams56870182020-11-20 13:57:53 +0000189 std::vector<int32_t> tensorData;
190 PopulateTensorWithDataGeneric<int32_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100191 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000192 dataFile,
193 [](const std::string& s)
194 { return std::stoi(s); });
195
196 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000197 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100198 else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 ||
199 params.m_InputTypes[inputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000200 {
201 auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000202
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000203 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000204 {
205 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
206 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
207 return EXIT_FAILURE;
208 }
209
Finn Williams56870182020-11-20 13:57:53 +0000210 std::vector<uint8_t> tensorData;
211 PopulateTensorWithDataGeneric<uint8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100212 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000213 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);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000218 }
219 else
220 {
221 ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". ";
222 return EXIT_FAILURE;
223 }
224 }
225
Tamas Nyiri00564232021-11-28 21:31:33 +0000226 // Run inference, print the output of the inference
Sadik Armagan5d03e312020-11-17 16:43:56 +0000227 for (size_t x = 0; x < params.m_Iterations; x++)
228 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100229 // Start timer to record inference time in milliseconds.
230 const auto start_time = armnn::GetTimeNow();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000231 // Run the inference
Finn Williamsf806c4d2021-02-22 15:13:12 +0000232 status = tfLiteInterpreter->Invoke();
Colm Donelan3cff15a2021-10-12 15:06:19 +0100233 const auto duration = armnn::GetTimeDuration(start_time);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000234
Tamas Nyiri00564232021-11-28 21:31:33 +0000235 // 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)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000238 {
Tamas Nyiri00564232021-11-28 21:31:33 +0000239 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 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000259 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000260 TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims;
Colm Donelan3cff15a2021-10-12 15:06:19 +0100261 // 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 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000280 long outputSize = 1;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000281 for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim)
282 {
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000283 outputSize *= outputDims->data[dim];
Sadik Armagan5d03e312020-11-17 16:43:56 +0000284 }
285
Tamas Nyiri00564232021-11-28 21:31:33 +0000286 std::cout << tfLiteInterpreter->GetOutputName(outputIndex) << ": ";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000287 if (params.m_OutputTypes[outputIndex].compare("float") == 0)
288 {
289 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000290 if(tfLiteDelageOutputData == NULL)
291 {
292 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
293 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
294 return EXIT_FAILURE;
295 }
296
Jan Eilers284b5d12021-09-07 12:46:15 +0100297 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000298 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100299 for (int i = 0; i < outputSize; ++i)
300 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100301 fprintf(outputTensorFile, "%f ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100302 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000303 }
304 }
305 else if (params.m_OutputTypes[outputIndex].compare("int") == 0)
306 {
307 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000308 if(tfLiteDelageOutputData == NULL)
309 {
310 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
311 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
312 return EXIT_FAILURE;
313 }
314
Jan Eilers284b5d12021-09-07 12:46:15 +0100315 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000316 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100317 for (int i = 0; i < outputSize; ++i)
318 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100319 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100320 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000321 }
322 }
Finn Williams35e7c1d2022-01-21 19:33:46 +0000323 else if (params.m_OutputTypes[outputIndex].compare("qsymms8") == 0 ||
324 params.m_OutputTypes[outputIndex].compare("qasymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000325 {
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[outputIndex] << "\" may be incorrect.";
331 return EXIT_FAILURE;
332 }
333
Jan Eilers284b5d12021-09-07 12:46:15 +0100334 if (!params.m_DontPrintOutputs)
Finn Williams56870182020-11-20 13:57:53 +0000335 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100336 for (int i = 0; i < outputSize; ++i)
337 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100338 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100339 }
Finn Williams56870182020-11-20 13:57:53 +0000340 }
341 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100342 else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0 ||
343 params.m_OutputTypes[outputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000344 {
345 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000346 if(tfLiteDelageOutputData == NULL)
347 {
348 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
349 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
350 return EXIT_FAILURE;
351 }
352
Jan Eilers284b5d12021-09-07 12:46:15 +0100353 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000354 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100355 for (int i = 0; i < outputSize; ++i)
356 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100357 fprintf(outputTensorFile, "%u ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100358 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000359 }
360 }
361 else
362 {
363 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
364 "\"" << params.m_OutputTypes[outputIndex] <<
365 "\" may be incorrect. Output type can be specified with -z argument";
366 return EXIT_FAILURE;
367 }
368 std::cout << std::endl;
369 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100370 CheckInferenceTimeThreshold(duration, params.m_ThresholdTime);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000371 }
372
373 return status;
374}
375#endif
Jan Eilers45274902020-10-15 18:34:43 +0100376template<typename TParser, typename TDataType>
377int MainImpl(const ExecuteNetworkParams& params,
378 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
379{
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100380 using namespace std::chrono;
Jan Eilers45274902020-10-15 18:34:43 +0100381
Francis Murtagh40d27412021-10-28 11:11:35 +0100382 std::vector<std::vector<armnnUtils::TContainer>> inputs;
383 std::vector<std::vector<armnnUtils::TContainer>> outputs;
Jan Eilers45274902020-10-15 18:34:43 +0100384
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_IsModelBinary = params.m_IsModelBinary;
391 inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices;
392 inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath;
393 inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate;
394 inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails;
395 inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported;
396 inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape;
397 inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath;
Matthew Sloyan42432112021-01-08 10:30:51 +0000398 inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork;
399 inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath;
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000400 inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads;
Finn Williams40646322021-02-11 16:16:42 +0000401 inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100402 inferenceModelParams.m_AsyncEnabled = params.m_Concurrent;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100403 inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize;
Keith Davisf4874862021-08-09 16:49:18 +0100404 inferenceModelParams.m_OutputDetailsToStdOut = params.m_OutputDetailsToStdOut;
Keith Davis4914d0c2021-08-18 17:14:05 +0100405 inferenceModelParams.m_OutputDetailsOnlyToStdOut = params.m_OutputDetailsOnlyToStdOut;
Jan Eilers45274902020-10-15 18:34:43 +0100406
407 for(const std::string& inputName: params.m_InputNames)
408 {
409 inferenceModelParams.m_InputBindings.push_back(inputName);
410 }
411
412 for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i)
413 {
414 inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]);
415 }
416
417 for(const std::string& outputName: params.m_OutputNames)
418 {
419 inferenceModelParams.m_OutputBindings.push_back(outputName);
420 }
421
422 inferenceModelParams.m_SubgraphId = params.m_SubgraphId;
423 inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode;
424 inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode;
425
426 InferenceModel<TParser, TDataType> model(inferenceModelParams,
427 params.m_EnableProfiling,
428 params.m_DynamicBackendsPath,
429 runtime);
430
431 const size_t numInputs = inferenceModelParams.m_InputBindings.size();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100432
433 armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ?
434 armnn::MakeOptional<QuantizationParams>(
435 model.GetInputQuantizationParams()) :
436 armnn::EmptyOptional();
437
Jan Eilersf17fcd52021-07-26 22:20:00 +0100438 if (params.m_InputTensorDataFilePaths.size() > numInputs)
439 {
440 ARMNN_LOG(info) << "Given network has " << numInputs << " input/s. One input-tensor-data file is required "
441 << "for each input. The user provided "
442 << params.m_InputTensorDataFilePaths.size()
443 << " input-tensor-data file/s which will be used to fill the input/s.\n";
444 }
445
446 for(unsigned int j = 0; j < params.m_Iterations ; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100447 {
Francis Murtagh40d27412021-10-28 11:11:35 +0100448 std::vector<armnnUtils::TContainer> inputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100449 for(unsigned int i = 0; i < numInputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100450 {
Tamas Nyiri00564232021-11-28 21:31:33 +0000451 // If there are fewer input files given than required for the execution of
Jan Eilersf17fcd52021-07-26 22:20:00 +0100452 // params.m_Iterations we simply start with the first input file again
453 size_t inputFileIndex = j * numInputs + i;
454 if (!params.m_InputTensorDataFilePaths.empty())
455 {
456 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
457 }
458
Sadik Armagana04a9d72021-04-27 10:02:10 +0100459 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
460 armnn::EmptyOptional() :
461 armnn::MakeOptional<std::string>(
Jan Eilersf17fcd52021-07-26 22:20:00 +0100462 params.m_InputTensorDataFilePaths.at(inputFileIndex));
Sadik Armagana04a9d72021-04-27 10:02:10 +0100463
464 unsigned int numElements = model.GetInputSize(i);
465 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
466 {
467 // If the user has provided a tensor shape for the current input,
468 // override numElements
469 numElements = params.m_InputTensorShapes[i]->GetNumElements();
470 }
471
Francis Murtagh40d27412021-10-28 11:11:35 +0100472 armnnUtils::TContainer tensorData;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100473 PopulateTensorWithData(tensorData,
474 numElements,
475 params.m_InputTypes[i],
476 qParams,
477 dataFile);
478
479 inputDataContainers.push_back(tensorData);
Jan Eilers45274902020-10-15 18:34:43 +0100480 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100481 inputs.push_back(inputDataContainers);
Jan Eilers45274902020-10-15 18:34:43 +0100482 }
483
484 const size_t numOutputs = inferenceModelParams.m_OutputBindings.size();
Jan Eilers45274902020-10-15 18:34:43 +0100485
Colm Donelanc5e41982021-10-28 20:19:43 +0100486 // The user is allowed to specify the data type of each output tensor. It is used here to construct the
487 // result tensors for each iteration. It is possible for the user to specify a type that does not match
488 // the data type of the corresponding model output. It may not make sense, but it is historically allowed.
489 // The potential problem here is a buffer overrun when a larger data type is written into the space for a
490 // smaller one. Issue a warning to highlight the potential problem.
491 for (unsigned int outputIdx = 0; outputIdx < model.GetOutputBindingInfos().size(); ++outputIdx)
492 {
493 armnn::DataType type = model.GetOutputBindingInfo(outputIdx).second.GetDataType();
494 switch (type)
495 {
David Monahan67cc5fc2021-11-03 12:56:41 +0000496 // --output-type only supports float, int, qasymms8 or qasymmu8.
Colm Donelanc5e41982021-10-28 20:19:43 +0100497 case armnn::DataType::Float32:
498 if (params.m_OutputTypes[outputIdx].compare("float") != 0)
499 {
500 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Float32. The " <<
501 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
502 ". This may cause unexpected problems or random failures.";
503 }
504 break;
505 case armnn::DataType::QAsymmU8:
506 if (params.m_OutputTypes[outputIdx].compare("qasymmu8") != 0)
507 {
508 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmU8. The " <<
509 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
510 ". This may cause unexpected problemsor random failures.";
511 }
512 break;
513 case armnn::DataType::Signed32:
514 if (params.m_OutputTypes[outputIdx].compare("int") != 0)
515 {
516 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Signed32. The " <<
517 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
518 ". This may cause unexpected problems or random failures.";
519 }
520 break;
521 case armnn::DataType::QAsymmS8:
522 if (params.m_OutputTypes[outputIdx].compare("qasymms8") != 0)
523 {
524 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmS8. The " <<
525 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
526 ". This may cause unexpected problems or random failures.";
527 }
528 break;
529 default:
530 break;
531 }
532 }
Jan Eilersf17fcd52021-07-26 22:20:00 +0100533 for (unsigned int j = 0; j < params.m_Iterations; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100534 {
Francis Murtagh40d27412021-10-28 11:11:35 +0100535 std::vector <armnnUtils::TContainer> outputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100536 for (unsigned int i = 0; i < numOutputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100537 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100538 if (params.m_OutputTypes[i].compare("float") == 0)
Jan Eilers45274902020-10-15 18:34:43 +0100539 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100540 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100541 }
542 else if (params.m_OutputTypes[i].compare("int") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100543 {
544 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100545 }
546 else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
547 params.m_OutputTypes[i].compare("qasymmu8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100548 {
549 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100550 }
551 else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100552 {
553 outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
554 } else
555 {
556 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
557 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100558 }
559 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100560 outputs.push_back(outputDataContainers);
561 }
562
Jan Eilersf17fcd52021-07-26 22:20:00 +0100563 if (params.m_Iterations > 1)
564 {
565 std::stringstream msg;
566 msg << "Network will be executed " << params.m_Iterations;
567 if (params.m_Concurrent)
568 {
569 msg << " times in an asynchronous manner. ";
570 }
571 else
572 {
573 msg << " times successively. ";
574 }
575 msg << "The input-tensor-data files will be reused recursively if the user didn't provide enough to "
576 "cover each execution.";
577 ARMNN_LOG(info) << msg.str();
578 }
579
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100580 // Synchronous execution
Sadik Armagana04a9d72021-04-27 10:02:10 +0100581 if (!params.m_Concurrent)
582 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100583 for (size_t x = 0; x < params.m_Iterations; x++)
584 {
585 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
Jan Eilersf17fcd52021-07-26 22:20:00 +0100586 auto inference_duration = model.Run(inputs[x], outputs[x]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100587
588 if (params.m_GenerateTensorData)
589 {
590 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
591 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100592 if (params.m_DontPrintOutputs)
593 {
594 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
595 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100596
597 // Print output tensors
598 const auto& infosOut = model.GetOutputBindingInfos();
599 for (size_t i = 0; i < numOutputs; i++)
600 {
601 const armnn::TensorInfo& infoOut = infosOut[i].second;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100602
Jan Eilers284b5d12021-09-07 12:46:15 +0100603 // We've made sure before that the number of output files either equals numOutputs, in which
604 // case we override those files when processing the results of each iteration (only the result
605 // of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100606 // output files for each output of each iteration.
607 size_t outputFileIndex = x * numOutputs + i;
608 if (!params.m_OutputTensorFiles.empty())
609 {
610 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
611 ARMNN_LOG(info) << "Writing output " << i << " named: '"
612 << inferenceModelParams.m_OutputBindings[i]
613 << "' of iteration: " << x+1 << " to file: '"
614 << params.m_OutputTensorFiles[outputFileIndex] << "'";
615 }
616 auto outputTensorFile = params.m_OutputTensorFiles.empty()
617 ? ""
618 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100619
620 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
621 infoOut,
622 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100623 params.m_DequantizeOutput,
624 !params.m_DontPrintOutputs);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100625 mapbox::util::apply_visitor(printer, outputs[x][i]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100626 }
627
628 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
629 << std::fixed << inference_duration.count() << " ms\n";
630
631 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
632 if (params.m_ThresholdTime != 0.0)
633 {
634 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
635 << std::fixed << params.m_ThresholdTime << " ms";
636 auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count();
637 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
638 << std::fixed << thresholdMinusInference << " ms" << "\n";
639
640 if (thresholdMinusInference < 0)
641 {
642 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
643 ARMNN_LOG(fatal) << errorMessage;
644 }
645 }
646 }
647 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100648 // Asynchronous execution using the Arm NN thread pool
Kevin May94dd4db2021-05-26 16:01:08 +0100649 else if (params.m_ThreadPoolSize >= 1)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100650 {
651 try
652 {
653 ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100654 armnn::AsyncCallbackManager callbackManager;
Francis Murtagh40d27412021-10-28 11:11:35 +0100655 std::unordered_map<armnn::InferenceId, std::vector<armnnUtils::TContainer>&> inferenceOutputMap;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100656
657 // Declare the latest and earliest inference times here to be used when calculating overall time
658 std::chrono::high_resolution_clock::time_point earliestStartTime;
659 std::chrono::high_resolution_clock::time_point latestEndTime =
660 std::chrono::high_resolution_clock::now();
661
662 // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the
663 // LoadedNetwork with each scheduled inference having a specific priority
Jan Eilersf17fcd52021-07-26 22:20:00 +0100664 for (size_t i = 0; i < params.m_Iterations; ++i)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100665 {
Finn Williamsf364d532021-06-09 17:07:33 +0100666 std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback();
667 inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]});
668 model.RunAsync(inputs[i], outputs[i], cb);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100669 }
670
671 // Check the results
672 unsigned int j = 0;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100673 for (size_t iteration = 0; iteration < params.m_Iterations; ++iteration)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100674 {
Finn Williamsf364d532021-06-09 17:07:33 +0100675 auto cb = callbackManager.GetNotifiedCallback();
676
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100677 // Get the results
678 auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime());
679 auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime());
680 auto inferenceDuration = endTime - startTime;
681
682 if (latestEndTime < cb->GetEndTime())
683 {
684 latestEndTime = cb->GetEndTime();
685 }
686
687 if (earliestStartTime.time_since_epoch().count() == 0)
688 {
689 earliestStartTime = cb->GetStartTime();
690 }
691 else if (earliestStartTime > cb->GetStartTime())
692 {
693 earliestStartTime = cb->GetStartTime();
694 }
695
696 if (params.m_GenerateTensorData)
697 {
698 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
699 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100700 if (params.m_DontPrintOutputs)
701 {
702 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
703 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100704
705 // Print output tensors
706 const auto& infosOut = model.GetOutputBindingInfos();
707 for (size_t i = 0; i < numOutputs; i++)
708 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100709 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100710 // case we override those files when processing the results of each iteration (only the
711 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100712 // output files for each output of each iteration.
713 size_t outputFileIndex = iteration * numOutputs + i;
714 if (!params.m_OutputTensorFiles.empty())
715 {
716 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
717 ARMNN_LOG(info) << "Writing output " << i << " named: '"
718 << inferenceModelParams.m_OutputBindings[i]
719 << "' of iteration: " << iteration+1 << " to file: '"
720 << params.m_OutputTensorFiles[outputFileIndex] << "'";
721 }
722
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100723 const armnn::TensorInfo& infoOut = infosOut[i].second;
724 auto outputTensorFile = params.m_OutputTensorFiles.empty()
725 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100726 : params.m_OutputTensorFiles[outputFileIndex];
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100727
728 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
729 infoOut,
730 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100731 params.m_DequantizeOutput,
732 !params.m_DontPrintOutputs);
Finn Williamsf364d532021-06-09 17:07:33 +0100733 mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100734 }
735
Colm Donelan3cff15a2021-10-12 15:06:19 +0100736 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100737 ++j;
738 }
739 //print duration difference between overallStartTime and overallEndTime
740 auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime);
741 auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime);
742 auto totalInferenceDuration = overallEndTime - overallStartTime;
743 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
744 << std::fixed << totalInferenceDuration.count() << " ms\n";
745 }
746 catch (const armnn::Exception& e)
747 {
748 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
749 return EXIT_FAILURE;
750 }
751 }
752 // Asynchronous execution using std::launch::async
Sadik Armagana04a9d72021-04-27 10:02:10 +0100753 else
754 {
755 try
756 {
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100757 ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100758 std::vector<std::future<std::tuple<unsigned int,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100759 std::chrono::duration<double, std::milli>>>> inferenceResults;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100760 inferenceResults.reserve(params.m_Iterations);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100761
762 // Create WorkingMemHandles for each inference
763 std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100764 workingMemHandles.reserve(params.m_Iterations);
765 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100766 {
767 workingMemHandles.push_back(model.CreateWorkingMemHandle());
768 }
769
770 // Run each inference in its own thread
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100771 // start a timer
772 const auto start_time = armnn::GetTimeNow();
Jan Eilersf17fcd52021-07-26 22:20:00 +0100773 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100774 {
775 armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get();
Finn Williamsf364d532021-06-09 17:07:33 +0100776
Sadik Armagana04a9d72021-04-27 10:02:10 +0100777 inferenceResults.push_back(std::async(
778 std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() {
Finn Williamsf364d532021-06-09 17:07:33 +0100779 return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100780 }
781 ));
782 }
783
784 // Check the results
785 for (unsigned int j = 0; j < inferenceResults.size(); ++j)
786 {
787 // Get the results
788 auto inferenceResult = inferenceResults[j].get();
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100789 auto inferenceDuration = std::get<1>(inferenceResult);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100790 auto inferenceID = std::get<0>(inferenceResult);
791
792 if (params.m_GenerateTensorData)
793 {
794 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
795 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100796 if (params.m_DontPrintOutputs)
797 {
798 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
799 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100800
801 // Print output tensors
802 const auto& infosOut = model.GetOutputBindingInfos();
803 for (size_t i = 0; i < numOutputs; i++)
804 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100805 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100806 // case we override those files when processing the results of each iteration (only the
807 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100808 // output files for each output of each iteration.
809 size_t outputFileIndex = j * numOutputs + i;
810 if (!params.m_OutputTensorFiles.empty())
811 {
812 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
813 ARMNN_LOG(info) << "Writing output " << i << " named: '"
814 << inferenceModelParams.m_OutputBindings[i]
815 << "' of iteration: " << j+1 << " to file: '"
816 << params.m_OutputTensorFiles[outputFileIndex] << "'";
817 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100818 const armnn::TensorInfo& infoOut = infosOut[i].second;
819 auto outputTensorFile = params.m_OutputTensorFiles.empty()
820 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100821 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100822
823 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
824 infoOut,
825 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100826 params.m_DequantizeOutput,
827 !params.m_DontPrintOutputs);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100828 mapbox::util::apply_visitor(printer, outputs[j][i]);
829 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100830 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100831 ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100832 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100833 // finish timer
834 const auto duration = armnn::GetTimeDuration(start_time);
835 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
836 << std::fixed << duration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100837 }
838 catch (const armnn::Exception& e)
839 {
840 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
841 return EXIT_FAILURE;
842 }
Jan Eilers45274902020-10-15 18:34:43 +0100843 }
844 }
845 catch (const armnn::Exception& e)
846 {
847 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
848 return EXIT_FAILURE;
849 }
850
851 return EXIT_SUCCESS;
852}
853
James Conroy7b4886f2019-04-11 10:23:58 +0100854// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +0100855int main(int argc, const char* argv[])
856{
857 // Configures logging for both the ARMNN library and this test program.
Jan Eilers45274902020-10-15 18:34:43 +0100858 #ifdef NDEBUG
telsoa01c577f2c2018-08-31 09:22:23 +0100859 armnn::LogSeverity level = armnn::LogSeverity::Info;
Jan Eilers45274902020-10-15 18:34:43 +0100860 #else
telsoa01c577f2c2018-08-31 09:22:23 +0100861 armnn::LogSeverity level = armnn::LogSeverity::Debug;
Jan Eilers45274902020-10-15 18:34:43 +0100862 #endif
telsoa01c577f2c2018-08-31 09:22:23 +0100863 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +0100864
telsoa01c577f2c2018-08-31 09:22:23 +0100865
Jan Eilers45274902020-10-15 18:34:43 +0100866 // Get ExecuteNetwork parameters and runtime options from command line
Jan Eilersf17fcd52021-07-26 22:20:00 +0100867 // This might throw an InvalidArgumentException if the user provided invalid inputs
868 ProgramOptions ProgramOptions;
869 try {
870 ProgramOptions.ParseOptions(argc, argv);
871 } catch (const std::exception &e){
872 ARMNN_LOG(fatal) << e.what();
873 return EXIT_FAILURE;
874 }
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000875
Keith Davis4914d0c2021-08-18 17:14:05 +0100876 if ((ProgramOptions.m_ExNetParams.m_OutputDetailsToStdOut ||
877 ProgramOptions.m_ExNetParams.m_OutputDetailsOnlyToStdOut)
878 && !ProgramOptions.m_ExNetParams.m_EnableProfiling)
Keith Davisf4874862021-08-09 16:49:18 +0100879 {
880 ARMNN_LOG(fatal) << "You must enable profiling if you would like to output layer details";
881 return EXIT_FAILURE;
882 }
883
Jan Eilers45274902020-10-15 18:34:43 +0100884 std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat;
885
886 // Forward to implementation based on the parser type
887 if (modelFormat.find("armnn") != std::string::npos)
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100888 {
Jan Eilers45274902020-10-15 18:34:43 +0100889 #if defined(ARMNN_SERIALIZER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000890 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +0100891 return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime);
892 #else
893 ARMNN_LOG(fatal) << "Not built with serialization support.";
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100894 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100895 #endif
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100896 }
Jan Eilers45274902020-10-15 18:34:43 +0100897 else if (modelFormat.find("onnx") != std::string::npos)
telsoa01c577f2c2018-08-31 09:22:23 +0100898 {
Jan Eilers45274902020-10-15 18:34:43 +0100899 #if defined(ARMNN_ONNX_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000900 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +0100901 return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime);
902 #else
903 ARMNN_LOG(fatal) << "Not built with Onnx parser support.";
904 return EXIT_FAILURE;
905 #endif
906 }
Jan Eilers45274902020-10-15 18:34:43 +0100907 else if(modelFormat.find("tflite") != std::string::npos)
908 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000909 if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser)
910 {
911 #if defined(ARMNN_TF_LITE_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000912 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
913 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime);
Finn Williamsf806c4d2021-02-22 15:13:12 +0000914 #else
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000915 ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support.";
916 return EXIT_FAILURE;
Finn Williamsf806c4d2021-02-22 15:13:12 +0000917 #endif
918 }
919 else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
920 ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
921 ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
922 ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000923 {
924 #if defined(ARMNN_TF_LITE_DELEGATE)
Colm Donelan45142282021-10-21 23:39:52 +0100925 return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, ProgramOptions.m_RuntimeOptions);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000926 #else
Finn Williamsbbbefec2020-11-25 14:32:42 +0000927 ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000928 return EXIT_FAILURE;
929 #endif
930 }
Jan Eilers45274902020-10-15 18:34:43 +0100931 }
932 else
933 {
934 ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat
Nikhil Raj5d955cf2021-04-19 16:59:48 +0100935 << "'. Please include 'tflite' or 'onnx'";
Jan Eilers45274902020-10-15 18:34:43 +0100936 return EXIT_FAILURE;
telsoa014fcda012018-03-09 14:13:49 +0000937 }
938}