summaryrefslogtreecommitdiff
path: root/runtime/onert/core/include/ir/Graph.h
blob: a61d1861c73f67cc67ecdabb9924562aeb6deb41 (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
109
110
111
112
113
114
/*
 * Copyright (c) 2018 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_IR_GRAPH_H__
#define __ONERT_IR_GRAPH_H__

#include <functional>
#include <unordered_map>

#include "ir/Operands.h"
#include "ir/Operations.h"
#include "ir/OpSequence.h"
#include "ir/OpSequences.h"

namespace onert
{
namespace backend
{
namespace custom
{
class IKernelBuilder;
} // namespace custom
} // namespace backend
} // namespace onert

namespace onert
{
namespace ir
{

class Graph
{
private:
  enum class Phase
  {
    BUILDING,
    MODEL
  };

public:
  Graph(void);
  ~Graph(void);

  // Graph Building
public:
  OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
  OperationIndex addOperation(std::unique_ptr<Operation> &&node);
  void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
  void addInput(const OperandIndex &ind);
  void addOutput(const OperandIndex &ind);
  void finishBuilding(void);
  void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
  bool isBuildingPhase(void) const { return _phase == Phase::BUILDING; }
  void setLayout(Layout layout) { _layout = layout; }

private:
  void initializeUseDef();

  // Custom operations support
public:
  void
  bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
  {
    _kernel_builder = kernel_builder;
  }

  const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
  {
    return _kernel_builder;
  }

private:
  std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;

  // Accessors
public:
  const OperandIndexSequence &getInputs() const { return _inputs; }
  OperandIndexSequence &getInputs() { return _inputs; }
  const OperandIndexSequence &getOutputs() const { return _outputs; }
  OperandIndexSequence &getOutputs() { return _outputs; }
  const Operands &operands() const { return _operands; }
  Operands &operands() { return _operands; } // TODO Remove this non-const accessor
  const Operations &operations() const { return _operations; }
  Operations &operations() { return _operations; }
  Layout layout() { return _layout; }

private:
  Phase _phase{Phase::BUILDING};
  Operations _operations;
  Operands _operands;
  OperandIndexSequence _inputs;
  OperandIndexSequence _outputs;
  std::unordered_map<SubgraphIndex, std::shared_ptr<Graph>> _subgraphs;
  // TFLite and circle's default layout is NHWC;
  Layout _layout{Layout::NHWC};
};

} // namespace ir
} // namespace onert

#endif // __ONERT_IR_GRAPH_H__