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