forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_irparser.cpp
308 lines (290 loc) · 7.7 KB
/
test_irparser.cpp
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
297
298
299
300
301
302
303
304
305
306
307
308
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/irparser.h>
#include <torch/csrc/jit/testing/file_check.h>
#include "test/cpp/jit/test_base.h"
#include <sstream>
#include <string>
namespace torch {
namespace jit {
/** \brief Parse IR from \p S, print the parsed graph and verify that the output
* string matches the original string.
*
* The function is sensitive to value naming and whitespace, so it should be
* used with care. Nevertheless, it helps to keep tests more compact.
*/
static void checkRoundtrip(const std::string& s) {
auto graph = std::make_shared<Graph>();
parseIR(s, &*graph);
std::ostringstream ss;
ss << *graph;
std::string parsed = ss.str();
// Skip whitespace in the beginning of the input string.
int i = 0;
for (char c : s) {
if (!isspace(c)) {
break;
}
i++;
}
std::string original = s.substr(i, s.size());
if (original != parsed) {
std::cerr << "Input:" << std::endl << original << std::endl;
std::cerr << "Parsed:" << std::endl << parsed << std::endl;
}
AT_ASSERT(original == parsed);
}
void testIRParser() {
{
auto graph = std::make_shared<Graph>();
std::unordered_map<std::string, Value*> vmap;
parseIR(
R"IR(
graph(%0 : Tensor, %1 : Tensor):
%2 : Tensor = foo::add(%0, %1)
%res, %3 = foo::mul(%0, %2)
%x, %y = foo::combine(%res, %2, %3)
return (%x, %y, %res))IR",
&*graph,
vmap);
AT_ASSERT(graph->inputs().size() == 2);
AT_ASSERT(graph->outputs().size() == 3);
Value* x = graph->outputs()[0];
Value* y = graph->outputs()[1];
Value* res = graph->outputs()[2];
Value* t0 = graph->inputs()[0];
Value* t1 = graph->inputs()[1];
AT_ASSERT(vmap["x"] == x);
AT_ASSERT(vmap["y"] == y);
AT_ASSERT(vmap["res"] == res);
AT_ASSERT(vmap["0"] == t0);
AT_ASSERT(vmap["1"] == t1);
AT_ASSERT(x->node() == y->node());
Node* comb = x->node();
Value* t2 = comb->inputs()[1];
Value* t3 = comb->inputs()[2];
AT_ASSERT(vmap["2"] == t2);
AT_ASSERT(vmap["3"] == t3);
AT_ASSERT(comb->kind().toQualString() == std::string("foo::combine"));
AT_ASSERT(comb->outputs() == std::vector<Value*>({x, y}));
AT_ASSERT(comb->inputs() == std::vector<Value*>({res, t2, t3}));
Node* mul = res->node();
AT_ASSERT(mul->kind().toQualString() == std::string("foo::mul"));
AT_ASSERT(mul->inputs() == std::vector<Value*>({t0, t2}));
AT_ASSERT(mul->outputs() == std::vector<Value*>({res, t3}));
Node* add = t2->node();
AT_ASSERT(add->kind().toQualString() == std::string("foo::add"));
AT_ASSERT(add->inputs() == std::vector<Value*>({t0, t1}));
AT_ASSERT(add->outputs() == std::vector<Value*>({t2}));
}
{
checkRoundtrip(R"IR(
graph():
%0 : Tensor = a::a()
block0():
%1 : Tensor = b::b()
block0():
%2 : Tensor = c::c()
-> ()
-> ()
%3 : Tensor = d::d()
return (%3)
)IR");
}
{
checkRoundtrip(R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : int = prim::Constant[value=1]()
%4 : Tensor = aten::add(%0, %1, %3)
%5 : Tensor = prim::If(%2)
block0():
%6 : int = prim::Constant[value=1]()
%7 : Tensor = aten::add(%1, %3, %6)
%8 : int = prim::Constant[value=1]()
%9 : Tensor = aten::add(%7, %3, %8)
-> (%9)
%10 : int = prim::Constant[value=1]()
%11 : Tensor = aten::add(%5, %3, %10)
return (%11)
)IR");
}
{
auto graph = std::make_shared<Graph>();
parseIR(
R"IR(
graph(%a):
return (%a))IR",
&*graph);
AT_ASSERT(graph->inputs()[0]->type()->isSubtypeOf(TensorType::get()));
}
{
// Check that parser correctly handles values reusing the same name.
auto graph = std::make_shared<Graph>();
parseIR(
R"IR(
graph(%x):
%x = a::a(%x)
%x = b::b(%x)
return (%x))IR",
&*graph);
Value* x0 = graph->inputs()[0];
Value* x2 = graph->outputs()[0];
Node* b = x2->node();
Value* x1 = b->inputs()[0];
Node* a = x1->node();
AT_ASSERT(a->inputs() == std::vector<Value*>({x0}));
AT_ASSERT(a->outputs() == std::vector<Value*>({x1}));
AT_ASSERT(b->inputs() == std::vector<Value*>({x1}));
AT_ASSERT(b->outputs() == std::vector<Value*>({x2}));
}
{
// Check that parser handles attributes and types.
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : int, %4 : Tensor = qqq::qqq[i_asdf=2, f_asdf=3., s_asdf="hello", ss_asdf=["hello world", "bye bye"]](%0)
%5 : int, %6 : Tensor = ppp::ppp[i_asdf=2, f_asdf=3., s_asdf="\"\"\"\"\nhe\"llo", q=[3, 2, 4]](%0)
%7 : float = vvv::vvv[s_asdf="hello"](%0)
%8 : string = z::z()
return (%7)
)IR");
}
{
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : int? = prim::Constant()
return (%3)
)IR");
}
{
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : Float(*, *, *) = prim::Constant()
return (%3)
)IR");
}
{
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : Long() = prim::Constant()
return (%3)
)IR");
}
{
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : Double(4, 4, 5) = prim::Constant()
return (%3)
)IR");
}
{
checkRoundtrip(
R"IR(
graph():
%0 : float[] = prim::Constant[value=[1., 2., 3.]]()
%1 : str[] = prim::Constant[value=["ab", "cd", "ef"]]()
%2 : (float[], str[]) = prim::TupleConstruct(%0, %1)
return (%2)
)IR");
}
{
bool error_thrown = false;
try {
checkRoundtrip(
R"IR(
graph(%0 : Tensor,
%1 : Tensor,
%2 : Tensor):
%3 : Double(4!, 4, 5) = prim::Constant()
return (%3)
)IR");
} catch (const std::exception& error) {
error_thrown = true;
}
AT_ASSERT(error_thrown);
}
{
auto graph = std::make_shared<Graph>();
const std::string& text =
R"IR(
graph(%a):
# CHECK: return
return (%a))IR";
parseIR(text, &*graph);
AT_ASSERT(graph->inputs()[0]->type()->isSubtypeOf(TensorType::get()));
torch::jit::testing::FileCheck().run(text, *graph);
}
{
auto graph = std::make_shared<Graph>();
std::unordered_map<std::string, Value*> vmap;
parseIR(
R"IR(
graph(%a : Float(4, 5),
%b : Float(4:5, 5:1),
%c : Double(*, *)):
return (%a)
)IR",
&*graph,
vmap);
Value* a = graph->inputs()[0];
Value* b = graph->inputs()[1];
Value* c = graph->inputs()[2];
auto a_type = a->type()->cast<TensorType>();
auto a_sizes = *a_type->sizes().concrete_sizes();
auto a_strides = a_type->strides().concrete_sizes();
AT_ASSERT(a_sizes[0] == 4 && a_sizes[1] == 5);
AT_ASSERT(a_strides == c10::nullopt);
auto b_type = b->type()->cast<TensorType>();
auto b_sizes = *b_type->sizes().concrete_sizes();
auto b_strides = *(b_type->strides().sizes());
AT_ASSERT(b_sizes[0] == 4 && b_sizes[1] == 5);
AT_ASSERT(*b_strides[0] == 5 && *b_strides[1] == 1);
auto c_type = c->type()->cast<TensorType>();
AT_ASSERT(*c_type->sizes().size() == 2);
AT_ASSERT(c_type->sizes().concrete_sizes() == c10::nullopt);
AT_ASSERT(c_type->strides().concrete_sizes() == c10::nullopt);
}
{
auto graph = std::make_shared<Graph>();
std::unordered_map<std::string, Value*> vmap;
bool error_thrown = false;
try {
parseIR(
R"IR(
graph(%a : Float(4:5, 5)):
return (%a)
)IR",
&*graph,
vmap);
} catch (const std::exception& error) {
error_thrown = true;
}
AT_ASSERT(error_thrown);
}
{
checkRoundtrip(
R"IR(
graph(%a : Float(4, 5),
%b : Float(4:5, 5:1),
%c : Double(*, *)):
return (%a)
)IR");
}
}
} // namespace jit
} // namespace torch