blob: 59afa636961ae19e4e7a8c1ddb96b2510b853a0c [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 {
Liam Barry213a5432022-05-09 17:06:19 +010025namespace app {
26 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
27 namespace inference_runner {
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010028#if defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE)
29
30static uint8_t* GetModelPointer()
31{
32 info("Model pointer: 0x%08x\n", DYNAMIC_MODEL_BASE);
Liam Barry213a5432022-05-09 17:06:19 +010033 return reinterpret_cast<uint8_t*>(DYNAMIC_MODEL_BASE);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010034}
35
36static 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
46extern uint8_t* GetModelPointer();
47extern size_t GetModelLen();
48
49#endif /* defined(DYNAMIC_MODEL_BASE) && defined(DYNAMIC_MODEL_SIZE) */
Liam Barry213a5432022-05-09 17:06:19 +010050 } /* namespace inference_runner */
51} /* namespace app */
52} /* namespace arm */
alexander3c798932021-03-26 21:42:19 +000053
54enum 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 Sisodia4cc40212022-04-08 09:54:53 +010060void main_loop()
alexander3c798932021-03-26 21:42:19 +000061{
62 arm::app::TestModel model; /* Model wrapper object. */
63
64 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010065 if (!model.Init(arm::app::tensorArena,
66 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010067 arm::app::inference_runner::GetModelPointer(),
68 arm::app::inference_runner::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000069 printf_err("Failed to initialise model\n");
70 return;
71 }
72
73 /* Instantiate application context. */
74 arm::app::ApplicationContext caseContext;
75
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010076 arm::app::Profiler profiler{"inference_runner"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010077 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000078 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}