alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 "hal.h" /* Brings in platform definitions. */ |
| 18 | #include "TestModel.hpp" /* Model class for running inference. */ |
| 19 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 20 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 21 | #include "log_macros.h" /* Logging functions */ |
| 22 | #include "BufAttributes.hpp" /* Buffer attributes to be applied */ |
| 23 | |
| 24 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 25 | namespace app { |
| 26 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
| 27 | namespace inference_runner { |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 28 | #if defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) |
| 29 | |
| 30 | static uint8_t* GetModelPointer() |
| 31 | { |
| 32 | info("Model pointer: 0x%08x\n", DYNAMIC_MODEL_BASE); |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 33 | return reinterpret_cast<uint8_t*>(DYNAMIC_MODEL_BASE); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static size_t GetModelLen() |
| 37 | { |
| 38 | /* TODO: Can we get the actual model size here somehow? |
| 39 | * Currently we return the reserved space. It is possible to do |
| 40 | * so by reading the memory pattern but it will not be reliable. */ |
| 41 | return static_cast<size_t>(DYNAMIC_MODEL_SIZE); |
| 42 | } |
| 43 | |
| 44 | #else /* defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) */ |
| 45 | |
| 46 | extern uint8_t* GetModelPointer(); |
| 47 | extern size_t GetModelLen(); |
| 48 | |
| 49 | #endif /* defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) */ |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 50 | } /* namespace inference_runner */ |
| 51 | } /* namespace app */ |
| 52 | } /* namespace arm */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | |
| 54 | enum opcodes |
| 55 | { |
| 56 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 57 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 58 | }; |
| 59 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 60 | void main_loop() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | { |
| 62 | arm::app::TestModel model; /* Model wrapper object. */ |
| 63 | |
| 64 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 65 | if (!model.Init(arm::app::tensorArena, |
| 66 | sizeof(arm::app::tensorArena), |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 67 | arm::app::inference_runner::GetModelPointer(), |
| 68 | arm::app::inference_runner::GetModelLen())) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | printf_err("Failed to initialise model\n"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /* Instantiate application context. */ |
| 74 | arm::app::ApplicationContext caseContext; |
| 75 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 76 | arm::app::Profiler profiler{"inference_runner"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 77 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | caseContext.Set<arm::app::Model&>("model", model); |
| 79 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 80 | |
| 81 | /* Loop. */ |
| 82 | if (RunInferenceHandler(caseContext)) { |
| 83 | info("Inference completed.\n"); |
| 84 | } else { |
| 85 | printf_err("Inference failed.\n"); |
| 86 | } |
| 87 | } |