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