blob: 11258305fb4af85a9655b7b92c6893e5dcd41605 [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 "UseCaseHandler.hpp"
18
19#include "TestModel.hpp"
20#include "UseCaseCommonUtils.hpp"
21#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000022#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000023
24#include <cstdlib>
25
26namespace arm {
27namespace app {
28
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010029static void PopulateInputTensor(const Model& model)
30{
31 const size_t numInputs = model.GetNumInputs();
alexander3c798932021-03-26 21:42:19 +000032
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010033#if defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE)
34 size_t curInputIdx = 0;
35#endif /* defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) */
alexander3c798932021-03-26 21:42:19 +000036
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010037 /* Populate each input tensor with random data. */
38 for (size_t inputIndex = 0; inputIndex < numInputs; inputIndex++) {
alexander3c798932021-03-26 21:42:19 +000039
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010040 TfLiteTensor* inputTensor = model.GetInputTensor(inputIndex);
alexander3c798932021-03-26 21:42:19 +000041
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010042 debug("Populating input tensor %zu@%p\n", inputIndex, inputTensor);
43 debug("Total input size to be populated: %zu\n", inputTensor->bytes);
alexander80eecfb2021-07-06 19:47:59 +010044
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010045 if (inputTensor->bytes > 0) {
alexander3c798932021-03-26 21:42:19 +000046
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010047 uint8_t* tData = tflite::GetTensorData<uint8_t>(inputTensor);
alexander3c798932021-03-26 21:42:19 +000048
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010049#if defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE)
50 if (curInputIdx + inputTensor->bytes > DYNAMIC_IFM_SIZE) {
51 printf_err("IFM reserved buffer size insufficient\n");
52 return;
alexander3c798932021-03-26 21:42:19 +000053 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010054 memcpy(tData, reinterpret_cast<void *>(DYNAMIC_IFM_BASE + curInputIdx),
55 inputTensor->bytes);
56 curInputIdx += inputTensor->bytes;
57#else /* defined(DYNAMIC_IFM_BASE) */
58 /* Create a random input. */
59 for (size_t j = 0; j < inputTensor->bytes; ++j) {
60 tData[j] = static_cast<uint8_t>(std::rand() & 0xFF);
61 }
62#endif /* defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) */
alexander3c798932021-03-26 21:42:19 +000063 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010064 }
alexander3c798932021-03-26 21:42:19 +000065
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010066#if defined(DYNAMIC_IFM_BASE)
67 info("%d input tensor/s populated with %d bytes with data read from 0x%08x\n",
68 numInputs, curInputIdx, DYNAMIC_IFM_BASE);
69#endif /* defined(DYNAMIC_IFM_BASE) */
70}
alexander3c798932021-03-26 21:42:19 +000071
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010072#if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE)
73static void PopulateDynamicOfm(const Model& model)
74{
75 /* Dump the output to a known memory location */
76 const size_t numOutputs = model.GetNumOutputs();
77 size_t curCopyIdx = 0;
78 uint8_t* const dstPtr = reinterpret_cast<uint8_t *>(DYNAMIC_OFM_BASE);
alexander3c798932021-03-26 21:42:19 +000079
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010080 for (size_t outputIdx = 0; outputIdx < numOutputs; ++outputIdx) {
81 TfLiteTensor* outputTensor = model.GetOutputTensor(outputIdx);
82 uint8_t* const tData = tflite::GetTensorData<uint8_t>(outputTensor);
83
84 if (tData && outputTensor->bytes > 0) {
85 if (curCopyIdx + outputTensor->bytes > DYNAMIC_OFM_SIZE) {
86 printf_err("OFM reserved buffer size insufficient\n");
87 return;
88 }
89 memcpy(dstPtr + curCopyIdx, tData, outputTensor->bytes);
90 curCopyIdx += outputTensor->bytes;
alexander27b62d92021-05-04 20:46:08 +010091 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010092 }
alexander3c798932021-03-26 21:42:19 +000093
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010094 info("%d output tensor/s worth %d bytes copied to 0x%08x\n",
95 numOutputs, curCopyIdx, DYNAMIC_OFM_BASE);
96}
97#endif /* defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010098
alexander3c798932021-03-26 21:42:19 +000099#if VERIFY_TEST_OUTPUT
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100100static void DumpInputs(const Model& model, const char* message)
101{
102 info("%s\n", message);
103 for (size_t inputIndex = 0; inputIndex < model.GetNumInputs(); inputIndex++) {
104 arm::app::DumpTensor(model.GetInputTensor(inputIndex));
105 }
106}
107
108static void DumpOutputs(const Model& model, const char* message)
109{
110 info("%s\n", message);
111 for (size_t outputIndex = 0; outputIndex < model.GetNumOutputs(); outputIndex++) {
112 arm::app::DumpTensor(model.GetOutputTensor(outputIndex));
113 }
114}
alexander3c798932021-03-26 21:42:19 +0000115#endif /* VERIFY_TEST_OUTPUT */
116
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100117bool RunInferenceHandler(ApplicationContext& ctx)
118{
119 auto& platform = ctx.Get<hal_platform&>("platform");
120 auto& profiler = ctx.Get<Profiler&>("profiler");
121 auto& model = ctx.Get<Model&>("model");
122
123 constexpr uint32_t dataPsnTxtInfStartX = 150;
124 constexpr uint32_t dataPsnTxtInfStartY = 40;
125
126 if (!model.IsInited()) {
127 printf_err("Model is not initialised! Terminating processing.\n");
128 return false;
alexander3c798932021-03-26 21:42:19 +0000129 }
130
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100131#if VERIFY_TEST_OUTPUT
132 DumpInputs(model, "Initial input tensors values");
133 DumpOutputs(model, "Initial output tensors values");
134#endif /* VERIFY_TEST_OUTPUT */
135
136 PopulateInputTensor(model);
137
138#if VERIFY_TEST_OUTPUT
139 DumpInputs(model, "input tensors populated");
140#endif /* VERIFY_TEST_OUTPUT */
141
142 /* Strings for presentation/logging. */
143 std::string str_inf{"Running inference... "};
144
145 /* Display message on the LCD - inference running. */
146 platform.data_psn->present_data_text(
147 str_inf.c_str(), str_inf.size(),
148 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
149
150 if (!RunInference(model, profiler)) {
151 return false;
152 }
153
154 /* Erase. */
155 str_inf = std::string(str_inf.size(), ' ');
156 platform.data_psn->present_data_text(
157 str_inf.c_str(), str_inf.size(),
158 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
159
160 info("Final results:\n");
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +0000161 info("Total number of inferences: 1\n");
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100162 profiler.PrintProfilingResult();
163
164#if VERIFY_TEST_OUTPUT
165 DumpOutputs(model, "output tensors post inference");
166#endif /* VERIFY_TEST_OUTPUT */
167
168#if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE)
169 PopulateDynamicOfm(model);
170#endif /* defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) */
171
172 return true;
173}
174
alexander3c798932021-03-26 21:42:19 +0000175} /* namespace app */
176} /* namespace arm */