blob: 264c4ba2a988aac6cdf9699ff8b07f380c76e890 [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
Jonny Svärd5adf5a62022-02-09 16:42:10 +010030
31#include "crc.hpp"
32
Anton Moberg07cf70b2021-07-07 11:08:17 +020033#include "ethosu_log.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020034
Kristofer Jonsson641c0912020-08-31 11:34:14 +020035#include "inference_process.hpp"
36
Per Åstrandd9afc082020-10-06 13:25:08 +020037#include "cmsis_compiler.h"
38
Per Åstrand91a91732020-09-25 15:04:26 +020039#include <inttypes.h>
40
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020041using namespace std;
42
Kristofer Jonsson641c0912020-08-31 11:34:14 +020043namespace InferenceProcess {
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020044DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020045
Kristofer Jonsson34e24962020-11-23 16:22:10 +010046void DataPtr::invalidate() {
47#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010048 SCB_InvalidateDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
49#endif
50}
51
52void DataPtr::clean() {
53#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
Kristofer Jonsson34e24962020-11-23 16:22:10 +010054 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(data), size);
55#endif
56}
57
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010058char *DataPtr::begin() const {
59 return static_cast<char *>(data);
60}
61
62char *DataPtr::end() const {
63 return static_cast<char *>(data) + size;
64}
65
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010066InferenceJob::InferenceJob() : numBytesToPrint(0), externalContext(nullptr) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020067
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020068InferenceJob::InferenceJob(const string &_name,
69 const DataPtr &_networkModel,
70 const vector<DataPtr> &_input,
71 const vector<DataPtr> &_output,
72 const vector<DataPtr> &_expectedOutput,
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010073 const size_t _numBytesToPrint,
74 void *_externalContext) :
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020075 name(_name),
76 networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput),
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010077 numBytesToPrint(_numBytesToPrint), externalContext(_externalContext) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020078
Kristofer Jonsson34e24962020-11-23 16:22:10 +010079void InferenceJob::invalidate() {
80 networkModel.invalidate();
81
82 for (auto &it : input) {
83 it.invalidate();
84 }
85
86 for (auto &it : output) {
87 it.invalidate();
88 }
89
90 for (auto &it : expectedOutput) {
91 it.invalidate();
92 }
93}
94
95void InferenceJob::clean() {
96 networkModel.clean();
97
98 for (auto &it : input) {
99 it.clean();
100 }
101
102 for (auto &it : output) {
103 it.clean();
104 }
105
106 for (auto &it : expectedOutput) {
107 it.clean();
108 }
109}
110
Kristofer Jonsson40d886e2021-12-15 11:16:26 +0100111InferenceProcess::InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize) :
112 tensorArena(_tensorArena), tensorArenaSize(_tensorArenaSize) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200113
114bool InferenceProcess::runJob(InferenceJob &job) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100115 LOG_INFO("Running inference job: %s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200116
Bhavik Patelffe845d2020-11-16 12:13:56 +0100117 // Register debug log callback for profiling
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100118 RegisterDebugLogCallback(tfluDebugLog);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200119
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200120 // Get model handle and verify that the version is correct
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200121 const tflite::Model *model = ::tflite::GetModel(job.networkModel.data);
122 if (model->version() != TFLITE_SCHEMA_VERSION) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100123 LOG_ERR("Model schema version unsupported: version=%" PRIu32 ", supported=%d.",
Anton Moberg07cf70b2021-07-07 11:08:17 +0200124 model->version(),
125 TFLITE_SCHEMA_VERSION);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200126 return true;
127 }
128
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200129 // Create the TFL micro interpreter
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200130 tflite::AllOpsResolver resolver;
Jens Elofsson955288a2021-04-22 20:57:15 +0200131 tflite::ArmProfiler profiler;
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100132 tflite::MicroErrorReporter errorReporter;
133 tflite::MicroInterpreter interpreter(
134 model, resolver, tensorArena, tensorArenaSize, &errorReporter, nullptr, &profiler);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200135
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100136 // Set external context
Davide Grohmann165f00a2022-02-09 14:53:58 +0100137 if (job.externalContext != nullptr) {
138 interpreter.SetMicroExternalContext(job.externalContext);
139 }
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +0100140
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200141 // Allocate tensors
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100142 TfLiteStatus status = interpreter.AllocateTensors();
143 if (status != kTfLiteOk) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100144 LOG_ERR("Failed to allocate tensors for inference: job=%s", job.name.c_str());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200145 return true;
146 }
147
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100148 // Copy IFM data from job descriptor to TFLu arena
149 if (copyIfm(job, interpreter)) {
150 return true;
151 }
152
153 // Run the inference
154 status = interpreter.Invoke();
155 if (status != kTfLiteOk) {
156 LOG_ERR("Invoke failed for inference: job=%s", job.name.c_str());
157 return true;
158 }
159
Kristofer Jonssona78c7a82022-02-10 14:17:24 +0100160 LOG("Inference runtime: %" PRIu64 " cycles\n", profiler.GetTotalTicks());
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100161
162 // Copy output data from TFLu arena to job descriptor
163 if (copyOfm(job, interpreter)) {
164 return true;
165 }
166
167 printJob(job, interpreter);
168
169 // Compare the OFM with the expected reference data
170 if (compareOfm(job, interpreter)) {
171 return true;
172 }
173
174 LOG_INFO("Finished running job: %s", job.name.c_str());
175
176 return false;
177}
178
179bool InferenceProcess::copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200180 // Create a filtered list of non empty input tensors
181 vector<TfLiteTensor *> inputTensors;
182 for (size_t i = 0; i < interpreter.inputs_size(); ++i) {
183 TfLiteTensor *tensor = interpreter.input(i);
184
185 if (tensor->bytes > 0) {
186 inputTensors.push_back(tensor);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200187 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200188 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100189
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200190 if (job.input.size() != inputTensors.size()) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100191 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 +0200192 job.input.size(),
193 inputTensors.size());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200194 return true;
195 }
196
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100197 // Copy input data from job to TFLu arena
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200198 for (size_t i = 0; i < inputTensors.size(); ++i) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100199 DataPtr &input = job.input[i];
200 TfLiteTensor *tensor = inputTensors[i];
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200201
202 if (input.size != tensor->bytes) {
Kristofer Jonssoneb912392021-11-12 12:51:27 +0100203 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 +0200204 job.name.c_str(),
205 i,
206 input.size,
207 tensor->bytes);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200208 return true;
209 }
210
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100211 copy(input.begin(), input.end(), tensor->data.uint8);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200212 }
213
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100214 return false;
215}
216
217bool InferenceProcess::copyOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
218 // Skip copy if output is empty
219 if (job.output.empty()) {
220 return false;
221 }
222
223 if (interpreter.outputs_size() != job.output.size()) {
224 LOG_ERR("Output size mismatch: job=%zu, network=%u", job.output.size(), interpreter.outputs_size());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200225 return true;
226 }
227
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100228 for (unsigned i = 0; i < interpreter.outputs_size(); ++i) {
229 DataPtr &output = job.output[i];
230 TfLiteTensor *tensor = interpreter.output(i);
Bhavik Patelffe845d2020-11-16 12:13:56 +0100231
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100232 if (tensor->bytes > output.size) {
233 LOG_ERR("Tensor size mismatch: tensor=%d, expected=%d", tensor->bytes, output.size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200234 return true;
235 }
236
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100237 copy(tensor->data.uint8, tensor->data.uint8 + tensor->bytes, output.begin());
238 }
239
240 return false;
241}
242
243bool InferenceProcess::compareOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
244 // Skip verification if expected output is empty
245 if (job.expectedOutput.empty()) {
246 return false;
247 }
248
249 if (job.expectedOutput.size() != interpreter.outputs_size()) {
250 LOG_ERR("Expected number of output tensors mismatch: job=%s, expected=%zu, network=%zu",
251 job.name.c_str(),
252 job.expectedOutput.size(),
253 interpreter.outputs_size());
254 return true;
255 }
256
257 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
258 const DataPtr &expected = job.expectedOutput[i];
259 const TfLiteTensor *output = interpreter.output(i);
260
261 if (expected.size != output->bytes) {
262 LOG_ERR("Expected output tensor size mismatch: job=%s, index=%u, expected=%zu, network=%zu",
263 job.name.c_str(),
264 i,
265 expected.size,
266 output->bytes);
267 return true;
268 }
269
270 const char *exp = expected.begin();
271 for (unsigned int j = 0; j < output->bytes; ++j) {
272 if (output->data.uint8[j] != exp[j]) {
273 LOG_ERR("Expected output tensor data mismatch: job=%s, index=%u, offset=%u, "
274 "expected=%02x, network=%02x\n",
275 job.name.c_str(),
276 i,
277 j,
278 exp[j],
279 output->data.uint8[j]);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200280 return true;
281 }
282 }
283 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200284
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100285 return false;
286}
287
288void InferenceProcess::printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100289 LOG("arena_used_bytes : %zu\n", interpreter.arena_used_bytes());
290
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100291 // Print all of the output data, or the first NUM_BYTES_TO_PRINT bytes,
292 // whichever comes first as well as the output shape.
293 LOG("num_of_outputs: %d\n", interpreter.outputs_size());
294 LOG("output_begin\n");
295 LOG("[\n");
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100296
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100297 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100298 printOutputTensor(interpreter.output(i), job.numBytesToPrint);
299
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100300 if (i != interpreter.outputs_size() - 1) {
301 LOG(",\n");
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200302 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200303 }
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100304
Henrik Hoglindae4d8302021-12-08 15:06:02 +0100305 LOG("]\n");
306 LOG("output_end\n");
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100307}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200308
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100309void InferenceProcess::printOutputTensor(TfLiteTensor *output, size_t bytesToPrint) {
310 constexpr auto crc = Crc();
311 const uint32_t crc32 = crc.crc32(output->data.data, output->bytes);
312 const int numBytesToPrint = min(output->bytes, bytesToPrint);
313 int dims_size = output->dims->size;
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200314
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100315 LOG("{\n");
316 LOG("\"dims\": [%d,", dims_size);
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200317
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100318 for (int i = 0; i < output->dims->size - 1; ++i) {
319 LOG("%d,", output->dims->data[i]);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200320 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200321
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100322 LOG("%d],\n", output->dims->data[dims_size - 1]);
323 LOG("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data);
Henrik Hoglind59096ef2022-03-18 08:58:11 +0100324 LOG("\"data_bytes\": %d,\n", output->bytes);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200325
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +0100326 if (numBytesToPrint) {
327 LOG("\"crc32\": \"%08" PRIx32 "\",\n", crc32);
328 LOG("\"data\":\"");
329
330 for (int i = 0; i < numBytesToPrint - 1; ++i) {
331 /*
332 * Workaround an issue when compiling with GCC where by
333 * printing only a '\n' the produced global output is wrong.
334 */
335 if (i % 15 == 0 && i != 0) {
336 LOG("0x%02x,\n", output->data.uint8[i]);
337 } else {
338 LOG("0x%02x,", output->data.uint8[i]);
339 }
340 }
341
342 LOG("0x%02x\"\n", output->data.uint8[numBytesToPrint - 1]);
343 } else {
344 LOG("\"crc32\": \"%08" PRIx32 "\"\n", crc32);
345 }
346
347 LOG("}");
348}
349
350void InferenceProcess::tfluDebugLog(const char *s) {
351 LOG("%s", s);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200352}
353
354} // namespace InferenceProcess