blob: f0a3d0821eab28f945cf883e7a550c7a8314a6e1 [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>
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000014#include <ProfilingOptionsConverter.hpp>
Jan Eilers45274902020-10-15 18:34:43 +010015#include <InferenceTest.hpp>
16
17#if defined(ARMNN_SERIALIZER)
18#include "armnnDeserializer/IDeserializer.hpp"
19#endif
Jan Eilers45274902020-10-15 18:34:43 +010020#if defined(ARMNN_TF_LITE_PARSER)
21#include "armnnTfLiteParser/ITfLiteParser.hpp"
22#endif
23#if defined(ARMNN_ONNX_PARSER)
24#include "armnnOnnxParser/IOnnxParser.hpp"
25#endif
Sadik Armagan5d03e312020-11-17 16:43:56 +000026#if defined(ARMNN_TFLITE_DELEGATE)
27#include <armnn_delegate.hpp>
28#include <DelegateOptions.hpp>
29
30#include <tensorflow/lite/builtin_ops.h>
31#include <tensorflow/lite/c/builtin_op_data.h>
32#include <tensorflow/lite/c/common.h>
33#include <tensorflow/lite/optional_debug_tools.h>
34#include <tensorflow/lite/kernels/builtin_op_kernels.h>
35#include <tensorflow/lite/interpreter.h>
36#include <tensorflow/lite/kernels/register.h>
37#endif
Jan Eilers45274902020-10-15 18:34:43 +010038
39#include <future>
Colm Donelan3cff15a2021-10-12 15:06:19 +010040
41/**
42 * Given a measured duration and a threshold time tell the user whether we succeeded or not.
43 *
44 * @param duration the measured inference duration.
45 * @param thresholdTime the threshold time in milliseconds.
46 * @return false if the measured time exceeded the threshold.
47 */
48bool CheckInferenceTimeThreshold(const std::chrono::duration<double, std::milli>& duration,
49 const double& thresholdTime)
50{
Jan Eilers17d34da2021-12-08 16:15:12 +000051 ARMNN_LOG(info) << "Inference time: " << std::setprecision(2)
Colm Donelan3cff15a2021-10-12 15:06:19 +010052 << std::fixed << duration.count() << " ms\n";
53 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
54 if (thresholdTime != 0.0)
55 {
56 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
57 << std::fixed << thresholdTime << " ms";
58 auto thresholdMinusInference = thresholdTime - duration.count();
59 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
60 << std::fixed << thresholdMinusInference << " ms" << "\n";
61 if (thresholdMinusInference < 0)
62 {
63 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
64 ARMNN_LOG(fatal) << errorMessage;
65 return false;
66 }
67 }
68 return true;
69}
70
Sadik Armagan5d03e312020-11-17 16:43:56 +000071#if defined(ARMNN_TFLITE_DELEGATE)
Colm Donelan45142282021-10-21 23:39:52 +010072int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params, const armnn::IRuntime::CreationOptions runtimeOptions)
Sadik Armagan5d03e312020-11-17 16:43:56 +000073{
Tamas Nyiri00564232021-11-28 21:31:33 +000074 // Build model and corresponding interpreter
Sadik Armagan5d03e312020-11-17 16:43:56 +000075 using namespace tflite;
Jan Eilers45274902020-10-15 18:34:43 +010076
Sadik Armagan5d03e312020-11-17 16:43:56 +000077 std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(params.m_ModelPath.c_str());
78
79 auto tfLiteInterpreter = std::make_unique<Interpreter>();
80 tflite::ops::builtin::BuiltinOpResolver resolver;
81
82 tflite::InterpreterBuilder builder(*model, resolver);
83 builder(&tfLiteInterpreter);
84 tfLiteInterpreter->AllocateTensors();
85
Finn Williamsf806c4d2021-02-22 15:13:12 +000086 int status = 0;
Tamas Nyiri00564232021-11-28 21:31:33 +000087
88 // Create & populate Armnn Delegate, then register it to TfLiteInterpreter
Finn Williamsf806c4d2021-02-22 15:13:12 +000089 if (params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate)
Sadik Armagan19a1c032021-01-20 12:17:00 +000090 {
Finn Williamsf806c4d2021-02-22 15:13:12 +000091 // Create the Armnn Delegate
Colm Donelan3cff15a2021-10-12 15:06:19 +010092 // Populate a DelegateOptions from the ExecuteNetworkParams.
93 armnnDelegate::DelegateOptions delegateOptions = params.ToDelegateOptions();
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000094 delegateOptions.SetExternalProfilingParams(
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000095 arm::pipe::ConvertExternalProfilingOptions(runtimeOptions.m_ProfilingOptions));
Colm Donelan3cff15a2021-10-12 15:06:19 +010096
Finn Williamsf806c4d2021-02-22 15:13:12 +000097 std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)>
98 theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions),
99 armnnDelegate::TfLiteArmnnDelegateDelete);
100 // Register armnn_delegate to TfLiteInterpreter
101 status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate));
Ryan OSheaab8a4462022-02-03 10:45:51 +0000102 if (status != kTfLiteOk)
Finn Williamsf806c4d2021-02-22 15:13:12 +0000103 {
104 ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!";
105 return EXIT_FAILURE;
106 }
Sadik Armagan19a1c032021-01-20 12:17:00 +0000107 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000108 else
109 {
110 std::cout << "Running on TfLite without ArmNN delegate\n";
111 }
112
Tamas Nyiri00564232021-11-28 21:31:33 +0000113 // Load (or generate) input data for inference
Sadik Armagan5d03e312020-11-17 16:43:56 +0000114 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData
115 ? armnn::EmptyOptional()
116 : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]);
117
Colm Donelan3cff15a2021-10-12 15:06:19 +0100118 const size_t numInputs = params.m_InputNames.size();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000119
Tamas Nyiri00564232021-11-28 21:31:33 +0000120 // Populate input tensor of interpreter
Sadik Armagan5d03e312020-11-17 16:43:56 +0000121 for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex)
122 {
123 int input = tfLiteInterpreter->inputs()[inputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000124 TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims;
125
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100126 unsigned int inputSize = 1;
127 if (params.m_InputTensorShapes.size() > 0)
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000128 {
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100129 inputSize = params.m_InputTensorShapes[inputIndex]->GetNumElements();
130 }
131 else
132 {
133 for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim)
134 {
135 inputSize *= inputDims->data[dim];
136 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000137 }
138
Sadik Armagan5d03e312020-11-17 16:43:56 +0000139 if (params.m_InputTypes[inputIndex].compare("float") == 0)
140 {
141 auto inputData = tfLiteInterpreter->typed_tensor<float>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000142
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000143 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000144 {
145 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
146 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
147 return EXIT_FAILURE;
148 }
149
Finn Williams56870182020-11-20 13:57:53 +0000150 std::vector<float> tensorData;
151 PopulateTensorWithDataGeneric<float>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100152 inputSize,
153 dataFile,
154 [](const std::string& s)
155 { return std::stof(s); });
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000156
Finn Williams56870182020-11-20 13:57:53 +0000157 std::copy(tensorData.begin(), tensorData.end(), inputData);
158 }
Finn Williams35e7c1d2022-01-21 19:33:46 +0000159 else if (params.m_InputTypes[inputIndex].compare("qsymms8") == 0 ||
160 params.m_InputTypes[inputIndex].compare("qasymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000161 {
162 auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000163
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000164 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000165 {
166 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
167 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
168 return EXIT_FAILURE;
169 }
170
Finn Williams56870182020-11-20 13:57:53 +0000171 std::vector<int8_t> tensorData;
172 PopulateTensorWithDataGeneric<int8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100173 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000174 dataFile,
175 [](const std::string& s)
176 { return armnn::numeric_cast<int8_t>(std::stoi(s)); });
177
178 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000179 }
180 else if (params.m_InputTypes[inputIndex].compare("int") == 0)
181 {
182 auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000183
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000184 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000185 {
186 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
187 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
188 return EXIT_FAILURE;
189 }
190
Finn Williams56870182020-11-20 13:57:53 +0000191 std::vector<int32_t> tensorData;
192 PopulateTensorWithDataGeneric<int32_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100193 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000194 dataFile,
195 [](const std::string& s)
196 { return std::stoi(s); });
197
198 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000199 }
Mike Kellyd7ed6d42021-07-21 09:42:43 +0100200 else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0 ||
201 params.m_InputTypes[inputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000202 {
203 auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input);
Finn Williamsbbbefec2020-11-25 14:32:42 +0000204
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000205 if(inputData == NULL)
Finn Williamsbbbefec2020-11-25 14:32:42 +0000206 {
207 ARMNN_LOG(fatal) << "Input tensor is null, input type: "
208 "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect.";
209 return EXIT_FAILURE;
210 }
211
Finn Williams56870182020-11-20 13:57:53 +0000212 std::vector<uint8_t> tensorData;
213 PopulateTensorWithDataGeneric<uint8_t>(tensorData,
Mike Kelly00e9ebf2021-09-01 17:09:12 +0100214 inputSize,
Finn Williams56870182020-11-20 13:57:53 +0000215 dataFile,
216 [](const std::string& s)
217 { return armnn::numeric_cast<uint8_t>(std::stoi(s)); });
218
219 std::copy(tensorData.begin(), tensorData.end(), inputData);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000220 }
221 else
222 {
223 ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". ";
224 return EXIT_FAILURE;
225 }
226 }
227
Tamas Nyiri00564232021-11-28 21:31:33 +0000228 // Run inference, print the output of the inference
Sadik Armagan5d03e312020-11-17 16:43:56 +0000229 for (size_t x = 0; x < params.m_Iterations; x++)
230 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100231 // Start timer to record inference time in milliseconds.
232 const auto start_time = armnn::GetTimeNow();
Sadik Armagan5d03e312020-11-17 16:43:56 +0000233 // Run the inference
Finn Williamsf806c4d2021-02-22 15:13:12 +0000234 status = tfLiteInterpreter->Invoke();
Colm Donelan3cff15a2021-10-12 15:06:19 +0100235 const auto duration = armnn::GetTimeDuration(start_time);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000236
Tamas Nyiri00564232021-11-28 21:31:33 +0000237 // The TFLite interpreter's outputs might be in a different order than the user inputted output names.
238 std::map<unsigned int, int> paramToTfliteOutputIndex;
239 for (unsigned int paramIndex = 0; paramIndex < params.m_OutputNames.size(); ++paramIndex)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000240 {
Tamas Nyiri00564232021-11-28 21:31:33 +0000241 paramToTfliteOutputIndex[paramIndex] = -1;
242 for (unsigned int tfLiteIndex = 0; tfLiteIndex < tfLiteInterpreter->outputs().size(); ++tfLiteIndex)
243 {
244 if (params.m_OutputNames[paramIndex] == tfLiteInterpreter->GetOutputName(tfLiteIndex))
245 {
246 paramToTfliteOutputIndex[paramIndex] = tfLiteIndex;
247 }
248 }
249 }
250
251 // Print out the output
252 for (unsigned int paramOutputIndex = 0; paramOutputIndex < params.m_OutputNames.size(); ++paramOutputIndex)
253 {
254 int outputIndex = paramToTfliteOutputIndex[paramOutputIndex];
255 if (outputIndex == -1)
256 {
257 std::cout << fmt::format("Output name: {} doesn't exist.", params.m_OutputNames[paramOutputIndex]) <<
258 std::endl;
259 continue;
260 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000261 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex];
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000262 TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims;
Colm Donelan3cff15a2021-10-12 15:06:19 +0100263 // If we've been asked to write to a file then set a file output stream. Otherwise use stdout.
264 FILE* outputTensorFile = stdout;
265 if (!params.m_OutputTensorFiles.empty())
266 {
267 outputTensorFile = fopen(params.m_OutputTensorFiles[outputIndex].c_str(), "w");
268 if (outputTensorFile == NULL)
269 {
270 ARMNN_LOG(fatal) << "Specified output tensor file, \"" <<
271 params.m_OutputTensorFiles[outputIndex] <<
272 "\", cannot be created. Defaulting to stdout. " <<
273 "Error was: " << std::strerror(errno);
274 outputTensorFile = stdout;
275 }
276 else
277 {
278 ARMNN_LOG(info) << "Writing output " << outputIndex << "' of iteration: " << x+1 << " to file: '"
279 << params.m_OutputTensorFiles[outputIndex] << "'";
280 }
281 }
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000282 long outputSize = 1;
Sadik Armagan5d03e312020-11-17 16:43:56 +0000283 for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim)
284 {
Sadik Armagan15f7fae2020-11-18 09:37:03 +0000285 outputSize *= outputDims->data[dim];
Sadik Armagan5d03e312020-11-17 16:43:56 +0000286 }
287
Tamas Nyiri00564232021-11-28 21:31:33 +0000288 std::cout << tfLiteInterpreter->GetOutputName(outputIndex) << ": ";
Ryan OSheaab8a4462022-02-03 10:45:51 +0000289 if (params.m_OutputTypes[paramOutputIndex].compare("float") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000290 {
291 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000292 if(tfLiteDelageOutputData == NULL)
293 {
294 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
Ryan OSheaab8a4462022-02-03 10:45:51 +0000295 "\"" << params.m_OutputTypes[paramOutputIndex] << "\" may be incorrect.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000296 return EXIT_FAILURE;
297 }
298
Jan Eilers284b5d12021-09-07 12:46:15 +0100299 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000300 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100301 for (int i = 0; i < outputSize; ++i)
302 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100303 fprintf(outputTensorFile, "%f ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100304 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000305 }
306 }
Ryan OSheaab8a4462022-02-03 10:45:51 +0000307 else if (params.m_OutputTypes[paramOutputIndex].compare("int") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000308 {
309 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000310 if(tfLiteDelageOutputData == NULL)
311 {
312 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
Ryan OSheaab8a4462022-02-03 10:45:51 +0000313 "\"" << params.m_OutputTypes[paramOutputIndex] << "\" may be incorrect.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000314 return EXIT_FAILURE;
315 }
316
Jan Eilers284b5d12021-09-07 12:46:15 +0100317 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000318 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100319 for (int i = 0; i < outputSize; ++i)
320 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100321 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100322 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000323 }
324 }
Ryan OSheaab8a4462022-02-03 10:45:51 +0000325 else if (params.m_OutputTypes[paramOutputIndex].compare("qsymms8") == 0 ||
326 params.m_OutputTypes[paramOutputIndex].compare("qasymms8") == 0)
Finn Williams56870182020-11-20 13:57:53 +0000327 {
328 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId);
329 if(tfLiteDelageOutputData == NULL)
330 {
331 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
Ryan OSheaab8a4462022-02-03 10:45:51 +0000332 "\"" << params.m_OutputTypes[paramOutputIndex] << "\" may be incorrect.";
Finn Williams56870182020-11-20 13:57:53 +0000333 return EXIT_FAILURE;
334 }
335
Jan Eilers284b5d12021-09-07 12:46:15 +0100336 if (!params.m_DontPrintOutputs)
Finn Williams56870182020-11-20 13:57:53 +0000337 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100338 for (int i = 0; i < outputSize; ++i)
339 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100340 fprintf(outputTensorFile, "%d ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100341 }
Finn Williams56870182020-11-20 13:57:53 +0000342 }
343 }
Ryan OSheaab8a4462022-02-03 10:45:51 +0000344 else if (params.m_OutputTypes[paramOutputIndex].compare("qasymm8") == 0 ||
345 params.m_OutputTypes[paramOutputIndex].compare("qasymmu8") == 0)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000346 {
347 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000348 if(tfLiteDelageOutputData == NULL)
349 {
350 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
Ryan OSheaab8a4462022-02-03 10:45:51 +0000351 "\"" << params.m_OutputTypes[paramOutputIndex] << "\" may be incorrect.";
Sadik Armagan5d03e312020-11-17 16:43:56 +0000352 return EXIT_FAILURE;
353 }
354
Jan Eilers284b5d12021-09-07 12:46:15 +0100355 if (!params.m_DontPrintOutputs)
Sadik Armagan5d03e312020-11-17 16:43:56 +0000356 {
Jan Eilers284b5d12021-09-07 12:46:15 +0100357 for (int i = 0; i < outputSize; ++i)
358 {
Colm Donelan3cff15a2021-10-12 15:06:19 +0100359 fprintf(outputTensorFile, "%u ", tfLiteDelageOutputData[i]);
Jan Eilers284b5d12021-09-07 12:46:15 +0100360 }
Sadik Armagan5d03e312020-11-17 16:43:56 +0000361 }
362 }
363 else
364 {
365 ARMNN_LOG(fatal) << "Output tensor is null, output type: "
Ryan OSheaab8a4462022-02-03 10:45:51 +0000366 "\"" << params.m_OutputTypes[paramOutputIndex] <<
Sadik Armagan5d03e312020-11-17 16:43:56 +0000367 "\" may be incorrect. Output type can be specified with -z argument";
368 return EXIT_FAILURE;
369 }
370 std::cout << std::endl;
371 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100372 CheckInferenceTimeThreshold(duration, params.m_ThresholdTime);
Sadik Armagan5d03e312020-11-17 16:43:56 +0000373 }
374
375 return status;
376}
377#endif
Jan Eilers45274902020-10-15 18:34:43 +0100378template<typename TParser, typename TDataType>
379int MainImpl(const ExecuteNetworkParams& params,
380 const std::shared_ptr<armnn::IRuntime>& runtime = nullptr)
381{
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100382 using namespace std::chrono;
Jan Eilers45274902020-10-15 18:34:43 +0100383
Francis Murtagh40d27412021-10-28 11:11:35 +0100384 std::vector<std::vector<armnnUtils::TContainer>> inputs;
385 std::vector<std::vector<armnnUtils::TContainer>> outputs;
Jan Eilers45274902020-10-15 18:34:43 +0100386
387 try
388 {
389 // Creates an InferenceModel, which will parse the model and load it into an IRuntime.
390 typename InferenceModel<TParser, TDataType>::Params inferenceModelParams;
391 inferenceModelParams.m_ModelPath = params.m_ModelPath;
Mike Kelly80512b02022-05-16 23:10:42 +0100392 inferenceModelParams.m_AllowExpandedDims = params.m_AllowExpandedDims;
Jan Eilers45274902020-10-15 18:34:43 +0100393 inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary;
394 inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices;
395 inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath;
396 inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate;
397 inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails;
398 inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported;
399 inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape;
400 inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath;
Matthew Sloyan42432112021-01-08 10:30:51 +0000401 inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork;
402 inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath;
Matthew Sloyan0a7dc6b2021-02-10 16:50:53 +0000403 inferenceModelParams.m_NumberOfThreads = params.m_NumberOfThreads;
Finn Williams40646322021-02-11 16:16:42 +0000404 inferenceModelParams.m_MLGOTuningFilePath = params.m_MLGOTuningFilePath;
Sadik Armagana04a9d72021-04-27 10:02:10 +0100405 inferenceModelParams.m_AsyncEnabled = params.m_Concurrent;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100406 inferenceModelParams.m_ThreadPoolSize = params.m_ThreadPoolSize;
Keith Davisf4874862021-08-09 16:49:18 +0100407 inferenceModelParams.m_OutputDetailsToStdOut = params.m_OutputDetailsToStdOut;
Keith Davis4914d0c2021-08-18 17:14:05 +0100408 inferenceModelParams.m_OutputDetailsOnlyToStdOut = params.m_OutputDetailsOnlyToStdOut;
Jim Flynn15425812022-02-15 16:53:13 +0000409 inferenceModelParams.m_ImportInputsIfAligned = params.m_ImportInputsIfAligned;
Jan Eilers45274902020-10-15 18:34:43 +0100410
411 for(const std::string& inputName: params.m_InputNames)
412 {
413 inferenceModelParams.m_InputBindings.push_back(inputName);
414 }
415
416 for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i)
417 {
418 inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]);
419 }
420
421 for(const std::string& outputName: params.m_OutputNames)
422 {
423 inferenceModelParams.m_OutputBindings.push_back(outputName);
424 }
425
426 inferenceModelParams.m_SubgraphId = params.m_SubgraphId;
427 inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode;
428 inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode;
429
430 InferenceModel<TParser, TDataType> model(inferenceModelParams,
431 params.m_EnableProfiling,
432 params.m_DynamicBackendsPath,
433 runtime);
434
435 const size_t numInputs = inferenceModelParams.m_InputBindings.size();
Sadik Armagana04a9d72021-04-27 10:02:10 +0100436
437 armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ?
438 armnn::MakeOptional<QuantizationParams>(
439 model.GetInputQuantizationParams()) :
440 armnn::EmptyOptional();
441
Jan Eilersf17fcd52021-07-26 22:20:00 +0100442 if (params.m_InputTensorDataFilePaths.size() > numInputs)
443 {
444 ARMNN_LOG(info) << "Given network has " << numInputs << " input/s. One input-tensor-data file is required "
445 << "for each input. The user provided "
446 << params.m_InputTensorDataFilePaths.size()
447 << " input-tensor-data file/s which will be used to fill the input/s.\n";
448 }
449
Jan Eilers45274902020-10-15 18:34:43 +0100450 const size_t numOutputs = inferenceModelParams.m_OutputBindings.size();
Jan Eilers45274902020-10-15 18:34:43 +0100451
Colm Donelanc5e41982021-10-28 20:19:43 +0100452 // The user is allowed to specify the data type of each output tensor. It is used here to construct the
453 // result tensors for each iteration. It is possible for the user to specify a type that does not match
454 // the data type of the corresponding model output. It may not make sense, but it is historically allowed.
455 // The potential problem here is a buffer overrun when a larger data type is written into the space for a
456 // smaller one. Issue a warning to highlight the potential problem.
457 for (unsigned int outputIdx = 0; outputIdx < model.GetOutputBindingInfos().size(); ++outputIdx)
458 {
459 armnn::DataType type = model.GetOutputBindingInfo(outputIdx).second.GetDataType();
460 switch (type)
461 {
David Monahan67cc5fc2021-11-03 12:56:41 +0000462 // --output-type only supports float, int, qasymms8 or qasymmu8.
Colm Donelanc5e41982021-10-28 20:19:43 +0100463 case armnn::DataType::Float32:
464 if (params.m_OutputTypes[outputIdx].compare("float") != 0)
465 {
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100466 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Float32. The "
467 << "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
Colm Donelanc5e41982021-10-28 20:19:43 +0100468 ". This may cause unexpected problems or random failures.";
469 }
470 break;
471 case armnn::DataType::QAsymmU8:
472 if (params.m_OutputTypes[outputIdx].compare("qasymmu8") != 0)
473 {
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100474 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmU8. The "
475 << "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
476 ". This may cause unexpected problems or random failures.";
Colm Donelanc5e41982021-10-28 20:19:43 +0100477 }
478 break;
479 case armnn::DataType::Signed32:
480 if (params.m_OutputTypes[outputIdx].compare("int") != 0)
481 {
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100482 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Signed32. The "
483 << "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
Colm Donelanc5e41982021-10-28 20:19:43 +0100484 ". This may cause unexpected problems or random failures.";
485 }
486 break;
487 case armnn::DataType::QAsymmS8:
488 if (params.m_OutputTypes[outputIdx].compare("qasymms8") != 0)
489 {
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100490 ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type QAsymmS8. The "
491 << "corresponding --output-type is " << params.m_OutputTypes[outputIdx] <<
Colm Donelanc5e41982021-10-28 20:19:43 +0100492 ". This may cause unexpected problems or random failures.";
493 }
494 break;
495 default:
496 break;
497 }
498 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100499
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100500 if (!params.m_ReuseBuffers)
501 {
502 for (unsigned int j = 0; j < params.m_Iterations; ++j)
503 {
504 std::vector<armnnUtils::TContainer> inputDataContainers;
505 for (unsigned int i = 0; i < numInputs; ++i)
506 {
507 // If there are fewer input files given than required for the execution of
508 // params.m_Iterations we simply start with the first input file again
509 size_t inputFileIndex = j * numInputs + i;
510 if (!params.m_InputTensorDataFilePaths.empty())
511 {
512 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
513 }
514
515 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
516 armnn::EmptyOptional() :
517 armnn::MakeOptional<std::string>(
518 params.m_InputTensorDataFilePaths.at(
519 inputFileIndex));
520
521 unsigned int numElements = model.GetInputSize(i);
522 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
523 {
524 // If the user has provided a tensor shape for the current input,
525 // override numElements
526 numElements = params.m_InputTensorShapes[i]->GetNumElements();
527 }
528
529 armnnUtils::TContainer tensorData;
530 PopulateTensorWithData(tensorData,
531 numElements,
532 params.m_InputTypes[i],
533 qParams,
534 dataFile);
535
536 inputDataContainers.push_back(tensorData);
537 }
538 inputs.push_back(inputDataContainers);
539 }
540
541 for (unsigned int j = 0; j < params.m_Iterations; ++j)
542 {
543 std::vector<armnnUtils::TContainer> outputDataContainers;
544 for (unsigned int i = 0; i < numOutputs; ++i)
545 {
546 if (params.m_OutputTypes[i].compare("float") == 0)
547 {
548 outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i)));
549 }
550 else if (params.m_OutputTypes[i].compare("int") == 0)
551 {
552 outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i)));
553 }
554 else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
555 params.m_OutputTypes[i].compare("qasymmu8") == 0)
556 {
557 outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
558 }
559 else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
560 {
561 outputDataContainers.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
562 }
563 else
564 {
565 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
566 return EXIT_FAILURE;
567 }
568 }
569 outputs.push_back(outputDataContainers);
570 }
571 }
Jan Eilersf17fcd52021-07-26 22:20:00 +0100572 if (params.m_Iterations > 1)
573 {
574 std::stringstream msg;
575 msg << "Network will be executed " << params.m_Iterations;
576 if (params.m_Concurrent)
577 {
578 msg << " times in an asynchronous manner. ";
579 }
580 else
581 {
582 msg << " times successively. ";
583 }
584 msg << "The input-tensor-data files will be reused recursively if the user didn't provide enough to "
585 "cover each execution.";
586 ARMNN_LOG(info) << msg.str();
587 }
588
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100589 // Synchronous execution
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100590 if (!params.m_Concurrent && !params.m_ReuseBuffers)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100591 {
Sadik Armagana04a9d72021-04-27 10:02:10 +0100592 for (size_t x = 0; x < params.m_Iterations; x++)
593 {
594 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
Jan Eilersf17fcd52021-07-26 22:20:00 +0100595 auto inference_duration = model.Run(inputs[x], outputs[x]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100596
597 if (params.m_GenerateTensorData)
598 {
599 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
600 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100601 if (params.m_DontPrintOutputs)
602 {
603 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
604 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100605
606 // Print output tensors
607 const auto& infosOut = model.GetOutputBindingInfos();
608 for (size_t i = 0; i < numOutputs; i++)
609 {
610 const armnn::TensorInfo& infoOut = infosOut[i].second;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100611
Jan Eilers284b5d12021-09-07 12:46:15 +0100612 // We've made sure before that the number of output files either equals numOutputs, in which
613 // case we override those files when processing the results of each iteration (only the result
614 // of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100615 // output files for each output of each iteration.
616 size_t outputFileIndex = x * numOutputs + i;
617 if (!params.m_OutputTensorFiles.empty())
618 {
619 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
620 ARMNN_LOG(info) << "Writing output " << i << " named: '"
621 << inferenceModelParams.m_OutputBindings[i]
622 << "' of iteration: " << x+1 << " to file: '"
623 << params.m_OutputTensorFiles[outputFileIndex] << "'";
624 }
625 auto outputTensorFile = params.m_OutputTensorFiles.empty()
626 ? ""
627 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100628
629 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
630 infoOut,
631 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100632 params.m_DequantizeOutput,
633 !params.m_DontPrintOutputs);
Jan Eilersf17fcd52021-07-26 22:20:00 +0100634 mapbox::util::apply_visitor(printer, outputs[x][i]);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100635 }
636
637 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
638 << std::fixed << inference_duration.count() << " ms\n";
639
640 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
641 if (params.m_ThresholdTime != 0.0)
642 {
643 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
644 << std::fixed << params.m_ThresholdTime << " ms";
645 auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count();
646 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
647 << std::fixed << thresholdMinusInference << " ms" << "\n";
648
649 if (thresholdMinusInference < 0)
650 {
651 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
652 ARMNN_LOG(fatal) << errorMessage;
653 }
654 }
655 }
656 }
Ryan OSheadfbec2d2022-03-28 10:55:48 +0100657 // Synchronous Execution using a single buffer for input and output data
658 else if(!params.m_Concurrent)
659 {
660 std::vector<armnnUtils::TContainer> input;
661 std::vector<armnnUtils::TContainer> output;
662
663 for (unsigned int i = 0; i < numInputs; ++i)
664 {
665 // If there are fewer input files given than required for the execution of
666 // params.m_Iterations we simply start with the first input file again
667 size_t inputFileIndex = numInputs + i;
668 if (!params.m_InputTensorDataFilePaths.empty())
669 {
670 inputFileIndex = inputFileIndex % params.m_InputTensorDataFilePaths.size();
671 }
672
673 armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ?
674 armnn::EmptyOptional() :
675 armnn::MakeOptional<std::string>(
676 params.m_InputTensorDataFilePaths.at(
677 inputFileIndex));
678
679 unsigned int numElements = model.GetInputSize(i);
680 if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i])
681 {
682 // If the user has provided a tensor shape for the current input,
683 // override numElements
684 numElements = params.m_InputTensorShapes[i]->GetNumElements();
685 }
686
687 armnnUtils::TContainer tensorData;
688 PopulateTensorWithData(tensorData,
689 numElements,
690 params.m_InputTypes[i],
691 qParams,
692 dataFile);
693
694 input.push_back(tensorData);
695 }
696
697 for (unsigned int i = 0; i < numOutputs; ++i)
698 {
699 if (params.m_OutputTypes[i].compare("float") == 0)
700 {
701 output.push_back(std::vector<float>(model.GetOutputSize(i)));
702 } else if (params.m_OutputTypes[i].compare("int") == 0) {
703 output.push_back(std::vector<int>(model.GetOutputSize(i)));
704 } else if (params.m_OutputTypes[i].compare("qasymm8") == 0 ||
705 params.m_OutputTypes[i].compare("qasymmu8") == 0)
706 {
707 output.push_back(std::vector<uint8_t>(model.GetOutputSize(i)));
708 } else if (params.m_OutputTypes[i].compare("qasymms8") == 0)
709 {
710 output.push_back(std::vector<int8_t>(model.GetOutputSize(i)));
711 } else {
712 ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". ";
713 return EXIT_FAILURE;
714 }
715 }
716
717 std::vector<std::chrono::duration<double, std::milli>> timings;
718 timings.reserve(params.m_Iterations);
719 for (size_t x = 0; x < params.m_Iterations; x++)
720 {
721 // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds)
722 auto inference_duration = model.Run(input, output);
723 timings.push_back(inference_duration);
724 }
725
726 if (params.m_GenerateTensorData)
727 {
728 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
729 }
730 if (params.m_DontPrintOutputs)
731 {
732 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
733 }
734
735 // Print output. This only needs to happen once as input is the same for each iteration.
736 const auto &infosOut = model.GetOutputBindingInfos();
737 for (size_t i = 0; i < numOutputs; i++)
738 {
739 const armnn::TensorInfo &infoOut = infosOut[i].second;
740
741 // We've made sure before that the number of output files either equals numOutputs, in which
742 // case we override those files when processing the results of each iteration (only the result
743 // of the last iteration will be stored), or there are enough
744 // output files for each output of each iteration.
745 size_t outputFileIndex = numOutputs + i;
746 if (!params.m_OutputTensorFiles.empty())
747 {
748 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
749 ARMNN_LOG(info) << "Writing output " << i << " named: '"
750 << inferenceModelParams.m_OutputBindings[i] <<" to file: '"
751 << params.m_OutputTensorFiles[outputFileIndex] << "'";
752 }
753 auto outputTensorFile = params.m_OutputTensorFiles.empty()
754 ? ""
755 : params.m_OutputTensorFiles[outputFileIndex];
756
757 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
758 infoOut,
759 outputTensorFile,
760 params.m_DequantizeOutput,
761 !params.m_DontPrintOutputs);
762 mapbox::util::apply_visitor(printer, output[i]);
763 }
764
765 for(auto inference: timings)
766 {
767
768 ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2)
769 << std::fixed << inference.count() << " ms\n";
770
771 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
772 if (params.m_ThresholdTime != 0.0)
773 {
774 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
775 << std::fixed << params.m_ThresholdTime << " ms";
776 auto thresholdMinusInference = params.m_ThresholdTime - inference.count();
777 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
778 << std::fixed << thresholdMinusInference << " ms" << "\n";
779
780 if (thresholdMinusInference < 0)
781 {
782 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
783 ARMNN_LOG(fatal) << errorMessage;
784 }
785 }
786 }
787 }
788
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100789 // Asynchronous execution using the Arm NN thread pool
Kevin May94dd4db2021-05-26 16:01:08 +0100790 else if (params.m_ThreadPoolSize >= 1)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100791 {
792 try
793 {
794 ARMNN_LOG(info) << "Asynchronous execution with Arm NN thread pool... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100795 armnn::AsyncCallbackManager callbackManager;
Francis Murtagh40d27412021-10-28 11:11:35 +0100796 std::unordered_map<armnn::InferenceId, std::vector<armnnUtils::TContainer>&> inferenceOutputMap;
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100797
798 // Declare the latest and earliest inference times here to be used when calculating overall time
799 std::chrono::high_resolution_clock::time_point earliestStartTime;
800 std::chrono::high_resolution_clock::time_point latestEndTime =
801 std::chrono::high_resolution_clock::now();
802
803 // For the asynchronous execution, we are adding a pool of working memory handles (1 per thread) in the
804 // LoadedNetwork with each scheduled inference having a specific priority
Jan Eilersf17fcd52021-07-26 22:20:00 +0100805 for (size_t i = 0; i < params.m_Iterations; ++i)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100806 {
Finn Williamsf364d532021-06-09 17:07:33 +0100807 std::shared_ptr<armnn::AsyncExecutionCallback> cb = callbackManager.GetNewCallback();
808 inferenceOutputMap.insert({cb->GetInferenceId(), outputs[i]});
809 model.RunAsync(inputs[i], outputs[i], cb);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100810 }
811
812 // Check the results
813 unsigned int j = 0;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100814 for (size_t iteration = 0; iteration < params.m_Iterations; ++iteration)
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100815 {
Finn Williamsf364d532021-06-09 17:07:33 +0100816 auto cb = callbackManager.GetNotifiedCallback();
817
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100818 // Get the results
819 auto endTime = time_point_cast<std::chrono::milliseconds>(cb->GetEndTime());
820 auto startTime = time_point_cast<std::chrono::milliseconds>(cb->GetStartTime());
821 auto inferenceDuration = endTime - startTime;
822
823 if (latestEndTime < cb->GetEndTime())
824 {
825 latestEndTime = cb->GetEndTime();
826 }
827
828 if (earliestStartTime.time_since_epoch().count() == 0)
829 {
830 earliestStartTime = cb->GetStartTime();
831 }
832 else if (earliestStartTime > cb->GetStartTime())
833 {
834 earliestStartTime = cb->GetStartTime();
835 }
836
837 if (params.m_GenerateTensorData)
838 {
839 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
840 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100841 if (params.m_DontPrintOutputs)
842 {
843 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
844 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100845
846 // Print output tensors
847 const auto& infosOut = model.GetOutputBindingInfos();
848 for (size_t i = 0; i < numOutputs; i++)
849 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100850 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100851 // case we override those files when processing the results of each iteration (only the
852 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100853 // output files for each output of each iteration.
854 size_t outputFileIndex = iteration * numOutputs + i;
855 if (!params.m_OutputTensorFiles.empty())
856 {
857 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
858 ARMNN_LOG(info) << "Writing output " << i << " named: '"
859 << inferenceModelParams.m_OutputBindings[i]
860 << "' of iteration: " << iteration+1 << " to file: '"
861 << params.m_OutputTensorFiles[outputFileIndex] << "'";
862 }
863
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100864 const armnn::TensorInfo& infoOut = infosOut[i].second;
865 auto outputTensorFile = params.m_OutputTensorFiles.empty()
866 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100867 : params.m_OutputTensorFiles[outputFileIndex];
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100868
869 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
870 infoOut,
871 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100872 params.m_DequantizeOutput,
873 !params.m_DontPrintOutputs);
Finn Williamsf364d532021-06-09 17:07:33 +0100874 mapbox::util::apply_visitor(printer, inferenceOutputMap.at(cb->GetInferenceId())[i]);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100875 }
876
Colm Donelan3cff15a2021-10-12 15:06:19 +0100877 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100878 ++j;
879 }
880 //print duration difference between overallStartTime and overallEndTime
881 auto overallEndTime = time_point_cast<std::chrono::milliseconds>(latestEndTime);
882 auto overallStartTime = time_point_cast<std::chrono::milliseconds>(earliestStartTime);
883 auto totalInferenceDuration = overallEndTime - overallStartTime;
884 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
885 << std::fixed << totalInferenceDuration.count() << " ms\n";
886 }
887 catch (const armnn::Exception& e)
888 {
889 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
890 return EXIT_FAILURE;
891 }
892 }
893 // Asynchronous execution using std::launch::async
Sadik Armagana04a9d72021-04-27 10:02:10 +0100894 else
895 {
896 try
897 {
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100898 ARMNN_LOG(info) << "Asynchronous Execution with std::launch:async... \n";
Finn Williamsf364d532021-06-09 17:07:33 +0100899 std::vector<std::future<std::tuple<unsigned int,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100900 std::chrono::duration<double, std::milli>>>> inferenceResults;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100901 inferenceResults.reserve(params.m_Iterations);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100902
903 // Create WorkingMemHandles for each inference
904 std::vector<std::unique_ptr<armnn::experimental::IWorkingMemHandle>> workingMemHandles;
Jan Eilersf17fcd52021-07-26 22:20:00 +0100905 workingMemHandles.reserve(params.m_Iterations);
906 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100907 {
908 workingMemHandles.push_back(model.CreateWorkingMemHandle());
909 }
910
911 // Run each inference in its own thread
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100912 // start a timer
913 const auto start_time = armnn::GetTimeNow();
Jan Eilersf17fcd52021-07-26 22:20:00 +0100914 for (unsigned int i = 0; i < params.m_Iterations; ++i)
Sadik Armagana04a9d72021-04-27 10:02:10 +0100915 {
916 armnn::experimental::IWorkingMemHandle& workingMemHandleRef = *workingMemHandles[i].get();
Finn Williamsf364d532021-06-09 17:07:33 +0100917
Sadik Armagana04a9d72021-04-27 10:02:10 +0100918 inferenceResults.push_back(std::async(
919 std::launch::async, [&model, &workingMemHandleRef, &inputs, &outputs, i]() {
Finn Williamsf364d532021-06-09 17:07:33 +0100920 return model.RunAsync(workingMemHandleRef, inputs[i], outputs[i], i);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100921 }
922 ));
923 }
924
925 // Check the results
926 for (unsigned int j = 0; j < inferenceResults.size(); ++j)
927 {
928 // Get the results
929 auto inferenceResult = inferenceResults[j].get();
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100930 auto inferenceDuration = std::get<1>(inferenceResult);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100931 auto inferenceID = std::get<0>(inferenceResult);
932
933 if (params.m_GenerateTensorData)
934 {
935 ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful";
936 }
Jan Eilers284b5d12021-09-07 12:46:15 +0100937 if (params.m_DontPrintOutputs)
938 {
939 ARMNN_LOG(info) << "Printing outputs to console is disabled.";
940 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100941
942 // Print output tensors
943 const auto& infosOut = model.GetOutputBindingInfos();
944 for (size_t i = 0; i < numOutputs; i++)
945 {
Jan Eilersf17fcd52021-07-26 22:20:00 +0100946 // We've made sure before that the number of output files either equals numOutputs, in which
Jan Eilers284b5d12021-09-07 12:46:15 +0100947 // case we override those files when processing the results of each iteration (only the
948 // result of the last iteration will be stored), or there are enough
Jan Eilersf17fcd52021-07-26 22:20:00 +0100949 // output files for each output of each iteration.
950 size_t outputFileIndex = j * numOutputs + i;
951 if (!params.m_OutputTensorFiles.empty())
952 {
953 outputFileIndex = outputFileIndex % params.m_OutputTensorFiles.size();
954 ARMNN_LOG(info) << "Writing output " << i << " named: '"
955 << inferenceModelParams.m_OutputBindings[i]
956 << "' of iteration: " << j+1 << " to file: '"
957 << params.m_OutputTensorFiles[outputFileIndex] << "'";
958 }
Sadik Armagana04a9d72021-04-27 10:02:10 +0100959 const armnn::TensorInfo& infoOut = infosOut[i].second;
960 auto outputTensorFile = params.m_OutputTensorFiles.empty()
961 ? ""
Jan Eilersf17fcd52021-07-26 22:20:00 +0100962 : params.m_OutputTensorFiles[outputFileIndex];
Sadik Armagana04a9d72021-04-27 10:02:10 +0100963
964 TensorPrinter printer(inferenceModelParams.m_OutputBindings[i],
965 infoOut,
966 outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +0100967 params.m_DequantizeOutput,
968 !params.m_DontPrintOutputs);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100969 mapbox::util::apply_visitor(printer, outputs[j][i]);
970 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100971 CheckInferenceTimeThreshold(inferenceDuration, params.m_ThresholdTime);
Sadik Armagana04a9d72021-04-27 10:02:10 +0100972 ARMNN_LOG(info) << "Asynchronous Execution is finished for Inference ID: " << inferenceID << " \n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100973 }
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100974 // finish timer
975 const auto duration = armnn::GetTimeDuration(start_time);
976 ARMNN_LOG(info) << "\nOverall Inference time: " << std::setprecision(2)
977 << std::fixed << duration.count() << " ms\n";
Sadik Armagana04a9d72021-04-27 10:02:10 +0100978 }
979 catch (const armnn::Exception& e)
980 {
981 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
982 return EXIT_FAILURE;
983 }
Jan Eilers45274902020-10-15 18:34:43 +0100984 }
985 }
986 catch (const armnn::Exception& e)
987 {
988 ARMNN_LOG(fatal) << "Armnn Error: " << e.what();
989 return EXIT_FAILURE;
990 }
991
992 return EXIT_SUCCESS;
993}
994
James Conroy7b4886f2019-04-11 10:23:58 +0100995// MAIN
telsoa01c577f2c2018-08-31 09:22:23 +0100996int main(int argc, const char* argv[])
997{
998 // Configures logging for both the ARMNN library and this test program.
Jan Eilers45274902020-10-15 18:34:43 +0100999 #ifdef NDEBUG
telsoa01c577f2c2018-08-31 09:22:23 +01001000 armnn::LogSeverity level = armnn::LogSeverity::Info;
Jan Eilers45274902020-10-15 18:34:43 +01001001 #else
telsoa01c577f2c2018-08-31 09:22:23 +01001002 armnn::LogSeverity level = armnn::LogSeverity::Debug;
Jan Eilers45274902020-10-15 18:34:43 +01001003 #endif
telsoa01c577f2c2018-08-31 09:22:23 +01001004 armnn::ConfigureLogging(true, true, level);
telsoa01c577f2c2018-08-31 09:22:23 +01001005
telsoa01c577f2c2018-08-31 09:22:23 +01001006
Jan Eilers45274902020-10-15 18:34:43 +01001007 // Get ExecuteNetwork parameters and runtime options from command line
Jan Eilersf17fcd52021-07-26 22:20:00 +01001008 // This might throw an InvalidArgumentException if the user provided invalid inputs
1009 ProgramOptions ProgramOptions;
1010 try {
1011 ProgramOptions.ParseOptions(argc, argv);
1012 } catch (const std::exception &e){
1013 ARMNN_LOG(fatal) << e.what();
1014 return EXIT_FAILURE;
1015 }
Narumol Prangnawaratd8cc8112020-03-24 13:54:05 +00001016
Keith Davis4914d0c2021-08-18 17:14:05 +01001017 if ((ProgramOptions.m_ExNetParams.m_OutputDetailsToStdOut ||
1018 ProgramOptions.m_ExNetParams.m_OutputDetailsOnlyToStdOut)
1019 && !ProgramOptions.m_ExNetParams.m_EnableProfiling)
Keith Davisf4874862021-08-09 16:49:18 +01001020 {
1021 ARMNN_LOG(fatal) << "You must enable profiling if you would like to output layer details";
1022 return EXIT_FAILURE;
1023 }
1024
Jan Eilers45274902020-10-15 18:34:43 +01001025 std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat;
1026
1027 // Forward to implementation based on the parser type
1028 if (modelFormat.find("armnn") != std::string::npos)
Finn Williamsd7fcafa2020-04-23 17:55:18 +01001029 {
Jan Eilers45274902020-10-15 18:34:43 +01001030 #if defined(ARMNN_SERIALIZER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +00001031 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +01001032 return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime);
1033 #else
1034 ARMNN_LOG(fatal) << "Not built with serialization support.";
Finn Williamsd7fcafa2020-04-23 17:55:18 +01001035 return EXIT_FAILURE;
Jan Eilers45274902020-10-15 18:34:43 +01001036 #endif
Finn Williamsd7fcafa2020-04-23 17:55:18 +01001037 }
Jan Eilers45274902020-10-15 18:34:43 +01001038 else if (modelFormat.find("onnx") != std::string::npos)
telsoa01c577f2c2018-08-31 09:22:23 +01001039 {
Jan Eilers45274902020-10-15 18:34:43 +01001040 #if defined(ARMNN_ONNX_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +00001041 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
Jan Eilers45274902020-10-15 18:34:43 +01001042 return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime);
1043 #else
1044 ARMNN_LOG(fatal) << "Not built with Onnx parser support.";
1045 return EXIT_FAILURE;
1046 #endif
1047 }
Jan Eilers45274902020-10-15 18:34:43 +01001048 else if(modelFormat.find("tflite") != std::string::npos)
1049 {
Finn Williamsf806c4d2021-02-22 15:13:12 +00001050 if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser)
1051 {
1052 #if defined(ARMNN_TF_LITE_PARSER)
Cathal Corbett5b0d8872021-12-06 17:06:12 +00001053 std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions));
1054 return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime);
Finn Williamsf806c4d2021-02-22 15:13:12 +00001055 #else
Cathal Corbett5b0d8872021-12-06 17:06:12 +00001056 ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support.";
1057 return EXIT_FAILURE;
Finn Williamsf806c4d2021-02-22 15:13:12 +00001058 #endif
1059 }
1060 else if (ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
1061 ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
1062 ProgramOptions.m_ExNetParams.m_TfLiteExecutor ==
1063 ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
Sadik Armagan5d03e312020-11-17 16:43:56 +00001064 {
1065 #if defined(ARMNN_TF_LITE_DELEGATE)
Colm Donelan45142282021-10-21 23:39:52 +01001066 return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, ProgramOptions.m_RuntimeOptions);
Sadik Armagan5d03e312020-11-17 16:43:56 +00001067 #else
Finn Williamsbbbefec2020-11-25 14:32:42 +00001068 ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
Sadik Armagan5d03e312020-11-17 16:43:56 +00001069 return EXIT_FAILURE;
1070 #endif
1071 }
Jan Eilers45274902020-10-15 18:34:43 +01001072 }
1073 else
1074 {
1075 ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat
Nikhil Raj5d955cf2021-04-19 16:59:48 +01001076 << "'. Please include 'tflite' or 'onnx'";
Jan Eilers45274902020-10-15 18:34:43 +01001077 return EXIT_FAILURE;
telsoa014fcda012018-03-09 14:13:49 +00001078 }
1079}