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