forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_dag_utils_test.cc
296 lines (279 loc) · 6.35 KB
/
net_dag_utils_test.cc
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <gtest/gtest.h>
#include "caffe2/core/net_dag_utils.h"
#include "caffe2/core/operator.h"
namespace caffe2 {
namespace {
class DummySyncOp final : public Operator<CPUContext> {
public:
DummySyncOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<CPUContext>(operator_def, ws) {}
bool RunOnDevice() override {
return true;
}
};
class DummyAsyncOp final : public Operator<CPUContext> {
public:
DummyAsyncOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<CPUContext>(operator_def, ws) {}
bool RunOnDevice() override {
return true;
}
bool HasAsyncPart() const override {
return true;
}
};
REGISTER_CPU_OPERATOR(DagUtilTestDummySync, DummySyncOp);
REGISTER_CPU_OPERATOR(DagUtilTestDummyAsync, DummyAsyncOp);
OPERATOR_SCHEMA(DagUtilTestDummySync)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX);
OPERATOR_SCHEMA(DagUtilTestDummyAsync)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX);
class DagUtilTestContext {
public:
DagUtilTestContext(const std::string& spec, Workspace* ws) {
net_def_ = std::make_shared<NetDef>();
CAFFE_ENFORCE(TextFormat::ParseFromString(spec, net_def_.get()));
operator_nodes_ = dag_utils::prepareOperatorNodes(net_def_, ws);
}
dag_utils::ExecutionChains computeChains() {
return dag_utils::computeGroups(operator_nodes_);
}
private:
std::shared_ptr<NetDef> net_def_{nullptr};
std::vector<dag_utils::OperatorNode> operator_nodes_;
};
void PrintChains(const dag_utils::ExecutionChains& chains) {
for (const auto kv : chains) {
std::stringstream ss;
ss << kv.first << ": ";
for (const auto& v : kv.second) {
ss << v << ", ";
}
LOG(INFO) << ss.str();
}
}
} // namespace
TEST(DagUtilTest, Empty) {
const auto spec = R"DOC(
name: "test0"
type: "async_scheduling"
)DOC";
Workspace ws;
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
EXPECT_TRUE(chains.empty());
}
// 4 sync ops forming a diamond
TEST(DagUtilTest, AllSync) {
const auto spec = R"DOC(
name: "test1"
type: "async_scheduling"
external_input: "in"
op {
input: "in"
output: "n1"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
output: "n2"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
output: "n3"
type: "DagUtilTestDummySync"
}
op {
input: "n2"
input: "n3"
output: "out"
type: "DagUtilTestDummySync"
}
)DOC";
Workspace ws;
ws.CreateBlob("in");
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
dag_utils::ExecutionChains expected{{0, {0, 1, 2, 3}}};
EXPECT_EQ(chains, expected);
}
// 3 async ops forming an L shape
TEST(DagUtilTest, AllAsync) {
const auto spec = R"DOC(
name: "test2"
type: "async_scheduling"
external_input: "in0"
external_input: "in1"
op {
input: "in0"
output: "n1"
type: "DagUtilTestDummyAsync"
}
op {
input: "in1"
output: "n2"
type: "DagUtilTestDummyAsync"
}
op {
input: "n1"
output: "n3"
type: "DagUtilTestDummyAsync"
}
)DOC";
Workspace ws;
ws.CreateBlob("in0");
ws.CreateBlob("in1");
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
dag_utils::ExecutionChains expected{{0, {0}}, {1, {1}}, {2, {2}}};
EXPECT_EQ(chains, expected);
}
// 3 sync ops and 1 async op (#2) forming a diamond
TEST(DagUtilTest, Mixed0) {
const auto spec = R"DOC(
name: "test3"
type: "async_scheduling"
external_input: "in"
op {
input: "in"
output: "n1"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
output: "n2"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
output: "n3"
type: "DagUtilTestDummyAsync"
}
op {
input: "n2"
input: "n3"
output: "out"
type: "DagUtilTestDummySync"
}
)DOC";
Workspace ws;
ws.CreateBlob("in");
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
dag_utils::ExecutionChains expected{{0, {0, 1}}, {2, {2}}, {3, {3}}};
EXPECT_EQ(chains, expected);
}
// 3 sync ops and 1 async op (#2) forming a Y shape
TEST(DagUtilTest, Mixed1) {
const auto spec = R"DOC(
name: "test3"
type: "async_scheduling"
external_input: "in0"
external_input: "in1"
op {
input: "in0"
output: "n1"
type: "DagUtilTestDummySync"
}
op {
input: "in1"
output: "n2"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
input: "n2"
output: "n3"
type: "DagUtilTestDummyAsync"
}
op {
input: "n3"
output: "out"
type: "DagUtilTestDummySync"
}
)DOC";
Workspace ws;
ws.CreateBlob("in0");
ws.CreateBlob("in1");
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
dag_utils::ExecutionChains expected{{0, {0, 1}}, {2, {2}}, {3, {3}}};
EXPECT_EQ(chains, expected);
}
// More complicated mixed case. * means async
// 0* -> 1* -> 2
// |
// 3 -> 4 -> 5
// | |
// | 6
// - -> 8*
// 7* -/
TEST(DagUtilTest, Mixed2) {
const auto spec = R"DOC(
name: "test4"
type: "async_scheduling"
external_input: "in0"
external_input: "in1"
external_input: "in2"
op {
input: "in0"
output: "n1"
type: "DagUtilTestDummyAsync"
}
op {
input: "n1"
output: "n2"
type: "DagUtilTestDummyAsync"
}
op {
input: "n2"
output: "out0"
type: "DagUtilTestDummySync"
}
op {
input: "in1"
output: "n3"
type: "DagUtilTestDummySync"
}
op {
input: "n1"
input: "n3"
output: "n4"
type: "DagUtilTestDummySync"
}
op {
input: "n4"
output: "out1"
type: "DagUtilTestDummySync"
}
op {
input: "n3"
output: "out2"
type: "DagUtilTestDummySync"
}
op {
input: "in2"
output: "n7"
type: "DagUtilTestDummyAsync"
}
op {
input: "n3"
input: "n7"
output: "out3"
type: "DagUtilTestDummyAsync"
}
)DOC";
Workspace ws;
ws.CreateBlob("in0");
ws.CreateBlob("in1");
ws.CreateBlob("in2");
DagUtilTestContext t(spec, &ws);
auto chains = t.computeChains();
dag_utils::ExecutionChains expected{
{0, {0}}, {1, {1}}, {3, {3, 6}}, {4, {4, 2, 5}}, {7, {7}}, {8, {8}}};
EXPECT_EQ(chains, expected);
}
} // namespace caffe2