Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
| 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 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" |
| 23 | #include "tensorflow/lite/schema/schema_generated.h" |
| 24 | #include "tensorflow/lite/version.h" |
| 25 | |
| 26 | #include "inference_process.hpp" |
| 27 | |
Per Åstrand | d9afc08 | 2020-10-06 13:25:08 +0200 | [diff] [blame] | 28 | #include "cmsis_compiler.h" |
| 29 | |
Per Åstrand | 91a9173 | 2020-09-25 15:04:26 +0200 | [diff] [blame] | 30 | #include <inttypes.h> |
| 31 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 32 | #ifndef TENSOR_ARENA_SIZE |
| 33 | #define TENSOR_ARENA_SIZE (1024) |
| 34 | #endif |
| 35 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 38 | __attribute__((section(".bss.NoInit"), aligned(16))) uint8_t inferenceProcessTensorArena[TENSOR_ARENA_SIZE]; |
| 39 | |
| 40 | namespace { |
Måns Nilsson | 231e1d9 | 2020-11-05 12:19:34 +0100 | [diff] [blame] | 41 | |
| 42 | void tflu_debug_log(const char *s) { |
| 43 | fprintf(stderr, "%s", s); |
| 44 | } |
| 45 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 46 | void print_output_data(TfLiteTensor *output, size_t bytesToPrint) { |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 47 | const int numBytesToPrint = min(output->bytes, bytesToPrint); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 48 | |
| 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 Åstrand | 91a9173 | 2020-09-25 15:04:26 +0200 | [diff] [blame] | 57 | printf("\"data_address\": \"%08" PRIx32 "\",\n", (uint32_t)output->data.data); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 58 | 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 | |
| 69 | bool 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 Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 79 | copy(src.data.uint8, src.data.uint8 + src.bytes, static_cast<uint8_t *>(dst.data)); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 80 | dst.size = src.bytes; |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | namespace InferenceProcess { |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 88 | DataPtr::DataPtr(void *_data, size_t _size) : data(_data), size(_size) {} |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 89 | |
| 90 | InferenceJob::InferenceJob() : numBytesToPrint(0) {} |
| 91 | |
Per Åstrand | bbd9c8f | 2020-09-25 15:07:35 +0200 | [diff] [blame] | 92 | InferenceJob::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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 101 | |
| 102 | InferenceProcess::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? |
| 107 | void 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? |
| 124 | void InferenceProcess::freeLock() { |
| 125 | // Ensure memory operations completed before releasing lock |
| 126 | __DMB(); |
| 127 | |
| 128 | lock = 0; |
| 129 | } |
| 130 | |
| 131 | bool InferenceProcess::push(const InferenceJob &job) { |
| 132 | getLock(); |
| 133 | inferenceJobQueue.push(job); |
| 134 | freeLock(); |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool InferenceProcess::runJob(InferenceJob &job) { |
| 140 | printf("Running inference job: %s\n", job.name.c_str()); |
| 141 | |
| 142 | tflite::MicroErrorReporter microErrorReporter; |
| 143 | tflite::ErrorReporter *reporter = µErrorReporter; |
| 144 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 145 | // Get model handle and verify that the version is correct |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 146 | const tflite::Model *model = ::tflite::GetModel(job.networkModel.data); |
| 147 | if (model->version() != TFLITE_SCHEMA_VERSION) { |
Per Åstrand | 91a9173 | 2020-09-25 15:04:26 +0200 | [diff] [blame] | 148 | printf("Model provided is schema version %" PRIu32 " not equal to supported version %d.\n", |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 149 | model->version(), |
| 150 | TFLITE_SCHEMA_VERSION); |
| 151 | return true; |
| 152 | } |
| 153 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 154 | // Create the TFL micro interpreter |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 155 | tflite::AllOpsResolver resolver; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 156 | tflite::MicroInterpreter interpreter(model, resolver, inferenceProcessTensorArena, TENSOR_ARENA_SIZE, reporter); |
| 157 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 158 | // Allocate tensors |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 159 | 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 Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 165 | // 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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 172 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 173 | } |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 174 | |
| 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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 182 | // 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 Nilsson | 231e1d9 | 2020-11-05 12:19:34 +0100 | [diff] [blame] | 199 | // Register debug log callback for profiling |
| 200 | RegisterDebugLogCallback(tflu_debug_log); |
| 201 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 202 | // Run the inference |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 203 | 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 Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 209 | // 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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 222 | |
| 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 Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 229 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 230 | 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 Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 237 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 238 | printf("]\n"); |
| 239 | printf("output_end\n"); |
| 240 | } |
| 241 | |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 242 | 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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 262 | return true; |
| 263 | } |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 264 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 265 | for (unsigned int j = 0; j < output->bytes; ++j) { |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 266 | 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 Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 271 | j, |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 272 | static_cast<uint8_t *>(expected.data)[j], |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 273 | output->data.uint8[j]); |
| 274 | } |
| 275 | } |
| 276 | } |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 277 | } |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 278 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 279 | printf("Finished running job: %s\n", job.name.c_str()); |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | bool 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 |