blob: 085721c6bbdfe19e64c281ee86bd5ed551f02217 [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 Williamsf806c4d2021-02-22 15:13:12 +0000157 else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000158 {
159 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000160
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000161 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000162 {
163 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
164 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
165 return EXIT_FAILURE;
166 }
167
Finn Williams56870182020-11-20 13:57:53 +0000168 std::vector<int8_t> tensorData;
169 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100170 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000171 dataFile,
172 [](const std::string& s)
173 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
174
175 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000176 }
177 else if (params.m_InputTypes[inputIndex].compare("int") == 0)
178 {
179 auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000180
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000181 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000182 {
183 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
184 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
185 return EXIT_FAILURE;
186 }
187
Finn Williams56870182020-11-20 13:57:53 +0000188 std::vector<int32_t> tensorData;
189 PopulateTensorWithDataGeneric<int32_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100190 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000191 dataFile,
192 [](const std::string& s)
193 { return std::stoi(s); });
194
195 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000196 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100197 else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 ||
198 params.m_InputTypes[inputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000199 {
200 auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000201
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000202 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000203 {
204 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
205 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
206 return EXIT_FAILURE;
207 }
208
Finn Williams56870182020-11-20 13:57:53 +0000209 std::vector<uint8_t> tensorData;
210 PopulateTensorWithDataGeneric<uint8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100211 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000212 dataFile,
213 [](const std::string& s)
214 { return armnn::numeric_cast<uint8_t>(std::stoi(s)); });
215
216 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000217 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100218 else if (params.m_InputTypes[inputIndex].compare("qasymms8") == 0)
219 {
220 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
221
222 if(inputData == NULL)
223 {
224 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
225 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
226 return EXIT_FAILURE;
227 }
228
229 std::vector<int8_t> tensorData;
230 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100231 inputSize,
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100232 dataFile,
233 [](const std::string& s)
234 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
235
236 std::copy(tensorData.begin(), tensorData.end(), inputData);
237 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000238 else
239 {
240 ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". ";
241 return EXIT_FAILURE;
242 }
243 }
244
Tamas Nyiri00564232021-11-28 21:31:33 +0000245 // Run inference, print the output of the inference
Sadik Armagan5d03e312020-11-17 16:43:56 +0000246 for (size_t x = 0; x < params.m_Iterations; x++)
247 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100248 // Start timer to record inference time in milliseconds.
249 const auto start_time = armnn::GetTimeNow();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000250 // Run the inference
Finn Williamsf806c4d2021-02-22 15:13:12 +0000251 status = tfLiteInterpreter->Invoke();
Colm Donelan3cff15a2021-10-12 15:06:19 +0100252 const auto duration = armnn::GetTimeDuration(start_time);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000253
Tamas Nyiri00564232021-11-28 21:31:33 +0000254 // The TFLite interpreter's outputs might be in a different order than the user inputted output names.
255 std::map<unsigned int, int> paramToTfliteOutputIndex;
256 for (unsigned int paramIndex = 0; paramIndex < params.m_OutputNames.size(); ++paramIndex)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000257 {
Tamas Nyiri00564232021-11-28 21:31:33 +0000258 paramToTfliteOutputIndex[paramIndex] = -1;
259 for (unsigned int tfLiteIndex = 0; tfLiteIndex < tfLiteInterpreter->outputs().size(); ++tfLiteIndex)
260 {
261 if (params.m_OutputNames[paramIndex] == tfLiteInterpreter->GetOutputName(tfLiteIndex))
262 {
263 paramToTfliteOutputIndex[paramIndex] = tfLiteIndex;
264 }
265 }
266 }
267
268 // Print out the output
269 for (unsigned int paramOutputIndex = 0; paramOutputIndex < params.m_OutputNames.size(); ++paramOutputIndex)
270 {
271 int outputIndex = paramToTfliteOutputIndex[paramOutputIndex];
272 if (outputIndex == -1)
273 {
274 std::cout << fmt::format("Output name: {} doesn't exist.", params.m_OutputNames[paramOutputIndex]) <<
275 std::endl;
276 continue;
277 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000278 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000279 TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims;
Colm Donelan3cff15a2021-10-12 15:06:19 +0100280 // If we've been asked to write to a file then set a file output stream. Otherwise use stdout.
281 FILE* outputTensorFile = stdout;
282 if (!params.m_OutputTensorFiles.empty())
283 {
284 outputTensorFile = fopen(params.m_OutputTensorFiles[outputIndex].c_str(), "w");
285 if (outputTensorFile == NULL)
286 {
287 ARMNN_LOG(fatal) << "Specified output tensor file, \"" <<
288 params.m_OutputTensorFiles[outputIndex] <<
289 "\", cannot be created. Defaulting to stdout. " <<
290 "Error was: " << std::strerror(errno);
291 outputTensorFile = stdout;
292 }
293 else
294 {
295 ARMNN_LOG(info) << "Writing output " << outputIndex << "' of iteration: " << x+1 << " to file: '"
296 << params.m_OutputTensorFiles[outputIndex] << "'";
297 }
298 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000299 long outputSize = 1;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000300 for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim)
301 {
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000302 outputSize *= outputDims->data[dim];
Sadik Armagan5d03e312020-11-17 16:43:56 +0000303 }
304
Tamas Nyiri00564232021-11-28 21:31:33 +0000305 std::cout << tfLiteInterpreter->GetOutputName(outputIndex) << ": ";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000306 if (params.m_OutputTypes[outputIndex].compare("float") == 0)
307 {
308 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000309 if(tfLiteDelageOutputData == NULL)
310 {
311 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
312 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
313 return EXIT_FAILURE;
314 }
315
Jan Eilers284b5d12021-09-07 12:46:15 +0100316 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000317 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100318 for (int i = 0; i < outputSize; ++i)
319 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100320 fprintf(outputTensorFile, "%f ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100321 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000322 }
323 }
324 else if (params.m_OutputTypes[outputIndex].compare("int") == 0)
325 {
326 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000327 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)
Sadik Armagan5d03e312020-11-17 16:43:56 +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 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000340 }
341 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000342 else if (params.m_OutputTypes[outputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000343 {
344 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId);
345 if(tfLiteDelageOutputData == NULL)
346 {
347 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
348 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
349 return EXIT_FAILURE;
350 }
351
Jan Eilers284b5d12021-09-07 12:46:15 +0100352 if (!params.m_DontPrintOutputs)
Finn Williams56870182020-11-20 13:57:53 +0000353 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100354 for (int i = 0; i < outputSize; ++i)
355 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100356 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100357 }
Finn Williams56870182020-11-20 13:57:53 +0000358 }
359 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100360 else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0 ||
361 params.m_OutputTypes[outputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000362 {
363 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000364 if(tfLiteDelageOutputData == NULL)
365 {
366 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
367 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
368 return EXIT_FAILURE;
369 }
370
Jan Eilers284b5d12021-09-07 12:46:15 +0100371 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000372 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100373 for (int i = 0; i < outputSize; ++i)
374 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100375 fprintf(outputTensorFile, "%u ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100376 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000377 }
378 }
379 else
380 {
381 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
382 "\"" << params.m_OutputTypes[outputIndex] <<
383 "\" may be incorrect. Output type can be specified with -z argument";
384 return EXIT_FAILURE;
385 }
386 std::cout << std::endl;
387 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100388 CheckInferenceTimeThreshold(duration, params.m_ThresholdTime);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000389 }
390
391 return status;
392}
393#endif
Jan Eilers45274902020-10-15 18:34:43 +0100394template<typename TParser, typename TDataType>
395int MainImpl(const ExecuteNetworkParams& params,
396 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
397{
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100398 using namespace std::chrono;
Jan Eilers45274902020-10-15 18:34:43 +0100399
Francis Murtagh40d27412021-10-28 11:11:35 +0100400 std::vector<std::vector<armnnUtils::TContainer>> inputs;
401 std::vector<std::vector<armnnUtils::TContainer>> outputs;
Jan Eilers45274902020-10-15 18:34:43 +0100402
403 try
404 {
405 // Creates an InferenceModel, which will parse the model and load it into an IRuntime.
406 typename InferenceModel<TParser, TDataType>::Params inferenceModelParams;
407 inferenceModelParams.m_ModelPath = params.m_ModelPath;
408 inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary;
409 inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices;
410 inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath;
411 inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate;
412 inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails;
413 inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported;
414 inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape;
415 inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath;
Matthew Sloyan42432112021-01-08 10:30:51 +0000416 inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork;
417 inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath;
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000418 inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads;
Finn Williams40646322021-02-11 16:16:42 +0000419 inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100420 inferenceModelParams.m_AsyncEnabled = params.m_Concurrent;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100421 inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize;
Keith Davisf4874862021-08-09 16:49:18 +0100422 inferenceModelParams.m_OutputDetailsToStdOut = params.m_OutputDetailsToStdOut;
Keith Davis4914d0c2021-08-18 17:14:05 +0100423 inferenceModelParams.m_OutputDetailsOnlyToStdOut = params.m_OutputDetailsOnlyToStdOut;
Jan Eilers45274902020-10-15 18:34:43 +0100424
425 for(const std::string& inputName: params.m_InputNames)
426 {
427 inferenceModelParams.m_InputBindings.push_back(inputName);
428 }
429
430 for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i)
431 {
432 inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]);
433 }
434
435 for(const std::string& outputName: params.m_OutputNames)
436 {
437 inferenceModelParams.m_OutputBindings.push_back(outputName);
438 }
439
440 inferenceModelParams.m_SubgraphId = params.m_SubgraphId;
441 inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode;
442 inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode;
443
444 InferenceModel<TParser, TDataType> model(inferenceModelParams,
445 params.m_EnableProfiling,
446 params.m_DynamicBackendsPath,
447 runtime);
448
449 const size_t numInputs = inferenceModelParams.m_InputBindings.size();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100450
451 armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ?
452 armnn::MakeOptional<QuantizationParams>(
453 model.GetInputQuantizationParams()) :
454 armnn::EmptyOptional();
455
Jan Eilersf17fcd52021-07-26 22:20:00 +0100456 if (params.m_InputTensorDataFilePaths.size() > numInputs)
457 {
458 ARMNN_LOG(info) << "Given network has " << numInputs << " input/s. One input-tensor-data file is required "
459 << "for each input. The user provided "
460 << params.m_InputTensorDataFilePaths.size()
461 << " input-tensor-data file/s which will be used to fill the input/s.\n";
462 }
463
464 for(unsigned int j = 0; j < params.m_Iterations ; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100465 {
Francis Murtagh40d27412021-10-28 11:11:35 +0100466 std::vector<armnnUtils::TContainer> inputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100467 for(unsigned int i = 0; i < numInputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100468 {
Tamas Nyiri00564232021-11-28 21:31:33 +0000469 // If there are fewer input files given than required for the execution of
Jan Eilersf17fcd52021-07-26 22:20:00 +0100470 // params.m_Iterations we simply start with the first input file again
471 size_t inputFileIndex = j * numInputs + i;
472 if (!params.m_InputTensorDataFilePaths.empty())
473 {
474 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
475 }
476
Sadik Armagana04a9d72021-04-27 10:02:10 +0100477 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
478 armnn::EmptyOptional() :
479 armnn::MakeOptional<std::string>(
Jan Eilersf17fcd52021-07-26 22:20:00 +0100480 params.m_InputTensorDataFilePaths.at(inputFileIndex));
Sadik Armagana04a9d72021-04-27 10:02:10 +0100481
482 unsigned int numElements = model.GetInputSize(i);
483 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
484 {
485 // If the user has provided a tensor shape for the current input,
486 // override numElements
487 numElements = params.m_InputTensorShapes[i]->GetNumElements();
488 }
489
Francis Murtagh40d27412021-10-28 11:11:35 +0100490 armnnUtils::TContainer tensorData;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100491 PopulateTensorWithData(tensorData,
492 numElements,
493 params.m_InputTypes[i],
494 qParams,
495 dataFile);
496
497 inputDataContainers.push_back(tensorData);
Jan Eilers45274902020-10-15 18:34:43 +0100498 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100499 inputs.push_back(inputDataContainers);
Jan Eilers45274902020-10-15 18:34:43 +0100500 }
501
502 const size_t numOutputs = inferenceModelParams.m_OutputBindings.size();
Jan Eilers45274902020-10-15 18:34:43 +0100503
Colm Donelanc5e41982021-10-28 20:19:43 +0100504 // The user is allowed to specify the data type of each output tensor. It is used here to construct the
505 // result tensors for each iteration. It is possible for the user to specify a type that does not match
506 // the data type of the corresponding model output. It may not make sense, but it is historically allowed.
507 // The potential problem here is a buffer overrun when a larger data type is written into the space for a
508 // smaller one. Issue a warning to highlight the potential problem.
509 for (unsigned int outputIdx = 0; outputIdx < model.GetOutputBindingInfos().size(); ++outputIdx)
510 {
511 armnn::DataType type = model.GetOutputBindingInfo(outputIdx).second.GetDataType();
512 switch (type)
513 {
David Monahan67cc5fc2021-11-03 12:56:41 +0000514 // --output-type only supports float, int, qasymms8 or qasymmu8.
Colm Donelanc5e41982021-10-28 20:19:43 +0100515 case armnn::DataType::Float32:
516 if (params.m_OutputTypes[outputIdx].compare("float") != 0)
517 {
518 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Float32. The " <<
519 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
520 ". This may cause unexpected problems or random failures.";
521 }
522 break;
523 case armnn::DataType::QAsymmU8:
524 if (params.m_OutputTypes[outputIdx].compare("qasymmu8") != 0)
525 {
526 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmU8. The " <<
527 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
528 ". This may cause unexpected problemsor random failures.";
529 }
530 break;
531 case armnn::DataType::Signed32:
532 if (params.m_OutputTypes[outputIdx].compare("int") != 0)
533 {
534 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Signed32. The " <<
535 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
536 ". This may cause unexpected problems or random failures.";
537 }
538 break;
539 case armnn::DataType::QAsymmS8:
540 if (params.m_OutputTypes[outputIdx].compare("qasymms8") != 0)
541 {
542 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmS8. The " <<
543 "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
544 ". This may cause unexpected problems or random failures.";
545 }
546 break;
547 default:
548 break;
549 }
550 }
Jan Eilersf17fcd52021-07-26 22:20:00 +0100551 for (unsigned int j = 0; j < params.m_Iterations; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100552 {
Francis Murtagh40d27412021-10-28 11:11:35 +0100553 std::vector <armnnUtils::TContainer> outputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100554 for (unsigned int i = 0; i < numOutputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100555 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100556 if (params.m_OutputTypes[i].compare("float") == 0)
Jan Eilers45274902020-10-15 18:34:43 +0100557 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100558 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100559 }
560 else if (params.m_OutputTypes[i].compare("int") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100561 {
562 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100563 }
564 else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
565 params.m_OutputTypes[i].compare("qasymmu8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100566 {
567 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100568 }
569 else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100570 {
571 outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
572 } else
573 {
574 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
575 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100576 }
577 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100578 outputs.push_back(outputDataContainers);
579 }
580
Jan Eilersf17fcd52021-07-26 22:20:00 +0100581 if (params.m_Iterations > 1)
582 {
583 std::stringstream msg;
584 msg << "Network will be executed " << params.m_Iterations;
585 if (params.m_Concurrent)
586 {
587 msg << " times in an asynchronous manner. ";
588 }
589 else
590 {
591 msg << " times successively. ";
592 }
593 msg << "The input-tensor-data files will be reused recursively if the user didn't provide enough to "
594 "cover each execution.";
595 ARMNN_LOG(info) << msg.str();
596 }
597
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100598 // Synchronous execution
Sadik Armagana04a9d72021-04-27 10:02:10 +0100599 if (!params.m_Concurrent)
600 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100601 for (size_t x = 0; x < params.m_Iterations; x++)
602 {
603 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
Jan Eilersf17fcd52021-07-26 22:20:00 +0100604 auto inference_duration = model.Run(inputs[x], outputs[x]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100605
606 if (params.m_GenerateTensorData)
607 {
608 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
609 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100610 if (params.m_DontPrintOutputs)
611 {
612 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
613 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100614
615 // Print output tensors
616 const auto& infosOut = model.GetOutputBindingInfos();
617 for (size_t i = 0; i < numOutputs; i++)
618 {
619 const armnn::TensorInfo& infoOut = infosOut[i].second;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100620
Jan Eilers284b5d12021-09-07 12:46:15 +0100621 // We've made sure before that the number of output files either equals numOutputs, in which
622 // case we override those files when processing the results of each iteration (only the result
623 // of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100624 // output files for each output of each iteration.
625 size_t outputFileIndex = x * numOutputs + i;
626 if (!params.m_OutputTensorFiles.empty())
627 {
628 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
629 ARMNN_LOG(info) << "Writing output " << i << " named: '"
630 << inferenceModelParams.m_OutputBindings[i]
631 << "' of iteration: " << x+1 << " to file: '"
632 << params.m_OutputTensorFiles[outputFileIndex] << "'";
633 }
634 auto outputTensorFile = params.m_OutputTensorFiles.empty()
635 ? ""
636 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100637
638 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
639 infoOut,
640 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100641 params.m_DequantizeOutput,
642 !params.m_DontPrintOutputs);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100643 mapbox::util::apply_visitor(printer, outputs[x][i]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100644 }
645
646 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
647 << std::fixed << inference_duration.count() << " ms\n";
648
649 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
650 if (params.m_ThresholdTime != 0.0)
651 {
652 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
653 << std::fixed << params.m_ThresholdTime << " ms";
654 auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count();
655 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
656 << std::fixed << thresholdMinusInference << " ms" << "\n";
657
658 if (thresholdMinusInference < 0)
659 {
660 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
661 ARMNN_LOG(fatal) << errorMessage;
662 }
663 }
664 }
665 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100666 // Asynchronous execution using the Arm NN thread pool
Kevin May94dd4db2021-05-26 16:01:08 +0100667 else if (params.m_ThreadPoolSize >= 1)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100668 {
669 try
670 {
671 ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100672 armnn::AsyncCallbackManager callbackManager;
Francis Murtagh40d27412021-10-28 11:11:35 +0100673 std::unordered_map<armnn::InferenceId, std::vector<armnnUtils::TContainer>&> inferenceOutputMap;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100674
675 // Declare the latest and earliest inference times here to be used when calculating overall time
676 std::chrono::high_resolution_clock::time_point earliestStartTime;
677 std::chrono::high_resolution_clock::time_point latestEndTime =
678 std::chrono::high_resolution_clock::now();
679
680 // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the
681 // LoadedNetwork with each scheduled inference having a specific priority
Jan Eilersf17fcd52021-07-26 22:20:00 +0100682 for (size_t i = 0; i < params.m_Iterations; ++i)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100683 {
Finn Williamsf364d532021-06-09 17:07:33 +0100684 std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback();
685 inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]});
686 model.RunAsync(inputs[i], outputs[i], cb);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100687 }
688
689 // Check the results
690 unsigned int j = 0;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100691 for (size_t iteration = 0; iteration < params.m_Iterations; ++iteration)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100692 {
Finn Williamsf364d532021-06-09 17:07:33 +0100693 auto cb = callbackManager.GetNotifiedCallback();
694
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100695 // Get the results
696 auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime());
697 auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime());
698 auto inferenceDuration = endTime - startTime;
699
700 if (latestEndTime < cb->GetEndTime())
701 {
702 latestEndTime = cb->GetEndTime();
703 }
704
705 if (earliestStartTime.time_since_epoch().count() == 0)
706 {
707 earliestStartTime = cb->GetStartTime();
708 }
709 else if (earliestStartTime > cb->GetStartTime())
710 {
711 earliestStartTime = cb->GetStartTime();
712 }
713
714 if (params.m_GenerateTensorData)
715 {
716 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
717 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100718 if (params.m_DontPrintOutputs)
719 {
720 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
721 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100722
723 // Print output tensors
724 const auto& infosOut = model.GetOutputBindingInfos();
725 for (size_t i = 0; i < numOutputs; i++)
726 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100727 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100728 // case we override those files when processing the results of each iteration (only the
729 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100730 // output files for each output of each iteration.
731 size_t outputFileIndex = iteration * numOutputs + i;
732 if (!params.m_OutputTensorFiles.empty())
733 {
734 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
735 ARMNN_LOG(info) << "Writing output " << i << " named: '"
736 << inferenceModelParams.m_OutputBindings[i]
737 << "' of iteration: " << iteration+1 << " to file: '"
738 << params.m_OutputTensorFiles[outputFileIndex] << "'";
739 }
740
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100741 const armnn::TensorInfo& infoOut = infosOut[i].second;
742 auto outputTensorFile = params.m_OutputTensorFiles.empty()
743 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100744 : params.m_OutputTensorFiles[outputFileIndex];
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100745
746 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
747 infoOut,
748 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100749 params.m_DequantizeOutput,
750 !params.m_DontPrintOutputs);
Finn Williamsf364d532021-06-09 17:07:33 +0100751 mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100752 }
753
Colm Donelan3cff15a2021-10-12 15:06:19 +0100754 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100755 ++j;
756 }
757 //print duration difference between overallStartTime and overallEndTime
758 auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime);
759 auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime);
760 auto totalInferenceDuration = overallEndTime - overallStartTime;
761 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
762 << std::fixed << totalInferenceDuration.count() << " ms\n";
763 }
764 catch (const armnn::Exception& e)
765 {
766 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
767 return EXIT_FAILURE;
768 }
769 }
770 // Asynchronous execution using std::launch::async
Sadik Armagana04a9d72021-04-27 10:02:10 +0100771 else
772 {
773 try
774 {
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100775 ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100776 std::vector<std::future<std::tuple<unsigned int,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100777 std::chrono::duration<double, std::milli>>>> inferenceResults;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100778 inferenceResults.reserve(params.m_Iterations);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100779
780 // Create WorkingMemHandles for each inference
781 std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100782 workingMemHandles.reserve(params.m_Iterations);
783 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100784 {
785 workingMemHandles.push_back(model.CreateWorkingMemHandle());
786 }
787
788 // Run each inference in its own thread
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100789 // start a timer
790 const auto start_time = armnn::GetTimeNow();
Jan Eilersf17fcd52021-07-26 22:20:00 +0100791 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100792 {
793 armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get();
Finn Williamsf364d532021-06-09 17:07:33 +0100794
Sadik Armagana04a9d72021-04-27 10:02:10 +0100795 inferenceResults.push_back(std::async(
796 std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() {
Finn Williamsf364d532021-06-09 17:07:33 +0100797 return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100798 }
799 ));
800 }
801
802 // Check the results
803 for (unsigned int j = 0; j < inferenceResults.size(); ++j)
804 {
805 // Get the results
806 auto inferenceResult = inferenceResults[j].get();
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100807 auto inferenceDuration = std::get<1>(inferenceResult);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100808 auto inferenceID = std::get<0>(inferenceResult);
809
810 if (params.m_GenerateTensorData)
811 {
812 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
813 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100814 if (params.m_DontPrintOutputs)
815 {
816 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
817 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100818
819 // Print output tensors
820 const auto& infosOut = model.GetOutputBindingInfos();
821 for (size_t i = 0; i < numOutputs; i++)
822 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100823 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100824 // case we override those files when processing the results of each iteration (only the
825 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100826 // output files for each output of each iteration.
827 size_t outputFileIndex = j * numOutputs + i;
828 if (!params.m_OutputTensorFiles.empty())
829 {
830 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
831 ARMNN_LOG(info) << "Writing output " << i << " named: '"
832 << inferenceModelParams.m_OutputBindings[i]
833 << "' of iteration: " << j+1 << " to file: '"
834 << params.m_OutputTensorFiles[outputFileIndex] << "'";
835 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100836 const armnn::TensorInfo& infoOut = infosOut[i].second;
837 auto outputTensorFile = params.m_OutputTensorFiles.empty()
838 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100839 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100840
841 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
842 infoOut,
843 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100844 params.m_DequantizeOutput,
845 !params.m_DontPrintOutputs);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100846 mapbox::util::apply_visitor(printer, outputs[j][i]);
847 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100848 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100849 ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100850 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100851 // finish timer
852 const auto duration = armnn::GetTimeDuration(start_time);
853 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
854 << std::fixed << duration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100855 }
856 catch (const armnn::Exception& e)
857 {
858 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
859 return EXIT_FAILURE;
860 }
Jan Eilers45274902020-10-15 18:34:43 +0100861 }
862 }
863 catch (const armnn::Exception& e)
864 {
865 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
866 return EXIT_FAILURE;
867 }
868
869 return EXIT_SUCCESS;
870}
871
James Conroy7b4886f2019-04-11 10:23:58 +0100872// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +0100873int main(int argc, const char* argv[])
874{
875 // Configures logging for both the ARMNN library and this test program.
Jan Eilers45274902020-10-15 18:34:43 +0100876 #ifdef NDEBUG
telsoa01c577f2c2018-08-31 09:22:23 +0100877 armnn::LogSeverity level = armnn::LogSeverity::Info;
Jan Eilers45274902020-10-15 18:34:43 +0100878 #else
telsoa01c577f2c2018-08-31 09:22:23 +0100879 armnn::LogSeverity level = armnn::LogSeverity::Debug;
Jan Eilers45274902020-10-15 18:34:43 +0100880 #endif
telsoa01c577f2c2018-08-31 09:22:23 +0100881 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +0100882
telsoa01c577f2c2018-08-31 09:22:23 +0100883
Jan Eilers45274902020-10-15 18:34:43 +0100884 // Get ExecuteNetwork parameters and runtime options from command line
Jan Eilersf17fcd52021-07-26 22:20:00 +0100885 // This might throw an InvalidArgumentException if the user provided invalid inputs
886 ProgramOptions ProgramOptions;
887 try {
888 ProgramOptions.ParseOptions(argc, argv);
889 } catch (const std::exception &e){
890 ARMNN_LOG(fatal) << e.what();
891 return EXIT_FAILURE;
892 }
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000893
Keith Davis4914d0c2021-08-18 17:14:05 +0100894 if ((ProgramOptions.m_ExNetParams.m_OutputDetailsToStdOut ||
895 ProgramOptions.m_ExNetParams.m_OutputDetailsOnlyToStdOut)
896 && !ProgramOptions.m_ExNetParams.m_EnableProfiling)
Keith Davisf4874862021-08-09 16:49:18 +0100897 {
898 ARMNN_LOG(fatal) << "You must enable profiling if you would like to output layer details";
899 return EXIT_FAILURE;
900 }
901
Jan Eilers45274902020-10-15 18:34:43 +0100902 std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat;
903
904 // Forward to implementation based on the parser type
905 if (modelFormat.find("armnn") != std::string::npos)
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100906 {
Jan Eilers45274902020-10-15 18:34:43 +0100907 #if defined(ARMNN_SERIALIZER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000908 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +0100909 return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime);
910 #else
911 ARMNN_LOG(fatal) << "Not built with serialization support.";
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100912 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100913 #endif
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100914 }
Jan Eilers45274902020-10-15 18:34:43 +0100915 else if (modelFormat.find("onnx") != std::string::npos)
telsoa01c577f2c2018-08-31 09:22:23 +0100916 {
Jan Eilers45274902020-10-15 18:34:43 +0100917 #if defined(ARMNN_ONNX_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000918 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +0100919 return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime);
920 #else
921 ARMNN_LOG(fatal) << "Not built with Onnx parser support.";
922 return EXIT_FAILURE;
923 #endif
924 }
Jan Eilers45274902020-10-15 18:34:43 +0100925 else if(modelFormat.find("tflite") != std::string::npos)
926 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000927 if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser)
928 {
929 #if defined(ARMNN_TF_LITE_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000930 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
931 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime);
Finn Williamsf806c4d2021-02-22 15:13:12 +0000932 #else
Cathal Corbett5b0d8872021-12-06 17:06:12 +0000933 ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support.";
934 return EXIT_FAILURE;
Finn Williamsf806c4d2021-02-22 15:13:12 +0000935 #endif
936 }
937 else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
938 ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
939 ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
940 ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000941 {
942 #if defined(ARMNN_TF_LITE_DELEGATE)
Colm Donelan45142282021-10-21 23:39:52 +0100943 return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, ProgramOptions.m_RuntimeOptions);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000944 #else
Finn Williamsbbbefec2020-11-25 14:32:42 +0000945 ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000946 return EXIT_FAILURE;
947 #endif
948 }
Jan Eilers45274902020-10-15 18:34:43 +0100949 }
950 else
951 {
952 ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat
Nikhil Raj5d955cf2021-04-19 16:59:48 +0100953 << "'. Please include 'tflite' or 'onnx'";
Jan Eilers45274902020-10-15 18:34:43 +0100954 return EXIT_FAILURE;
telsoa014fcda012018-03-09 14:13:49 +0000955 }
956}