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