blob: e96d6017be6914f8d1ff5d7ece3879e27b033c72 [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"
Jonny Svärd2ebaac72022-05-10 17:29:30 +020024#include "tensorflow/lite/micro/micro_time.h"
Kristofer Jonsson641c0912020-08-31 11:34:14 +020025#include "tensorflow/lite/schema/schema_generated.h"
Kristofer Jonsson641c0912020-08-31 11:34:14 +020026
Jens Elofsson955288a2021-04-22 20:57:15 +020027#include "arm_profiler.hpp"
Kristofer Jonsson3bd34232021-08-30 13:55:55 +020028#ifdef LAYER_BY_LAYER_PROFILER
Jens Elofsson701a63b2021-05-23 17:37:07 +020029#include "layer_by_layer_profiler.hpp"
Jens Elofsson955288a2021-04-22 20:57:15 +020030#endif
Jonny Svärd5adf5a62022-02-09 16:42:10 +010031
32#include "crc.hpp"
33
Anton Moberg07cf70b2021-07-07 11:08:17 +020034#include "ethosu_log.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020035
Kristofer Jonsson641c0912020-08-31 11:34:14 +020036#include "inference_process.hpp"
37
Per Åstrandd9afc082020-10-06 13:25:08 +020038#include "cmsis_compiler.h"
39
Per Åstrand91a91732020-09-25 15:04:26 +020040#include <inttypes.h>
41
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020042using namespace std;
43
Kristofer Jonsson641c0912020-08-31 11:34:14 +020044namespace InferenceProcess {
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020045DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020046
Kristofer Jonsson34e24962020-11-23 16:22:10 +010047void DataPtr::invalidate() {
48#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010049 SCB_InvalidateDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
50#endif
51}
52
53void DataPtr::clean() {
54#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010055 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
56#endif
57}
58
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010059char *DataPtr::begin() const {
60 return static_cast<char *>(data);
61}
62
63char *DataPtr::end() const {
64 return static_cast<char *>(data) + size;
65}
66
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010067InferenceJob::InferenceJob() : numBytesToPrint(0), externalContext(nullptr) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020068
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020069InferenceJob::InferenceJob(const string &_name,
70 const DataPtr &_networkModel,
71 const vector<DataPtr> &_input,
72 const vector<DataPtr> &_output,
73 const vector<DataPtr> &_expectedOutput,
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010074 const size_t _numBytesToPrint,
75 void *_externalContext) :
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020076 name(_name),
77 networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput),
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010078 numBytesToPrint(_numBytesToPrint), externalContext(_externalContext) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020079
Kristofer Jonsson34e24962020-11-23 16:22:10 +010080void 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
96void 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 Jonsson40d886e2021-12-15 11:16:26 +0100112InferenceProcess::InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize) :
113 tensorArena(_tensorArena), tensorArenaSize(_tensorArenaSize) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200114
115bool InferenceProcess::runJob(InferenceJob &job) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100116 LOG_INFO("Running inference job: %s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200117
Bhavik Patelffe845d2020-11-16 12:13:56 +0100118 // Register debug log callback for profiling
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100119 RegisterDebugLogCallback(tfluDebugLog);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200120
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200121 // Get model handle and verify that the version is correct
Davide Grohmann30b17b92022-06-14 15:17:18 +0200122 const tflite::Model *model = parser.getModel(job.networkModel.data, job.networkModel.size);
123 if (model == nullptr) {
124 LOG_ERR("Invalid model");
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200125 return true;
126 }
127
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200128 // Create the TFL micro interpreter
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200129 tflite::AllOpsResolver resolver;
Jens Elofsson955288a2021-04-22 20:57:15 +0200130 tflite::ArmProfiler profiler;
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100131 tflite::MicroErrorReporter errorReporter;
132 tflite::MicroInterpreter interpreter(
133 model, resolver, tensorArena, tensorArenaSize, &errorReporter, nullptr, &profiler);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200134
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100135 // Set external context
Davide Grohmann165f00a2022-02-09 14:53:58 +0100136 if (job.externalContext != nullptr) {
137 interpreter.SetMicroExternalContext(job.externalContext);
138 }
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100139
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200140 // Allocate tensors
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100141 TfLiteStatus status = interpreter.AllocateTensors();
142 if (status != kTfLiteOk) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100143 LOG_ERR("Failed to allocate tensors for inference: job=%s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200144 return true;
145 }
146
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100147 // Copy IFM data from job descriptor to TFLu arena
148 if (copyIfm(job, interpreter)) {
149 return true;
150 }
151
Jonny Svärd2ebaac72022-05-10 17:29:30 +0200152 // Get the current cycle counter value
153 uint32_t cpuCyclesBegin = tflite::GetCurrentTimeTicks();
154
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100155 // Run the inference
156 status = interpreter.Invoke();
Jonny Svärd2ebaac72022-05-10 17:29:30 +0200157
158 // Calculate nbr of CPU cycles for the Invoke call
159 job.cpuCycles = tflite::GetCurrentTimeTicks() - cpuCyclesBegin;
160
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100161 if (status != kTfLiteOk) {
162 LOG_ERR("Invoke failed for inference: job=%s", job.name.c_str());
163 return true;
164 }
165
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100166 // 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ärd2ebaac72022-05-10 17:29:30 +0200178 LOG_INFO("\n");
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100179 LOG_INFO("Finished running job: %s", job.name.c_str());
180
Jonny Svärd2ebaac72022-05-10 17:29:30 +0200181 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 Jonssondcc1ce02021-12-21 16:25:19 +0100188 return false;
189}
190
191bool InferenceProcess::copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200192 // 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 Jonsson641c0912020-08-31 11:34:14 +0200199 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200200 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100201
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200202 if (job.input.size() != inputTensors.size()) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100203 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 +0200204 job.input.size(),
205 inputTensors.size());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200206 return true;
207 }
208
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100209 // Copy input data from job to TFLu arena
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200210 for (size_t i = 0; i < inputTensors.size(); ++i) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100211 DataPtr &input = job.input[i];
212 TfLiteTensor *tensor = inputTensors[i];
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200213
214 if (input.size != tensor->bytes) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100215 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 +0200216 job.name.c_str(),
217 i,
218 input.size,
219 tensor->bytes);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200220 return true;
221 }
222
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100223 copy(input.begin(), input.end(), tensor->data.uint8);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200224 }
225
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100226 return false;
227}
228
229bool 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 Jonsson641c0912020-08-31 11:34:14 +0200237 return true;
238 }
239
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100240 for (unsigned i = 0; i < interpreter.outputs_size(); ++i) {
241 DataPtr &output = job.output[i];
242 TfLiteTensor *tensor = interpreter.output(i);
Bhavik Patelffe845d2020-11-16 12:13:56 +0100243
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100244 if (tensor->bytes > output.size) {
245 LOG_ERR("Tensor size mismatch: tensor=%d, expected=%d", tensor->bytes, output.size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200246 return true;
247 }
248
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100249 copy(tensor->data.uint8, tensor->data.uint8 + tensor->bytes, output.begin());
250 }
251
252 return false;
253}
254
255bool 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 Jonsson72fa50b2020-09-10 13:26:41 +0200292 return true;
293 }
294 }
295 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200296
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100297 return false;
298}
299
300void InferenceProcess::printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100301 LOG("arena_used_bytes : %zu\n", interpreter.arena_used_bytes());
302
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100303 // 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 Jonssondcc1ce02021-12-21 16:25:19 +0100308
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100309 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100310 printOutputTensor(interpreter.output(i), job.numBytesToPrint);
311
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100312 if (i != interpreter.outputs_size() - 1) {
313 LOG(",\n");
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200314 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200315 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100316
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100317 LOG("]\n");
318 LOG("output_end\n");
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100319}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200320
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100321void 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 Jonsson72fa50b2020-09-10 13:26:41 +0200326
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100327 LOG("{\n");
328 LOG("\"dims\": [%d,", dims_size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200329
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100330 for (int i = 0; i < output->dims->size - 1; ++i) {
331 LOG("%d,", output->dims->data[i]);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200332 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200333
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100334 LOG("%d],\n", output->dims->data[dims_size - 1]);
335 LOG("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data);
Henrik Hoglind59096ef2022-03-18 08:58:11 +0100336 LOG("\"data_bytes\": %d,\n", output->bytes);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200337
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100338 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
362void InferenceProcess::tfluDebugLog(const char *s) {
363 LOG("%s", s);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200364}
365
366} // namespace InferenceProcess