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