blob: 6ab453c799af92b0fe5787a8bbca914b11157dad [file] [log] [blame]
Kristofer Jonsson641c0912020-08-31 11:34:14 +02001/*
Anton Moberge348f8f2021-03-31 11:08:58 +02002 * Copyright (c) 2019-2021 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;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020055 size_t numBytesToPrint;
Bhavik Patelffe845d2020-11-16 12:13:56 +010056 std::vector<uint8_t> pmuEventConfig;
Jonny Svärd4c11a482021-12-17 17:04:08 +010057 bool pmuCycleCounterEnable;
Bhavik Patelffe845d2020-11-16 12:13:56 +010058 std::vector<uint32_t> pmuEventCount;
59 uint64_t pmuCycleCounterCount;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020060
61 InferenceJob();
62 InferenceJob(const std::string &name,
63 const DataPtr &networkModel,
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020064 const std::vector<DataPtr> &input,
65 const std::vector<DataPtr> &output,
66 const std::vector<DataPtr> &expectedOutput,
Bhavik Patelffe845d2020-11-16 12:13:56 +010067 size_t numBytesToPrint,
68 const std::vector<uint8_t> &pmuEventConfig,
Jonny Svärd4c11a482021-12-17 17:04:08 +010069 const bool pmuCycleCounterEnable);
Kristofer Jonsson34e24962020-11-23 16:22:10 +010070
71 void invalidate();
72 void clean();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020073};
74
75class InferenceProcess {
76public:
Kristofer Jonsson40d886e2021-12-15 11:16:26 +010077 InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020078
Kristofer Jonsson641c0912020-08-31 11:34:14 +020079 bool runJob(InferenceJob &job);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020080
81private:
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010082 static bool copyIfm(InferenceJob &job, tflite::MicroInterpreter &interpreter);
83 static bool copyOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter);
84 static bool compareOfm(InferenceJob &job, tflite::MicroInterpreter &interpreter);
85 static void printJob(InferenceJob &job, tflite::MicroInterpreter &interpreter);
86 static void printOutputTensor(TfLiteTensor *output, size_t bytesToPrint);
87 static void tfluDebugLog(const char *s);
88
Anton Moberg66ed1822021-02-10 08:49:28 +010089 uint8_t *tensorArena;
90 const size_t tensorArenaSize;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020091};
92} // namespace InferenceProcess