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