blob: f8d7fd89b4bd0b92f76023aee7a0b8862a8540c3 [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
Davide Grohmann30b17b92022-06-14 15:17:18 +020021#include "inference_parser.hpp"
22
Bhavik Patelffe845d2020-11-16 12:13:56 +010023#include <array>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020024#include <queue>
25#include <stdlib.h>
26#include <string>
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020027#include <vector>
Kristofer Jonsson641c0912020-08-31 11:34:14 +020028
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010029struct TfLiteTensor;
30
31namespace tflite {
32// Forward declarations
33class MicroInterpreter;
34class MicroResourceVariables;
35} // namespace tflite
36
Kristofer Jonsson641c0912020-08-31 11:34:14 +020037namespace InferenceProcess {
38struct DataPtr {
39 void *data;
40 size_t size;
41
42 DataPtr(void *data = nullptr, size_t size = 0);
Kristofer Jonsson34e24962020-11-23 16:22:10 +010043
44 void invalidate();
45 void clean();
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010046
47 char *begin() const;
48 char *end() const;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020049};
50
51struct InferenceJob {
52 std::string name;
53 DataPtr networkModel;
Kristofer Jonsson72fa50b2020-09-10 13:26:41 +020054 std::vector<DataPtr> input;
55 std::vector<DataPtr> output;
56 std::vector<DataPtr> expectedOutput;
Jonny Svärd2ebaac72022-05-10 17:29:30 +020057 uint64_t cpuCycles{0};
Kristofer Jonsson641c0912020-08-31 11:34:14 +020058 size_t numBytesToPrint;
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010059 void *externalContext;
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,
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010067 const size_t numBytesToPrint = 0,
68 void *externalContext = nullptr);
Kristofer Jonsson34e24962020-11-23 16:22:10 +010069
70 void invalidate();
71 void clean();
Kristofer Jonsson641c0912020-08-31 11:34:14 +020072};
73
74class InferenceProcess {
75public:
Kristofer Jonsson40d886e2021-12-15 11:16:26 +010076 InferenceProcess(uint8_t *_tensorArena, size_t _tensorArenaSize);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020077
Kristofer Jonsson641c0912020-08-31 11:34:14 +020078 bool runJob(InferenceJob &job);
Kristofer Jonsson641c0912020-08-31 11:34:14 +020079
80private:
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010081 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 Moberg66ed1822021-02-10 08:49:28 +010088 uint8_t *tensorArena;
89 const size_t tensorArenaSize;
Davide Grohmann30b17b92022-06-14 15:17:18 +020090 InferenceParser parser;
Kristofer Jonsson641c0912020-08-31 11:34:14 +020091};
92} // namespace InferenceProcess