blob: fc54ae0cc76c2fe7ef9d53005134a5ff033cbcff [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#pragma once
20
Bhavik Patelffe845d2020-11-16 12:13:56 +010021#include <array>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020022#include <queue>
23#include <stdlib.h>
24#include <string>
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020025#include <vector>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020026
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010027struct TfLiteTensor;
28
29namespace tflite {
30// Forward declarations
31class MicroInterpreter;
32class MicroResourceVariables;
33} // namespace tflite
34
Kristofer Jonsson641c0912020-08-31 11:34:14 +020035namespace InferenceProcess {
36struct DataPtr {
37 void *data;
38 size_t size;
39
40 DataPtr(void *data = nullptr, size_t size = 0);
Kristofer Jonsson34e24962020-11-23 16:22:10 +010041
42 void invalidate();
43 void clean();
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010044
45 char *begin() const;
46 char *end() const;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020047};
48
49struct InferenceJob {
50 std::string name;
51 DataPtr networkModel;
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020052 std::vector<DataPtr> input;
53 std::vector<DataPtr> output;
54 std::vector<DataPtr> expectedOutput;
Jonny Svärd2ebaac72022-05-10 17:29:30 +020055 uint64_t cpuCycles{0};
Kristofer Jonsson641c0912020-08-31 11:34:14 +020056 size_t numBytesToPrint;
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010057 void *externalContext;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020058
59 InferenceJob();
60 InferenceJob(const std::string &name,
61 const DataPtr &networkModel,
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020062 const std::vector<DataPtr> &input,
63 const std::vector<DataPtr> &output,
64 const std::vector<DataPtr> &expectedOutput,
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010065 const size_t numBytesToPrint = 0,
66 void *externalContext = nullptr);
Kristofer Jonsson34e24962020-11-23 16:22:10 +010067
68 void invalidate();
69 void clean();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020070};
71
72class InferenceProcess {
73public:
Kristofer Jonsson40d886e2021-12-15 11:16:26 +010074 InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020075
Kristofer Jonsson641c0912020-08-31 11:34:14 +020076 bool runJob(InferenceJob &job);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020077
78private:
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010079 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 Moberg66ed1822021-02-10 08:49:28 +010086 uint8_t *tensorArena;
87 const size_t tensorArenaSize;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020088};
89} // namespace InferenceProcess