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