blob: 0991b7b5cf4fcb0b52cc34990ead3cba358c9955 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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 "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 Sisodiaaa4bcb12022-05-06 09:13:03 +010021#include "log_macros.h" /* Logging functions */
22#include "BufAttributes.hpp" /* Buffer attributes to be applied */
23
24namespace arm {
25 namespace app {
26 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
27 } /* namespace app */
28} /* namespace arm */
29
30#if defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE)
31
32static uint8_t* GetModelPointer()
33{
34 info("Model pointer: 0x%08x\n", DYNAMIC_MODEL_BASE);
35 return reinterpret_cast<uint8_t *>(DYNAMIC_MODEL_BASE);
36}
37
38static size_t GetModelLen()
39{
40 /* TODO: Can we get the actual model size here somehow?
41 * Currently we return the reserved space. It is possible to do
42 * so by reading the memory pattern but it will not be reliable. */
43 return static_cast<size_t>(DYNAMIC_MODEL_SIZE);
44}
45
46#else /* defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) */
47
48extern uint8_t* GetModelPointer();
49extern size_t GetModelLen();
50
51#endif /* defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) */
alexander3c798932021-03-26 21:42:19 +000052
53enum opcodes
54{
55 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
56 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
57};
58
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010059void main_loop()
alexander3c798932021-03-26 21:42:19 +000060{
61 arm::app::TestModel model; /* Model wrapper object. */
62
63 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010064 if (!model.Init(arm::app::tensorArena,
65 sizeof(arm::app::tensorArena),
66 GetModelPointer(),
67 GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000068 printf_err("Failed to initialise model\n");
69 return;
70 }
71
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010072#if !defined(ARM_NPU)
73 /* If it is not a NPU build check if the model contains a NPU operator */
74 if (model.ContainsEthosUOperator()) {
75 printf_err("No driver support for Ethos-U operator found in the model.\n");
76 return;
77 }
78#endif /* ARM_NPU */
79
alexander3c798932021-03-26 21:42:19 +000080 /* Instantiate application context. */
81 arm::app::ApplicationContext caseContext;
82
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010083 arm::app::Profiler profiler{"inference_runner"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010084 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000085 caseContext.Set<arm::app::Model&>("model", model);
86 caseContext.Set<uint32_t>("imgIndex", 0);
87
88 /* Loop. */
89 if (RunInferenceHandler(caseContext)) {
90 info("Inference completed.\n");
91 } else {
92 printf_err("Inference failed.\n");
93 }
94}