Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 5446a4d | 2023-01-20 15:51:05 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TfliteExecutor.hpp" |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 7 | #include "tensorflow/lite/kernels/kernel_util.h" |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 8 | |
Colm Donelan | 35a0689 | 2023-02-06 15:01:57 +0000 | [diff] [blame] | 9 | TfLiteExecutor::TfLiteExecutor(const ExecuteNetworkParams& params, armnn::IRuntime::CreationOptions runtimeOptions) |
| 10 | : m_Params(params) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 11 | { |
Teresa Charlin | c814e80 | 2022-08-05 13:57:04 +0100 | [diff] [blame] | 12 | m_Model = tflite::FlatBufferModel::BuildFromFile(m_Params.m_ModelPath.c_str()); |
Colm Donelan | 18e6f04 | 2023-01-24 22:10:12 +0000 | [diff] [blame] | 13 | if (!m_Model) |
| 14 | { |
| 15 | LogAndThrow("Failed to load TfLite model from: " + m_Params.m_ModelPath); |
| 16 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 17 | m_TfLiteInterpreter = std::make_unique<Interpreter>(); |
| 18 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 19 | |
Teresa Charlin | c814e80 | 2022-08-05 13:57:04 +0100 | [diff] [blame] | 20 | tflite::InterpreterBuilder builder(*m_Model, resolver); |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 21 | builder(&m_TfLiteInterpreter); |
| 22 | m_TfLiteInterpreter->AllocateTensors(); |
| 23 | |
| 24 | int status = kTfLiteError; |
| 25 | if (m_Params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate) |
| 26 | { |
| 27 | // Create the Armnn Delegate |
| 28 | // Populate a DelegateOptions from the ExecuteNetworkParams. |
| 29 | armnnDelegate::DelegateOptions delegateOptions = m_Params.ToDelegateOptions(); |
Colm Donelan | 35a0689 | 2023-02-06 15:01:57 +0000 | [diff] [blame] | 30 | delegateOptions.SetRuntimeOptions(runtimeOptions); |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 31 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 32 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 33 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 34 | // Register armnn_delegate to TfLiteInterpreter |
| 35 | status = m_TfLiteInterpreter->ModifyGraphWithDelegate(std::move(theArmnnDelegate)); |
Cathal Corbett | aa21230 | 2022-08-04 17:58:09 +0100 | [diff] [blame] | 36 | if (status != kTfLiteOk) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 37 | { |
| 38 | LogAndThrow("Could not register ArmNN TfLite Delegate to TfLiteInterpreter"); |
| 39 | } |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | std::cout << "Running on TfLite without ArmNN delegate\n"; |
| 44 | } |
| 45 | |
Teresa Charlin | f53b28f | 2022-11-11 11:14:50 +0000 | [diff] [blame] | 46 | const size_t numInputs = m_TfLiteInterpreter->inputs().size(); |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 47 | |
| 48 | for(unsigned int inputIndex = 0; inputIndex < numInputs; ++inputIndex) |
| 49 | { |
Cathal Corbett | aa21230 | 2022-08-04 17:58:09 +0100 | [diff] [blame] | 50 | armnn::Optional<std::string> dataFile = m_Params.m_GenerateTensorData |
| 51 | ? armnn::EmptyOptional() |
| 52 | : armnn::MakeOptional<std::string>(m_Params.m_InputTensorDataFilePaths[inputIndex]); |
| 53 | |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 54 | int input = m_TfLiteInterpreter->inputs()[inputIndex]; |
Cathal Corbett | aa21230 | 2022-08-04 17:58:09 +0100 | [diff] [blame] | 55 | const auto& inputName = m_TfLiteInterpreter->tensor(input)->name; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 56 | |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 57 | // Before we start, check if the tensor is constant. |
| 58 | if (!tflite::IsConstantTensor(m_TfLiteInterpreter->tensor(input))) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 59 | { |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 60 | TfLiteIntArray* inputDims = m_TfLiteInterpreter->tensor(input)->dims; |
| 61 | |
| 62 | unsigned int inputSize = 1; |
| 63 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(inputDims->size); ++dim) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 64 | { |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 65 | inputSize *= inputDims->data[dim]; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 66 | } |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 67 | |
| 68 | const auto& dataType = m_TfLiteInterpreter->tensor(input)->type; |
| 69 | |
| 70 | switch (dataType) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 71 | { |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 72 | case kTfLiteFloat32: |
| 73 | { |
| 74 | auto inputData = m_TfLiteInterpreter->typed_tensor<float>(input); |
| 75 | PopulateTensorWithData<float>(inputData, inputSize, dataFile, inputName); |
| 76 | break; |
| 77 | } |
| 78 | case kTfLiteInt32: |
| 79 | { |
| 80 | auto inputData = m_TfLiteInterpreter->typed_tensor<int32_t>(input); |
| 81 | PopulateTensorWithData<int32_t>(inputData, inputSize, dataFile, inputName); |
| 82 | break; |
| 83 | } |
| 84 | case kTfLiteUInt8: |
| 85 | { |
| 86 | auto inputData = m_TfLiteInterpreter->typed_tensor<uint8_t>(input); |
| 87 | PopulateTensorWithData<uint8_t>(inputData, inputSize, dataFile, inputName); |
| 88 | break; |
| 89 | } |
| 90 | case kTfLiteInt16: |
| 91 | { |
| 92 | auto inputData = m_TfLiteInterpreter->typed_tensor<int16_t>(input); |
| 93 | PopulateTensorWithData<int16_t>(inputData, inputSize, dataFile, inputName); |
| 94 | break; |
| 95 | } |
| 96 | case kTfLiteInt8: |
| 97 | { |
| 98 | auto inputData = m_TfLiteInterpreter->typed_tensor<int8_t>(input); |
| 99 | PopulateTensorWithData<int8_t>(inputData, inputSize, dataFile, inputName); |
| 100 | break; |
| 101 | } |
| 102 | default: |
| 103 | { |
| 104 | LogAndThrow("Unsupported input tensor data type"); |
| 105 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 106 | } |
Colm Donelan | 3811a97 | 2023-01-25 21:19:49 +0000 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
| 110 | ARMNN_LOG(info) << "Input tensor \"" << inputName << "\" is constant and will not be populated with data."; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | std::vector<const void *> TfLiteExecutor::Execute() |
| 116 | { |
| 117 | int status = 0; |
| 118 | std::vector<const void*> results; |
| 119 | for (size_t x = 0; x < m_Params.m_Iterations; x++) |
| 120 | { |
| 121 | // Start timer to record inference time in milliseconds. |
| 122 | const auto start_time = armnn::GetTimeNow(); |
| 123 | // Run the inference |
| 124 | status = m_TfLiteInterpreter->Invoke(); |
| 125 | const auto duration = armnn::GetTimeDuration(start_time); |
| 126 | |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 127 | if (!m_Params.m_DontPrintOutputs) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 128 | { |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 129 | // Print out the output |
| 130 | for (unsigned int outputIndex = 0; outputIndex < m_TfLiteInterpreter->outputs().size(); ++outputIndex) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 131 | { |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 132 | auto tfLiteDelegateOutputId = m_TfLiteInterpreter->outputs()[outputIndex]; |
| 133 | TfLiteIntArray* outputDims = m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->dims; |
| 134 | // If we've been asked to write to a file then set a file output stream. Otherwise use stdout. |
| 135 | FILE* outputTensorFile = stdout; |
| 136 | if (!m_Params.m_OutputTensorFiles.empty()) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 137 | { |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 138 | outputTensorFile = fopen(m_Params.m_OutputTensorFiles[outputIndex].c_str(), "w"); |
| 139 | if (outputTensorFile == NULL) |
| 140 | { |
| 141 | LogAndThrow("Specified output tensor file, \"" + m_Params.m_OutputTensorFiles[outputIndex] + |
| 142 | "\", cannot be created. Defaulting to stdout. Error was: " + std::strerror(errno)); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | ARMNN_LOG(info) << "Writing output " << outputIndex << "' of iteration: " << x + 1 |
| 147 | << " to file: '" << m_Params.m_OutputTensorFiles[outputIndex] << "'"; |
| 148 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 149 | } |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 150 | long outputSize = 1; |
| 151 | for (unsigned int dim = 0; dim < static_cast<unsigned int>(outputDims->size); ++dim) |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 152 | { |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 153 | outputSize *= outputDims->data[dim]; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 154 | } |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 155 | |
| 156 | std::cout << m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->name << ": "; |
| 157 | results.push_back(m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->allocation); |
| 158 | |
| 159 | switch (m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->type) |
| 160 | { |
| 161 | |
| 162 | case kTfLiteFloat32: |
| 163 | { |
| 164 | auto tfLiteDelegateOutputData = m_TfLiteInterpreter->typed_tensor<float>( |
| 165 | tfLiteDelegateOutputId); |
| 166 | |
| 167 | for (int i = 0; i < outputSize; ++i) |
| 168 | { |
| 169 | fprintf(outputTensorFile, "%f ", tfLiteDelegateOutputData[i]); |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | case kTfLiteInt32: |
| 174 | { |
| 175 | auto tfLiteDelegateOutputData = m_TfLiteInterpreter->typed_tensor<int32_t>( |
| 176 | tfLiteDelegateOutputId); |
| 177 | for (int i = 0; i < outputSize; ++i) |
| 178 | { |
| 179 | fprintf(outputTensorFile, "%d ", tfLiteDelegateOutputData[i]); |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | case kTfLiteUInt8: |
| 184 | { |
| 185 | auto tfLiteDelegateOutputData = m_TfLiteInterpreter->typed_tensor<uint8_t>( |
| 186 | tfLiteDelegateOutputId); |
| 187 | for (int i = 0; i < outputSize; ++i) |
| 188 | { |
| 189 | fprintf(outputTensorFile, "%u ", tfLiteDelegateOutputData[i]); |
| 190 | } |
| 191 | break; |
| 192 | } |
| 193 | case kTfLiteInt8: |
| 194 | { |
| 195 | auto tfLiteDelegateOutputData = m_TfLiteInterpreter->typed_tensor<int8_t>( |
| 196 | tfLiteDelegateOutputId); |
| 197 | for (int i = 0; i < outputSize; ++i) |
| 198 | { |
| 199 | fprintf(outputTensorFile, "%d ", tfLiteDelegateOutputData[i]); |
| 200 | } |
| 201 | break; |
| 202 | } |
Ryan OShea | 39831da | 2023-01-26 17:43:45 +0000 | [diff] [blame] | 203 | case kTfLiteBool: |
| 204 | { |
| 205 | auto tfLiteDelegateOutputData = m_TfLiteInterpreter->typed_tensor<bool>( |
| 206 | tfLiteDelegateOutputId); |
| 207 | for (int i = 0; i < outputSize; ++i) { |
| 208 | fprintf(outputTensorFile, "%u ", tfLiteDelegateOutputData[i]); |
| 209 | } |
| 210 | break; |
| 211 | } |
Kevin May | 2fef6f6 | 2022-11-14 17:07:49 +0000 | [diff] [blame] | 212 | default: |
| 213 | { |
| 214 | LogAndThrow("Unsupported output type"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | std::cout << std::endl; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 219 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 220 | } |
| 221 | CheckInferenceTimeThreshold(duration, m_Params.m_ThresholdTime); |
| 222 | } |
| 223 | |
| 224 | std::cout << status; |
| 225 | return results; |
| 226 | } |
| 227 | |
| 228 | void TfLiteExecutor::CompareAndPrintResult(std::vector<const void*> otherOutput) |
| 229 | { |
| 230 | for (unsigned int outputIndex = 0; outputIndex < m_TfLiteInterpreter->outputs().size(); ++outputIndex) |
| 231 | { |
| 232 | auto tfLiteDelegateOutputId = m_TfLiteInterpreter->outputs()[outputIndex]; |
Colm Donelan | d047262 | 2023-03-06 12:34:54 +0000 | [diff] [blame] | 233 | size_t size = m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->bytes; |
| 234 | double result = ComputeByteLevelRMSE(m_TfLiteInterpreter->tensor(tfLiteDelegateOutputId)->allocation, |
| 235 | otherOutput[outputIndex], size); |
| 236 | std::cout << "Byte level root mean square error: " << result << "\n"; |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 237 | } |
| 238 | }; |