Imported Upstream version 1.12.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 "IPermuteFunction.h"
21 #include "exec/IExecutor.h"
22 #include "exec/ExecTime.h"
23 #include "exec/ExecutionObservee.h"
24 #include "exec/IFunction.h"
25 #include "exec/IODescription.h"
26 #include "ir/Graph.h"
27 #include "ir/Index.h"
28 #include "ir/LowerInfoMap.h"
29 #include "ir/OperationIndexMap.h"
30 #include "compiler/LoweredGraph.h"
31 #include "compiler/TensorRegistries.h"
32 #include "backend/controlflow/IOTensor.h"
33 #include "util/TracingCtx.h"
34
35 #include <cstdint>
36 #include <memory>
37 #include <mutex>
38 #include <vector>
39
40 namespace onert
41 {
42 namespace exec
43 {
44
45 class ExecutorBase : public IExecutor
46 {
47 public:
48   /**
49    * @brief Construct a new ExecutorBase object
50    * @param graph Graph object
51    * @param tensor_builders Tensor builders that are currently used
52    */
53   ExecutorBase(std::unique_ptr<compiler::LoweredGraph> &&lowered_graph,
54                const compiler::TensorRegistries &tensor_regs, const util::TracingCtx *tracing_ctx);
55
56   virtual ~ExecutorBase() = default;
57
58   const ir::Graph &graph() final { return _graph; }
59
60   void execute(const IODescription &desc) final;
61
62   void execute(const std::vector<backend::IPortableTensor *> &inputs,
63                const std::vector<backend::IPortableTensor *> &outputs) override;
64
65   // Used only in Dataflow and Parallel Executors
66   void setIndexedRanks(std::shared_ptr<ir::OperationIndexMap<int64_t>> ranks) final
67   {
68     _indexed_ranks = std::move(ranks);
69   };
70
71   virtual void executeImpl(void) = 0;
72
73   void addObserver(std::unique_ptr<IExecutionObserver> ref) { _subject.add(std::move(ref)); };
74
75   const std::vector<backend::controlflow::IOTensor *> &getOutputTensors() const override
76   {
77     return _output_tensors;
78   }
79
80 protected:
81   /**
82    * @brief Returns @c true if any input tensor is dynamic; @c false if all are static tensors
83    */
84   bool hasDynamicInput();
85
86 protected:
87   ExecutionObservee _subject;
88   std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
89   std::unique_ptr<compiler::LoweredGraph> _lowered_graph;
90   const ir::Graph &_graph;
91   std::vector<backend::controlflow::IOTensor *> _input_tensors;
92   std::vector<backend::controlflow::IOTensor *> _output_tensors;
93   std::mutex _mutex;
94   const util::TracingCtx *_tracing_ctx;
95
96 private:
97   void handleDynamicInputTensor(ir::IOIndex input_index, const IODescription &desc);
98 };
99
100 } // namespace exec
101 } // namespace onert
102
103 #endif // __ONERT_EXEC_EXECUTOR_BASE_H__