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