overview.md 5.8 KB
Newer Older
X
Xin Pan 已提交
1 2
## Motivation

X
Xin Pan 已提交
3 4
There is a `gap` between the `Program` defined by
user and the `Executable` that can be scheduled
X
Xin Pan 已提交
5 6 7
efficiently on heterogeneous hardware, either locally
or distributedly.

X
Xin Pan 已提交
8
Usually, the `gap` is bridged by
X
Xin Pan 已提交
9 10 11

* A serious transformations with defined order.

X
Xin Pan 已提交
12
* These transformations usually involve
X
Xin Pan 已提交
13
`insert, delete, clustering, split, dependency analysis`.
X
Xin Pan 已提交
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

* Has a simple way to verify and debug each transformation.

* Flexible to add, remove or customize transformations to fit
the requirements of various algorithms (models) and hardware secenarios.

Some other events also push us to a better unified pattern.

* The deep learning framework is built around the concepts of graphs.
To leverage tools such as compilation (e.g. TVM and nGraph) or
cross-framework conversion (e.g. ONNX), we also need a intermediate
representation that can be connected to the rest of the ecosystem.


We need a unified pattern to naturally support the requirements
described above. The pattern should fit both training, inference
and other offline serielized model transformations.
Learned from LLVM and other deep learning framework, we draft the
design below.


## Design

### Major Concepts

#### Node

X
Xin Pan 已提交
41
`Node` represents an operation that performs some computation or
X
Xin Pan 已提交
42 43
a variable that is input or output of operation.

X
Xin Pan 已提交
44
`Node`s are connected to other `Node`s via inputs and outputs.
X
Xin Pan 已提交
45

X
Xin Pan 已提交
46
Other properties (maybe device placement information) can be added
X
Xin Pan 已提交
47 48 49 50
to `Node` in the future if it's a
common requirement of many other `Pass`es. Otherwise, it should live
in a `Node` wrapper class that is private to some `Pass` or be
a local member of a `Pass`.
X
Xin Pan 已提交
51

X
Xin Pan 已提交
52 53
#### Graph

X
Xin Pan 已提交
54
`Graph` contains a list of `Node`s, which are connected to
X
Xin Pan 已提交
55
each other via inputs and outputs.
X
Xin Pan 已提交
56 57 58

TODO: Better definitions for the graph.

X
Xin Pan 已提交
59 60 61 62
`Graph` can also contain `Attribute`s. `Attribute`s
can be `any` thing. For example, it can be a list of "wraper"
nodes. The `wrapper` nodes compose `Node`s and provide
helper method for execution or transformation. `Attribute`
X
Xin Pan 已提交
63
can also contain other things that describe some properties of
X
Xin Pan 已提交
64 65
the `Graph` or `Graph` nodes. `Attribute` can be passed
across `Pass`. However, it should be used with care.
X
Xin Pan 已提交
66

X
Xin Pan 已提交
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
```cpp
class Graph {
 public:
  explicit Graph(const ProgramDesc &program);

  bool Has(const std::string &attr_name) const;

  template <typename AttrType>
  AttrType &Get(const std::string &attr_name) const;

  template <typename AttrType>
  void Set(const std::string &attr_name, AttrType *attr);
  const std::unordered_set<ir::Node *> &Nodes() const;

  // Create a normal variable with non-null VarDesc.
  ir::Node *CreateVarNode(VarDesc *var_desc);

  // Create a normal runnable operator with OpDesc.
  ir::Node *CreateOpNode(OpDesc *op_desc);

  // Create a control dependency var that connects 2 operations. The
  // var doesn't hold any data. Other than that, it's no different from
  // other var, considering dependency analysis.
  ir::Node *CreateControlDepVar();

  // A more free style way of creating a graph node. Mostly use for test
  // or "copy" from another node. Avoid using it if possible.
  ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type);

  // Clear all node information of the graph and return the ownership of the
  // nodes.
  std::vector<std::unique_ptr<ir::Node>> ReleaseNodes();
};
```

