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