Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
Per Åstrand | 9045545 | 2021-02-25 11:10:08 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 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 | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 103 | InferenceJob::InferenceJob() : numBytesToPrint(0) {} |
| 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, |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 110 | size_t _numBytesToPrint, |
| 111 | const vector<uint8_t> &_pmuEventConfig, |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 112 | const bool _pmuCycleCounterEnable) : |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 113 | name(_name), |
| 114 | networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput), |
Bhavik Patel | 97906eb | 2020-12-17 15:32:16 +0100 | [diff] [blame] | 115 | numBytesToPrint(_numBytesToPrint), pmuEventConfig(_pmuEventConfig), pmuCycleCounterEnable(_pmuCycleCounterEnable), |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 116 | pmuEventCount(), pmuCycleCounterCount(0) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 117 | |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 118 | void InferenceJob::invalidate() { |
| 119 | networkModel.invalidate(); |
| 120 | |
| 121 | for (auto &it : input) { |
| 122 | it.invalidate(); |
| 123 | } |
| 124 | |
| 125 | for (auto &it : output) { |
| 126 | it.invalidate(); |
| 127 | } |
| 128 | |
| 129 | for (auto &it : expectedOutput) { |
| 130 | it.invalidate(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void InferenceJob::clean() { |
| 135 | networkModel.clean(); |
| 136 | |
| 137 | for (auto &it : input) { |
| 138 | it.clean(); |
| 139 | } |
| 140 | |
| 141 | for (auto &it : output) { |
| 142 | it.clean(); |
| 143 | } |
| 144 | |
| 145 | for (auto &it : expectedOutput) { |
| 146 | it.clean(); |
| 147 | } |
| 148 | } |
| 149 | |
Kristofer Jonsson | 40d886e | 2021-12-15 11:16:26 +0100 | [diff] [blame] | 150 | InferenceProcess::InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize) : |
| 151 | tensorArena(_tensorArena), tensorArenaSize(_tensorArenaSize) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 152 | |
| 153 | bool InferenceProcess::runJob(InferenceJob &job) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 154 | LOG_INFO("Running inference job: %s", job.name.c_str()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 155 | |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 156 | // Register debug log callback for profiling |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 157 | RegisterDebugLogCallback(tfluDebugLog); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 158 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 159 | // Get model handle and verify that the version is correct |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 160 | const tflite::Model *model = ::tflite::GetModel(job.networkModel.data); |
| 161 | if (model->version() != TFLITE_SCHEMA_VERSION) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 162 | LOG_ERR("Model schema version unsupported: version=%" PRIu32 ", supported=%d.", |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame] | 163 | model->version(), |
| 164 | TFLITE_SCHEMA_VERSION); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 168 | // Create the TFL micro interpreter |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 169 | tflite::AllOpsResolver resolver; |
Kristofer Jonsson | 3bd3423 | 2021-08-30 13:55:55 +0200 | [diff] [blame] | 170 | #ifdef LAYER_BY_LAYER_PROFILER |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 171 | tflite::LayerByLayerProfiler profiler(job.pmuEventConfig, job.pmuCycleCounterEnable); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 172 | #else |
| 173 | tflite::ArmProfiler profiler; |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 174 | #endif |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 175 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 176 | tflite::MicroErrorReporter errorReporter; |
| 177 | tflite::MicroInterpreter interpreter( |
| 178 | model, resolver, tensorArena, tensorArenaSize, &errorReporter, nullptr, &profiler); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 179 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 180 | // Allocate tensors |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 181 | TfLiteStatus status = interpreter.AllocateTensors(); |
| 182 | if (status != kTfLiteOk) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 183 | 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] | 184 | return true; |
| 185 | } |
| 186 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 187 | // Copy IFM data from job descriptor to TFLu arena |
| 188 | if (copyIfm(job, interpreter)) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // Run the inference |
| 193 | status = interpreter.Invoke(); |
| 194 | if (status != kTfLiteOk) { |
| 195 | LOG_ERR("Invoke failed for inference: job=%s", job.name.c_str()); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | #ifdef LAYER_BY_LAYER_PROFILER |
| 200 | if (job.pmuCycleCounterEnable) { |
| 201 | job.pmuCycleCounterCount = profiler.GetPmuCycleCounterCount(); |
| 202 | } |
| 203 | |
| 204 | job.pmuEventCount.assign(profiler.GetPmuEventCount().begin(), profiler.GetPmuEventCount().end()); |
| 205 | #endif |
| 206 | |
| 207 | LOG("Inference runtime: %" PRId32 " cycles\n", profiler.GetTotalTicks()); |
| 208 | |
| 209 | // Copy output data from TFLu arena to job descriptor |
| 210 | if (copyOfm(job, interpreter)) { |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | printJob(job, interpreter); |
| 215 | |
| 216 | // Compare the OFM with the expected reference data |
| 217 | if (compareOfm(job, interpreter)) { |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | LOG_INFO("Finished running job: %s", job.name.c_str()); |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | bool InferenceProcess::copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 227 | // Create a filtered list of non empty input tensors |
| 228 | vector<TfLiteTensor *> inputTensors; |
| 229 | for (size_t i = 0; i < interpreter.inputs_size(); ++i) { |
| 230 | TfLiteTensor *tensor = interpreter.input(i); |
| 231 | |
| 232 | if (tensor->bytes > 0) { |
| 233 | inputTensors.push_back(tensor); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 234 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 235 | } |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 236 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 237 | if (job.input.size() != inputTensors.size()) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 238 | 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] | 239 | job.input.size(), |
| 240 | inputTensors.size()); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 244 | // Copy input data from job to TFLu arena |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 245 | for (size_t i = 0; i < inputTensors.size(); ++i) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 246 | DataPtr &input = job.input[i]; |
| 247 | TfLiteTensor *tensor = inputTensors[i]; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 248 | |
| 249 | if (input.size != tensor->bytes) { |
Kristofer Jonsson | eb91239 | 2021-11-12 12:51:27 +0100 | [diff] [blame] | 250 | 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] | 251 | job.name.c_str(), |
| 252 | i, |
| 253 | input.size, |
| 254 | tensor->bytes); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 258 | copy(input.begin(), input.end(), tensor->data.uint8); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | bool InferenceProcess::copyOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
| 265 | // Skip copy if output is empty |
| 266 | if (job.output.empty()) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | if (interpreter.outputs_size() != job.output.size()) { |
| 271 | 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] | 272 | return true; |
| 273 | } |
| 274 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 275 | for (unsigned i = 0; i < interpreter.outputs_size(); ++i) { |
| 276 | DataPtr &output = job.output[i]; |
| 277 | TfLiteTensor *tensor = interpreter.output(i); |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 278 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 279 | if (tensor->bytes > output.size) { |
| 280 | 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] | 281 | return true; |
| 282 | } |
| 283 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 284 | copy(tensor->data.uint8, tensor->data.uint8 + tensor->bytes, output.begin()); |
| 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool InferenceProcess::compareOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
| 291 | // Skip verification if expected output is empty |
| 292 | if (job.expectedOutput.empty()) { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | if (job.expectedOutput.size() != interpreter.outputs_size()) { |
| 297 | LOG_ERR("Expected number of output tensors mismatch: job=%s, expected=%zu, network=%zu", |
| 298 | job.name.c_str(), |
| 299 | job.expectedOutput.size(), |
| 300 | interpreter.outputs_size()); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | for (unsigned int i = 0; i < interpreter.outputs_size(); i++) { |
| 305 | const DataPtr &expected = job.expectedOutput[i]; |
| 306 | const TfLiteTensor *output = interpreter.output(i); |
| 307 | |
| 308 | if (expected.size != output->bytes) { |
| 309 | LOG_ERR("Expected output tensor size mismatch: job=%s, index=%u, expected=%zu, network=%zu", |
| 310 | job.name.c_str(), |
| 311 | i, |
| 312 | expected.size, |
| 313 | output->bytes); |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | const char *exp = expected.begin(); |
| 318 | for (unsigned int j = 0; j < output->bytes; ++j) { |
| 319 | if (output->data.uint8[j] != exp[j]) { |
| 320 | LOG_ERR("Expected output tensor data mismatch: job=%s, index=%u, offset=%u, " |
| 321 | "expected=%02x, network=%02x\n", |
| 322 | job.name.c_str(), |
| 323 | i, |
| 324 | j, |
| 325 | exp[j], |
| 326 | output->data.uint8[j]); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 331 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 332 | return false; |
| 333 | } |
| 334 | |
| 335 | void InferenceProcess::printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter) { |
| 336 | for (size_t i = 0; i < job.pmuEventCount.size(); i++) { |
| 337 | LOG("ethosu_pmu_cntr%zu : %" PRIu32 "\n", i, job.pmuEventCount[i]); |
| 338 | } |
| 339 | |
| 340 | LOG("arena_used_bytes : %zu\n", interpreter.arena_used_bytes()); |
| 341 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 342 | // Print all of the output data, or the first NUM_BYTES_TO_PRINT bytes, |
| 343 | // whichever comes first as well as the output shape. |
| 344 | LOG("num_of_outputs: %d\n", interpreter.outputs_size()); |
| 345 | LOG("output_begin\n"); |
| 346 | LOG("[\n"); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 347 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 348 | for (unsigned int i = 0; i < interpreter.outputs_size(); i++) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 349 | printOutputTensor(interpreter.output(i), job.numBytesToPrint); |
| 350 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 351 | if (i != interpreter.outputs_size() - 1) { |
| 352 | LOG(",\n"); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 353 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 354 | } |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 355 | |
Henrik Hoglind | ae4d830 | 2021-12-08 15:06:02 +0100 | [diff] [blame] | 356 | LOG("]\n"); |
| 357 | LOG("output_end\n"); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 358 | } |
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 | void InferenceProcess::printOutputTensor(TfLiteTensor *output, size_t bytesToPrint) { |
| 361 | constexpr auto crc = Crc(); |
| 362 | const uint32_t crc32 = crc.crc32(output->data.data, output->bytes); |
| 363 | const int numBytesToPrint = min(output->bytes, bytesToPrint); |
| 364 | int dims_size = output->dims->size; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 365 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 366 | LOG("{\n"); |
| 367 | LOG("\"dims\": [%d,", dims_size); |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 368 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 369 | for (int i = 0; i < output->dims->size - 1; ++i) { |
| 370 | LOG("%d,", output->dims->data[i]); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 371 | } |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 372 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 373 | LOG("%d],\n", output->dims->data[dims_size - 1]); |
| 374 | LOG("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 375 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame^] | 376 | if (numBytesToPrint) { |
| 377 | LOG("\"crc32\": \"%08" PRIx32 "\",\n", crc32); |
| 378 | LOG("\"data\":\""); |
| 379 | |
| 380 | for (int i = 0; i < numBytesToPrint - 1; ++i) { |
| 381 | /* |
| 382 | * Workaround an issue when compiling with GCC where by |
| 383 | * printing only a '\n' the produced global output is wrong. |
| 384 | */ |
| 385 | if (i % 15 == 0 && i != 0) { |
| 386 | LOG("0x%02x,\n", output->data.uint8[i]); |
| 387 | } else { |
| 388 | LOG("0x%02x,", output->data.uint8[i]); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | LOG("0x%02x\"\n", output->data.uint8[numBytesToPrint - 1]); |
| 393 | } else { |
| 394 | LOG("\"crc32\": \"%08" PRIx32 "\"\n", crc32); |
| 395 | } |
| 396 | |
| 397 | LOG("}"); |
| 398 | } |
| 399 | |
| 400 | void InferenceProcess::tfluDebugLog(const char *s) { |
| 401 | LOG("%s", s); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | } // namespace InferenceProcess |