summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/include/graph/Graph.h
blob: 909bba9e579780c3b9e64be9b6b1247fee3b2c48 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * 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 __NEURUN_GRAPH_GRAPH_H__
#define __NEURUN_GRAPH_GRAPH_H__

#include <functional>

#include "model/operation/Node.h"
#include "model/Model.h"
#include "graph/LowerInfoMap.h"
#include "model/operation/Subgraph.h"

namespace neurun
{
namespace graph
{
namespace operand
{
class LowerInfo;
} // namespace operand
} // namespace graph
} // namespace neurun

namespace neurun
{
namespace linear
{
class Linear;
} // namespace linear
} // namespace neurun

namespace neurun
{
namespace compiler
{
class BackendResolver;
} // namespace compiler
} // namespace neurun

namespace neurun
{
namespace graph
{

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

public:
  template <bool is_const> class Iterator
  {
  public:
    using GraphRef = typename std::conditional<is_const, const Graph &, Graph &>::type;
    using IndexRef = const model::operation::Index &;
    using NodeRef = typename std::conditional<is_const, const model::operation::Node &,
                                              model::operation::Node &>::type;
    using IterFn = std::function<void(IndexRef, NodeRef)>;

  public:
    virtual ~Iterator() = default;
    virtual void iterate(GraphRef graph, const IterFn &fn) const = 0;
  };

  template <bool is_const = false> class DefaultIterator final : public Iterator<is_const>
  {
  public:
    using GraphRef = typename Iterator<is_const>::GraphRef;
    using IndexRef = typename Iterator<is_const>::IndexRef;
    using NodeRef = typename Iterator<is_const>::NodeRef;
    using IterFn = typename Iterator<is_const>::IterFn;

  public:
    void iterate(GraphRef graph, const IterFn &fn) const;
  };
  using DefaultConstIterator = DefaultIterator<true>;

  template <bool is_const = false> class PostDfsIterator final : public Iterator<is_const>
  {
  public:
    using GraphRef = typename Iterator<is_const>::GraphRef;
    using IndexRef = typename Iterator<is_const>::IndexRef;
    using NodeRef = typename Iterator<is_const>::NodeRef;
    using IterFn = typename Iterator<is_const>::IterFn;

  public:
    void iterate(GraphRef graph, const IterFn &fn) const;
  };
  using PostDfsConstIterator = PostDfsIterator<true>;

public:
  Graph(void) = delete;
  Graph(std::unique_ptr<model::Model> &&model);
  ~Graph(void);

  // Graph Building
public:
  model::operand::Index addOperand(const model::operand::Shape &shape,
                                   const model::operand::TypeInfo &type);
  model::operation::Index addOperation(std::unique_ptr<model::operation::Node> &&node);
  void setOperandValue(const model::operand::Index &ind,
                       std::unique_ptr<model::operand::Data> &&data);
  void addInput(const model::operand::Index &ind);
  void addOutput(const model::operand::Index &ind);
  void finishBuilding(void);
  void lower(void);
  void removeOperand(const model::operand::Index &ind) { _model->operands.remove(ind); }
  std::unique_ptr<linear::Linear> linearize(void);
  bool isBuildingPhase(void) const { return _phase == Phase::BUILDING; }
  std::shared_ptr<const model::Model> shareModel() { return _model; }
  std::unique_ptr<graph::LowerInfoMap> releaseLowerInfo() { return std::move(_lower_info_map); }
  // TODO Change this to releaseSubgraphContext()
  std::unique_ptr<std::vector<std::unique_ptr<model::operation::Subgraph>>>
  releaseSubgraphSequence()
  {
    return std::move(_subg_seq);
  }

private:
  void initializeUseDef();

  // Accessors
public:
  const model::operand::IndexSet &getInputs() const { return _model->inputs; }
  model::operand::IndexSet &getInputs() { return _model->inputs; }
  const model::operand::IndexSet &getOutputs() const { return _model->outputs; }
  model::operand::IndexSet &getOutputs() { return _model->outputs; }
  const model::operand::Set &operands() const { return _model->operands; }
  model::operand::Set &operands()
  {
    return _model->operands;
  } // TODO Remove this non-const accessor
  const model::operation::Set &operations() const { return _model->operations; }
  model::operation::Set &operations() { return _model->operations; }
  const compiler::BackendResolver *backend_resolver() const { return _backend_resolver.get(); }

private:
  Phase _phase{Phase::BUILDING};
  std::shared_ptr<model::Model> _model;

  // For LOWERED phase
public:
  const operation::LowerInfo *getLowerInfo(const model::operation::Index &index) const;
  void setLowerInfo(const model::operation::Index &index,
                    std::unique_ptr<operation::LowerInfo> &&lower_info);
  const operand::LowerInfo *getLowerInfo(const model::operand::Index &index) const;
  operand::LowerInfo *getLowerInfo(const model::operand::Index &index);
  void setLowerInfo(const model::operand::Index &index,
                    std::unique_ptr<operand::LowerInfo> &&lower_info);

private:
  std::unique_ptr<compiler::BackendResolver> _backend_resolver;
  std::unique_ptr<LowerInfoMap> _lower_info_map;

  // For Subgraph
private:
  void partition();

private:
  std::unique_ptr<std::vector<std::unique_ptr<model::operation::Subgraph>>> _subg_seq;
};

} // namespace graph
} // namespace neurun

#endif // __NEURUN_GRAPH_GRAPH_H__