X
Xin Pan 已提交
102 103
#### Pass

X
Xin Pan 已提交
104 105 106 107
`Pass` represents a transformation of `Graph`. Its input
is a `Graph` and its output is also a `Graph`. For example,
a `Pass` can simply print out the `Graph`. A `Pass`
can also fuse some `Graph`'s `Node`s.
X
Xin Pan 已提交
108

X
Xin Pan 已提交
109 110 111 112
```cpp
class Pass {
 public:

X
Xin Pan 已提交
113 114 115 116 117 118
  std::unique_ptr<Graph> Apply(std::unique_ptr<Graph> graph) const {
    // Some correctness check.
    auto new_graph = ApplyImpl(std::move(graph));
    // Some correctness check.
    return new_graph;
  }
X
Xin Pan 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131

  // Get a reference to the attributed previously set.
  template <typename AttrType>
  AttrType &Get(const std::string &attr_name) const;

  // Set a pointer to the attribute. Pass takes ownership of the attribute.
  template <typename AttrType>
  void Set(const std::string &attr_name, AttrType *attr) ;

  // Set a pointer to the attribute. Pass doesn't take ownership. Caller
  // should delete the attribute.
  template <typename AttrType>
  void SetNotOwned(const std::string &attr_name, AttrType *attr);
X
Xin Pan 已提交
132 133 134

 protected:
  virtual std::unique_ptr<Graph> ApplyImpl(std::unique_ptr<Graph> graph) const = 0;
X
Xin Pan 已提交
135 136 137 138
};

// In my_pass.cc
class MyPass : public Pass {
X
Xin Pan 已提交
139 140
 protected:
  std::unique_ptr<Graph> ApplyImpl(std::unique_ptr<Graph> graph) const override {
X
Xin Pan 已提交
141 142 143 144
    // do something.
    return graph;
  }
}
X
Xin Pan 已提交
145 146 147
REGISTER_PASS(my_pass, MyPass)
.RequirePassAttr("places")
.RequireGraphAttr("dep_vars");
X
Xin Pan 已提交
148 149 150 151 152 153 154 155 156


// To use the pass.
auto my_pass = ir::PassRegistry::Instance().Get("my_pass");
graph = my_pass->Apply(std::move(graph));
// Note: to force link my_pass.cc, in the code:
USE_PASS(my_pass);
```

X
Xin Pan 已提交
157 158
#### Optimize

X
Xin Pan 已提交
159 160 161
`Optimize` contains a series of `Pass` with defined order.
`Optimize` transforms a `Graph` that only contains raw
modeling logic to a `Graph` that can be run efficiently while
X
Xin Pan 已提交
162 163 164
maintaining the original modeling logic.


X
Xin Pan 已提交
165
### Optimize Process
X
Xin Pan 已提交
166 167 168 169 170 171

* Program is first converted to Graph.
* Graph goes through a series of Pass
* Graph is transformed from raw model logic to a
form that is efficient to execute.

X
Xin Pan 已提交
172 173 174 175 176 177 178 179
```
// Program->ProgramToGraph->Graph->Pass1->Graph->Pass2->Graph->Pass3->Graph->Executor
auto graph = Graph(program);
graph = PassRegistry::Instance().Get("op_fuse_pass").Apply(std::move(grah));
// For more complex Pass, Optimize Process can provide Pass attributes.
auto mem_opt_pass = PassRegistry::Instance().Get("memory_optimization_pass");
mem_opt_pass.SetNotOwned<int>("optimize_level", 1);
mem_opt_pass->Apply(std::move(graph));
X
Xin Pan 已提交
180 181
graph = PassRegistry::Instance().Get("multi_devices_pass").Apply(std::move(grah));
graph = PassRegistry::Instance().Get("multi_devices_check_pass").Apply(std::move(grah));
X
Xin Pan 已提交
182 183 184 185
Executor exe;
exe.Run(graph);

```