Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / include / luci_interpreter / Interpreter.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef LUCI_INTERPRETER_INTERPRETER_H
18 #define LUCI_INTERPRETER_INTERPRETER_H
19
20 #include "luci_interpreter/core/Tensor.h"
21
22 #include <luci/IR/Nodes/CircleInput.h>
23 #include <luci/IR/Nodes/CircleOutput.h>
24
25 #include "luci_interpreter/MemoryManager.h"
26 #include <luci/IR/Module.h>
27
28 #include <memory>
29 #include <vector>
30 #include <unordered_map>
31
32 namespace luci_interpreter
33 {
34
35 class ExecutionObserver
36 {
37 public:
38   virtual ~ExecutionObserver();
39
40   // Called when the value of a tensor has been updated during execution.
41   virtual void postTensorWrite(const luci::CircleNode *node, const Tensor *tensor);
42
43   // Called before / after executing an operator.
44   // Note that these methods are not called for auxiliary operators (CircleInput, CircleOutput,
45   // CircleConst and Circle*Out).
46   virtual void preOperatorExecute(const luci::CircleNode *node);
47   virtual void postOperatorExecute(const luci::CircleNode *node);
48 };
49
50 class Interpreter
51 {
52 public:
53   explicit Interpreter(const luci::Module *module, IMemoryManager *memory_manager = nullptr);
54
55   ~Interpreter();
56
57   void writeInputTensor(const luci::CircleInput *input_node, const void *data, size_t data_size);
58
59   void readOutputTensor(const luci::CircleOutput *output_node, void *data, size_t data_size);
60
61   void interpret();
62
63   void attachObserver(ExecutionObserver *observer);
64
65   const Tensor *getTensor(const loco::Node *node) { return _node_to_tensor[node]; }
66
67 private:
68   // _default_memory_manager should be before _runtime_module due to
69   // the order of deletion in the destructor
70   std::unique_ptr<IMemoryManager> _default_memory_manager = nullptr;
71   std::unique_ptr<class RuntimeModule> _runtime_module;
72   IMemoryManager *_memory_manager = nullptr;
73
74   // Observer functionality support.
75   std::unique_ptr<struct RuntimeToIR> _runtime_to_ir;
76   std::unordered_map<const loco::Node *, Tensor *> _node_to_tensor;
77   std::unique_ptr<class EventNotifier> _event_notifier;
78   std::vector<ExecutionObserver *> _observers;
79 };
80
81 } // namespace luci_interpreter
82
83 #endif // LUCI_INTERPRETER_INTERPRETER_H