blob: c010f42b00b3a1fa6a3b5e5ee48b642aceac31b6 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100119 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. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100145 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
146 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100147
148 if (!RunInference(model, profiler)) {
149 return false;
150 }
151
152 /* Erase. */
153 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100154 hal_lcd_display_text(
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100155 str_inf.c_str(), str_inf.size(),
156 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
157
158 info("Final results:\n");
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +0000159 info("Total number of inferences: 1\n");
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100160 profiler.PrintProfilingResult();
161
162#if VERIFY_TEST_OUTPUT
163 DumpOutputs(model, "output tensors post inference");
164#endif /* VERIFY_TEST_OUTPUT */
165
166#if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE)
167 PopulateDynamicOfm(model);
168#endif /* defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) */
169
170 return true;
171}
172
alexander3c798932021-03-26 21:42:19 +0000173} /* namespace app */
174} /* namespace arm */