blob: 66be8fd02a8ba3543771ba3701514aafb65ca39b [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>
David Monahan6bb47a72021-10-22 12:57:28 +010012#include <armnn/Utils.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010013#include <armnnUtils/Filesystem.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{
50 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
51 << 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 Donelan3cff15a2021-10-12 15:06:19 +010071int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params, const armnn::IRuntime::CreationOptions runtimeOptions,
Sadik Armagan5d03e312020-11-17 16:43:56 +000072 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
73{
74 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;
86 if (params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate)
Sadik Armagan19a1c032021-01-20 12:17:00 +000087 {
Finn Williamsf806c4d2021-02-22 15:13:12 +000088 // Create the Armnn Delegate
Colm Donelan3cff15a2021-10-12 15:06:19 +010089 // Populate a DelegateOptions from the ExecuteNetworkParams.
90 armnnDelegate::DelegateOptions delegateOptions = params.ToDelegateOptions();
91 delegateOptions.SetExternalProfilingParams(runtimeOptions.m_ProfilingOptions);
92
Finn Williamsf806c4d2021-02-22 15:13:12 +000093 std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)>
94 theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions),
95 armnnDelegate::TfLiteArmnnDelegateDelete);
96 // Register armnn_delegate to TfLiteInterpreter
97 status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate));
98 if (status == kTfLiteError)
99 {
100 ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!";
101 return EXIT_FAILURE;
102 }
Sadik Armagan19a1c032021-01-20 12:17:00 +0000103 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000104 else
105 {
106 std::cout << "Running on TfLite without ArmNN delegate\n";
107 }
108
Sadik Armagan5d03e312020-11-17 16:43:56 +0000109 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData
110 ? armnn::EmptyOptional()
111 : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]);
112
Colm Donelan3cff15a2021-10-12 15:06:19 +0100113 const size_t numInputs = params.m_InputNames.size();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000114
115 for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex)
116 {
117 int input = tfLiteInterpreter->inputs()[inputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000118 TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims;
119
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100120 unsigned int inputSize = 1;
121 if (params.m_InputTensorShapes.size() > 0)
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000122 {
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100123 inputSize = params.m_InputTensorShapes[inputIndex]->GetNumElements();
124 }
125 else
126 {
127 for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim)
128 {
129 inputSize *= inputDims->data[dim];
130 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000131 }
132
Sadik Armagan5d03e312020-11-17 16:43:56 +0000133 if (params.m_InputTypes[inputIndex].compare("float") == 0)
134 {
135 auto inputData = tfLiteInterpreter->typed_tensor<float>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000136
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000137 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000138 {
139 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
140 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
141 return EXIT_FAILURE;
142 }
143
Finn Williams56870182020-11-20 13:57:53 +0000144 std::vector<float> tensorData;
145 PopulateTensorWithDataGeneric<float>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100146 inputSize,
147 dataFile,
148 [](const std::string& s)
149 { return std::stof(s); });
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000150
Finn Williams56870182020-11-20 13:57:53 +0000151 std::copy(tensorData.begin(), tensorData.end(), inputData);
152 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000153 else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000154 {
155 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000156
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000157 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000158 {
159 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
160 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
161 return EXIT_FAILURE;
162 }
163
Finn Williams56870182020-11-20 13:57:53 +0000164 std::vector<int8_t> tensorData;
165 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100166 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000167 dataFile,
168 [](const std::string& s)
169 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
170
171 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000172 }
173 else if (params.m_InputTypes[inputIndex].compare("int") == 0)
174 {
175 auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000176
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000177 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000178 {
179 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
180 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
181 return EXIT_FAILURE;
182 }
183
Finn Williams56870182020-11-20 13:57:53 +0000184 std::vector<int32_t> tensorData;
185 PopulateTensorWithDataGeneric<int32_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100186 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000187 dataFile,
188 [](const std::string& s)
189 { return std::stoi(s); });
190
191 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000192 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100193 else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 ||
194 params.m_InputTypes[inputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000195 {
196 auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000197
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000198 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000199 {
200 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
201 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
202 return EXIT_FAILURE;
203 }
204
Finn Williams56870182020-11-20 13:57:53 +0000205 std::vector<uint8_t> tensorData;
206 PopulateTensorWithDataGeneric<uint8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100207 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000208 dataFile,
209 [](const std::string& s)
210 { return armnn::numeric_cast<uint8_t>(std::stoi(s)); });
211
212 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000213 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100214 else if (params.m_InputTypes[inputIndex].compare("qasymms8") == 0)
215 {
216 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
217
218 if(inputData == NULL)
219 {
220 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
221 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
222 return EXIT_FAILURE;
223 }
224
225 std::vector<int8_t> tensorData;
226 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100227 inputSize,
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100228 dataFile,
229 [](const std::string& s)
230 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
231
232 std::copy(tensorData.begin(), tensorData.end(), inputData);
233 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000234 else
235 {
236 ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". ";
237 return EXIT_FAILURE;
238 }
239 }
240
241 for (size_t x = 0; x < params.m_Iterations; x++)
242 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100243 // Start timer to record inference time in milliseconds.
244 const auto start_time = armnn::GetTimeNow();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000245 // Run the inference
Finn Williamsf806c4d2021-02-22 15:13:12 +0000246 status = tfLiteInterpreter->Invoke();
Colm Donelan3cff15a2021-10-12 15:06:19 +0100247 const auto duration = armnn::GetTimeDuration(start_time);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000248
249 // Print out the output
250 for (unsigned int outputIndex = 0; outputIndex < params.m_OutputNames.size(); ++outputIndex)
251 {
Sadik Armagan5d03e312020-11-17 16:43:56 +0000252 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000253 TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims;
Colm Donelan3cff15a2021-10-12 15:06:19 +0100254 // If we've been asked to write to a file then set a file output stream. Otherwise use stdout.
255 FILE* outputTensorFile = stdout;
256 if (!params.m_OutputTensorFiles.empty())
257 {
258 outputTensorFile = fopen(params.m_OutputTensorFiles[outputIndex].c_str(), "w");
259 if (outputTensorFile == NULL)
260 {
261 ARMNN_LOG(fatal) << "Specified output tensor file, \"" <<
262 params.m_OutputTensorFiles[outputIndex] <<
263 "\", cannot be created. Defaulting to stdout. " <<
264 "Error was: " << std::strerror(errno);
265 outputTensorFile = stdout;
266 }
267 else
268 {
269 ARMNN_LOG(info) << "Writing output " << outputIndex << "' of iteration: " << x+1 << " to file: '"
270 << params.m_OutputTensorFiles[outputIndex] << "'";
271 }
272 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000273 long outputSize = 1;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000274 for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim)
275 {
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000276 outputSize *= outputDims->data[dim];
Sadik Armagan5d03e312020-11-17 16:43:56 +0000277 }
278
279 std::cout << params.m_OutputNames[outputIndex] << ": ";
280 if (params.m_OutputTypes[outputIndex].compare("float") == 0)
281 {
282 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000283 if(tfLiteDelageOutputData == NULL)
284 {
285 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
286 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
287 return EXIT_FAILURE;
288 }
289
Jan Eilers284b5d12021-09-07 12:46:15 +0100290 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000291 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100292 for (int i = 0; i < outputSize; ++i)
293 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100294 fprintf(outputTensorFile, "%f ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100295 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000296 }
297 }
298 else if (params.m_OutputTypes[outputIndex].compare("int") == 0)
299 {
300 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000301 if(tfLiteDelageOutputData == NULL)
302 {
303 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
304 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
305 return EXIT_FAILURE;
306 }
307
Jan Eilers284b5d12021-09-07 12:46:15 +0100308 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000309 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100310 for (int i = 0; i < outputSize; ++i)
311 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100312 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100313 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000314 }
315 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000316 else if (params.m_OutputTypes[outputIndex].compare("qsymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000317 {
318 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId);
319 if(tfLiteDelageOutputData == NULL)
320 {
321 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
322 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
323 return EXIT_FAILURE;
324 }
325
Jan Eilers284b5d12021-09-07 12:46:15 +0100326 if (!params.m_DontPrintOutputs)
Finn Williams56870182020-11-20 13:57:53 +0000327 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100328 for (int i = 0; i < outputSize; ++i)
329 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100330 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100331 }
Finn Williams56870182020-11-20 13:57:53 +0000332 }
333 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100334 else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0 ||
335 params.m_OutputTypes[outputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000336 {
337 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000338 if(tfLiteDelageOutputData == NULL)
339 {
340 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
341 "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect.";
342 return EXIT_FAILURE;
343 }
344
Jan Eilers284b5d12021-09-07 12:46:15 +0100345 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000346 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100347 for (int i = 0; i < outputSize; ++i)
348 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100349 fprintf(outputTensorFile, "%u ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100350 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000351 }
352 }
353 else
354 {
355 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
356 "\"" << params.m_OutputTypes[outputIndex] <<
357 "\" may be incorrect. Output type can be specified with -z argument";
358 return EXIT_FAILURE;
359 }
360 std::cout << std::endl;
361 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100362 CheckInferenceTimeThreshold(duration, params.m_ThresholdTime);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000363 }
364
365 return status;
366}
367#endif
Jan Eilers45274902020-10-15 18:34:43 +0100368template<typename TParser, typename TDataType>
369int MainImpl(const ExecuteNetworkParams& params,
370 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
371{
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100372 using namespace std::chrono;
Jan Eilers45274902020-10-15 18:34:43 +0100373
David Monahan6bb47a72021-10-22 12:57:28 +0100374 std::vector<std::vector<armnn::TContainer>> inputs;
375 std::vector<std::vector<armnn::TContainer>> outputs;
Jan Eilers45274902020-10-15 18:34:43 +0100376
377 try
378 {
379 // Creates an InferenceModel, which will parse the model and load it into an IRuntime.
380 typename InferenceModel<TParser, TDataType>::Params inferenceModelParams;
381 inferenceModelParams.m_ModelPath = params.m_ModelPath;
382 inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary;
383 inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices;
384 inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath;
385 inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate;
386 inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails;
387 inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported;
388 inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape;
389 inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath;
Matthew Sloyan42432112021-01-08 10:30:51 +0000390 inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork;
391 inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath;
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000392 inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads;
Finn Williams40646322021-02-11 16:16:42 +0000393 inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100394 inferenceModelParams.m_AsyncEnabled = params.m_Concurrent;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100395 inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize;
Keith Davisf4874862021-08-09 16:49:18 +0100396 inferenceModelParams.m_OutputDetailsToStdOut = params.m_OutputDetailsToStdOut;
Keith Davis4914d0c2021-08-18 17:14:05 +0100397 inferenceModelParams.m_OutputDetailsOnlyToStdOut = params.m_OutputDetailsOnlyToStdOut;
Jan Eilers45274902020-10-15 18:34:43 +0100398
399 for(const std::string& inputName: params.m_InputNames)
400 {
401 inferenceModelParams.m_InputBindings.push_back(inputName);
402 }
403
404 for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i)
405 {
406 inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]);
407 }
408
409 for(const std::string& outputName: params.m_OutputNames)
410 {
411 inferenceModelParams.m_OutputBindings.push_back(outputName);
412 }
413
414 inferenceModelParams.m_SubgraphId = params.m_SubgraphId;
415 inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode;
416 inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode;
417
418 InferenceModel<TParser, TDataType> model(inferenceModelParams,
419 params.m_EnableProfiling,
420 params.m_DynamicBackendsPath,
421 runtime);
422
423 const size_t numInputs = inferenceModelParams.m_InputBindings.size();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100424
425 armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ?
426 armnn::MakeOptional<QuantizationParams>(
427 model.GetInputQuantizationParams()) :
428 armnn::EmptyOptional();
429
Jan Eilersf17fcd52021-07-26 22:20:00 +0100430 if (params.m_InputTensorDataFilePaths.size() > numInputs)
431 {
432 ARMNN_LOG(info) << "Given network has " << numInputs << " input/s. One input-tensor-data file is required "
433 << "for each input. The user provided "
434 << params.m_InputTensorDataFilePaths.size()
435 << " input-tensor-data file/s which will be used to fill the input/s.\n";
436 }
437
438 for(unsigned int j = 0; j < params.m_Iterations ; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100439 {
David Monahan6bb47a72021-10-22 12:57:28 +0100440 std::vector<armnn::TContainer> inputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100441 for(unsigned int i = 0; i < numInputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100442 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100443 // If there are less input files given than required for the execution of
444 // params.m_Iterations we simply start with the first input file again
445 size_t inputFileIndex = j * numInputs + i;
446 if (!params.m_InputTensorDataFilePaths.empty())
447 {
448 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
449 }
450
Sadik Armagana04a9d72021-04-27 10:02:10 +0100451 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
452 armnn::EmptyOptional() :
453 armnn::MakeOptional<std::string>(
Jan Eilersf17fcd52021-07-26 22:20:00 +0100454 params.m_InputTensorDataFilePaths.at(inputFileIndex));
Sadik Armagana04a9d72021-04-27 10:02:10 +0100455
456 unsigned int numElements = model.GetInputSize(i);
457 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
458 {
459 // If the user has provided a tensor shape for the current input,
460 // override numElements
461 numElements = params.m_InputTensorShapes[i]->GetNumElements();
462 }
463
David Monahan6bb47a72021-10-22 12:57:28 +0100464 armnn::TContainer tensorData;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100465 PopulateTensorWithData(tensorData,
466 numElements,
467 params.m_InputTypes[i],
468 qParams,
469 dataFile);
470
471 inputDataContainers.push_back(tensorData);
Jan Eilers45274902020-10-15 18:34:43 +0100472 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100473 inputs.push_back(inputDataContainers);
Jan Eilers45274902020-10-15 18:34:43 +0100474 }
475
476 const size_t numOutputs = inferenceModelParams.m_OutputBindings.size();
Jan Eilers45274902020-10-15 18:34:43 +0100477
Jan Eilersf17fcd52021-07-26 22:20:00 +0100478 for (unsigned int j = 0; j < params.m_Iterations; ++j)
Jan Eilers45274902020-10-15 18:34:43 +0100479 {
David Monahan6bb47a72021-10-22 12:57:28 +0100480 std::vector <armnn::TContainer> outputDataContainers;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100481 for (unsigned int i = 0; i < numOutputs; ++i)
Jan Eilers45274902020-10-15 18:34:43 +0100482 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100483 if (params.m_OutputTypes[i].compare("float") == 0)
Jan Eilers45274902020-10-15 18:34:43 +0100484 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100485 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100486 }
487 else if (params.m_OutputTypes[i].compare("int") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100488 {
489 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100490 }
491 else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
492 params.m_OutputTypes[i].compare("qasymmu8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100493 {
494 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100495 }
496 else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100497 {
498 outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
499 } else
500 {
501 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
502 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100503 }
504 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100505 outputs.push_back(outputDataContainers);
506 }
507
Jan Eilersf17fcd52021-07-26 22:20:00 +0100508 if (params.m_Iterations > 1)
509 {
510 std::stringstream msg;
511 msg << "Network will be executed " << params.m_Iterations;
512 if (params.m_Concurrent)
513 {
514 msg << " times in an asynchronous manner. ";
515 }
516 else
517 {
518 msg << " times successively. ";
519 }
520 msg << "The input-tensor-data files will be reused recursively if the user didn't provide enough to "
521 "cover each execution.";
522 ARMNN_LOG(info) << msg.str();
523 }
524
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100525 // Synchronous execution
Sadik Armagana04a9d72021-04-27 10:02:10 +0100526 if (!params.m_Concurrent)
527 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100528 for (size_t x = 0; x < params.m_Iterations; x++)
529 {
530 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
Jan Eilersf17fcd52021-07-26 22:20:00 +0100531 auto inference_duration = model.Run(inputs[x], outputs[x]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100532
533 if (params.m_GenerateTensorData)
534 {
535 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
536 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100537 if (params.m_DontPrintOutputs)
538 {
539 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
540 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100541
542 // Print output tensors
543 const auto& infosOut = model.GetOutputBindingInfos();
544 for (size_t i = 0; i < numOutputs; i++)
545 {
546 const armnn::TensorInfo& infoOut = infosOut[i].second;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100547
Jan Eilers284b5d12021-09-07 12:46:15 +0100548 // We've made sure before that the number of output files either equals numOutputs, in which
549 // case we override those files when processing the results of each iteration (only the result
550 // of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100551 // output files for each output of each iteration.
552 size_t outputFileIndex = x * numOutputs + i;
553 if (!params.m_OutputTensorFiles.empty())
554 {
555 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
556 ARMNN_LOG(info) << "Writing output " << i << " named: '"
557 << inferenceModelParams.m_OutputBindings[i]
558 << "' of iteration: " << x+1 << " to file: '"
559 << params.m_OutputTensorFiles[outputFileIndex] << "'";
560 }
561 auto outputTensorFile = params.m_OutputTensorFiles.empty()
562 ? ""
563 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100564
565 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
566 infoOut,
567 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100568 params.m_DequantizeOutput,
569 !params.m_DontPrintOutputs);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100570 mapbox::util::apply_visitor(printer, outputs[x][i]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100571 }
572
573 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
574 << std::fixed << inference_duration.count() << " ms\n";
575
576 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
577 if (params.m_ThresholdTime != 0.0)
578 {
579 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
580 << std::fixed << params.m_ThresholdTime << " ms";
581 auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count();
582 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
583 << std::fixed << thresholdMinusInference << " ms" << "\n";
584
585 if (thresholdMinusInference < 0)
586 {
587 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
588 ARMNN_LOG(fatal) << errorMessage;
589 }
590 }
591 }
592 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100593 // Asynchronous execution using the Arm NN thread pool
Kevin May94dd4db2021-05-26 16:01:08 +0100594 else if (params.m_ThreadPoolSize >= 1)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100595 {
596 try
597 {
598 ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100599 armnn::AsyncCallbackManager callbackManager;
David Monahan6bb47a72021-10-22 12:57:28 +0100600 std::unordered_map<armnn::InferenceId, std::vector<armnn::TContainer>&> inferenceOutputMap;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100601
602 // Declare the latest and earliest inference times here to be used when calculating overall time
603 std::chrono::high_resolution_clock::time_point earliestStartTime;
604 std::chrono::high_resolution_clock::time_point latestEndTime =
605 std::chrono::high_resolution_clock::now();
606
607 // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the
608 // LoadedNetwork with each scheduled inference having a specific priority
Jan Eilersf17fcd52021-07-26 22:20:00 +0100609 for (size_t i = 0; i < params.m_Iterations; ++i)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100610 {
Finn Williamsf364d532021-06-09 17:07:33 +0100611 std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback();
612 inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]});
613 model.RunAsync(inputs[i], outputs[i], cb);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100614 }
615
616 // Check the results
617 unsigned int j = 0;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100618 for (size_t iteration = 0; iteration < params.m_Iterations; ++iteration)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100619 {
Finn Williamsf364d532021-06-09 17:07:33 +0100620 auto cb = callbackManager.GetNotifiedCallback();
621
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100622 // Get the results
623 auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime());
624 auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime());
625 auto inferenceDuration = endTime - startTime;
626
627 if (latestEndTime < cb->GetEndTime())
628 {
629 latestEndTime = cb->GetEndTime();
630 }
631
632 if (earliestStartTime.time_since_epoch().count() == 0)
633 {
634 earliestStartTime = cb->GetStartTime();
635 }
636 else if (earliestStartTime > cb->GetStartTime())
637 {
638 earliestStartTime = cb->GetStartTime();
639 }
640
641 if (params.m_GenerateTensorData)
642 {
643 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
644 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100645 if (params.m_DontPrintOutputs)
646 {
647 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
648 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100649
650 // Print output tensors
651 const auto& infosOut = model.GetOutputBindingInfos();
652 for (size_t i = 0; i < numOutputs; i++)
653 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100654 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100655 // case we override those files when processing the results of each iteration (only the
656 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100657 // output files for each output of each iteration.
658 size_t outputFileIndex = iteration * numOutputs + i;
659 if (!params.m_OutputTensorFiles.empty())
660 {
661 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
662 ARMNN_LOG(info) << "Writing output " << i << " named: '"
663 << inferenceModelParams.m_OutputBindings[i]
664 << "' of iteration: " << iteration+1 << " to file: '"
665 << params.m_OutputTensorFiles[outputFileIndex] << "'";
666 }
667
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100668 const armnn::TensorInfo& infoOut = infosOut[i].second;
669 auto outputTensorFile = params.m_OutputTensorFiles.empty()
670 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100671 : params.m_OutputTensorFiles[outputFileIndex];
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100672
673 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
674 infoOut,
675 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100676 params.m_DequantizeOutput,
677 !params.m_DontPrintOutputs);
Finn Williamsf364d532021-06-09 17:07:33 +0100678 mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100679 }
680
Colm Donelan3cff15a2021-10-12 15:06:19 +0100681 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100682 ++j;
683 }
684 //print duration difference between overallStartTime and overallEndTime
685 auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime);
686 auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime);
687 auto totalInferenceDuration = overallEndTime - overallStartTime;
688 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
689 << std::fixed << totalInferenceDuration.count() << " ms\n";
690 }
691 catch (const armnn::Exception& e)
692 {
693 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
694 return EXIT_FAILURE;
695 }
696 }
697 // Asynchronous execution using std::launch::async
Sadik Armagana04a9d72021-04-27 10:02:10 +0100698 else
699 {
700 try
701 {
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100702 ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100703 std::vector<std::future<std::tuple<unsigned int,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100704 std::chrono::duration<double, std::milli>>>> inferenceResults;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100705 inferenceResults.reserve(params.m_Iterations);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100706
707 // Create WorkingMemHandles for each inference
708 std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100709 workingMemHandles.reserve(params.m_Iterations);
710 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100711 {
712 workingMemHandles.push_back(model.CreateWorkingMemHandle());
713 }
714
715 // Run each inference in its own thread
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100716 // start a timer
717 const auto start_time = armnn::GetTimeNow();
Jan Eilersf17fcd52021-07-26 22:20:00 +0100718 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100719 {
720 armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get();
Finn Williamsf364d532021-06-09 17:07:33 +0100721
Sadik Armagana04a9d72021-04-27 10:02:10 +0100722 inferenceResults.push_back(std::async(
723 std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() {
Finn Williamsf364d532021-06-09 17:07:33 +0100724 return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100725 }
726 ));
727 }
728
729 // Check the results
730 for (unsigned int j = 0; j < inferenceResults.size(); ++j)
731 {
732 // Get the results
733 auto inferenceResult = inferenceResults[j].get();
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100734 auto inferenceDuration = std::get<1>(inferenceResult);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100735 auto inferenceID = std::get<0>(inferenceResult);
736
737 if (params.m_GenerateTensorData)
738 {
739 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
740 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100741 if (params.m_DontPrintOutputs)
742 {
743 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
744 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100745
746 // Print output tensors
747 const auto& infosOut = model.GetOutputBindingInfos();
748 for (size_t i = 0; i < numOutputs; i++)
749 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100750 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100751 // case we override those files when processing the results of each iteration (only the
752 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100753 // output files for each output of each iteration.
754 size_t outputFileIndex = j * numOutputs + i;
755 if (!params.m_OutputTensorFiles.empty())
756 {
757 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
758 ARMNN_LOG(info) << "Writing output " << i << " named: '"
759 << inferenceModelParams.m_OutputBindings[i]
760 << "' of iteration: " << j+1 << " to file: '"
761 << params.m_OutputTensorFiles[outputFileIndex] << "'";
762 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100763 const armnn::TensorInfo& infoOut = infosOut[i].second;
764 auto outputTensorFile = params.m_OutputTensorFiles.empty()
765 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100766 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100767
768 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
769 infoOut,
770 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100771 params.m_DequantizeOutput,
772 !params.m_DontPrintOutputs);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100773 mapbox::util::apply_visitor(printer, outputs[j][i]);
774 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100775 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100776 ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100777 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100778 // finish timer
779 const auto duration = armnn::GetTimeDuration(start_time);
780 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
781 << std::fixed << duration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100782 }
783 catch (const armnn::Exception& e)
784 {
785 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
786 return EXIT_FAILURE;
787 }
Jan Eilers45274902020-10-15 18:34:43 +0100788 }
789 }
790 catch (const armnn::Exception& e)
791 {
792 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
793 return EXIT_FAILURE;
794 }
795
796 return EXIT_SUCCESS;
797}
798
James Conroy7b4886f2019-04-11 10:23:58 +0100799// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +0100800int main(int argc, const char* argv[])
801{
802 // Configures logging for both the ARMNN library and this test program.
Jan Eilers45274902020-10-15 18:34:43 +0100803 #ifdef NDEBUG
telsoa01c577f2c2018-08-31 09:22:23 +0100804 armnn::LogSeverity level = armnn::LogSeverity::Info;
Jan Eilers45274902020-10-15 18:34:43 +0100805 #else
telsoa01c577f2c2018-08-31 09:22:23 +0100806 armnn::LogSeverity level = armnn::LogSeverity::Debug;
Jan Eilers45274902020-10-15 18:34:43 +0100807 #endif
telsoa01c577f2c2018-08-31 09:22:23 +0100808 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +0100809
telsoa01c577f2c2018-08-31 09:22:23 +0100810
Jan Eilers45274902020-10-15 18:34:43 +0100811 // Get ExecuteNetwork parameters and runtime options from command line
Jan Eilersf17fcd52021-07-26 22:20:00 +0100812 // This might throw an InvalidArgumentException if the user provided invalid inputs
813 ProgramOptions ProgramOptions;
814 try {
815 ProgramOptions.ParseOptions(argc, argv);
816 } catch (const std::exception &e){
817 ARMNN_LOG(fatal) << e.what();
818 return EXIT_FAILURE;
819 }
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +0000820
Keith Davis4914d0c2021-08-18 17:14:05 +0100821 if ((ProgramOptions.m_ExNetParams.m_OutputDetailsToStdOut ||
822 ProgramOptions.m_ExNetParams.m_OutputDetailsOnlyToStdOut)
823 && !ProgramOptions.m_ExNetParams.m_EnableProfiling)
Keith Davisf4874862021-08-09 16:49:18 +0100824 {
825 ARMNN_LOG(fatal) << "You must enable profiling if you would like to output layer details";
826 return EXIT_FAILURE;
827 }
828
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100829 // Create runtime
Jan Eilers45274902020-10-15 18:34:43 +0100830 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100831
Jan Eilers45274902020-10-15 18:34:43 +0100832 std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat;
833
834 // Forward to implementation based on the parser type
835 if (modelFormat.find("armnn") != std::string::npos)
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100836 {
Jan Eilers45274902020-10-15 18:34:43 +0100837 #if defined(ARMNN_SERIALIZER)
838 return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime);
839 #else
840 ARMNN_LOG(fatal) << "Not built with serialization support.";
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100841 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +0100842 #endif
Finn Williamsd7fcafa2020-04-23 17:55:18 +0100843 }
Jan Eilers45274902020-10-15 18:34:43 +0100844 else if (modelFormat.find("onnx") != std::string::npos)
telsoa01c577f2c2018-08-31 09:22:23 +0100845 {
Jan Eilers45274902020-10-15 18:34:43 +0100846 #if defined(ARMNN_ONNX_PARSER)
847 return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime);
848 #else
849 ARMNN_LOG(fatal) << "Not built with Onnx parser support.";
850 return EXIT_FAILURE;
851 #endif
852 }
Jan Eilers45274902020-10-15 18:34:43 +0100853 else if(modelFormat.find("tflite") != std::string::npos)
854 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000855 if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser)
856 {
857 #if defined(ARMNN_TF_LITE_PARSER)
858 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime);
859 #else
860 ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support.";
861 return EXIT_FAILURE;
862 #endif
863 }
864 else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
865 ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
866 ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
867 ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000868 {
869 #if defined(ARMNN_TF_LITE_DELEGATE)
Colm Donelan3cff15a2021-10-12 15:06:19 +0100870 return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, ProgramOptions.m_RuntimeOptions, runtime);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000871 #else
Finn Williamsbbbefec2020-11-25 14:32:42 +0000872 ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000873 return EXIT_FAILURE;
874 #endif
875 }
Jan Eilers45274902020-10-15 18:34:43 +0100876 }
877 else
878 {
879 ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat
Nikhil Raj5d955cf2021-04-19 16:59:48 +0100880 << "'. Please include 'tflite' or 'onnx'";
Jan Eilers45274902020-10-15 18:34:43 +0100881 return EXIT_FAILURE;
telsoa014fcda012018-03-09 14:13:49 +0000882 }
883}