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