blob: e757d2c99253e7078026e861668146c55d8239d1 [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>
Jan Eilers45274902020-10-15 18:34:43 +010013#include <InferenceTest.hpp>
14
15#if defined(ARMNN_SERIALIZER)
16#include "armnnDeserializer/IDeserializer.hpp"
17#endif
Jan Eilers45274902020-10-15 18:34:43 +010018#if defined(ARMNN_TF_LITE_PARSER)
19#include "armnnTfLiteParser/ITfLiteParser.hpp"
20#endif
21#if defined(ARMNN_ONNX_PARSER)
22#include "armnnOnnxParser/IOnnxParser.hpp"
23#endif
Sadik Armagan5d03e312020-11-17 16:43:56 +000024#if defined(ARMNN_TFLITE_DELEGATE)
25#include <armnn_delegate.hpp>
26#include <DelegateOptions.hpp>
27
28#include <tensorflow/lite/builtin_ops.h>
29#include <tensorflow/lite/c/builtin_op_data.h>
30#include <tensorflow/lite/c/common.h>
31#include <tensorflow/lite/optional_debug_tools.h>
32#include <tensorflow/lite/kernels/builtin_op_kernels.h>
33#include <tensorflow/lite/interpreter.h>
34#include <tensorflow/lite/kernels/register.h>
35#endif
Jan Eilers45274902020-10-15 18:34:43 +010036
37#include <future>
Sadik Armagan5d03e312020-11-17 16:43:56 +000038#if defined(ARMNN_TFLITE_DELEGATE)
39int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params,
40 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
41{
42 using namespace tflite;
Jan Eilers45274902020-10-15 18:34:43 +010043
Sadik Armagan5d03e312020-11-17 16:43:56 +000044 std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(params.m_ModelPath.c_str());
45
46 auto tfLiteInterpreter = std::make_unique<Interpreter>();
47 tflite::ops::builtin::BuiltinOpResolver resolver;
48
49 tflite::InterpreterBuilder builder(*model, resolver);
50 builder(&tfLiteInterpreter);
51 tfLiteInterpreter->AllocateTensors();
52
Finn Williamsf806c4d2021-02-22 15:13:12 +000053 int status = 0;
54 if (params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate)
Sadik Armagan19a1c032021-01-20 12:17:00 +000055 {
Finn Williamsf806c4d2021-02-22 15:13:12 +000056 // Create the Armnn Delegate
57 armnnDelegate::DelegateOptions delegateOptions(params.m_ComputeDevices);
58 std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)>
59 theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions),
60 armnnDelegate::TfLiteArmnnDelegateDelete);
61 // Register armnn_delegate to TfLiteInterpreter
62 status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate));
63 if (status == kTfLiteError)
64 {
65 ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!";
66 return EXIT_FAILURE;
67 }
Sadik Armagan19a1c032021-01-20 12:17:00 +000068 }
Finn Williamsf806c4d2021-02-22 15:13:12 +000069 else
70 {
71 std::cout << "Running on TfLite without ArmNN delegate\n";
72 }
73
Sadik Armagan5d03e312020-11-17 16:43:56 +000074
75 std::vector<std::string> inputBindings;
76 for (const std::string& inputName: params.m_InputNames)
77 {
78 inputBindings.push_back(inputName);
79 }
80
81 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData
82 ? armnn::EmptyOptional()
83 : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]);
84
85 const size_t numInputs = inputBindings.size();
86
87 for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex)
88 {
89 int input = tfLiteInterpreter->inputs()[inputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +000090 TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims;
91
92 long inputSize = 1;
93 for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim)
94 {
95 inputSize *= inputDims->data[dim];
96 }
97
Sadik Armagan5d03e312020-11-17 16:43:56 +000098 if (params.m_InputTypes[inputIndex].compare("float") == 0)
99 {
100 auto inputData = tfLiteInterpreter->typed_tensor<float>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000101
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000102 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000103 {
104 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
105 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
106 return EXIT_FAILURE;
107 }
108
Finn Williams56870182020-11-20 13:57:53 +0000109 std::vector<float> tensorData;
110 PopulateTensorWithDataGeneric<float>(tensorData,
111 params.m_InputTensorShapes[inputIndex]->GetNumElements(),
112 dataFile,
113 [](const std::string& s)
114 { return std::stof(s); });
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000115
Finn Williams56870182020-11-20 13:57:53 +0000116 std::copy(tensorData.begin(), tensorData.end(), inputData);
117 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000118 else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000119 {
120 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000121
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000122 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000123 {
124 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
125 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
126 return EXIT_FAILURE;
127 }
128
Finn Williams56870182020-11-20 13:57:53 +0000129 std::vector<int8_t> tensorData;
130 PopulateTensorWithDataGeneric<int8_t>(tensorData,
131 params.m_InputTensorShapes[inputIndex]->GetNumElements(),
132 dataFile,
133 [](const std::string& s)
134 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
135
136 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000137 }
138 else if (params.m_InputTypes[inputIndex].compare("int") == 0)
139 {
140 auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000141
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000142 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000143 {
144 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
145 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
146 return EXIT_FAILURE;
147 }
148
Finn Williams56870182020-11-20 13:57:53 +0000149 std::vector<int32_t> tensorData;
150 PopulateTensorWithDataGeneric<int32_t>(tensorData,
151 params.m_InputTensorShapes[inputIndex]->GetNumElements(),
152 dataFile,
153 [](const std::string& s)
154 { return std::stoi(s); });
155
156 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000157 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100158 else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 ||
159 params.m_InputTypes[inputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000160 {
161 auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000162
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000163 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000164 {
165 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
166 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
167 return EXIT_FAILURE;
168 }
169
Finn Williams56870182020-11-20 13:57:53 +0000170 std::vector<uint8_t> tensorData;
171 PopulateTensorWithDataGeneric<uint8_t>(tensorData,
172 params.m_InputTensorShapes[inputIndex]->GetNumElements(),
173 dataFile,
174 [](const std::string& s)
175 { return armnn::numeric_cast<uint8_t>(std::stoi(s)); });
176
177 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000178 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100179 else if (params.m_InputTypes[inputIndex].compare("qasymms8") == 0)
180 {
181 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
182
183 if(inputData == NULL)
184 {
185 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
186 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
187 return EXIT_FAILURE;
188 }
189
190 std::vector<int8_t> tensorData;
191 PopulateTensorWithDataGeneric<int8_t>(tensorData,
192 params.m_InputTensorShapes[inputIndex]->GetNumElements(),
193 dataFile,
194 [](const std::string& s)
195 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
196
197 std::copy(tensorData.begin(), tensorData.end(), inputData);
198 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000199 else
200 {
201 ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". ";
202 return EXIT_FAILURE;
203 }
204 }
205
206 for (size_t x = 0; x < params.m_Iterations; x++)
207 {
208 // Run the inference
Finn Williamsf806c4d2021-02-22 15:13:12 +0000209 status = tfLiteInterpreter->Invoke();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000210
211 // Print out the output
212 for (unsigned int outputIndex = 0; outputIndex < params.m_OutputNames.size(); ++outputIndex)
213 {
Sadik Armagan5d03e312020-11-17 16:43:56 +0000214 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000215 TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000216
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000217 long outputSize = 1;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000218 for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim)
219 {
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000220 outputSize *= outputDims->data[dim];
Sadik Armagan5d03e312020-11-17 16:43:56 +0000221 }
222
223 std::cout << params.m_OutputNames[outputIndex] << ": ";
224 if (params.m_OutputTypes[outputIndex].compare("float") == 0)
225 {
226 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000227 if(tfLiteDelageOutputData == NULL)
228 {
229 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
230 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
231 return EXIT_FAILURE;
232 }
233
234 for (int i = 0; i < outputSize; ++i)
235 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000236 printf("%f ", tfLiteDelageOutputData[i]);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000237 }
238 }
239 else if (params.m_OutputTypes[outputIndex].compare("int") == 0)
240 {
241 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000242 if(tfLiteDelageOutputData == NULL)
243 {
244 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
245 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
246 return EXIT_FAILURE;
247 }
248
249 for (int i = 0; i < outputSize; ++i)
250 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000251 printf("%d ", tfLiteDelageOutputData[i]);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000252 }
253 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000254 else if (params.m_OutputTypes[outputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000255 {
256 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId);
257 if(tfLiteDelageOutputData == NULL)
258 {
259 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
260 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
261 return EXIT_FAILURE;
262 }
263
264 for (int i = 0; i < outputSize; ++i)
265 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000266 printf("%d ", tfLiteDelageOutputData[i]);
Finn Williams56870182020-11-20 13:57:53 +0000267 }
268 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100269 else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0 ||
270 params.m_OutputTypes[outputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000271 {
272 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000273 if(tfLiteDelageOutputData == NULL)
274 {
275 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
276 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
277 return EXIT_FAILURE;
278 }
279
280 for (int i = 0; i < outputSize; ++i)
281 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000282 printf("%u ", tfLiteDelageOutputData[i]);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000283 }
284 }
285 else
286 {
287 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
288 "\"" << params.m_OutputTypes[outputIndex] <<
289 "\" may be incorrect. Output type can be specified with -z argument";
290 return EXIT_FAILURE;
291 }
292 std::cout << std::endl;
293 }
294 }
295
296 return status;
297}
298#endif
Jan Eilers45274902020-10-15 18:34:43 +0100299template<typename TParser, typename TDataType>
300int MainImpl(const ExecuteNetworkParams& params,
301 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
302{
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100303 using namespace std::chrono;
Jan Eilers45274902020-10-15 18:34:43 +0100304
Sadik Armagana04a9d72021-04-27 10:02:10 +0100305 std::vector<std::vector<TContainer>> inputs;
306 std::vector<std::vector<TContainer>> outputs;
Jan Eilers45274902020-10-15 18:34:43 +0100307
308 try
309 {
310 // Creates an InferenceModel, which will parse the model and load it into an IRuntime.
311 typename InferenceModel<TParser, TDataType>::Params inferenceModelParams;
312 inferenceModelParams.m_ModelPath = params.m_ModelPath;
313 inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary;
314 inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices;
315 inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath;
316 inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate;
317 inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails;
318 inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported;
319 inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape;
320 inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath;
Matthew Sloyan42432112021-01-08 10:30:51 +0000321 inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork;
322 inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath;
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000323 inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads;
Finn Williams40646322021-02-11 16:16:42 +0000324 inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100325 inferenceModelParams.m_AsyncEnabled = params.m_Concurrent;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100326 inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize;
Jan Eilers45274902020-10-15 18:34:43 +0100327
328 for(const std::string& inputName: params.m_InputNames)
329 {
330 inferenceModelParams.m_InputBindings.push_back(inputName);
331 }
332
333 for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i)
334 {
335 inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]);
336 }
337
338 for(const std::string& outputName: params.m_OutputNames)
339 {
340 inferenceModelParams.m_OutputBindings.push_back(outputName);
341 }
342
343 inferenceModelParams.m_SubgraphId = params.m_SubgraphId;
344 inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode;
345 inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode;
346
347 InferenceModel<TParser, TDataType> model(inferenceModelParams,
348 params.m_EnableProfiling,
349 params.m_DynamicBackendsPath,
350 runtime);
351
352 const size_t numInputs = inferenceModelParams.m_InputBindings.size();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100353
354 armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ?
355 armnn::MakeOptional<QuantizationParams>(
356 model.GetInputQuantizationParams()) :
357 armnn::EmptyOptional();
358
Jan Eilersf17fcd52021-07-26 22:20:00 +0100359 if (params.m_InputTensorDataFilePaths.size() > numInputs)
360 {
361 ARMNN_LOG(info) << "Given network has " << numInputs << " input/s. One input-tensor-data file is required "
362 << "for each input. The user provided "
363 << params.m_InputTensorDataFilePaths.size()
364 << " input-tensor-data file/s which will be used to fill the input/s.\n";
365 }
366
367 for(unsigned int j = 0; j < params.m_Iterations ; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100368 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100369 std::vector<TContainer> inputDataContainers;
370 for(unsigned int i = 0; i < numInputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100371 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100372 // If there are less input files given than required for the execution of
373 // params.m_Iterations we simply start with the first input file again
374 size_t inputFileIndex = j * numInputs + i;
375 if (!params.m_InputTensorDataFilePaths.empty())
376 {
377 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
378 }
379
Sadik Armagana04a9d72021-04-27 10:02:10 +0100380 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
381 armnn::EmptyOptional() :
382 armnn::MakeOptional<std::string>(
Jan Eilersf17fcd52021-07-26 22:20:00 +0100383 params.m_InputTensorDataFilePaths.at(inputFileIndex));
Sadik Armagana04a9d72021-04-27 10:02:10 +0100384
385 unsigned int numElements = model.GetInputSize(i);
386 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
387 {
388 // If the user has provided a tensor shape for the current input,
389 // override numElements
390 numElements = params.m_InputTensorShapes[i]->GetNumElements();
391 }
392
393 TContainer tensorData;
394 PopulateTensorWithData(tensorData,
395 numElements,
396 params.m_InputTypes[i],
397 qParams,
398 dataFile);
399
400 inputDataContainers.push_back(tensorData);
Jan Eilers45274902020-10-15 18:34:43 +0100401 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100402 inputs.push_back(inputDataContainers);
Jan Eilers45274902020-10-15 18:34:43 +0100403 }
404
405 const size_t numOutputs = inferenceModelParams.m_OutputBindings.size();
Jan Eilers45274902020-10-15 18:34:43 +0100406
Jan Eilersf17fcd52021-07-26 22:20:00 +0100407 for (unsigned int j = 0; j < params.m_Iterations; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100408 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100409 std::vector <TContainer> outputDataContainers;
410 for (unsigned int i = 0; i < numOutputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100411 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100412 if (params.m_OutputTypes[i].compare("float") == 0)
Jan Eilers45274902020-10-15 18:34:43 +0100413 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100414 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100415 }
416 else if (params.m_OutputTypes[i].compare("int") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100417 {
418 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100419 }
420 else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
421 params.m_OutputTypes[i].compare("qasymmu8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100422 {
423 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100424 }
425 else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100426 {
427 outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
428 } else
429 {
430 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
431 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100432 }
433 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100434 outputs.push_back(outputDataContainers);
435 }
436
Jan Eilersf17fcd52021-07-26 22:20:00 +0100437 if (params.m_Iterations > 1)
438 {
439 std::stringstream msg;
440 msg << "Network will be executed " << params.m_Iterations;
441 if (params.m_Concurrent)
442 {
443 msg << " times in an asynchronous manner. ";
444 }
445 else
446 {
447 msg << " times successively. ";
448 }
449 msg << "The input-tensor-data files will be reused recursively if the user didn't provide enough to "
450 "cover each execution.";
451 ARMNN_LOG(info) << msg.str();
452 }
453
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100454 // Synchronous execution
Sadik Armagana04a9d72021-04-27 10:02:10 +0100455 if (!params.m_Concurrent)
456 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100457 for (size_t x = 0; x < params.m_Iterations; x++)
458 {
459 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
Jan Eilersf17fcd52021-07-26 22:20:00 +0100460 auto inference_duration = model.Run(inputs[x], outputs[x]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100461
462 if (params.m_GenerateTensorData)
463 {
464 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
465 }
466
467 // Print output tensors
468 const auto& infosOut = model.GetOutputBindingInfos();
469 for (size_t i = 0; i < numOutputs; i++)
470 {
471 const armnn::TensorInfo& infoOut = infosOut[i].second;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100472
473 // We've made sure before that the number of output files either equals numOutputs, in which case
474 // we override those files when processing the results of each iteration (only the result of the
475 // last iteration will be stored), or there are enough
476 // output files for each output of each iteration.
477 size_t outputFileIndex = x * numOutputs + i;
478 if (!params.m_OutputTensorFiles.empty())
479 {
480 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
481 ARMNN_LOG(info) << "Writing output " << i << " named: '"
482 << inferenceModelParams.m_OutputBindings[i]
483 << "' of iteration: " << x+1 << " to file: '"
484 << params.m_OutputTensorFiles[outputFileIndex] << "'";
485 }
486 auto outputTensorFile = params.m_OutputTensorFiles.empty()
487 ? ""
488 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100489
490 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
491 infoOut,
492 outputTensorFile,
493 params.m_DequantizeOutput);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100494 mapbox::util::apply_visitor(printer, outputs[x][i]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100495 }
496
497 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
498 << std::fixed << inference_duration.count() << " ms\n";
499
500 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
501 if (params.m_ThresholdTime != 0.0)
502 {
503 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
504 << std::fixed << params.m_ThresholdTime << " ms";
505 auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count();
506 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
507 << std::fixed << thresholdMinusInference << " ms" << "\n";
508
509 if (thresholdMinusInference < 0)
510 {
511 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
512 ARMNN_LOG(fatal) << errorMessage;
513 }
514 }
515 }
516 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100517 // Asynchronous execution using the Arm NN thread pool
Kevin May94dd4db2021-05-26 16:01:08 +0100518 else if (params.m_ThreadPoolSize >= 1)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100519 {
520 try
521 {
522 ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100523 armnn::AsyncCallbackManager callbackManager;
524 std::unordered_map<armnn::InferenceId, std::vector<TContainer>&> inferenceOutputMap;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100525
526 // Declare the latest and earliest inference times here to be used when calculating overall time
527 std::chrono::high_resolution_clock::time_point earliestStartTime;
528 std::chrono::high_resolution_clock::time_point latestEndTime =
529 std::chrono::high_resolution_clock::now();
530
531 // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the
532 // LoadedNetwork with each scheduled inference having a specific priority
Jan Eilersf17fcd52021-07-26 22:20:00 +0100533 for (size_t i = 0; i < params.m_Iterations; ++i)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100534 {
Finn Williamsf364d532021-06-09 17:07:33 +0100535 std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback();
536 inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]});
537 model.RunAsync(inputs[i], outputs[i], cb);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100538 }
539
540 // Check the results
541 unsigned int j = 0;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100542 for (size_t iteration = 0; iteration < params.m_Iterations; ++iteration)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100543 {
Finn Williamsf364d532021-06-09 17:07:33 +0100544 auto cb = callbackManager.GetNotifiedCallback();
545
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100546 // Get the results
547 auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime());
548 auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime());
549 auto inferenceDuration = endTime - startTime;
550
551 if (latestEndTime < cb->GetEndTime())
552 {
553 latestEndTime = cb->GetEndTime();
554 }
555
556 if (earliestStartTime.time_since_epoch().count() == 0)
557 {
558 earliestStartTime = cb->GetStartTime();
559 }
560 else if (earliestStartTime > cb->GetStartTime())
561 {
562 earliestStartTime = cb->GetStartTime();
563 }
564
565 if (params.m_GenerateTensorData)
566 {
567 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
568 }
569
570 // Print output tensors
571 const auto& infosOut = model.GetOutputBindingInfos();
572 for (size_t i = 0; i < numOutputs; i++)
573 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100574 // We've made sure before that the number of output files either equals numOutputs, in which
575 // case we override those files when processing the results of each iteration (only the result
576 // of the last iteration will be stored), or there are enough
577 // output files for each output of each iteration.
578 size_t outputFileIndex = iteration * numOutputs + i;
579 if (!params.m_OutputTensorFiles.empty())
580 {
581 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
582 ARMNN_LOG(info) << "Writing output " << i << " named: '"
583 << inferenceModelParams.m_OutputBindings[i]
584 << "' of iteration: " << iteration+1 << " to file: '"
585 << params.m_OutputTensorFiles[outputFileIndex] << "'";
586 }
587
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100588 const armnn::TensorInfo& infoOut = infosOut[i].second;
589 auto outputTensorFile = params.m_OutputTensorFiles.empty()
590 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100591 : params.m_OutputTensorFiles[outputFileIndex];
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100592
593 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
594 infoOut,
595 outputTensorFile,
596 params.m_DequantizeOutput);
Finn Williamsf364d532021-06-09 17:07:33 +0100597 mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100598 }
599
600 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
601 << std::fixed << inferenceDuration.count() << " ms\n";
602
603 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
604 if (params.m_ThresholdTime != 0.0)
605 {
606 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
607 << std::fixed << params.m_ThresholdTime << " ms";
608 auto thresholdMinusInference =
609 params.m_ThresholdTime - duration<double, std::milli>(inferenceDuration).count();
610 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
611 << std::fixed << thresholdMinusInference << " ms" << "\n";
612
613 if (thresholdMinusInference < 0)
614 {
615 ARMNN_LOG(fatal) << "Elapsed inference time is greater than provided threshold time. \n";
616 }
617 }
618 ++j;
619 }
620 //print duration difference between overallStartTime and overallEndTime
621 auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime);
622 auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime);
623 auto totalInferenceDuration = overallEndTime - overallStartTime;
624 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
625 << std::fixed << totalInferenceDuration.count() << " ms\n";
626 }
627 catch (const armnn::Exception& e)
628 {
629 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
630 return EXIT_FAILURE;
631 }
632 }
633 // Asynchronous execution using std::launch::async
Sadik Armagana04a9d72021-04-27 10:02:10 +0100634 else
635 {
636 try
637 {
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100638 ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100639 std::vector<std::future<std::tuple<unsigned int,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100640 std::chrono::duration<double, std::milli>>>> inferenceResults;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100641 inferenceResults.reserve(params.m_Iterations);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100642
643 // Create WorkingMemHandles for each inference
644 std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100645 workingMemHandles.reserve(params.m_Iterations);
646 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100647 {
648 workingMemHandles.push_back(model.CreateWorkingMemHandle());
649 }
650
651 // Run each inference in its own thread
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100652 // start a timer
653 const auto start_time = armnn::GetTimeNow();
Jan Eilersf17fcd52021-07-26 22:20:00 +0100654 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100655 {
656 armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get();
Finn Williamsf364d532021-06-09 17:07:33 +0100657
Sadik Armagana04a9d72021-04-27 10:02:10 +0100658 inferenceResults.push_back(std::async(
659 std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() {
Finn Williamsf364d532021-06-09 17:07:33 +0100660 return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100661 }
662 ));
663 }
664
665 // Check the results
666 for (unsigned int j = 0; j < inferenceResults.size(); ++j)
667 {
668 // Get the results
669 auto inferenceResult = inferenceResults[j].get();
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100670 auto inferenceDuration = std::get<1>(inferenceResult);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100671 auto inferenceID = std::get<0>(inferenceResult);
672
673 if (params.m_GenerateTensorData)
674 {
675 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
676 }
677
678 // Print output tensors
679 const auto& infosOut = model.GetOutputBindingInfos();
680 for (size_t i = 0; i < numOutputs; i++)
681 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100682 // We've made sure before that the number of output files either equals numOutputs, in which
683 // case we override those files when processing the results of each iteration (only the result
684 // of the last iteration will be stored), or there are enough
685 // output files for each output of each iteration.
686 size_t outputFileIndex = j * numOutputs + i;
687 if (!params.m_OutputTensorFiles.empty())
688 {
689 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
690 ARMNN_LOG(info) << "Writing output " << i << " named: '"
691 << inferenceModelParams.m_OutputBindings[i]
692 << "' of iteration: " << j+1 << " to file: '"
693 << params.m_OutputTensorFiles[outputFileIndex] << "'";
694 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100695 const armnn::TensorInfo& infoOut = infosOut[i].second;
696 auto outputTensorFile = params.m_OutputTensorFiles.empty()
697 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100698 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100699
700 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
701 infoOut,
702 outputTensorFile,
703 params.m_DequantizeOutput);
704 mapbox::util::apply_visitor(printer, outputs[j][i]);
705 }
706
707 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100708 << std::fixed << inferenceDuration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100709
710 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
711 if (params.m_ThresholdTime != 0.0)
712 {
713 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
714 << std::fixed << params.m_ThresholdTime << " ms";
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100715 auto thresholdMinusInference = params.m_ThresholdTime - inferenceDuration.count();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100716 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
717 << std::fixed << thresholdMinusInference << " ms" << "\n";
718
719 if (thresholdMinusInference < 0)
720 {
721 ARMNN_LOG(fatal) << "Elapsed inference time is greater than provided threshold time. \n";
722 }
723 }
724 ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n";
725
726 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100727 // finish timer
728 const auto duration = armnn::GetTimeDuration(start_time);
729 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
730 << std::fixed << duration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100731 }
732 catch (const armnn::Exception& e)
733 {
734 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
735 return EXIT_FAILURE;
736 }
Jan Eilers45274902020-10-15 18:34:43 +0100737 }
738 }
739 catch (const armnn::Exception& e)
740 {
741 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
742 return EXIT_FAILURE;
743 }
744
745 return EXIT_SUCCESS;
746}
747
telsoa01c577f2c2018-08-31 09:22:23 +0100748
James Conroy7b4886f2019-04-11 10:23:58 +0100749// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +0100750int main(int argc, const char* argv[])
751{
752 // Configures logging for both the ARMNN library and this test program.
Jan Eilers45274902020-10-15 18:34:43 +0100753 #ifdef NDEBUG
telsoa01c577f2c2018-08-31 09:22:23 +0100754 armnn::LogSeverity level = armnn::LogSeverity::Info;
Jan Eilers45274902020-10-15 18:34:43 +0100755 #else
telsoa01c577f2c2018-08-31 09:22:23 +0100756 armnn::LogSeverity level = armnn::LogSeverity::Debug;
Jan Eilers45274902020-10-15 18:34:43 +0100757 #endif
telsoa01c577f2c2018-08-31 09:22:23 +0100758 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +0100759
telsoa01c577f2c2018-08-31 09:22:23 +0100760
Jan Eilers45274902020-10-15 18:34:43 +0100761 // Get ExecuteNetwork parameters and runtime options from command line
Jan Eilersf17fcd52021-07-26 22:20:00 +0100762 // This might throw an InvalidArgumentException if the user provided invalid inputs
763 ProgramOptions ProgramOptions;
764 try {
765 ProgramOptions.ParseOptions(argc, argv);
766 } catch (const std::exception &e){
767 ARMNN_LOG(fatal) << e.what();
768 return EXIT_FAILURE;
769 }
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000770
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100771 // Create runtime
Jan Eilers45274902020-10-15 18:34:43 +0100772 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100773
Jan Eilers45274902020-10-15 18:34:43 +0100774 std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat;
775
776 // Forward to implementation based on the parser type
777 if (modelFormat.find("armnn") != std::string::npos)
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100778 {
Jan Eilers45274902020-10-15 18:34:43 +0100779 #if defined(ARMNN_SERIALIZER)
780 return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime);
781 #else
782 ARMNN_LOG(fatal) << "Not built with serialization support.";
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100783 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100784 #endif
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100785 }
Jan Eilers45274902020-10-15 18:34:43 +0100786 else if (modelFormat.find("onnx") != std::string::npos)
telsoa01c577f2c2018-08-31 09:22:23 +0100787 {
Jan Eilers45274902020-10-15 18:34:43 +0100788 #if defined(ARMNN_ONNX_PARSER)
789 return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime);
790 #else
791 ARMNN_LOG(fatal) << "Not built with Onnx parser support.";
792 return EXIT_FAILURE;
793 #endif
794 }
Jan Eilers45274902020-10-15 18:34:43 +0100795 else if(modelFormat.find("tflite") != std::string::npos)
796 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000797 if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser)
798 {
799 #if defined(ARMNN_TF_LITE_PARSER)
800 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime);
801 #else
802 ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support.";
803 return EXIT_FAILURE;
804 #endif
805 }
806 else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
807 ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
808 ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
809 ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000810 {
811 #if defined(ARMNN_TF_LITE_DELEGATE)
812 return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, runtime);
813 #else
Finn Williamsbbbefec2020-11-25 14:32:42 +0000814 ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000815 return EXIT_FAILURE;
816 #endif
817 }
Jan Eilers45274902020-10-15 18:34:43 +0100818 }
819 else
820 {
821 ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat
Nikhil Raj5d955cf2021-04-19 16:59:48 +0100822 << "'. Please include 'tflite' or 'onnx'";
Jan Eilers45274902020-10-15 18:34:43 +0100823 return EXIT_FAILURE;
telsoa014fcda012018-03-09 14:13:49 +0000824 }
825}