blob: 4c6500521475ed8735f05280b46ea66ffd4a7af9 [file] [log] [blame]
Kristofer Jonsson641c0912020-08-31 11:34:14 +02001/*
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +01002 * Copyright (c) 2019-2022 Arm Limited. All rights reserved.
Kristofer Jonsson641c0912020-08-31 11:34:14 +02003 *
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 Nilsson231e1d92020-11-05 12:19:34 +010020#include "tensorflow/lite/micro/cortex_m_generic/debug_log_callback.h"
Kristofer Jonsson641c0912020-08-31 11:34:14 +020021#include "tensorflow/lite/micro/micro_error_reporter.h"
22#include "tensorflow/lite/micro/micro_interpreter.h"
Bhavik Patelffe845d2020-11-16 12:13:56 +010023#include "tensorflow/lite/micro/micro_profiler.h"
Kristofer Jonsson641c0912020-08-31 11:34:14 +020024#include "tensorflow/lite/schema/schema_generated.h"
Kristofer Jonsson641c0912020-08-31 11:34:14 +020025
Jens Elofsson955288a2021-04-22 20:57:15 +020026#include "arm_profiler.hpp"
Kristofer Jonsson3bd34232021-08-30 13:55:55 +020027#ifdef LAYER_BY_LAYER_PROFILER
Jens Elofsson701a63b2021-05-23 17:37:07 +020028#include "layer_by_layer_profiler.hpp"
Jens Elofsson955288a2021-04-22 20:57:15 +020029#endif
Anton Moberg07cf70b2021-07-07 11:08:17 +020030#include "ethosu_log.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020031
Kristofer Jonsson641c0912020-08-31 11:34:14 +020032#include "inference_process.hpp"
33
Per Åstrandd9afc082020-10-06 13:25:08 +020034#include "cmsis_compiler.h"
35
Per Åstrand91a91732020-09-25 15:04:26 +020036#include <inttypes.h>
37
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020038using namespace std;
39
Kristofer Jonsson641c0912020-08-31 11:34:14 +020040namespace {
Måns Nilsson231e1d92020-11-05 12:19:34 +010041
Henrik Hoglindae4d8302021-12-08 15:06:02 +010042class Crc {
43public:
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
74private:
75 uint32_t table[256];
76};
77
Kristofer Jonsson641c0912020-08-31 11:34:14 +020078} // namespace
79
80namespace InferenceProcess {
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020081DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020082
Kristofer Jonsson34e24962020-11-23 16:22:10 +010083void DataPtr::invalidate() {
84#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010085 SCB_InvalidateDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
86#endif
87}
88
89void DataPtr::clean() {
90#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010091 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
92#endif
93}
94
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010095char *DataPtr::begin() const {
96 return static_cast<char *>(data);
97}
98
99char *DataPtr::end() const {
100 return static_cast<char *>(data) + size;
101}
102
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100103InferenceJob::InferenceJob() : numBytesToPrint(0), externalContext(nullptr) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200104
Per Åstrandbbd9c8f2020-09-25 15:07:35 +0200105InferenceJob::InferenceJob(const string &_name,
106 const DataPtr &_networkModel,
107 const vector<DataPtr> &_input,
108 const vector<DataPtr> &_output,
109 const vector<DataPtr> &_expectedOutput,
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100110 const size_t _numBytesToPrint,
111 void *_externalContext) :
Per Åstrandbbd9c8f2020-09-25 15:07:35 +0200112 name(_name),
113 networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput),
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100114 numBytesToPrint(_numBytesToPrint), externalContext(_externalContext) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200115
Kristofer Jonsson34e24962020-11-23 16:22:10 +0100116void 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
132void 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 Jonsson40d886e2021-12-15 11:16:26 +0100148InferenceProcess::InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize) :
149 tensorArena(_tensorArena), tensorArenaSize(_tensorArenaSize) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200150
151bool InferenceProcess::runJob(InferenceJob &job) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100152 LOG_INFO("Running inference job: %s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200153
Bhavik Patelffe845d2020-11-16 12:13:56 +0100154 // Register debug log callback for profiling
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100155 RegisterDebugLogCallback(tfluDebugLog);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200156
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200157 // Get model handle and verify that the version is correct
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200158 const tflite::Model *model = ::tflite::GetModel(job.networkModel.data);
159 if (model->version() != TFLITE_SCHEMA_VERSION) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100160 LOG_ERR("Model schema version unsupported: version=%" PRIu32 ", supported=%d.",
Anton Moberg07cf70b2021-07-07 11:08:17 +0200161 model->version(),
162 TFLITE_SCHEMA_VERSION);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200163 return true;
164 }
165
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200166 // Create the TFL micro interpreter
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200167 tflite::AllOpsResolver resolver;
Jens Elofsson955288a2021-04-22 20:57:15 +0200168 tflite::ArmProfiler profiler;
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100169 tflite::MicroErrorReporter errorReporter;
170 tflite::MicroInterpreter interpreter(
171 model, resolver, tensorArena, tensorArenaSize, &errorReporter, nullptr, &profiler);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200172
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100173 // Set external context
174 interpreter.SetMicroExternalContext(job.externalContext);
175
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200176 // Allocate tensors
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100177 TfLiteStatus status = interpreter.AllocateTensors();
178 if (status != kTfLiteOk) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100179 LOG_ERR("Failed to allocate tensors for inference: job=%s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200180 return true;
181 }
182
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100183 // 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 Jonssondcc1ce02021-12-21 16:25:19 +0100195 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
214bool InferenceProcess::copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200215 // 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 Jonsson641c0912020-08-31 11:34:14 +0200222 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200223 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100224
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200225 if (job.input.size() != inputTensors.size()) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100226 LOG_ERR("Number of input buffers does not match number of non empty network tensors: input=%zu, network=%zu",
Anton Moberg07cf70b2021-07-07 11:08:17 +0200227 job.input.size(),
228 inputTensors.size());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200229 return true;
230 }
231
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100232 // Copy input data from job to TFLu arena
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200233 for (size_t i = 0; i < inputTensors.size(); ++i) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100234 DataPtr &input = job.input[i];
235 TfLiteTensor *tensor = inputTensors[i];
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200236
237 if (input.size != tensor->bytes) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100238 LOG_ERR("Job input size does not match network input size: job=%s, index=%zu, input=%zu, network=%u",
Anton Moberg07cf70b2021-07-07 11:08:17 +0200239 job.name.c_str(),
240 i,
241 input.size,
242 tensor->bytes);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200243 return true;
244 }
245
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100246 copy(input.begin(), input.end(), tensor->data.uint8);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200247 }
248
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100249 return false;
250}
251
252bool 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 Jonsson641c0912020-08-31 11:34:14 +0200260 return true;
261 }
262
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100263 for (unsigned i = 0; i < interpreter.outputs_size(); ++i) {
264 DataPtr &output = job.output[i];
265 TfLiteTensor *tensor = interpreter.output(i);
Bhavik Patelffe845d2020-11-16 12:13:56 +0100266
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100267 if (tensor->bytes > output.size) {
268 LOG_ERR("Tensor size mismatch: tensor=%d, expected=%d", tensor->bytes, output.size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200269 return true;
270 }
271
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100272 copy(tensor->data.uint8, tensor->data.uint8 + tensor->bytes, output.begin());
273 }
274
275 return false;
276}
277
278bool 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 Jonsson72fa50b2020-09-10 13:26:41 +0200315 return true;
316 }
317 }
318 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200319
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100320 return false;
321}
322
323void InferenceProcess::printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100324 LOG("arena_used_bytes : %zu\n", interpreter.arena_used_bytes());
325
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100326 // 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 Jonssondcc1ce02021-12-21 16:25:19 +0100331
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100332 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100333 printOutputTensor(interpreter.output(i), job.numBytesToPrint);
334
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100335 if (i != interpreter.outputs_size() - 1) {
336 LOG(",\n");
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200337 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200338 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100339
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100340 LOG("]\n");
341 LOG("output_end\n");
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100342}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200343
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100344void 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 Jonsson72fa50b2020-09-10 13:26:41 +0200349
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100350 LOG("{\n");
351 LOG("\"dims\": [%d,", dims_size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200352
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100353 for (int i = 0; i < output->dims->size - 1; ++i) {
354 LOG("%d,", output->dims->data[i]);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200355 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200356
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100357 LOG("%d],\n", output->dims->data[dims_size - 1]);
358 LOG("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200359
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100360 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
384void InferenceProcess::tfluDebugLog(const char *s) {
385 LOG("%s", s);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200386}
387
388} // namespace InferenceProcess