Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. All rights reserved. |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 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 | #pragma once |
| 20 | |
Davide Grohmann | 30b17b9 | 2022-06-14 15:17:18 +0200 | [diff] [blame^] | 21 | #include "inference_parser.hpp" |
| 22 | |
Bhavik Patel | ffe845d | 2020-11-16 12:13:56 +0100 | [diff] [blame] | 23 | #include <array> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 24 | #include <queue> |
| 25 | #include <stdlib.h> |
| 26 | #include <string> |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 27 | #include <vector> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 28 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 29 | struct TfLiteTensor; |
| 30 | |
| 31 | namespace tflite { |
| 32 | // Forward declarations |
| 33 | class MicroInterpreter; |
| 34 | class MicroResourceVariables; |
| 35 | } // namespace tflite |
| 36 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 37 | namespace InferenceProcess { |
| 38 | struct DataPtr { |
| 39 | void *data; |
| 40 | size_t size; |
| 41 | |
| 42 | DataPtr(void *data = nullptr, size_t size = 0); |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 43 | |
| 44 | void invalidate(); |
| 45 | void clean(); |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 46 | |
| 47 | char *begin() const; |
| 48 | char *end() const; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | struct InferenceJob { |
| 52 | std::string name; |
| 53 | DataPtr networkModel; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 54 | std::vector<DataPtr> input; |
| 55 | std::vector<DataPtr> output; |
| 56 | std::vector<DataPtr> expectedOutput; |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 57 | uint64_t cpuCycles{0}; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 58 | size_t numBytesToPrint; |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 59 | void *externalContext; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 60 | |
| 61 | InferenceJob(); |
| 62 | InferenceJob(const std::string &name, |
| 63 | const DataPtr &networkModel, |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 64 | const std::vector<DataPtr> &input, |
| 65 | const std::vector<DataPtr> &output, |
| 66 | const std::vector<DataPtr> &expectedOutput, |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 67 | const size_t numBytesToPrint = 0, |
| 68 | void *externalContext = nullptr); |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 69 | |
| 70 | void invalidate(); |
| 71 | void clean(); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | class InferenceProcess { |
| 75 | public: |
Kristofer Jonsson | 40d886e | 2021-12-15 11:16:26 +0100 | [diff] [blame] | 76 | InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 77 | |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 78 | bool runJob(InferenceJob &job); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 79 | |
| 80 | private: |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 81 | static bool copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter); |
| 82 | static bool copyOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter); |
| 83 | static bool compareOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter); |
| 84 | static void printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter); |
| 85 | static void printOutputTensor(TfLiteTensor *output, size_t bytesToPrint); |
| 86 | static void tfluDebugLog(const char *s); |
| 87 | |
Anton Moberg | 66ed182 | 2021-02-10 08:49:28 +0100 | [diff] [blame] | 88 | uint8_t *tensorArena; |
| 89 | const size_t tensorArenaSize; |
Davide Grohmann | 30b17b9 | 2022-06-14 15:17:18 +0200 | [diff] [blame^] | 90 | InferenceParser parser; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 91 | }; |
| 92 | } // namespace InferenceProcess |