blob: 7743f8c4008e6c280338d2e9c3340e7e522ca639 [file] [log] [blame]
Kristofer Jonsson641c0912020-08-31 11:34:14 +02001/*
2 * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
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 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"
23#include "tensorflow/lite/schema/schema_generated.h"
24#include "tensorflow/lite/version.h"
25
26#include "inference_process.hpp"
27
Per Åstrandd9afc082020-10-06 13:25:08 +020028#include "cmsis_compiler.h"
29
Per Åstrand91a91732020-09-25 15:04:26 +020030#include <inttypes.h>
31
Kristofer Jonsson641c0912020-08-31 11:34:14 +020032#ifndef TENSOR_ARENA_SIZE
33#define TENSOR_ARENA_SIZE (1024)
34#endif
35
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020036using namespace std;
37
Kristofer Jonsson641c0912020-08-31 11:34:14 +020038__attribute__((section(".bss.NoInit"), aligned(16))) uint8_t inferenceProcessTensorArena[TENSOR_ARENA_SIZE];
39
40namespace {
Måns Nilsson231e1d92020-11-05 12:19:34 +010041
42void tflu_debug_log(const char *s) {
43 fprintf(stderr, "%s", s);
44}
45
Kristofer Jonsson641c0912020-08-31 11:34:14 +020046void print_output_data(TfLiteTensor *output, size_t bytesToPrint) {
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020047 const int numBytesToPrint = min(output->bytes, bytesToPrint);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020048
49 int dims_size = output->dims->size;
50 printf("{\n");
51 printf("\"dims\": [%d,", dims_size);
52 for (int i = 0; i < output->dims->size - 1; ++i) {
53 printf("%d,", output->dims->data[i]);
54 }
55 printf("%d],\n", output->dims->data[dims_size - 1]);
56
Per Åstrand91a91732020-09-25 15:04:26 +020057 printf("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020058 printf("\"data\":\"");
59 for (int i = 0; i < numBytesToPrint - 1; ++i) {
60 if (i % 16 == 0 && i != 0) {
61 printf("\n");
62 }
63 printf("0x%02x,", output->data.uint8[i]);
64 }
65 printf("0x%02x\"\n", output->data.uint8[numBytesToPrint - 1]);
66 printf("}");
67}
68
69bool copyOutput(const TfLiteTensor &src, InferenceProcess::DataPtr &dst) {
70 if (dst.data == nullptr) {
71 return false;
72 }
73
74 if (src.bytes > dst.size) {
75 printf("Tensor size %d does not match output size %d.\n", src.bytes, dst.size);
76 return true;
77 }
78
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020079 copy(src.data.uint8, src.data.uint8 + src.bytes, static_cast<uint8_t *>(dst.data));
Kristofer Jonsson641c0912020-08-31 11:34:14 +020080 dst.size = src.bytes;
81
82 return false;
83}
84
85} // namespace
86
87namespace InferenceProcess {
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020088DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +020089
90InferenceJob::InferenceJob() : numBytesToPrint(0) {}
91
Per Åstrandbbd9c8f2020-09-25 15:07:35 +020092InferenceJob::InferenceJob(const string &_name,
93 const DataPtr &_networkModel,
94 const vector<DataPtr> &_input,
95 const vector<DataPtr> &_output,
96 const vector<DataPtr> &_expectedOutput,
97 size_t _numBytesToPrint) :
98 name(_name),
99 networkModel(_networkModel), input(_input), output(_output), expectedOutput(_expectedOutput),
100 numBytesToPrint(_numBytesToPrint) {}
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200101
102InferenceProcess::InferenceProcess() : lock(0) {}
103
104// NOTE: Adding code for get_lock & free_lock with some corrections from
105// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHEJCHB.html
106// TODO: check correctness?
107void InferenceProcess::getLock() {
108 int status = 0;
109
110 do {
111 // Wait until lock_var is free
112 while (__LDREXW(&lock) != 0)
113 ;
114
115 // Try to set lock_var
116 status = __STREXW(1, &lock);
117 } while (status != 0);
118
119 // Do not start any other memory access until memory barrier is completed
120 __DMB();
121}
122
123// TODO: check correctness?
124void InferenceProcess::freeLock() {
125 // Ensure memory operations completed before releasing lock
126 __DMB();
127
128 lock = 0;
129}
130
131bool InferenceProcess::push(const InferenceJob &job) {
132 getLock();
133 inferenceJobQueue.push(job);
134 freeLock();
135
136 return true;
137}
138
139bool InferenceProcess::runJob(InferenceJob &job) {
140 printf("Running inference job: %s\n", job.name.c_str());
141
142 tflite::MicroErrorReporter microErrorReporter;
143 tflite::ErrorReporter *reporter = &microErrorReporter;
144
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200145 // Get model handle and verify that the version is correct
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200146 const tflite::Model *model = ::tflite::GetModel(job.networkModel.data);
147 if (model->version() != TFLITE_SCHEMA_VERSION) {
Per Åstrand91a91732020-09-25 15:04:26 +0200148 printf("Model provided is schema version %" PRIu32 " not equal to supported version %d.\n",
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200149 model->version(),
150 TFLITE_SCHEMA_VERSION);
151 return true;
152 }
153
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200154 // Create the TFL micro interpreter
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200155 tflite::AllOpsResolver resolver;
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200156 tflite::MicroInterpreter interpreter(model, resolver, inferenceProcessTensorArena, TENSOR_ARENA_SIZE, reporter);
157
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200158 // Allocate tensors
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200159 TfLiteStatus allocate_status = interpreter.AllocateTensors();
160 if (allocate_status != kTfLiteOk) {
161 printf("AllocateTensors failed for inference job: %s\n", job.name.c_str());
162 return true;
163 }
164
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200165 // Create a filtered list of non empty input tensors
166 vector<TfLiteTensor *> inputTensors;
167 for (size_t i = 0; i < interpreter.inputs_size(); ++i) {
168 TfLiteTensor *tensor = interpreter.input(i);
169
170 if (tensor->bytes > 0) {
171 inputTensors.push_back(tensor);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200172 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200173 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200174
175 if (job.input.size() != inputTensors.size()) {
176 printf("Number of input buffers does not match number of non empty network tensors. input=%zu, network=%zu\n",
177 job.input.size(),
178 inputTensors.size());
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200179 return true;
180 }
181
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200182 // Copy input data
183 for (size_t i = 0; i < inputTensors.size(); ++i) {
184 const DataPtr &input = job.input[i];
185 const TfLiteTensor *tensor = inputTensors[i];
186
187 if (input.size != tensor->bytes) {
188 printf("Input size does not match network size. job=%s, index=%zu, input=%zu, network=%u\n",
189 job.name.c_str(),
190 i,
191 input.size,
192 tensor->bytes);
193 return true;
194 }
195
196 copy(static_cast<char *>(input.data), static_cast<char *>(input.data) + input.size, tensor->data.uint8);
197 }
198
Måns Nilsson231e1d92020-11-05 12:19:34 +0100199 // Register debug log callback for profiling
200 RegisterDebugLogCallback(tflu_debug_log);
201
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200202 // Run the inference
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200203 TfLiteStatus invoke_status = interpreter.Invoke();
204 if (invoke_status != kTfLiteOk) {
205 printf("Invoke failed for inference job: %s\n", job.name.c_str());
206 return true;
207 }
208
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200209 // Copy output data
210 if (job.output.size() > 0) {
211 if (interpreter.outputs_size() != job.output.size()) {
212 printf("Number of outputs mismatch. job=%zu, network=%u\n", job.output.size(), interpreter.outputs_size());
213 return true;
214 }
215
216 for (unsigned i = 0; i < interpreter.outputs_size(); ++i) {
217 if (copyOutput(*interpreter.output(i), job.output[i])) {
218 return true;
219 }
220 }
221 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200222
223 if (job.numBytesToPrint > 0) {
224 // Print all of the output data, or the first NUM_BYTES_TO_PRINT bytes,
225 // whichever comes first as well as the output shape.
226 printf("num_of_outputs: %d\n", interpreter.outputs_size());
227 printf("output_begin\n");
228 printf("[\n");
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200229
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200230 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
231 TfLiteTensor *output = interpreter.output(i);
232 print_output_data(output, job.numBytesToPrint);
233 if (i != interpreter.outputs_size() - 1) {
234 printf(",\n");
235 }
236 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200237
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200238 printf("]\n");
239 printf("output_end\n");
240 }
241
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200242 if (job.expectedOutput.size() > 0) {
243 if (job.expectedOutput.size() != interpreter.outputs_size()) {
244 printf("Expeded number of output tensors does not match network. job=%s, expected=%zu, network=%zu\n",
245 job.name.c_str(),
246 job.expectedOutput.size(),
247 interpreter.outputs_size());
248 return true;
249 }
250
251 for (unsigned int i = 0; i < interpreter.outputs_size(); i++) {
252 const DataPtr &expected = job.expectedOutput[i];
253 const TfLiteTensor *output = interpreter.output(i);
254
255 if (expected.size != output->bytes) {
256 printf(
257 "Expected tensor size does not match network size. job=%s, index=%u, expected=%zu, network=%zu\n",
258 job.name.c_str(),
259 i,
260 expected.size,
261 output->bytes);
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200262 return true;
263 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200264
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200265 for (unsigned int j = 0; j < output->bytes; ++j) {
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200266 if (output->data.uint8[j] != static_cast<uint8_t *>(expected.data)[j]) {
267 printf("Expected tensor size does not match network size. job=%s, index=%u, offset=%u, "
268 "expected=%02x, network=%02x\n",
269 job.name.c_str(),
270 i,
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200271 j,
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200272 static_cast<uint8_t *>(expected.data)[j],
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200273 output->data.uint8[j]);
274 }
275 }
276 }
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200277 }
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +0200278
Kristofer Jonsson641c0912020-08-31 11:34:14 +0200279 printf("Finished running job: %s\n", job.name.c_str());
280
281 return false;
282}
283
284bool InferenceProcess::run(bool exitOnEmpty) {
285 bool anyJobFailed = false;
286
287 while (true) {
288 getLock();
289 bool empty = inferenceJobQueue.empty();
290 freeLock();
291
292 if (empty) {
293 if (exitOnEmpty) {
294 printf("Exit from InferenceProcess::run() on empty job queue!\n");
295 break;
296 }
297
298 continue;
299 }
300
301 getLock();
302 InferenceJob job = inferenceJobQueue.front();
303 inferenceJobQueue.pop();
304 freeLock();
305
306 if (runJob(job)) {
307 anyJobFailed = true;
308 continue;
309 }
310 }
311
312 return anyJobFailed;
313}
314
315} // namespace InferenceProcess