alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "Model.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 18 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 20 | #include <cinttypes> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
| 22 | /* Initialise the model */ |
| 23 | arm::app::Model::~Model() |
| 24 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 25 | delete this->m_pInterpreter; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | /** |
| 27 | * No clean-up function available for allocator in TensorFlow Lite Micro yet. |
| 28 | **/ |
| 29 | } |
| 30 | |
| 31 | arm::app::Model::Model() : |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 32 | m_inited (false), |
| 33 | m_type(kTfLiteNoType) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 34 | { |
Kshitij Sisodia | dd6d07b | 2022-05-03 10:10:14 +0100 | [diff] [blame] | 35 | this->m_pErrorReporter = tflite::GetMicroErrorReporter(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 38 | bool arm::app::Model::Init(uint8_t* tensorArenaAddr, |
| 39 | uint32_t tensorArenaSize, |
Kshitij Sisodia | 937052d | 2022-05-13 16:44:16 +0100 | [diff] [blame] | 40 | const uint8_t* nnModelAddr, |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 41 | uint32_t nnModelSize, |
| 42 | tflite::MicroAllocator* allocator) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | { |
| 44 | /* Following tf lite micro example: |
| 45 | * Map the model into a usable data structure. This doesn't involve any |
| 46 | * copying or parsing, it's a very lightweight operation. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 47 | debug("loading model from @ 0x%p\n", nnModelAddr); |
| 48 | debug("model size: %" PRIu32 " bytes.\n", nnModelSize); |
| 49 | |
| 50 | this->m_pModel = ::tflite::GetModel(nnModelAddr); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 52 | if (this->m_pModel->version() != TFLITE_SCHEMA_VERSION) { |
| 53 | this->m_pErrorReporter->Report( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 54 | "[ERROR] model's schema version %d is not equal " |
| 55 | "to supported version %d.", |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 56 | this->m_pModel->version(), TFLITE_SCHEMA_VERSION); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 60 | this->m_modelAddr = nnModelAddr; |
| 61 | this->m_modelSize = nnModelSize; |
| 62 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | /* Pull in only the operation implementations we need. |
| 64 | * This relies on a complete list of all the ops needed by this graph. |
| 65 | * An easier approach is to just use the AllOpsResolver, but this will |
| 66 | * incur some penalty in code space for op implementations that are not |
| 67 | * needed by this graph. |
| 68 | * static ::tflite::ops::micro::AllOpsResolver resolver; */ |
| 69 | /* NOLINTNEXTLINE(runtime-global-variables) */ |
| 70 | debug("loading op resolver\n"); |
| 71 | |
| 72 | this->EnlistOperations(); |
| 73 | |
| 74 | /* Create allocator instance, if it doesn't exist */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 75 | this->m_pAllocator = allocator; |
| 76 | if (!this->m_pAllocator) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | /* Create an allocator instance */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 78 | info("Creating allocator using tensor arena at 0x%p\n", tensorArenaAddr); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 80 | this->m_pAllocator = tflite::MicroAllocator::Create( |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 81 | tensorArenaAddr, |
| 82 | tensorArenaSize, |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 83 | this->m_pErrorReporter); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 85 | if (!this->m_pAllocator) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | printf_err("Failed to create allocator\n"); |
| 87 | return false; |
| 88 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 89 | debug("Created new allocator @ 0x%p\n", this->m_pAllocator); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | } else { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 91 | debug("Using existing allocator @ 0x%p\n", this->m_pAllocator); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 94 | this->m_pInterpreter = new ::tflite::MicroInterpreter( |
| 95 | this->m_pModel, this->GetOpResolver(), |
| 96 | this->m_pAllocator, this->m_pErrorReporter); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 98 | if (!this->m_pInterpreter) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | printf_err("Failed to allocate interpreter\n"); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /* Allocate memory from the tensor_arena for the model's tensors. */ |
| 104 | info("Allocating tensors\n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 105 | TfLiteStatus allocate_status = this->m_pInterpreter->AllocateTensors(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 106 | |
| 107 | if (allocate_status != kTfLiteOk) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | printf_err("tensor allocation failed!\n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 109 | delete this->m_pInterpreter; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /* Get information about the memory area to use for the model's input. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 114 | this->m_input.resize(this->GetNumInputs()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 115 | for (size_t inIndex = 0; inIndex < this->GetNumInputs(); inIndex++) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 116 | this->m_input[inIndex] = this->m_pInterpreter->input(inIndex); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 118 | this->m_output.resize(this->GetNumOutputs()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | for (size_t outIndex = 0; outIndex < this->GetNumOutputs(); outIndex++) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 120 | this->m_output[outIndex] = this->m_pInterpreter->output(outIndex); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 121 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 122 | if (this->m_input.empty() || this->m_output.empty()) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 123 | printf_err("failed to get tensors\n"); |
| 124 | return false; |
| 125 | } else { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 126 | this->m_type = this->m_input[0]->type; /* Input 0 should be the main input */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | |
| 128 | /* Clear the input & output tensors */ |
| 129 | for (size_t inIndex = 0; inIndex < this->GetNumInputs(); inIndex++) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 130 | std::memset(this->m_input[inIndex]->data.data, 0, this->m_input[inIndex]->bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | } |
| 132 | for (size_t outIndex = 0; outIndex < this->GetNumOutputs(); outIndex++) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 133 | std::memset(this->m_output[outIndex]->data.data, 0, this->m_output[outIndex]->bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | this->LogInterpreterInfo(); |
| 137 | } |
| 138 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 139 | this->m_inited = true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | tflite::MicroAllocator* arm::app::Model::GetAllocator() |
| 144 | { |
| 145 | if (this->IsInited()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 146 | return this->m_pAllocator; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 147 | } |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
| 151 | void arm::app::Model::LogTensorInfo(TfLiteTensor* tensor) |
| 152 | { |
| 153 | if (!tensor) { |
| 154 | printf_err("Invalid tensor\n"); |
| 155 | assert(tensor); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | debug("\ttensor is assigned to 0x%p\n", tensor); |
| 160 | info("\ttensor type is %s\n", TfLiteTypeGetName(tensor->type)); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 161 | info("\ttensor occupies %zu bytes with dimensions\n", |
| 162 | tensor->bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 163 | for (int i = 0 ; i < tensor->dims->size; ++i) { |
| 164 | info ("\t\t%d: %3d\n", i, tensor->dims->data[i]); |
| 165 | } |
| 166 | |
| 167 | TfLiteQuantization quant = tensor->quantization; |
| 168 | if (kTfLiteAffineQuantization == quant.type) { |
| 169 | auto* quantParams = (TfLiteAffineQuantization*)quant.params; |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 170 | info("Quant dimension: %" PRIi32 "\n", quantParams->quantized_dimension); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | for (int i = 0; i < quantParams->scale->size; ++i) { |
| 172 | info("Scale[%d] = %f\n", i, quantParams->scale->data[i]); |
| 173 | } |
| 174 | for (int i = 0; i < quantParams->zero_point->size; ++i) { |
| 175 | info("ZeroPoint[%d] = %d\n", i, quantParams->zero_point->data[i]); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void arm::app::Model::LogInterpreterInfo() |
| 181 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 182 | if (!this->m_pInterpreter) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | printf_err("Invalid interpreter\n"); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | info("Model INPUT tensors: \n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 188 | for (auto input : this->m_input) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 189 | this->LogTensorInfo(input); |
| 190 | } |
| 191 | |
| 192 | info("Model OUTPUT tensors: \n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 193 | for (auto output : this->m_output) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 194 | this->LogTensorInfo(output); |
| 195 | } |
| 196 | |
| 197 | info("Activation buffer (a.k.a tensor arena) size used: %zu\n", |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 198 | this->m_pInterpreter->arena_used_bytes()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 199 | |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 200 | /* We expect there to be only one subgraph. */ |
| 201 | const uint32_t nOperators = tflite::NumSubgraphOperators(this->m_pModel, 0); |
| 202 | info("Number of operators: %" PRIu32 "\n", nOperators); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 203 | |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 204 | const tflite::SubGraph* subgraph = this->m_pModel->subgraphs()->Get(0); |
| 205 | |
| 206 | auto* opcodes = this->m_pModel->operator_codes(); |
| 207 | |
| 208 | /* For each operator, display registration information. */ |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 209 | for (size_t i = 0 ; i < nOperators; ++i) { |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 210 | const tflite::Operator* op = subgraph->operators()->Get(i); |
| 211 | const tflite::OperatorCode* opcode = opcodes->Get(op->opcode_index()); |
| 212 | const TfLiteRegistration* reg = nullptr; |
| 213 | |
| 214 | tflite::GetRegistrationFromOpCode(opcode, this->GetOpResolver(), |
| 215 | this->m_pErrorReporter, ®); |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 216 | std::string opName; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 217 | |
| 218 | if (reg) { |
| 219 | if (tflite::BuiltinOperator_CUSTOM == reg->builtin_code) { |
| 220 | opName = std::string(reg->custom_name); |
| 221 | } else { |
| 222 | opName = std::string(EnumNameBuiltinOperator( |
| 223 | tflite::BuiltinOperator(reg->builtin_code))); |
| 224 | } |
| 225 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 226 | info("\tOperator %zu: %s\n", i, opName.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | bool arm::app::Model::IsInited() const |
| 231 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 232 | return this->m_inited; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool arm::app::Model::IsDataSigned() const |
| 236 | { |
| 237 | return this->GetType() == kTfLiteInt8; |
| 238 | } |
| 239 | |
Cisco Cervellera | 0210109 | 2021-09-07 11:34:43 +0100 | [diff] [blame] | 240 | bool arm::app::Model::ContainsEthosUOperator() const |
| 241 | { |
| 242 | /* We expect there to be only one subgraph. */ |
| 243 | const uint32_t nOperators = tflite::NumSubgraphOperators(this->m_pModel, 0); |
| 244 | const tflite::SubGraph* subgraph = this->m_pModel->subgraphs()->Get(0); |
| 245 | const auto* opcodes = this->m_pModel->operator_codes(); |
| 246 | |
| 247 | /* check for custom operators */ |
| 248 | for (size_t i = 0; (i < nOperators); ++i) |
| 249 | { |
| 250 | const tflite::Operator* op = subgraph->operators()->Get(i); |
| 251 | const tflite::OperatorCode* opcode = opcodes->Get(op->opcode_index()); |
| 252 | |
| 253 | auto builtin_code = tflite::GetBuiltinCode(opcode); |
| 254 | if ((builtin_code == tflite::BuiltinOperator_CUSTOM) && |
| 255 | ( nullptr != opcode->custom_code()) && |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 256 | ( "ethos-u" == std::string(opcode->custom_code()->c_str()))) |
Cisco Cervellera | 0210109 | 2021-09-07 11:34:43 +0100 | [diff] [blame] | 257 | { |
| 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 264 | bool arm::app::Model::RunInference() |
| 265 | { |
| 266 | bool inference_state = false; |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 267 | if (this->m_pModel && this->m_pInterpreter) { |
| 268 | if (kTfLiteOk != this->m_pInterpreter->Invoke()) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 269 | printf_err("Invoke failed.\n"); |
| 270 | } else { |
| 271 | inference_state = true; |
| 272 | } |
| 273 | } else { |
| 274 | printf_err("Error: No interpreter!\n"); |
| 275 | } |
| 276 | return inference_state; |
| 277 | } |
| 278 | |
| 279 | TfLiteTensor* arm::app::Model::GetInputTensor(size_t index) const |
| 280 | { |
| 281 | if (index < this->GetNumInputs()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 282 | return this->m_input.at(index); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 283 | } |
| 284 | return nullptr; |
| 285 | } |
| 286 | |
| 287 | TfLiteTensor* arm::app::Model::GetOutputTensor(size_t index) const |
| 288 | { |
| 289 | if (index < this->GetNumOutputs()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 290 | return this->m_output.at(index); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 291 | } |
| 292 | return nullptr; |
| 293 | } |
| 294 | |
| 295 | size_t arm::app::Model::GetNumInputs() const |
| 296 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 297 | if (this->m_pModel && this->m_pInterpreter) { |
| 298 | return this->m_pInterpreter->inputs_size(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | size_t arm::app::Model::GetNumOutputs() const |
| 304 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 305 | if (this->m_pModel && this->m_pInterpreter) { |
| 306 | return this->m_pInterpreter->outputs_size(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | TfLiteType arm::app::Model::GetType() const |
| 313 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 314 | return this->m_type; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TfLiteIntArray* arm::app::Model::GetInputShape(size_t index) const |
| 318 | { |
| 319 | if (index < this->GetNumInputs()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 320 | return this->m_input.at(index)->dims; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 321 | } |
| 322 | return nullptr; |
| 323 | } |
| 324 | |
| 325 | TfLiteIntArray* arm::app::Model::GetOutputShape(size_t index) const |
| 326 | { |
| 327 | if (index < this->GetNumOutputs()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 328 | return this->m_output.at(index)->dims; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 329 | } |
| 330 | return nullptr; |
| 331 | } |
| 332 | |
| 333 | bool arm::app::Model::ShowModelInfoHandler() |
| 334 | { |
| 335 | if (!this->IsInited()) { |
| 336 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | PrintTensorFlowVersion(); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 341 | info("Model address: 0x%p", this->ModelPointer()); |
| 342 | info("Model size: %" PRIu32 " bytes.", this->ModelSize()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 343 | info("Model info:\n"); |
| 344 | this->LogInterpreterInfo(); |
| 345 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 346 | info("The model is optimised for Ethos-U NPU: %s.\n", this->ContainsEthosUOperator()? "yes": "no"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 347 | |
| 348 | return true; |
| 349 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 350 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 351 | const uint8_t* arm::app::Model::ModelPointer() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 352 | { |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 353 | return this->m_modelAddr; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 356 | uint32_t arm::app::Model::ModelSize() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 357 | { |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 358 | return this->m_modelSize; |
| 359 | } |