summaryrefslogtreecommitdiff
path: root/runtime/onert/core/src/exec/ExecutorBase.h
blob: c0f609d1113b8ba7a636a6d0bad1a9da5519102f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __ONERT_EXEC_EXECUTOR_BASE_H__
#define __ONERT_EXEC_EXECUTOR_BASE_H__

#include "IPermuteFunction.h"
#include "exec/IExecutor.h"
#include "exec/ExecTime.h"
#include "exec/ExecutionObservee.h"
#include "exec/IFunction.h"
#include "exec/IODescription.h"
#include "ir/Graph.h"
#include "ir/Index.h"
#include "compiler/GraphLowerInfo.h"
#include "ir/OperationIndexMap.h"
#include "compiler/LoweredGraph.h"
#include "compiler/TensorRegistries.h"
#include "backend/builtin/IOTensor.h"
#include "util/TracingCtx.h"

#include <cstdint>
#include <memory>
#include <mutex>
#include <vector>

namespace onert
{
namespace exec
{

class ExecutorBase : public IExecutor
{
public:
  /**
   * @brief Construct a new ExecutorBase object
   * @param graph Graph object
   * @param tensor_builders Tensor builders that are currently used
   */
  ExecutorBase(std::unique_ptr<compiler::LoweredGraph> &&lowered_graph,
               backend::BackendContexts &&backend_contexts,
               const compiler::TensorRegistries &tensor_regs, const util::TracingCtx *tracing_ctx);

  virtual ~ExecutorBase() = default;

  const ir::Graph &graph() final { return _graph; }

  const ir::Graph &parent_graph() final { return _parent_graph; }

  void execute(const IODescription &desc) final;

  void execute(const std::vector<backend::IPortableTensor *> &inputs,
               const std::vector<backend::IPortableTensor *> &outputs) override;

  // Used only in Dataflow and Parallel Executors
  void setIndexedRanks(std::shared_ptr<ir::OperationIndexMap<int64_t>> ranks) final
  {
    _indexed_ranks = std::move(ranks);
  };

  virtual void executeImpl(void) = 0;

  void addObserver(std::unique_ptr<IExecutionObserver> ref) { _subject.add(std::move(ref)); };

  const std::vector<backend::builtin::IOTensor *> &getOutputTensors() const override
  {
    return _output_tensors;
  }

protected:
  /**
   * @brief Returns @c true if any input tensor is dynamic; @c false if all are static tensors
   */
  bool hasDynamicInput();

protected:
  ExecutionObservee _subject;
  std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
  std::unique_ptr<compiler::LoweredGraph> _lowered_graph;
  backend::BackendContexts _backend_contexts;
  const ir::Graph &_graph;
  const ir::Graph &_parent_graph;
  std::vector<backend::builtin::IOTensor *> _input_tensors;
  std::vector<backend::builtin::IOTensor *> _output_tensors;
  std::mutex _mutex;
  const util::TracingCtx *_tracing_ctx;

private:
  void handleDynamicInputTensor(ir::IOIndex input_index, const IODescription &desc);
};

} // namespace exec
} // namespace onert

#endif // __ONERT_EXEC_EXECUTOR_BASE_H__