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