forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_rewrite.cpp
444 lines (345 loc) · 13.9 KB
/
graph_rewrite.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <common_test_utils/ngraph_test_utils.hpp>
#include <ngraph/opsets/opset3.hpp>
#include <ngraph/pass/graph_rewrite.hpp>
#include <ngraph/pass/manager.hpp>
NGRAPH_SUPPRESS_DEPRECATED_START
using namespace ::testing;
using namespace std;
using namespace ngraph;
class TestPass : public ngraph::pass::MatcherPass {
public:
NGRAPH_RTTI_DECLARATION;
TestPass() : MatcherPass() {
auto divide =
std::make_shared<ngraph::pattern::op::Label>(element::f32, Shape{}, pattern::has_class<opset3::Divide>());
ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) {
if (transformation_callback(m.get_match_root())) {
auto relu = std::make_shared<ngraph::opset3::Relu>(m.get_match_root()->input_value(0));
ngraph::replace_node(m.get_match_root(), relu);
return true;
}
return false;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(divide, "TestMatcher");
this->register_matcher(m, callback);
}
};
class GatherNodesPass : public ngraph::pass::MatcherPass {
public:
NGRAPH_RTTI_DECLARATION;
GatherNodesPass(NodeVector& order) : MatcherPass() {
ngraph::matcher_pass_callback callback = [&order](pattern::Matcher& m) {
order.push_back(m.get_match_root());
return false;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(ngraph::pattern::any_input(), "GatherNodesPass");
this->register_matcher(m, callback);
}
};
class Anchor : public ngraph::pass::GraphRewrite {
public:
NGRAPH_RTTI_DECLARATION;
Anchor() : GraphRewrite() {}
};
NGRAPH_RTTI_DEFINITION(TestPass, "TestPass", 0);
NGRAPH_RTTI_DEFINITION(Anchor, "Anchor", 0);
NGRAPH_RTTI_DEFINITION(GatherNodesPass, "GatherNodesPass", 0);
std::shared_ptr<Function> get_function() {
auto data = std::make_shared<ngraph::opset3::Parameter>(ngraph::element::f32, ngraph::Shape{3, 1, 2});
auto divide_constant = ngraph::opset3::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.5});
auto divide = std::make_shared<ngraph::opset3::Divide>(data, divide_constant);
return std::make_shared<ngraph::Function>(ngraph::NodeVector{divide}, ngraph::ParameterVector{data});
}
ngraph::pass::param_callback get_callback() {
return [](const std::shared_ptr<const Node>& node) -> bool {
if (std::dynamic_pointer_cast<const opset3::Divide>(node)) {
return true;
} else {
return false;
}
};
}
TEST(GraphRewriteOrderTest, MatcherPass) {
auto f = get_function();
NodeVector order;
ngraph::pass::Manager m;
auto pass = m.register_pass<pass::GraphRewrite>();
pass->add_matcher<GatherNodesPass>(order);
m.run_passes(f);
ASSERT_EQ(order, f->get_ordered_ops());
}
TEST(BackwardGraphRewriteOrderTest, MatcherPass) {
auto f = get_function();
NodeVector order;
ngraph::pass::Manager m;
auto pass = m.register_pass<pass::BackwardGraphRewrite>();
pass->add_matcher<GatherNodesPass>(order);
m.run_passes(f);
auto ref_order = f->get_ordered_ops();
std::reverse(ref_order.begin(), ref_order.end());
ASSERT_EQ(order, ref_order);
}
TEST(GraphRewriteTest, MatcherPassCallback) {
auto f = get_function();
Anchor anchor;
anchor.add_matcher<TestPass>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, GraphRewriteCallback) {
auto f = get_function();
Anchor anchor;
anchor.add_matcher<TestPass>();
anchor.set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, ManagerCallbackDeprecated) {
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<Anchor>();
anchor->add_matcher<TestPass>();
manager.set_callback(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, ManagerCallback) {
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<Anchor>();
anchor->add_matcher<TestPass>();
auto pass_config = manager.get_pass_config();
pass_config->set_callback(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, ManagerCallback2) {
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<TestPass>();
manager.set_callback(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
class PrivateDivide : public ngraph::opset3::Divide {
public:
NGRAPH_RTTI_DECLARATION;
using ngraph::opset3::Divide::Divide;
};
NGRAPH_RTTI_DEFINITION(PrivateDivide, "PrivateDivide", 0, ngraph::opset3::Divide);
std::shared_ptr<Function> get_derived_function() {
auto data = std::make_shared<ngraph::opset3::Parameter>(ngraph::element::f32, ngraph::Shape{3, 1, 2});
auto divide_constant = ngraph::opset3::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.5});
auto divide = std::make_shared<PrivateDivide>(data, divide_constant);
return std::make_shared<ngraph::Function>(ngraph::NodeVector{divide}, ngraph::ParameterVector{data});
}
TEST(GraphRewriteTest, MatcherPassCallbackDerived) {
auto f = get_derived_function();
Anchor anchor;
anchor.add_matcher<TestPass>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
class TypeBasedTestPass : public ngraph::pass::MatcherPass {
public:
TypeBasedTestPass() : MatcherPass() {
auto divide = std::make_shared<ngraph::opset3::Divide>(std::make_shared<ngraph::pattern::op::Label>(),
std::make_shared<ngraph::pattern::op::Label>());
// element::f32, Shape{}, pattern::has_class<opset3::Divide>());
ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) {
if (transformation_callback(m.get_match_root())) {
auto relu = std::make_shared<ngraph::opset3::Relu>(m.get_match_root()->input_value(0));
ngraph::replace_node(m.get_match_root(), relu);
return true;
}
return false;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(divide, "TestMatcher");
this->register_matcher(m, callback);
}
};
class TypeBasedTestPassDerived : public ngraph::pass::MatcherPass {
public:
TypeBasedTestPassDerived() : MatcherPass() {
auto divide = std::make_shared<PrivateDivide>(std::make_shared<ngraph::pattern::op::Label>(),
std::make_shared<ngraph::pattern::op::Label>());
ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) {
if (transformation_callback(m.get_match_root())) {
auto tanh = std::make_shared<ngraph::opset3::Tanh>(m.get_match_root()->input_value(0));
ngraph::replace_node(m.get_match_root(), tanh);
return true;
}
return false;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(divide, "TestMatcher");
this->register_matcher(m, callback);
}
};
TEST(GraphRewriteTest, TypeBasedMatcherPassCallback) {
auto f = get_function();
Anchor anchor;
anchor.add_matcher<TypeBasedTestPass>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, TypeBasedMatcherPassCallbackDerived) {
auto f = get_derived_function();
Anchor anchor;
anchor.add_matcher<TypeBasedTestPass>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, TypeBasedMatcherPassOrder1) {
auto f = get_derived_function();
Anchor anchor;
anchor.add_matcher<TypeBasedTestPass>()->set_callback(get_callback());
anchor.add_matcher<TypeBasedTestPassDerived>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
TEST(GraphRewriteTest, TypeBasedMatcherPassOrder2) {
auto f = get_derived_function();
Anchor anchor;
anchor.add_matcher<TypeBasedTestPassDerived>()->set_callback(get_callback());
anchor.add_matcher<TypeBasedTestPass>()->set_callback(get_callback());
anchor.run_on_function(f);
ASSERT_EQ(count_ops_of_type<opset3::Tanh>(f), 1);
}
TEST(PassConfigTest, Test1) {
{
auto f = get_function();
pass::Manager manager;
manager.register_pass<TestPass>();
auto pass_config = manager.get_pass_config();
pass_config->set_callback(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto f = get_function();
pass::Manager manager;
manager.register_pass<TestPass>();
auto pass_config = manager.get_pass_config();
pass_config->set_callback<TestPass>(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto f = get_function();
auto pass_config = std::make_shared<ngraph::pass::PassConfig>();
pass::Manager manager(pass_config);
manager.register_pass<TestPass>();
pass_config->set_callback<TestPass>(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<Anchor>();
anchor->add_matcher<TestPass>();
auto pass_config = anchor->get_pass_config();
pass_config->set_callback(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<Anchor>();
anchor->add_matcher<TestPass>();
auto pass_config = anchor->get_pass_config();
pass_config->set_callback<TestPass>(get_callback());
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto pass_config = std::make_shared<pass::PassConfig>();
pass::Manager manager1(pass_config);
pass::Manager manager2(pass_config);
ASSERT_EQ(pass_config.use_count(), 3);
}
{
auto f = get_function();
pass::Manager manager;
manager.register_pass<TestPass>();
auto pass_config = manager.get_pass_config();
pass_config->set_callback<TestPass>(get_callback());
pass_config->disable<TestPass>();
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 0);
pass_config->enable<TestPass>();
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
{
auto f = get_function();
pass::Manager manager;
auto anchor = manager.register_pass<Anchor>();
anchor->add_matcher<TestPass>();
auto pass_config = manager.get_pass_config();
pass_config->set_callback<TestPass>(get_callback());
pass_config->disable<TestPass>();
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 0);
pass_config->enable<TestPass>();
manager.run_passes(f);
ASSERT_EQ(count_ops_of_type<opset3::Relu>(f), 1);
}
}
class CheckConsumers : public ngraph::pass::MatcherPass {
public:
NGRAPH_RTTI_DECLARATION;
CheckConsumers() {
ngraph::matcher_pass_callback callback = [](pattern::Matcher& m) -> bool {
auto node = m.get_match_root();
auto consumers = [](Node* node) {
int64_t cnt{0};
for (auto output : node->outputs()) {
cnt += output.get_target_inputs().size();
}
if (ov::as_type<op::Parameter>(node) || ov::as_type<op::Result>(node)) {
cnt += 1;
}
return cnt;
};
/* The expected number of use_count() for Node is equal to the sum of next components:
* 1. Each consumer holds a pointer to Output<Node> which holds a shared_ptr to Node
* 2. pattern::Matcher object holds a shared_ptr to the matched node
* 3. Local node variable increases use_counter
* 4. Some GraphRewrite facilities
*/
auto cnt = consumers(node.get());
if (node.use_count() != cnt + 7) {
throw ngraph::ngraph_error("Wrong number of consumers");
}
NodeVector nodes;
for (const auto& inputs : node->input_values()) {
nodes.emplace_back(inputs.get_node_shared_ptr());
}
/* The expected number of use_count() for Node is equal to the sum of next components:
* 1. Each consumer holds a pointer to Output<Node> which holds a shared_ptr to Node
* 2. Local input_node variable increases use_counter
*/
for (const auto& input_node : nodes) {
if (input_node.use_count() != consumers(input_node.get()) + 1) {
throw ngraph::ngraph_error("Wrong number of consumers");
}
}
return false;
};
auto m = std::make_shared<ngraph::pattern::Matcher>(ngraph::pattern::any_input(), "CheckConsumers");
this->register_matcher(m, callback);
}
};
NGRAPH_RTTI_DEFINITION(CheckConsumers, "CheckConsumers", 0);
TEST(GraphRewriteTest, nodes_use_count) {
auto f = get_function();
pass::Manager m;
m.register_pass<CheckConsumers>();
ASSERT_NO_THROW(m.run_passes(f));
}