8a6ec9174120a6ae983addfbb8eb735fb0fe4a66
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / ExecutorBase.h
1 /*
2  * Copyright (c) 2019 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 __ONERT_EXEC_EXECUTOR_BASE_H__
18 #define __ONERT_EXEC_EXECUTOR_BASE_H__
19
20 #include <mutex>
21
22 #include "IPermuteFunction.h"
23 #include "exec/ExecutionObservers.h"
24 #include "ShapeConverter.h"
25 #include "exec/IExecutor.h"
26 #include "compiler/LoweredGraph.h"
27 #include "ir/LowerInfoMap.h"
28 #include "backend/IConfig.h"
29 #include "backend/Backend.h"
30 #include "exec/ExecTime.h"
31 #include "exec/IFunction.h"
32 #include "backend/IDynamicTensorManager.h"
33 #include "backend/ITensorManager.h"
34 #include "exec/ExecutionObservee.h"
35 #include "compiler/TensorRegistries.h"
36 #include <list>
37
38 namespace onert
39 {
40 namespace exec
41 {
42
43 class ExecutorBase : public IExecutor
44 {
45 public:
46   /**
47    * @brief Construct a new ExecutorBase object
48    * @param graph Graph object
49    * @param tensor_builders Tensor builders that are currently used
50    */
51   ExecutorBase(std::unique_ptr<compiler::LoweredGraph> &&lowered_graph,
52                const std::vector<backend::ITensor *> &input_tensors,
53                const std::vector<backend::ITensor *> &output_tensors,
54                const compiler::TensorRegistries &tensor_regs);
55
56   virtual ~ExecutorBase() = default;
57
58   const ir::Graph &graph() final { return _graph; }
59
60   /**
61    * @brief Execute without IODescription
62    *
63    * @param src_tensor Tensor list that will be copied to input tensors of this
64    * @param pre_fn The permutation function that copy from src_tensor to input tensors of this
65    */
66   void execute(const std::vector<backend::ITensor *> &src_tensors,
67                const std::shared_ptr<IPermuteFunction> &pre_fn);
68
69   void execute(const IODescription &desc) final;
70
71   // Used only in Dataflow and Parallel Executors
72   void setIndexedRanks(std::shared_ptr<ir::OperationIndexMap<int64_t>> ranks) final
73   {
74     _indexed_ranks = std::move(ranks);
75   };
76
77   virtual void executeImpl(void) = 0;
78
79   void addObserver(std::unique_ptr<IExecutionObserver> ref) { _subject.add(std::move(ref)); };
80
81   const std::vector<backend::ITensor *> &getInputTensors() const { return _input_tensors; }
82
83   const std::vector<backend::ITensor *> &getOutputTensors() const { return _output_tensors; }
84
85 protected:
86   /**
87    * @brief Returns @c true if any input tensor is dynamic; @c false if all are static tensors
88    */
89   bool hasDynamicInput();
90
91 protected:
92   ExecutionObservee _subject;
93   std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
94   std::unique_ptr<compiler::LoweredGraph> _lowered_graph;
95   const ir::Graph &_graph;
96   std::vector<backend::ITensor *> _input_tensors;
97   std::vector<backend::ITensor *> _output_tensors;
98   std::mutex _mutex;
99
100 private:
101   void handleDynamicInputTensor(ir::IOIndex input_index, const IODescription &desc);
102 };
103
104 } // namespace exec
105 } // namespace onert
106
107 #endif // __ONERT_EXEC_EXECUTOR_BASE_H__