alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace arm { |
| 26 | namespace app { |
| 27 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 28 | static void PopulateInputTensor(const Model& model) |
| 29 | { |
| 30 | const size_t numInputs = model.GetNumInputs(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 31 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 32 | #if defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) |
| 33 | size_t curInputIdx = 0; |
| 34 | #endif /* defined(DYNAMIC_IFM_BASE) && defined(DYNAMIC_IFM_SIZE) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 36 | /* Populate each input tensor with random data. */ |
| 37 | for (size_t inputIndex = 0; inputIndex < numInputs; inputIndex++) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 39 | TfLiteTensor* inputTensor = model.GetInputTensor(inputIndex); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 40 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 41 | debug("Populating input tensor %zu@%p\n", inputIndex, inputTensor); |
| 42 | debug("Total input size to be populated: %zu\n", inputTensor->bytes); |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 43 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 44 | if (inputTensor->bytes > 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 46 | uint8_t* tData = tflite::GetTensorData<uint8_t>(inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 48 | #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; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 52 | } |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 53 | 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) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | } |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 63 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 65 | #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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 71 | #if defined (DYNAMIC_OFM_BASE) && defined(DYNAMIC_OFM_SIZE) |
| 72 | static 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); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 79 | 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; |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 90 | } |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 91 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 93 | 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 Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 97 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | #if VERIFY_TEST_OUTPUT |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 99 | static 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 | |
| 107 | static 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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | #endif /* VERIFY_TEST_OUTPUT */ |
| 115 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 116 | bool 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; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Kshitij Sisodia | aa5e1f6 | 2021-09-24 14:42:08 +0100 | [diff] [blame] | 130 | #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"); |
| 160 | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 173 | } /* namespace app */ |
| 174 | } /* namespace arm */ |