Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. All rights reserved. |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "tensorflow/lite/micro/all_ops_resolver.h" |
Måns Nilsson | 231e1d9 | 2020-11-05 12:19:34 +0100 | [diff] [blame] | 20 | #include "tensorflow/lite/micro/cortex_m_generic/debug_log_callback.h" |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 21 | #include "tensorflow/lite/micro/micro_error_reporter.h" |
| 22 | #include "tensorflow/lite/micro/micro_interpreter.h" |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 23 | #include "tensorflow/lite/micro/micro_profiler.h" |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 24 | #include "tensorflow/lite/micro/micro_time.h" |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 25 | #include "tensorflow/lite/schema/schema_generated.h" |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 26 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 27 | #include "arm_profiler.hpp" |
Kristofer Jonsson | 3bd3423 | 2021-08-30 13:55:55 +0200 | [diff] [blame] | 28 | #ifdef LAYER_BY_LAYER_PROFILER |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 29 | #include "layer_by_layer_profiler.hpp" |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 30 | #endif |
Jonny Svärd | 5adf5a6 | 2022-02-09 16:42:10 +0100 | [diff] [blame] | 31 | |
| 32 | #include "crc.hpp" |
| 33 | |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame] | 34 | #include "ethosu_log.h" |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 35 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 36 | #include "inference_process.hpp" |
| 37 | |
Per Åstrand | d9afc08 | 2020-10-06 13:25:08 +0200 | [diff] [blame] | 38 | #include "cmsis_compiler.h" |
| 39 | |
Per Åstrand | 91a9173 | 2020-09-25 15:04:26 +0200 | [diff] [blame] | 40 | #include <inttypes.h> |
| 41 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 42 | using namespace std; |
| 43 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 44 | namespace InferenceProcess { |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 45 | DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 46 | |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 47 | void DataPtr::invalidate() { |
| 48 | #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 49 | SCB_InvalidateDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | void DataPtr::clean() { |
| 54 | #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 55 | SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size); |
| 56 | #endif |
| 57 | } |
| 58 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 59 | char *DataPtr::begin() const { |
| 60 | return static_cast<char *>(data); |
| 61 | } |
| 62 | |
| 63 | char *DataPtr::end() const { |
| 64 | return static_cast<char *>(data) + size; |
| 65 | } |
| 66 | |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 67 | InferenceJob::InferenceJob() : numBytesToPrint(0), externalContext(nullptr) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 68 | |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 69 | InferenceJob::InferenceJob(const string &_name, |
| 70 | const DataPtr &_networkModel, |
| 71 | const vector<DataPtr> &_input, |
| 72 | const vector<DataPtr> &_output, |
| 73 | const vector<DataPtr> &_expectedOutput, |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 74 | const size_t _numBytesToPrint, |
| 75 | void *_externalContext) : |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 76 | name(_name), |
| 77 | networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput), |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 78 | numBytesToPrint(_numBytesToPrint), externalContext(_externalContext) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 79 | |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 80 | void InferenceJob::invalidate() { |
| 81 | networkModel.invalidate(); |
| 82 | |
| 83 | for (auto &it : input) { |
| 84 | it.invalidate(); |
| 85 | } |
| 86 | |
| 87 | for (auto &it : output) { |
| 88 | it.invalidate(); |
| 89 | } |
| 90 | |
| 91 | for (auto &it : expectedOutput) { |
| 92 | it.invalidate(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void InferenceJob::clean() { |
| 97 | networkModel.clean(); |
| 98 | |
| 99 | for (auto &it : input) { |
| 100 | it.clean(); |
| 101 | } |
| 102 | |
| 103 | for (auto &it : output) { |
| 104 | it.clean(); |
| 105 | } |
| 106 | |
| 107 | for (auto &it : expectedOutput) { |
| 108 | it.clean(); |
| 109 | } |
| 110 | } |
| 111 | |
Kristofer Jonsson | 40d886e | 2021-12-15 11:16:26 +0100 | [diff] [blame] | 112 | InferenceProcess::InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize) : |
| 113 | tensorArena(_tensorArena), tensorArenaSize(_tensorArenaSize) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 114 | |
| 115 | bool InferenceProcess::runJob(InferenceJob &job) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 116 | LOG_INFO("Running inference job: %s", job.name.c_str()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 117 | |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 118 | // Register debug log callback for profiling |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 119 | RegisterDebugLogCallback(tfluDebugLog); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 120 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 121 | // Get model handle and verify that the version is correct |
Davide Grohmann | 30b17b9 | 2022-06-14 15:17:18 +0200 | [diff] [blame^] | 122 | const tflite::Model *model = parser.getModel(job.networkModel.data, job.networkModel.size); |
| 123 | if (model == nullptr) { |
| 124 | LOG_ERR("Invalid model"); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 128 | // Create the TFL micro interpreter |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 129 | tflite::AllOpsResolver resolver; |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 130 | tflite::ArmProfiler profiler; |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 131 | tflite::MicroErrorReporter errorReporter; |
| 132 | tflite::MicroInterpreter interpreter( |
| 133 | model, resolver, tensorArena, tensorArenaSize, &errorReporter, nullptr, &profiler); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 134 | |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 135 | // Set external context |
Davide Grohmann | 165f00a | 2022-02-09 14:53:58 +0100 | [diff] [blame] | 136 | if (job.externalContext != nullptr) { |
| 137 | interpreter.SetMicroExternalContext(job.externalContext); |
| 138 | } |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 139 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 140 | // Allocate tensors |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 141 | TfLiteStatus status = interpreter.AllocateTensors(); |
| 142 | if (status != kTfLiteOk) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 143 | LOG_ERR("Failed to allocate tensors for inference: job=%s", job.name.c_str()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 147 | // Copy IFM data from job descriptor to TFLu arena |
| 148 | if (copyIfm(job, interpreter)) { |
| 149 | return true; |
| 150 | } |
| 151 | |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 152 | // Get the current cycle counter value |
| 153 | uint32_t cpuCyclesBegin = tflite::GetCurrentTimeTicks(); |
| 154 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 155 | // Run the inference |
| 156 | status = interpreter.Invoke(); |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 157 | |
| 158 | // Calculate nbr of CPU cycles for the Invoke call |
| 159 | job.cpuCycles = tflite::GetCurrentTimeTicks() - cpuCyclesBegin; |
| 160 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 161 | if (status != kTfLiteOk) { |
| 162 | LOG_ERR("Invoke failed for inference: job=%s", job.name.c_str()); |
| 163 | return true; |
| 164 | } |
| 165 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 166 | // Copy output data from TFLu arena to job descriptor |
| 167 | if (copyOfm(job, interpreter)) { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | printJob(job, interpreter); |
| 172 | |
| 173 | // Compare the OFM with the expected reference data |
| 174 | if (compareOfm(job, interpreter)) { |
| 175 | return true; |
| 176 | } |
| 177 | |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 178 | LOG_INFO("\n"); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 179 | LOG_INFO("Finished running job: %s", job.name.c_str()); |
| 180 | |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 181 | profiler.ReportResults(); |
| 182 | |
| 183 | LOG("\n"); |
| 184 | LOG("Operator(s) total: %" PRIu64 " CPU cycles\n\n", profiler.GetTotalTicks()); |
| 185 | |
| 186 | LOG("Inference runtime: %" PRIu64 " CPU cycles total\n\n", job.cpuCycles); |
| 187 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | bool InferenceProcess::copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 192 | // Create a filtered list of non empty input tensors |
| 193 | vector<TfLiteTensor *> inputTensors; |
| 194 | for (size_t i = 0; i < interpreter.inputs_size(); ++i) { |
| 195 | TfLiteTensor *tensor = interpreter.input(i); |
| 196 | |
| 197 | if (tensor->bytes > 0) { |
| 198 | inputTensors.push_back(tensor); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 199 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 200 | } |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 201 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 202 | if (job.input.size() != inputTensors.size()) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 203 | LOG_ERR("Number of input buffers does not match number of non empty network tensors: input=%zu, network=%zu", |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame] | 204 | job.input.size(), |
| 205 | inputTensors.size()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 209 | // Copy input data from job to TFLu arena |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 210 | for (size_t i = 0; i < inputTensors.size(); ++i) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 211 | DataPtr &input = job.input[i]; |
| 212 | TfLiteTensor *tensor = inputTensors[i]; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 213 | |
| 214 | if (input.size != tensor->bytes) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 215 | LOG_ERR("Job input size does not match network input size: job=%s, index=%zu, input=%zu, network=%u", |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame] | 216 | job.name.c_str(), |
| 217 | i, |
| 218 | input.size, |
| 219 | tensor->bytes); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 223 | copy(input.begin(), input.end(), tensor->data.uint8); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | bool InferenceProcess::copyOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
| 230 | // Skip copy if output is empty |
| 231 | if (job.output.empty()) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | if (interpreter.outputs_size() != job.output.size()) { |
| 236 | LOG_ERR("Output size mismatch: job=%zu, network=%u", job.output.size(), interpreter.outputs_size()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 240 | for (unsigned i = 0; i < interpreter.outputs_size(); ++i) { |
| 241 | DataPtr &output = job.output[i]; |
| 242 | TfLiteTensor *tensor = interpreter.output(i); |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 243 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 244 | if (tensor->bytes > output.size) { |
| 245 | LOG_ERR("Tensor size mismatch: tensor=%d, expected=%d", tensor->bytes, output.size); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 249 | copy(tensor->data.uint8, tensor->data.uint8 + tensor->bytes, output.begin()); |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | bool InferenceProcess::compareOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
| 256 | // Skip verification if expected output is empty |
| 257 | if (job.expectedOutput.empty()) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if (job.expectedOutput.size() != interpreter.outputs_size()) { |
| 262 | LOG_ERR("Expected number of output tensors mismatch: job=%s, expected=%zu, network=%zu", |
| 263 | job.name.c_str(), |
| 264 | job.expectedOutput.size(), |
| 265 | interpreter.outputs_size()); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | for (unsigned int i = 0; i < interpreter.outputs_size(); i++) { |
| 270 | const DataPtr &expected = job.expectedOutput[i]; |
| 271 | const TfLiteTensor *output = interpreter.output(i); |
| 272 | |
| 273 | if (expected.size != output->bytes) { |
| 274 | LOG_ERR("Expected output tensor size mismatch: job=%s, index=%u, expected=%zu, network=%zu", |
| 275 | job.name.c_str(), |
| 276 | i, |
| 277 | expected.size, |
| 278 | output->bytes); |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | const char *exp = expected.begin(); |
| 283 | for (unsigned int j = 0; j < output->bytes; ++j) { |
| 284 | if (output->data.uint8[j] != exp[j]) { |
| 285 | LOG_ERR("Expected output tensor data mismatch: job=%s, index=%u, offset=%u, " |
| 286 | "expected=%02x, network=%02x\n", |
| 287 | job.name.c_str(), |
| 288 | i, |
| 289 | j, |
| 290 | exp[j], |
| 291 | output->data.uint8[j]); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | } |
| 295 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 296 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | void InferenceProcess::printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 301 | LOG("arena_used_bytes : %zu\n", interpreter.arena_used_bytes()); |
| 302 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 303 | // Print all of the output data, or the first NUM_BYTES_TO_PRINT bytes, |
| 304 | // whichever comes first as well as the output shape. |
| 305 | LOG("num_of_outputs: %d\n", interpreter.outputs_size()); |
| 306 | LOG("output_begin\n"); |
| 307 | LOG("[\n"); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 308 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 309 | for (unsigned int i = 0; i < interpreter.outputs_size(); i++) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 310 | printOutputTensor(interpreter.output(i), job.numBytesToPrint); |
| 311 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 312 | if (i != interpreter.outputs_size() - 1) { |
| 313 | LOG(",\n"); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 314 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 315 | } |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 316 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 317 | LOG("]\n"); |
| 318 | LOG("output_end\n"); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 319 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 320 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 321 | void InferenceProcess::printOutputTensor(TfLiteTensor *output, size_t bytesToPrint) { |
| 322 | constexpr auto crc = Crc(); |
| 323 | const uint32_t crc32 = crc.crc32(output->data.data, output->bytes); |
| 324 | const int numBytesToPrint = min(output->bytes, bytesToPrint); |
| 325 | int dims_size = output->dims->size; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 326 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 327 | LOG("{\n"); |
| 328 | LOG("\"dims\": [%d,", dims_size); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 329 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 330 | for (int i = 0; i < output->dims->size - 1; ++i) { |
| 331 | LOG("%d,", output->dims->data[i]); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 332 | } |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 333 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 334 | LOG("%d],\n", output->dims->data[dims_size - 1]); |
| 335 | LOG("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data); |
Henrik Hoglind | 59096ef | 2022-03-18 08:58:11 +0100 | [diff] [blame] | 336 | LOG("\"data_bytes\": %d,\n", output->bytes); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 337 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 338 | if (numBytesToPrint) { |
| 339 | LOG("\"crc32\": \"%08" PRIx32 "\",\n", crc32); |
| 340 | LOG("\"data\":\""); |
| 341 | |
| 342 | for (int i = 0; i < numBytesToPrint - 1; ++i) { |
| 343 | /* |
| 344 | * Workaround an issue when compiling with GCC where by |
| 345 | * printing only a '\n' the produced global output is wrong. |
| 346 | */ |
| 347 | if (i % 15 == 0 && i != 0) { |
| 348 | LOG("0x%02x,\n", output->data.uint8[i]); |
| 349 | } else { |
| 350 | LOG("0x%02x,", output->data.uint8[i]); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | LOG("0x%02x\"\n", output->data.uint8[numBytesToPrint - 1]); |
| 355 | } else { |
| 356 | LOG("\"crc32\": \"%08" PRIx32 "\"\n", crc32); |
| 357 | } |
| 358 | |
| 359 | LOG("}"); |
| 360 | } |
| 361 | |
| 362 | void InferenceProcess::tfluDebugLog(const char *s) { |
| 363 | LOG("%s", s); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | } // namespace InferenceProcess |