Skip to content

Commit d96ee24

Browse files
authored
Merge pull request #12697 from panyx0718/ir2
test and doc IR Graph
2 parents 0a641ba + 891c3c0 commit d96ee24

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

paddle/fluid/framework/ir/graph.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ namespace paddle {
2828
namespace framework {
2929
namespace ir {
3030

31+
/*
32+
* The graph is a Directed Acyclic Single Static Assignment Graph.
33+
*
34+
* In more detail, the following properties must hold:
35+
*
36+
* The graph shouldn't contain cycle. Each node is a black-box to the graph
37+
* so the node itself could be a loop operator.
38+
*
39+
* Each Variable-type node has only one input (thus single static assignment).
40+
*
41+
* The output/input of operator is variable and the output/input of variable
42+
* is operator.
43+
*
44+
* The following data harzards in Program are addressed in the Graph:
45+
*
46+
* Write-After-Read
47+
* a = op1(x)
48+
* x = op2(b)
49+
* A control-dependency connection is created bettwen op1 and op2 such that
50+
* op1->op2, so as to ensure correct order.
51+
*
52+
* Write-After-Write
53+
* x = op1(a)
54+
* x = op2(b)
55+
* A control-dependency connection is created between op1 and op2 such that
56+
* op1->op2, so as to ensure correct order.
57+
*
58+
* Other properties currently hold, but is not enforced yet:
59+
*
60+
* Variable-type node (not control dep) with the same variable name share
61+
* the same underlying VarDesc.
62+
*/
3163
class Graph {
3264
public:
3365
explicit Graph(const ProgramDesc &program);

paddle/fluid/framework/ir/graph_test.cc

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SumOpMaker : public OpProtoAndCheckerMaker {
3636
public:
3737
void Make() {
3838
AddInput("X", "").AsDuplicable();
39-
AddOutput("Out", "");
39+
AddOutput("Out", "").AsDuplicable();
4040
AddComment("");
4141
}
4242
};
@@ -59,11 +59,27 @@ class SumOpVarTypeInference : public VarTypeInference {
5959
block->Var(out_var_name)->SetType(default_var_type);
6060
}
6161
};
62+
63+
class DummyOpMaker : public OpProtoAndCheckerMaker {
64+
public:
65+
void Make() {
66+
AddInput("X", "").AsDuplicable();
67+
AddOutput("Out", "").AsDuplicable();
68+
AddComment("");
69+
}
70+
};
71+
72+
class DummyOpVarTypeInference : public VarTypeInference {
73+
public:
74+
void operator()(const OpDesc &op_desc, BlockDesc *block) const override {}
75+
};
6276
} // namespace framework
6377
} // namespace paddle
6478

6579
REGISTER_OPERATOR(sum, paddle::framework::NOP, paddle::framework::SumOpMaker,
6680
paddle::framework::SumOpVarTypeInference);
81+
REGISTER_OPERATOR(dummy, paddle::framework::NOP, paddle::framework::SumOpMaker,
82+
paddle::framework::SumOpVarTypeInference);
6783
REGISTER_OPERATOR(sum_without_infer_var_type, paddle::framework::NOP,
6884
paddle::framework::SumOpMaker);
6985

@@ -110,5 +126,83 @@ TEST(GraphTest, Basic) {
110126
}
111127
ASSERT_EQ(nodes.size(), 5);
112128
}
129+
130+
TEST(GraphTest, WriteAfterRead) {
131+
// void Test() {
132+
ProgramDesc prog;
133+
auto *op = prog.MutableBlock(0)->AppendOp();
134+
op->SetType("sum");
135+
op->SetInput("X", {"a"});
136+
op->SetOutput("Out", {"b"});
137+
op->SetAttr("op_role", 1);
138+
139+
op = prog.MutableBlock(0)->AppendOp();
140+
op->SetType("dummy");
141+
op->SetInput("X", {"c"});
142+
op->SetOutput("Out", {"a"});
143+
op->SetAttr("op_role", 1);
144+
145+
prog.MutableBlock(0)->Var("a")->SetType(proto::VarType::LOD_TENSOR);
146+
prog.MutableBlock(0)->Var("b")->SetType(proto::VarType::LOD_TENSOR);
147+
prog.MutableBlock(0)->Var("c")->SetType(proto::VarType::LOD_TENSOR);
148+
149+
std::unique_ptr<ir::Graph> g(new ir::Graph(prog));
150+
ir::Node *control_dep1 = nullptr;
151+
ir::Node *control_dep2 = nullptr;
152+
for (ir::Node *n : g->Nodes()) {
153+
if (n->Name() == "sum") {
154+
ASSERT_EQ(n->outputs[0]->Name(), "b");
155+
ASSERT_TRUE(ir::IsControlDepVar(*n->outputs[1]));
156+
control_dep1 = n->outputs[1];
157+
ASSERT_EQ(n->outputs.size(), 2);
158+
}
159+
if (n->Name() == "dummy") {
160+
ASSERT_EQ(n->inputs[0]->Name(), "c");
161+
ASSERT_TRUE(ir::IsControlDepVar(*n->inputs[1]));
162+
control_dep2 = n->inputs[1];
163+
ASSERT_EQ(n->inputs.size(), 2);
164+
}
165+
}
166+
ASSERT_EQ(control_dep1, control_dep2);
167+
}
168+
169+
TEST(GraphTest, WriteAfterWrite) {
170+
// void Test() {
171+
ProgramDesc prog;
172+
auto *op = prog.MutableBlock(0)->AppendOp();
173+
op->SetType("sum");
174+
op->SetInput("X", {"a"});
175+
op->SetOutput("Out", {"b"});
176+
op->SetAttr("op_role", 1);
177+
178+
op = prog.MutableBlock(0)->AppendOp();
179+
op->SetType("dummy");
180+
op->SetInput("X", {"c"});
181+
op->SetOutput("Out", {"b"});
182+
op->SetAttr("op_role", 1);
183+
184+
prog.MutableBlock(0)->Var("a")->SetType(proto::VarType::LOD_TENSOR);
185+
prog.MutableBlock(0)->Var("b")->SetType(proto::VarType::LOD_TENSOR);
186+
prog.MutableBlock(0)->Var("c")->SetType(proto::VarType::LOD_TENSOR);
187+
188+
std::unique_ptr<ir::Graph> g(new ir::Graph(prog));
189+
ir::Node *control_dep1 = nullptr;
190+
ir::Node *control_dep2 = nullptr;
191+
for (ir::Node *n : g->Nodes()) {
192+
if (n->Name() == "sum") {
193+
ASSERT_EQ(n->outputs[0]->Name(), "b");
194+
ASSERT_TRUE(ir::IsControlDepVar(*n->outputs[1]));
195+
ASSERT_EQ(n->outputs.size(), 2);
196+
control_dep1 = n->outputs[1];
197+
}
198+
if (n->Name() == "dummy") {
199+
ASSERT_EQ(n->inputs[0]->Name(), "c");
200+
ASSERT_TRUE(ir::IsControlDepVar(*n->inputs[1]));
201+
control_dep2 = n->inputs[1];
202+
ASSERT_EQ(n->inputs.size(), 2);
203+
ASSERT_EQ(control_dep1, control_dep2);
204+
}
205+
}
206+
}
113207
} // namespace framework
114208
} // namespace paddle

0 commit comments

Comments
 (0)