Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 5 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 6 | #include "NetworkExecutionUtils/NetworkExecutionUtils.hpp" |
| 7 | #include "ExecuteNetworkProgramOptions.hpp" |
| 8 | |
| 9 | #include <armnn/Logging.hpp> |
| 10 | #include <Filesystem.hpp> |
| 11 | #include <InferenceTest.hpp> |
| 12 | |
| 13 | #if defined(ARMNN_SERIALIZER) |
| 14 | #include "armnnDeserializer/IDeserializer.hpp" |
| 15 | #endif |
| 16 | #if defined(ARMNN_CAFFE_PARSER) |
| 17 | #include "armnnCaffeParser/ICaffeParser.hpp" |
| 18 | #endif |
| 19 | #if defined(ARMNN_TF_PARSER) |
| 20 | #include "armnnTfParser/ITfParser.hpp" |
| 21 | #endif |
| 22 | #if defined(ARMNN_TF_LITE_PARSER) |
| 23 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 24 | #endif |
| 25 | #if defined(ARMNN_ONNX_PARSER) |
| 26 | #include "armnnOnnxParser/IOnnxParser.hpp" |
| 27 | #endif |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 28 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 29 | #include <armnn_delegate.hpp> |
| 30 | #include <DelegateOptions.hpp> |
| 31 | |
| 32 | #include <tensorflow/lite/builtin_ops.h> |
| 33 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 34 | #include <tensorflow/lite/c/common.h> |
| 35 | #include <tensorflow/lite/optional_debug_tools.h> |
| 36 | #include <tensorflow/lite/kernels/builtin_op_kernels.h> |
| 37 | #include <tensorflow/lite/interpreter.h> |
| 38 | #include <tensorflow/lite/kernels/register.h> |
| 39 | #endif |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 40 | |
| 41 | #include <future> |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 42 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 43 | int TfLiteDelegateMainImpl(const ExecuteNetworkParams& params, |
| 44 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 45 | { |
| 46 | using namespace tflite; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 47 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 48 | std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(params.m_ModelPath.c_str()); |
| 49 | |
| 50 | auto tfLiteInterpreter = std::make_unique<Interpreter>(); |
| 51 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 52 | |
| 53 | tflite::InterpreterBuilder builder(*model, resolver); |
| 54 | builder(&tfLiteInterpreter); |
| 55 | tfLiteInterpreter->AllocateTensors(); |
| 56 | |
| 57 | // Create the Armnn Delegate |
| 58 | armnnDelegate::DelegateOptions delegateOptions(params.m_ComputeDevices); |
| 59 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 60 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 61 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 62 | // Register armnn_delegate to TfLiteInterpreter |
| 63 | int status = tfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate)); |
Sadik Armagan | 19a1c03 | 2021-01-20 12:17:00 +0000 | [diff] [blame] | 64 | if (status == kTfLiteError) |
| 65 | { |
| 66 | ARMNN_LOG(fatal) << "Could not register ArmNN TfLite Delegate to TfLiteInterpreter!"; |
| 67 | return EXIT_FAILURE; |
| 68 | } |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 69 | |
| 70 | std::vector<std::string> inputBindings; |
| 71 | for (const std::string& inputName: params.m_InputNames) |
| 72 | { |
| 73 | inputBindings.push_back(inputName); |
| 74 | } |
| 75 | |
| 76 | armnn::Optional<std::string> dataFile = params.m_GenerateTensorData |
| 77 | ? armnn::EmptyOptional() |
| 78 | : armnn::MakeOptional<std::string>(params.m_InputTensorDataFilePaths[0]); |
| 79 | |
| 80 | const size_t numInputs = inputBindings.size(); |
| 81 | |
| 82 | for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex) |
| 83 | { |
| 84 | int input = tfLiteInterpreter->inputs()[inputIndex]; |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 85 | TfLiteIntArray* inputDims = tfLiteInterpreter->tensor(input)->dims; |
| 86 | |
| 87 | long inputSize = 1; |
| 88 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim) |
| 89 | { |
| 90 | inputSize *= inputDims->data[dim]; |
| 91 | } |
| 92 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 93 | if (params.m_InputTypes[inputIndex].compare("float") == 0) |
| 94 | { |
| 95 | auto inputData = tfLiteInterpreter->typed_tensor<float>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 96 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 97 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 98 | { |
| 99 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 100 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 101 | return EXIT_FAILURE; |
| 102 | } |
| 103 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 104 | std::vector<float> tensorData; |
| 105 | PopulateTensorWithDataGeneric<float>(tensorData, |
| 106 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 107 | dataFile, |
| 108 | [](const std::string& s) |
| 109 | { return std::stof(s); }); |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 110 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 111 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
| 112 | } |
| 113 | else if (params.m_InputTypes[inputIndex].compare("int8") == 0) |
| 114 | { |
| 115 | auto inputData = tfLiteInterpreter->typed_tensor<int8_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 116 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 117 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 118 | { |
| 119 | ARMNN_LOG(fatal) << "Input tensor is null, input type: " |
| 120 | "\"" << params.m_InputTypes[inputIndex] << "\" may be incorrect."; |
| 121 | return EXIT_FAILURE; |
| 122 | } |
| 123 | |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 124 | std::vector<int8_t> tensorData; |
| 125 | PopulateTensorWithDataGeneric<int8_t>(tensorData, |
| 126 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 127 | dataFile, |
| 128 | [](const std::string& s) |
| 129 | { return armnn::numeric_cast<int8_t>(std::stoi(s)); }); |
| 130 | |
| 131 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 132 | } |
| 133 | else if (params.m_InputTypes[inputIndex].compare("int") == 0) |
| 134 | { |
| 135 | auto inputData = tfLiteInterpreter->typed_tensor<int32_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 136 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 137 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 138 | { |
| 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 Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 144 | std::vector<int32_t> tensorData; |
| 145 | PopulateTensorWithDataGeneric<int32_t>(tensorData, |
| 146 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 147 | dataFile, |
| 148 | [](const std::string& s) |
| 149 | { return std::stoi(s); }); |
| 150 | |
| 151 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 152 | } |
| 153 | else if (params.m_InputTypes[inputIndex].compare("qasymm8") == 0) |
| 154 | { |
| 155 | auto inputData = tfLiteInterpreter->typed_tensor<uint8_t>(input); |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 156 | |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 157 | if(inputData == NULL) |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 158 | { |
| 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 Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 164 | std::vector<uint8_t> tensorData; |
| 165 | PopulateTensorWithDataGeneric<uint8_t>(tensorData, |
| 166 | params.m_InputTensorShapes[inputIndex]->GetNumElements(), |
| 167 | dataFile, |
| 168 | [](const std::string& s) |
| 169 | { return armnn::numeric_cast<uint8_t>(std::stoi(s)); }); |
| 170 | |
| 171 | std::copy(tensorData.begin(), tensorData.end(), inputData); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 172 | } |
| 173 | else |
| 174 | { |
| 175 | ARMNN_LOG(fatal) << "Unsupported input tensor data type \"" << params.m_InputTypes[inputIndex] << "\". "; |
| 176 | return EXIT_FAILURE; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | for (size_t x = 0; x < params.m_Iterations; x++) |
| 181 | { |
| 182 | // Run the inference |
| 183 | tfLiteInterpreter->Invoke(); |
| 184 | |
| 185 | // Print out the output |
| 186 | for (unsigned int outputIndex = 0; outputIndex < params.m_OutputNames.size(); ++outputIndex) |
| 187 | { |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 188 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[outputIndex]; |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 189 | TfLiteIntArray* outputDims = tfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 190 | |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 191 | long outputSize = 1; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 192 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim) |
| 193 | { |
Sadik Armagan | 15f7fae | 2020-11-18 09:37:03 +0000 | [diff] [blame] | 194 | outputSize *= outputDims->data[dim]; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | std::cout << params.m_OutputNames[outputIndex] << ": "; |
| 198 | if (params.m_OutputTypes[outputIndex].compare("float") == 0) |
| 199 | { |
| 200 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 201 | if(tfLiteDelageOutputData == NULL) |
| 202 | { |
| 203 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 204 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 205 | return EXIT_FAILURE; |
| 206 | } |
| 207 | |
| 208 | for (int i = 0; i < outputSize; ++i) |
| 209 | { |
| 210 | std::cout << tfLiteDelageOutputData[i] << ", "; |
| 211 | if (i % 60 == 0) |
| 212 | { |
| 213 | std::cout << std::endl; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | else if (params.m_OutputTypes[outputIndex].compare("int") == 0) |
| 218 | { |
| 219 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int32_t>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 220 | if(tfLiteDelageOutputData == NULL) |
| 221 | { |
| 222 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 223 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 224 | return EXIT_FAILURE; |
| 225 | } |
| 226 | |
| 227 | for (int i = 0; i < outputSize; ++i) |
| 228 | { |
| 229 | std::cout << tfLiteDelageOutputData[i] << ", "; |
| 230 | if (i % 60 == 0) |
| 231 | { |
| 232 | std::cout << std::endl; |
| 233 | } |
| 234 | } |
| 235 | } |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 236 | else if (params.m_OutputTypes[outputIndex].compare("int8") == 0) |
| 237 | { |
| 238 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<int8_t>(tfLiteDelegateOutputId); |
| 239 | if(tfLiteDelageOutputData == NULL) |
| 240 | { |
| 241 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 242 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 243 | return EXIT_FAILURE; |
| 244 | } |
| 245 | |
| 246 | for (int i = 0; i < outputSize; ++i) |
| 247 | { |
| 248 | std::cout << signed(tfLiteDelageOutputData[i]) << ", "; |
| 249 | if (i % 60 == 0) |
| 250 | { |
| 251 | std::cout << std::endl; |
| 252 | } |
| 253 | } |
| 254 | } |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 255 | else if (params.m_OutputTypes[outputIndex].compare("qasymm8") == 0) |
| 256 | { |
| 257 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<uint8_t>(tfLiteDelegateOutputId); |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 258 | if(tfLiteDelageOutputData == NULL) |
| 259 | { |
| 260 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 261 | "\"" << params.m_OutputTypes[outputIndex] << "\" may be incorrect."; |
| 262 | return EXIT_FAILURE; |
| 263 | } |
| 264 | |
| 265 | for (int i = 0; i < outputSize; ++i) |
| 266 | { |
| 267 | std::cout << unsigned(tfLiteDelageOutputData[i]) << ", "; |
| 268 | if (i % 60 == 0) |
| 269 | { |
| 270 | std::cout << std::endl; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | ARMNN_LOG(fatal) << "Output tensor is null, output type: " |
| 277 | "\"" << params.m_OutputTypes[outputIndex] << |
| 278 | "\" may be incorrect. Output type can be specified with -z argument"; |
| 279 | return EXIT_FAILURE; |
| 280 | } |
| 281 | std::cout << std::endl; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return status; |
| 286 | } |
| 287 | #endif |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 288 | template<typename TParser, typename TDataType> |
| 289 | int MainImpl(const ExecuteNetworkParams& params, |
| 290 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 291 | { |
| 292 | using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
| 293 | |
| 294 | std::vector<TContainer> inputDataContainers; |
| 295 | |
| 296 | try |
| 297 | { |
| 298 | // Creates an InferenceModel, which will parse the model and load it into an IRuntime. |
| 299 | typename InferenceModel<TParser, TDataType>::Params inferenceModelParams; |
| 300 | inferenceModelParams.m_ModelPath = params.m_ModelPath; |
| 301 | inferenceModelParams.m_IsModelBinary = params.m_IsModelBinary; |
| 302 | inferenceModelParams.m_ComputeDevices = params.m_ComputeDevices; |
| 303 | inferenceModelParams.m_DynamicBackendsPath = params.m_DynamicBackendsPath; |
| 304 | inferenceModelParams.m_PrintIntermediateLayers = params.m_PrintIntermediate; |
| 305 | inferenceModelParams.m_VisualizePostOptimizationModel = params.m_EnableLayerDetails; |
| 306 | inferenceModelParams.m_ParseUnsupported = params.m_ParseUnsupported; |
| 307 | inferenceModelParams.m_InferOutputShape = params.m_InferOutputShape; |
| 308 | inferenceModelParams.m_EnableFastMath = params.m_EnableFastMath; |
Matthew Sloyan | 4243211 | 2021-01-08 10:30:51 +0000 | [diff] [blame] | 309 | inferenceModelParams.m_SaveCachedNetwork = params.m_SaveCachedNetwork; |
| 310 | inferenceModelParams.m_CachedNetworkFilePath = params.m_CachedNetworkFilePath; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 311 | |
| 312 | for(const std::string& inputName: params.m_InputNames) |
| 313 | { |
| 314 | inferenceModelParams.m_InputBindings.push_back(inputName); |
| 315 | } |
| 316 | |
| 317 | for(unsigned int i = 0; i < params.m_InputTensorShapes.size(); ++i) |
| 318 | { |
| 319 | inferenceModelParams.m_InputShapes.push_back(*params.m_InputTensorShapes[i]); |
| 320 | } |
| 321 | |
| 322 | for(const std::string& outputName: params.m_OutputNames) |
| 323 | { |
| 324 | inferenceModelParams.m_OutputBindings.push_back(outputName); |
| 325 | } |
| 326 | |
| 327 | inferenceModelParams.m_SubgraphId = params.m_SubgraphId; |
| 328 | inferenceModelParams.m_EnableFp16TurboMode = params.m_EnableFp16TurboMode; |
| 329 | inferenceModelParams.m_EnableBf16TurboMode = params.m_EnableBf16TurboMode; |
| 330 | |
| 331 | InferenceModel<TParser, TDataType> model(inferenceModelParams, |
| 332 | params.m_EnableProfiling, |
| 333 | params.m_DynamicBackendsPath, |
| 334 | runtime); |
| 335 | |
| 336 | const size_t numInputs = inferenceModelParams.m_InputBindings.size(); |
| 337 | for(unsigned int i = 0; i < numInputs; ++i) |
| 338 | { |
| 339 | armnn::Optional<QuantizationParams> qParams = params.m_QuantizeInput ? |
| 340 | armnn::MakeOptional<QuantizationParams>( |
| 341 | model.GetInputQuantizationParams()) : |
| 342 | armnn::EmptyOptional(); |
| 343 | |
| 344 | armnn::Optional<std::string> dataFile = params.m_GenerateTensorData ? |
| 345 | armnn::EmptyOptional() : |
| 346 | armnn::MakeOptional<std::string>( |
| 347 | params.m_InputTensorDataFilePaths[i]); |
| 348 | |
| 349 | unsigned int numElements = model.GetInputSize(i); |
| 350 | if (params.m_InputTensorShapes.size() > i && params.m_InputTensorShapes[i]) |
| 351 | { |
| 352 | // If the user has provided a tensor shape for the current input, |
| 353 | // override numElements |
| 354 | numElements = params.m_InputTensorShapes[i]->GetNumElements(); |
| 355 | } |
| 356 | |
| 357 | TContainer tensorData; |
| 358 | PopulateTensorWithData(tensorData, |
| 359 | numElements, |
| 360 | params.m_InputTypes[i], |
| 361 | qParams, |
| 362 | dataFile); |
| 363 | |
| 364 | inputDataContainers.push_back(tensorData); |
| 365 | } |
| 366 | |
| 367 | const size_t numOutputs = inferenceModelParams.m_OutputBindings.size(); |
| 368 | std::vector<TContainer> outputDataContainers; |
| 369 | |
| 370 | for (unsigned int i = 0; i < numOutputs; ++i) |
| 371 | { |
| 372 | if (params.m_OutputTypes[i].compare("float") == 0) |
| 373 | { |
| 374 | outputDataContainers.push_back(std::vector<float>(model.GetOutputSize(i))); |
| 375 | } |
| 376 | else if (params.m_OutputTypes[i].compare("int") == 0) |
| 377 | { |
| 378 | outputDataContainers.push_back(std::vector<int>(model.GetOutputSize(i))); |
| 379 | } |
| 380 | else if (params.m_OutputTypes[i].compare("qasymm8") == 0) |
| 381 | { |
| 382 | outputDataContainers.push_back(std::vector<uint8_t>(model.GetOutputSize(i))); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | ARMNN_LOG(fatal) << "Unsupported tensor data type \"" << params.m_OutputTypes[i] << "\". "; |
| 387 | return EXIT_FAILURE; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | for (size_t x = 0; x < params.m_Iterations; x++) |
| 392 | { |
| 393 | // model.Run returns the inference time elapsed in EnqueueWorkload (in milliseconds) |
| 394 | auto inference_duration = model.Run(inputDataContainers, outputDataContainers); |
| 395 | |
| 396 | if (params.m_GenerateTensorData) |
| 397 | { |
| 398 | ARMNN_LOG(warning) << "The input data was generated, note that the output will not be useful"; |
| 399 | } |
| 400 | |
| 401 | // Print output tensors |
| 402 | const auto& infosOut = model.GetOutputBindingInfos(); |
| 403 | for (size_t i = 0; i < numOutputs; i++) |
| 404 | { |
| 405 | const armnn::TensorInfo& infoOut = infosOut[i].second; |
| 406 | auto outputTensorFile = params.m_OutputTensorFiles.empty() ? "" : params.m_OutputTensorFiles[i]; |
| 407 | |
| 408 | TensorPrinter printer(inferenceModelParams.m_OutputBindings[i], |
| 409 | infoOut, |
| 410 | outputTensorFile, |
| 411 | params.m_DequantizeOutput); |
| 412 | mapbox::util::apply_visitor(printer, outputDataContainers[i]); |
| 413 | } |
| 414 | |
| 415 | ARMNN_LOG(info) << "\nInference time: " << std::setprecision(2) |
| 416 | << std::fixed << inference_duration.count() << " ms\n"; |
| 417 | |
| 418 | // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line |
| 419 | if (params.m_ThresholdTime != 0.0) |
| 420 | { |
| 421 | ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2) |
| 422 | << std::fixed << params.m_ThresholdTime << " ms"; |
| 423 | auto thresholdMinusInference = params.m_ThresholdTime - inference_duration.count(); |
| 424 | ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2) |
| 425 | << std::fixed << thresholdMinusInference << " ms" << "\n"; |
| 426 | |
| 427 | if (thresholdMinusInference < 0) |
| 428 | { |
| 429 | std::string errorMessage = "Elapsed inference time is greater than provided threshold time."; |
| 430 | ARMNN_LOG(fatal) << errorMessage; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | catch (const armnn::Exception& e) |
| 436 | { |
| 437 | ARMNN_LOG(fatal) << "Armnn Error: " << e.what(); |
| 438 | return EXIT_FAILURE; |
| 439 | } |
| 440 | |
| 441 | return EXIT_SUCCESS; |
| 442 | } |
| 443 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 444 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 445 | // MAIN |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 446 | int main(int argc, const char* argv[]) |
| 447 | { |
| 448 | // Configures logging for both the ARMNN library and this test program. |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 449 | #ifdef NDEBUG |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 450 | armnn::LogSeverity level = armnn::LogSeverity::Info; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 451 | #else |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 452 | armnn::LogSeverity level = armnn::LogSeverity::Debug; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 453 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 454 | armnn::ConfigureLogging(true, true, level); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 455 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 456 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 457 | // Get ExecuteNetwork parameters and runtime options from command line |
| 458 | ProgramOptions ProgramOptions(argc, argv); |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 459 | |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 460 | // Create runtime |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 461 | std::shared_ptr<armnn::IRuntime> runtime(armnn::IRuntime::Create(ProgramOptions.m_RuntimeOptions)); |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 462 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 463 | std::string modelFormat = ProgramOptions.m_ExNetParams.m_ModelFormat; |
| 464 | |
| 465 | // Forward to implementation based on the parser type |
| 466 | if (modelFormat.find("armnn") != std::string::npos) |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 467 | { |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 468 | #if defined(ARMNN_SERIALIZER) |
| 469 | return MainImpl<armnnDeserializer::IDeserializer, float>(ProgramOptions.m_ExNetParams, runtime); |
| 470 | #else |
| 471 | ARMNN_LOG(fatal) << "Not built with serialization support."; |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 472 | return EXIT_FAILURE; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 473 | #endif |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 474 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 475 | else if (modelFormat.find("caffe") != std::string::npos) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 476 | { |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 477 | #if defined(ARMNN_CAFFE_PARSER) |
| 478 | return MainImpl<armnnCaffeParser::ICaffeParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 479 | #else |
| 480 | ARMNN_LOG(fatal) << "Not built with Caffe parser support."; |
| 481 | return EXIT_FAILURE; |
| 482 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 483 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 484 | else if (modelFormat.find("onnx") != std::string::npos) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 485 | { |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 486 | #if defined(ARMNN_ONNX_PARSER) |
| 487 | return MainImpl<armnnOnnxParser::IOnnxParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 488 | #else |
| 489 | ARMNN_LOG(fatal) << "Not built with Onnx parser support."; |
| 490 | return EXIT_FAILURE; |
| 491 | #endif |
| 492 | } |
| 493 | else if (modelFormat.find("tensorflow") != std::string::npos) |
| 494 | { |
| 495 | #if defined(ARMNN_TF_PARSER) |
| 496 | return MainImpl<armnnTfParser::ITfParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 497 | #else |
| 498 | ARMNN_LOG(fatal) << "Not built with Tensorflow parser support."; |
| 499 | return EXIT_FAILURE; |
| 500 | #endif |
| 501 | } |
| 502 | else if(modelFormat.find("tflite") != std::string::npos) |
| 503 | { |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 504 | |
| 505 | if (ProgramOptions.m_ExNetParams.m_EnableDelegate) |
| 506 | { |
| 507 | #if defined(ARMNN_TF_LITE_DELEGATE) |
| 508 | return TfLiteDelegateMainImpl(ProgramOptions.m_ExNetParams, runtime); |
| 509 | #else |
Finn Williams | bbbefec | 2020-11-25 14:32:42 +0000 | [diff] [blame] | 510 | ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support."; |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 511 | return EXIT_FAILURE; |
| 512 | #endif |
| 513 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 514 | #if defined(ARMNN_TF_LITE_PARSER) |
| 515 | return MainImpl<armnnTfLiteParser::ITfLiteParser, float>(ProgramOptions.m_ExNetParams, runtime); |
| 516 | #else |
| 517 | ARMNN_LOG(fatal) << "Not built with Tensorflow-Lite parser support."; |
| 518 | return EXIT_FAILURE; |
| 519 | #endif |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | ARMNN_LOG(fatal) << "Unknown model format: '" << modelFormat |
| 524 | << "'. Please include 'caffe', 'tensorflow', 'tflite' or 'onnx'"; |
| 525 | return EXIT_FAILURE; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 526 | } |
| 527 | } |