blob: 78d99b0da23cda5fcdfb1c22a4d68432d52488fd [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"
22
23#include <cstdlib>
24
25namespace arm {
26namespace app {
27
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010028static void PopulateInputTensor(const Model& model)
29{
30 const size_t numInputs = model.GetNumInputs();
alexander3c798932021-03-26 21:42:19 +000031
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010032#if defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE)
33 size_t curInputIdx = 0;
34#endif /* defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) */
alexander3c798932021-03-26 21:42:19 +000035
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010036 /* Populate each input tensor with random data. */
37 for (size_t inputIndex = 0; inputIndex < numInputs; inputIndex++) {
alexander3c798932021-03-26 21:42:19 +000038
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010039 TfLiteTensor* inputTensor = model.GetInputTensor(inputIndex);
alexander3c798932021-03-26 21:42:19 +000040
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010041 debug("Populating input tensor %zu@%p\n", inputIndex, inputTensor);
42 debug("Total input size to be populated: %zu\n", inputTensor->bytes);
alexander80eecfb2021-07-06 19:47:59 +010043
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010044 if (inputTensor->bytes > 0) {
alexander3c798932021-03-26 21:42:19 +000045
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010046 uint8_t* tData = tflite::GetTensorData<uint8_t>(inputTensor);
alexander3c798932021-03-26 21:42:19 +000047
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010048#if defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE)
49 if (curInputIdx + inputTensor->bytes > DYNAMIC_IFM_SIZE) {
50 printf_err("IFM reserved buffer size insufficient\n");
51 return;
alexander3c798932021-03-26 21:42:19 +000052 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010053 memcpy(tData, reinterpret_cast<void *>(DYNAMIC_IFM_BASE + curInputIdx),
54 inputTensor->bytes);
55 curInputIdx += inputTensor->bytes;
56#else /* defined(DYNAMIC_IFM_BASE) */
57 /* Create a random input. */
58 for (size_t j = 0; j < inputTensor->bytes; ++j) {
59 tData[j] = static_cast<uint8_t>(std::rand() & 0xFF);
60 }
61#endif /* defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) */
alexander3c798932021-03-26 21:42:19 +000062 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010063 }
alexander3c798932021-03-26 21:42:19 +000064
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010065#if defined(DYNAMIC_IFM_BASE)
66 info("%d input tensor/s populated with %d bytes with data read from 0x%08x\n",
67 numInputs, curInputIdx, DYNAMIC_IFM_BASE);
68#endif /* defined(DYNAMIC_IFM_BASE) */
69}
alexander3c798932021-03-26 21:42:19 +000070
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010071#if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE)
72static void PopulateDynamicOfm(const Model& model)
73{
74 /* Dump the output to a known memory location */
75 const size_t numOutputs = model.GetNumOutputs();
76 size_t curCopyIdx = 0;
77 uint8_t* const dstPtr = reinterpret_cast<uint8_t *>(DYNAMIC_OFM_BASE);
alexander3c798932021-03-26 21:42:19 +000078
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010079 for (size_t outputIdx = 0; outputIdx < numOutputs; ++outputIdx) {
80 TfLiteTensor* outputTensor = model.GetOutputTensor(outputIdx);
81 uint8_t* const tData = tflite::GetTensorData<uint8_t>(outputTensor);
82
83 if (tData && outputTensor->bytes > 0) {
84 if (curCopyIdx + outputTensor->bytes > DYNAMIC_OFM_SIZE) {
85 printf_err("OFM reserved buffer size insufficient\n");
86 return;
87 }
88 memcpy(dstPtr + curCopyIdx, tData, outputTensor->bytes);
89 curCopyIdx += outputTensor->bytes;
alexander27b62d92021-05-04 20:46:08 +010090 }
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010091 }
alexander3c798932021-03-26 21:42:19 +000092
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010093 info("%d output tensor/s worth %d bytes copied to 0x%08x\n",
94 numOutputs, curCopyIdx, DYNAMIC_OFM_BASE);
95}
96#endif /* defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010097
alexander3c798932021-03-26 21:42:19 +000098#if VERIFY_TEST_OUTPUT
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010099static void DumpInputs(const Model& model, const char* message)
100{
101 info("%s\n", message);
102 for (size_t inputIndex = 0; inputIndex < model.GetNumInputs(); inputIndex++) {
103 arm::app::DumpTensor(model.GetInputTensor(inputIndex));
104 }
105}
106
107static void DumpOutputs(const Model& model, const char* message)
108{
109 info("%s\n", message);
110 for (size_t outputIndex = 0; outputIndex < model.GetNumOutputs(); outputIndex++) {
111 arm::app::DumpTensor(model.GetOutputTensor(outputIndex));
112 }
113}
alexander3c798932021-03-26 21:42:19 +0000114#endif /* VERIFY_TEST_OUTPUT */
115
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100116bool RunInferenceHandler(ApplicationContext& ctx)
117{
118 auto& platform = ctx.Get<hal_platform&>("platform");
119 auto& profiler = ctx.Get<Profiler&>("profiler");
120 auto& model = ctx.Get<Model&>("model");
121
122 constexpr uint32_t dataPsnTxtInfStartX = 150;
123 constexpr uint32_t dataPsnTxtInfStartY = 40;
124
125 if (!model.IsInited()) {
126 printf_err("Model is not initialised! Terminating processing.\n");
127 return false;
alexander3c798932021-03-26 21:42:19 +0000128 }
129
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100130#if VERIFY_TEST_OUTPUT
131 DumpInputs(model, "Initial input tensors values");
132 DumpOutputs(model, "Initial output tensors values");
133#endif /* VERIFY_TEST_OUTPUT */
134
135 PopulateInputTensor(model);
136
137#if VERIFY_TEST_OUTPUT
138 DumpInputs(model, "input tensors populated");
139#endif /* VERIFY_TEST_OUTPUT */
140
141 /* Strings for presentation/logging. */
142 std::string str_inf{"Running inference... "};
143
144 /* Display message on the LCD - inference running. */
145 platform.data_psn->present_data_text(
146 str_inf.c_str(), str_inf.size(),
147 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
148
149 if (!RunInference(model, profiler)) {
150 return false;
151 }
152
153 /* Erase. */
154 str_inf = std::string(str_inf.size(), ' ');
155 platform.data_psn->present_data_text(
156 str_inf.c_str(), str_inf.size(),
157 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
158
159 info("Final results:\n");
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +0000160 info("Total number of inferences: 1\n");
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100161 profiler.PrintProfilingResult();
162
163#if VERIFY_TEST_OUTPUT
164 DumpOutputs(model, "output tensors post inference");
165#endif /* VERIFY_TEST_OUTPUT */
166
167#if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE)
168 PopulateDynamicOfm(model);
169#endif /* defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) */
170
171 return true;
172}
173
alexander3c798932021-03-26 21:42:19 +0000174} /* namespace app */
175} /* namespace arm */