forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.cpp
703 lines (608 loc) · 33.4 KB
/
pattern.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <memory>
#include "common_test_utils/matcher.hpp"
#include "common_test_utils/ov_test_utils.hpp"
#include "common_test_utils/test_tools.hpp"
#include "ngraph/pass/graph_rewrite.hpp"
#include "openvino/core/except.hpp"
#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/reduce_sum.hpp"
#include "openvino/op/relu.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/util/op_types.hpp"
#include "openvino/pass/graph_rewrite.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/pass/pattern/matcher.hpp"
#include "openvino/pass/pattern/op/branch.hpp"
#include "openvino/pass/pattern/op/label.hpp"
#include "openvino/pass/pattern/op/or.hpp"
#include "openvino/pass/pattern/op/true.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
using namespace ov;
using namespace ov::pass;
using namespace std;
static std::shared_ptr<Node> construct_constant_node(int n) {
return ov::op::v0::Constant::create(element::i32, Shape{}, {n});
}
static std::shared_ptr<pass::pattern::op::Label> construct_variance_graph() {
// construct varaiance
auto N = ov::op::v0::Constant::create(element::f32, Shape{3}, {2, 2, 2});
auto input = std::make_shared<pass::pattern::op::Label>(element::f32, Shape{2, 3});
auto input_sq = std::make_shared<op::v1::Multiply>(input, input);
auto sum_input = std::make_shared<op::v1::ReduceSum>(input, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto square_sumed_input = std::make_shared<op::v1::Multiply>(sum_input, sum_input);
auto sum_squared_input =
std::make_shared<op::v1::ReduceSum>(input_sq, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto avg_input_sum_sq = std::make_shared<op::v1::Divide>(square_sumed_input, N);
auto xmu = std::make_shared<op::v1::Subtract>(sum_squared_input, avg_input_sum_sq);
auto variance = std::make_shared<op::v1::Divide>(xmu, N);
auto variance_label = std::make_shared<pass::pattern::op::Label>(variance, nullptr, NodeVector{variance});
return variance_label;
}
static std::shared_ptr<pattern::op::Label> construct_mean_graph() {
// construct mean;
auto input = std::make_shared<pattern::op::Label>(element::f32, Shape{2, 3});
auto N = ov::op::v0::Constant::create(element::f32, Shape{3}, {2, 2, 2});
auto sum_input1 = std::make_shared<op::v1::ReduceSum>(input, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto mean = std::make_shared<op::v1::Divide>(sum_input1, N);
auto mean_label = std::make_shared<pattern::op::Label>(mean, nullptr, NodeVector{mean});
return mean_label;
}
class TestGraphRewrite : public ov::pass::GraphRewrite {
public:
void construct_multiply_by_one() {
// pattern #1 : a * 1 = a
auto iconst1 = construct_constant_node(1);
auto pattern = std::make_shared<pattern::op::Label>(iconst1);
auto callback = [pattern](pattern::Matcher& m) {
OPENVINO_DEBUG << "In a callback for construct_multiply_by_one against " << m.get_match_root()->get_name();
OPENVINO_ASSERT(m.get_match_root()->input_values().size() == 2);
auto pattern_map = m.get_pattern_map();
size_t const_node_index = m.get_match_root()->input_value(0).get_node_shared_ptr() == pattern_map[pattern];
auto const_node = ov::as_type_ptr<ov::op::v0::Constant>(
m.get_match_root()->input_value(const_node_index).get_node_shared_ptr());
auto second_node = m.get_match_root()->input_value(const_node_index).get_node_shared_ptr();
OPENVINO_DEBUG << "second_node = " << second_node->get_name()
<< " , pattern = " << pattern_map[pattern]->get_name();
if (pattern_map[pattern]->get_element_type() != const_node->get_element_type() ||
pattern_map[pattern]->get_shape() != const_node->get_shape()) {
OPENVINO_DEBUG << "Operands' types and/or shape don't match";
return false;
}
auto const_values = const_node->get_vector<int32_t>();
bool all_ones = std::all_of(begin(const_values), end(const_values), [](int e) {
return e == 1;
});
if (!all_ones) {
OPENVINO_DEBUG << "Constant vector's values aren't equal to 1";
return false;
}
ov::replace_node(m.get_match_root(), pattern_map[pattern]);
return true;
};
auto m = make_shared<TestMatcher>(make_shared<op::v1::Multiply>(pattern, iconst1));
auto match_pass = std::make_shared<ov::pass::MatcherPass>(
m->get_name(),
m,
[m, callback](const std::shared_ptr<Node>& node) -> bool {
OPENVINO_DEBUG << "Running matcher " << m->get_name() << " on " << node;
if (std::dynamic_pointer_cast<ov::pass::pattern::Matcher>(m)->match(node->output(0))) {
OPENVINO_DEBUG << "Matcher " << m->get_name() << " matched " << node;
bool status = callback(*m.get());
// explicitly clear Matcher state because it holds pointers to matched nodes
m->clear_state();
return status;
}
m->clear_state();
return false;
},
ov::pass::PassProperty::REQUIRE_STATIC_SHAPE);
this->add_matcher(match_pass);
}
void construct_add_zero() {
// pattern #2 : a + 0 = a
auto iconst0 = construct_constant_node(0);
auto pattern = std::make_shared<pattern::op::Label>(iconst0);
auto callback = [pattern](pattern::Matcher& m) {
OPENVINO_DEBUG << "In a callback for construct_add_zero against " << m.get_match_root()->get_name();
OPENVINO_ASSERT(m.get_match_root()->input_values().size() == 2);
auto pattern_map = m.get_pattern_map();
size_t const_node_index = m.get_match_root()->input_value(0).get_node_shared_ptr() == pattern_map[pattern];
auto const_node = ov::as_type_ptr<ov::op::v0::Constant>(
m.get_match_root()->input_value(const_node_index).get_node_shared_ptr());
auto second_node = m.get_match_root()->input_value(const_node_index).get_node_shared_ptr();
OPENVINO_DEBUG << "second_node = " << second_node->get_name()
<< " , pattern = " << pattern_map[pattern]->get_name();
if (pattern_map[pattern]->get_element_type() != const_node->get_element_type() ||
pattern_map[pattern]->get_shape() != const_node->get_shape()) {
OPENVINO_DEBUG << "Operands' types and/or shape don't match";
return false;
}
auto const_values = const_node->get_vector<int>();
bool all_zeros = std::all_of(begin(const_values), end(const_values), [](int e) {
return e == 0;
});
if (!all_zeros) {
OPENVINO_DEBUG << "Constant vector's values aren't equal to 0";
return false;
}
ov::replace_node(m.get_match_root(), pattern_map[pattern]);
return true;
};
auto add = make_shared<op::v1::Add>(pattern, iconst0);
auto m = make_shared<TestMatcher>(add);
auto match_pass = std::make_shared<ov::pass::MatcherPass>(
m->get_name(),
m,
[m, callback](const std::shared_ptr<Node>& node) -> bool {
OPENVINO_DEBUG << "Running matcher " << m->get_name() << " on " << node;
if (std::dynamic_pointer_cast<ov::pass::pattern::Matcher>(m)->match(node->output(0))) {
OPENVINO_DEBUG << "Matcher " << m->get_name() << " matched " << node;
bool status = callback(*m.get());
// explicitly clear Matcher state because it holds pointers to matched nodes
m->clear_state();
return status;
}
m->clear_state();
return false;
},
ov::pass::PassProperty::REQUIRE_STATIC_SHAPE);
this->add_matcher(match_pass);
}
TestGraphRewrite() : GraphRewrite() {
construct_multiply_by_one();
construct_add_zero();
}
};
static void run_passes(pass::Manager& pass_manager,
shared_ptr<Node> graph,
std::vector<shared_ptr<op::v0::Parameter>> parms) {
auto func = make_shared<Model>(graph, ParameterVector{parms});
pass_manager.run_passes(func);
}
TEST(pattern, graph_rewrite) {
Shape shape{};
pass::Manager pass_manager;
pass_manager.register_pass<TestGraphRewrite>();
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto c = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst0 = construct_constant_node(0);
auto graph_a = make_shared<op::v1::Add>(a, iconst0);
auto graph_b = make_shared<op::v1::Add>(b, iconst0);
auto f = std::make_shared<Model>(ov::NodeVector{a, b, graph_a, c, graph_b}, ParameterVector{a, b, c});
pass_manager.run_passes(f);
ASSERT_TRUE(graph_a->get_output_target_inputs(0).empty());
ASSERT_TRUE(graph_b->get_output_target_inputs(0).empty());
auto expected = ov::NodeVector{a, b, a, c, b};
ASSERT_TRUE(count_ops_of_type<op::v1::Add>(f) == 0);
}
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst0 = construct_constant_node(0);
auto sum = make_shared<op::v1::Add>(a, iconst0);
auto graph = make_shared<op::v1::Add>(b, sum);
run_passes(pass_manager, graph, {a, b});
ASSERT_EQ(graph->input_value(1).get_node_shared_ptr(), a);
ASSERT_EQ(graph->input_value(1), a->output(0)); // graph's input points to a's output
ASSERT_TRUE(sum->output(0).get_target_inputs().empty()); // graph's input is removed from sum's target inptus
ASSERT_TRUE(a->get_output_target_inputs(0).count(graph->input(1))); // a's output feeds into graph's input
}
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst1 = construct_constant_node(1);
auto mul = make_shared<op::v1::Multiply>(a, iconst1);
auto graph = make_shared<op::v1::Add>(b, mul);
run_passes(pass_manager, graph, {a, b});
ASSERT_EQ(graph->input_value(1).get_node_shared_ptr(), a);
ASSERT_EQ(graph->input_value(1), a->output(0)); // graph's input points to a's output
ASSERT_TRUE(mul->output(0).get_target_inputs().empty()); // graph's input is removed from sum's target inputs
ASSERT_TRUE(a->get_output_target_inputs(0).count(graph->input(1))); // a's output feeds into graph's input
}
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst1 = construct_constant_node(1);
auto multiply = make_shared<op::v1::Multiply>(make_shared<op::v1::Multiply>(a, iconst1), iconst1);
multiply = make_shared<op::v1::Multiply>(make_shared<op::v1::Multiply>(multiply, iconst1), iconst1);
auto graph = make_shared<op::v1::Add>(multiply, b);
run_passes(pass_manager, graph, {a, b});
ASSERT_EQ(graph->input_value(0).get_node_shared_ptr(), a);
ASSERT_EQ(graph->input_value(0), a->output(0)); // graph's input points to a's output
ASSERT_TRUE(a->get_output_target_inputs(0).count(graph->input(0))); // a's output feeds into graph's input
}
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst0 = construct_constant_node(0);
auto iconst1 = construct_constant_node(1);
auto mul = make_shared<op::v1::Multiply>(make_shared<op::v1::Add>(a, iconst0), iconst1);
auto graph = make_shared<op::v1::Add>(b, make_shared<op::v1::Add>(iconst0, mul));
run_passes(pass_manager, graph, {a, b});
ASSERT_EQ(graph->input_value(1).get_node_shared_ptr(), a);
ASSERT_EQ(graph->input_value(1), a->output(0)); // graph's input points to a's output
ASSERT_TRUE(a->get_output_target_inputs(0).count(graph->input(1))); // a's output feeds into graph's input
}
{
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto iconst1 = construct_constant_node(1);
auto mul = make_shared<op::v1::Multiply>(iconst1, make_shared<op::v1::Multiply>(iconst1, a));
mul = make_shared<op::v1::Multiply>(iconst1, make_shared<op::v1::Multiply>(iconst1, mul));
auto graph = make_shared<op::v1::Add>(b, mul);
run_passes(pass_manager, graph, {a, b});
ASSERT_EQ(graph->input_value(1).get_node_shared_ptr(), a);
ASSERT_EQ(graph->input_value(1), a->output(0)); // graph's input points to a's output
ASSERT_TRUE(a->get_output_target_inputs(0).count(graph->input(1))); // a's output feeds into graph's input
}
}
TEST(pattern, matcher) {
Shape shape{};
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
TestMatcher n;
ASSERT_TRUE(n.match(a, a));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{a}));
auto abs = make_shared<op::v0::Abs>(a);
auto any = std::make_shared<pattern::op::Skip>(a);
ASSERT_TRUE(n.match(any, abs));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{abs, a}));
auto false_pred = [](std::shared_ptr<Node> /* no */) {
return false;
};
auto any_false = std::make_shared<pattern::op::Skip>(a, false_pred);
ASSERT_TRUE(n.match(any_false, a));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{a, a}));
auto pattern = std::make_shared<pattern::op::Label>(a);
ASSERT_TRUE(n.match(pattern, a));
ASSERT_EQ(n.get_pattern_map()[pattern], a);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{a}));
auto pattern_false = std::make_shared<pattern::op::Label>(a, false_pred);
ASSERT_FALSE(n.match(pattern_false, a));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{}));
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto is_bea = [](std::shared_ptr<Node> node) -> bool {
return op::util::is_binary_elementwise_arithmetic(node);
};
auto bea = std::make_shared<pattern::op::Any>(a, is_bea, NodeVector{a, b});
auto add_ab = std::make_shared<op::v1::Add>(a, b);
ASSERT_TRUE(n.match(bea, add_ab));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{add_ab, a, b}));
ASSERT_TRUE(n.match(bea, std::make_shared<op::v1::Add>(b, a)));
auto bea_false = std::make_shared<pattern::op::Any>(a, false_pred, NodeVector{a, b});
ASSERT_FALSE(n.match(bea_false, std::make_shared<op::v1::Add>(a, b)));
auto add_abs_b = std::make_shared<op::v1::Add>(abs, b);
auto bea_any_of = std::make_shared<pattern::op::AnyOf>(a, is_bea, NodeVector{abs});
ASSERT_TRUE(n.match(bea_any_of, add_abs_b));
auto add_b_abs = std::make_shared<op::v1::Add>(b, abs);
ASSERT_TRUE(n.match(bea_any_of, add_b_abs));
auto bea_any_of_label = std::make_shared<pattern::op::Label>(a, nullptr, NodeVector{bea_any_of});
ASSERT_TRUE(n.match(bea_any_of_label, add_b_abs));
ASSERT_EQ(n.get_pattern_map()[bea_any_of_label], add_b_abs);
auto abs_label = std::make_shared<pattern::op::Label>(a, nullptr, NodeVector{abs});
auto bea_label_any_of = std::make_shared<pattern::op::AnyOf>(a, is_bea, NodeVector{abs_label});
ASSERT_TRUE(n.match(bea_label_any_of, add_b_abs));
ASSERT_EQ(n.get_pattern_map()[abs_label], abs);
auto bea_label = std::make_shared<pattern::op::Label>(a, nullptr, NodeVector{bea});
auto ab = std::make_shared<op::v1::Add>(a, b);
ASSERT_TRUE(n.match(bea_label, ab));
ASSERT_EQ(n.get_pattern_map()[bea_label], ab);
auto d = make_shared<op::v0::Parameter>(element::i32, shape);
ASSERT_FALSE(n.match(d, b));
ASSERT_FALSE(n.match(std::make_shared<op::v1::Add>(abs, b), std::make_shared<op::v1::Add>(b, b)));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{}));
auto add_absb = std::make_shared<op::v1::Add>(abs, b);
ASSERT_TRUE(n.match(std::make_shared<op::v1::Add>(any, b), add_absb));
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{add_absb, abs, a, b}));
ASSERT_TRUE(n.match(std::make_shared<op::v1::Add>(pattern, b), add_absb));
ASSERT_EQ(n.get_pattern_map()[pattern], abs);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{add_absb, abs, b}));
ASSERT_TRUE(n.match(std::make_shared<op::v1::Add>(b, pattern), add_absb));
ASSERT_EQ(n.get_pattern_map()[pattern], abs);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{add_absb, abs, b}));
auto c = make_shared<op::v0::Parameter>(element::i32, shape);
auto mul_add_absb = std::make_shared<op::v1::Multiply>(c, add_absb);
ASSERT_TRUE(
n.match(std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(b, pattern)), mul_add_absb));
ASSERT_EQ(n.get_pattern_map()[pattern], abs);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{mul_add_absb, c, add_absb, abs, b}));
ASSERT_TRUE(n.match(std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(any, b)),
mul_add_absb)); // nested any
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{mul_add_absb, c, add_absb, abs, a, b}));
ASSERT_TRUE(n.match(std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(any, b)),
std::make_shared<op::v1::Multiply>(std::make_shared<op::v1::Add>(b, abs),
c))); // permutations w/ any
auto mul_c_add_ab = make_shared<op::v1::Multiply>(c, add_ab);
ASSERT_TRUE(n.match(std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(any_false, b)),
std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(a, b)))); //
// nested any
ASSERT_TRUE(n.match(std::make_shared<op::v1::Multiply>(c, std::make_shared<op::v1::Add>(any_false, b)),
mul_c_add_ab)); // permutations w/ any_false
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{mul_c_add_ab, c, add_ab, a, a, b}));
auto iconst1_0 = construct_constant_node(1);
auto iconst1_1 = construct_constant_node(1);
ASSERT_TRUE(n.match(make_shared<op::v1::Multiply>(pattern, iconst1_0),
make_shared<op::v1::Multiply>(a, iconst1_1))); // different iconst
ASSERT_EQ(n.get_pattern_map()[pattern], a);
auto fconst1_0 = ov::op::v0::Constant::create(element::f32, shape, {1});
auto patternf = std::make_shared<pattern::op::Label>(fconst1_0);
ASSERT_TRUE(n.match(make_shared<op::v1::Multiply>(patternf, fconst1_0),
make_shared<op::v1::Multiply>(a, iconst1_1))); // different iconst
// Subgraph labels
auto add = std::make_shared<op::v1::Add>(a, b);
auto label = std::make_shared<pattern::op::Label>(add, nullptr, NodeVector{add});
ASSERT_TRUE(n.match(label, add));
ASSERT_EQ(n.get_pattern_map()[label], add);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{add, add, a, b}));
ASSERT_FALSE(n.match(label, std::make_shared<op::v1::Subtract>(a, b)));
ASSERT_TRUE(n.match(make_shared<op::v0::Abs>(label), make_shared<op::v0::Abs>(add)));
ASSERT_EQ(n.get_pattern_map()[label], add);
// Correct argument order
ASSERT_FALSE(n.match(make_shared<op::v1::Subtract>(b, a), make_shared<op::v1::Subtract>(a, b)));
auto aab = make_shared<op::v1::Multiply>(a, make_shared<op::v1::Subtract>(a, b));
auto paab = make_shared<op::v1::Multiply>(pattern, make_shared<op::v1::Subtract>(pattern, b));
ASSERT_TRUE(n.match(paab, aab));
auto aba = make_shared<op::v1::Multiply>(a, make_shared<op::v1::Subtract>(b, a));
ASSERT_FALSE(n.match(paab, aba));
auto paba = make_shared<op::v1::Multiply>(pattern, make_shared<op::v1::Subtract>(b, pattern));
ASSERT_FALSE(n.match(paba, aab));
// Correlations
auto label1 = std::make_shared<pattern::op::Label>(a);
auto tmp = std::make_shared<op::v1::Add>(label1, b);
auto label2 = std::make_shared<pattern::op::Label>(tmp, nullptr, NodeVector{tmp});
auto sub_label1 = std::make_shared<op::v1::Subtract>(label1, label2);
auto sub_add = std::make_shared<op::v1::Subtract>(a, add);
ASSERT_TRUE(n.match(sub_label1, sub_add));
ASSERT_EQ(n.get_pattern_map()[label1], a);
ASSERT_EQ(n.get_pattern_map()[label2], add);
ASSERT_EQ(n.get_matched_nodes(), (NodeVector{sub_add, a, add, add, a, b}));
ASSERT_FALSE(n.match(sub_label1, std::make_shared<op::v1::Subtract>(add, a)));
auto add_label1 = std::make_shared<op::v1::Add>(label1, label2);
ASSERT_TRUE(n.match(add_label1, std::make_shared<op::v1::Add>(add, a)));
ASSERT_EQ(n.get_pattern_map()[label1], a);
ASSERT_EQ(n.get_pattern_map()[label2], add);
// Or
ASSERT_TRUE(n.match(std::make_shared<pattern::op::Or>(OutputVector{std::make_shared<op::v1::Add>(a, b),
std::make_shared<op::v1::Subtract>(a, b)}),
std::make_shared<op::v1::Add>(a, b)));
ASSERT_TRUE(n.match(std::make_shared<pattern::op::Or>(OutputVector{std::make_shared<op::v1::Add>(a, b),
std::make_shared<op::v1::Subtract>(a, b)}),
std::make_shared<op::v1::Subtract>(a, b)));
// Branch
{
auto branch = std::make_shared<pattern::op::Branch>();
auto star = std::make_shared<pattern::op::Or>(OutputVector{branch, std::make_shared<pattern::op::True>()});
auto pattern = std::make_shared<op::v1::Add>(star, star);
branch->set_destination(pattern);
auto arg =
std::make_shared<op::v1::Add>(std::make_shared<op::v1::Add>(a, b), std::make_shared<op::v1::Add>(b, a));
ASSERT_TRUE(n.match(pattern, std::make_shared<op::v1::Add>(arg, a)));
ASSERT_EQ(n.get_matched_nodes().size(), 4);
}
// strict mode
{
TestMatcher sm(Output<Node>{}, "TestMatcher", true);
// exact shape and type
auto scalar_param = make_shared<op::v0::Parameter>(element::i32, Shape{});
auto label_dynamic_shape = make_shared<pattern::op::Label>(element::i32, PartialShape::dynamic());
auto param = make_shared<op::v0::Parameter>(element::f32, Shape{});
ASSERT_TRUE(sm.match(label_dynamic_shape, scalar_param));
// wrong type
auto scalar_param_wrong_type = make_shared<op::v0::Parameter>(element::f32, Shape{});
ASSERT_FALSE(sm.match(label, scalar_param_wrong_type));
// dynamic dimension
auto label_dynamic_dimension =
make_shared<pattern::op::Label>(element::i32, PartialShape{Dimension::dynamic()});
auto vector_param = make_shared<op::v0::Parameter>(element::i32, Shape{10});
ASSERT_TRUE(sm.match(label_dynamic_dimension, vector_param));
// dynamic type
auto label_dynamic_type = make_shared<pattern::op::Label>(element::dynamic, PartialShape{Dimension::dynamic()});
ASSERT_TRUE(sm.match(label_dynamic_type, vector_param));
}
}
TEST(pattern, mean) {
// construct mean
TestMatcher n;
auto input = std::make_shared<op::v0::Parameter>(element::f32, Shape{2, 3});
auto N = ov::op::v0::Constant::create(element::f32, Shape{3}, {2, 2, 2});
auto sum_input1 = std::make_shared<op::v1::ReduceSum>(input, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto mean = std::make_shared<op::v1::Divide>(sum_input1, N);
auto mean_graph = construct_mean_graph();
ASSERT_TRUE(n.match(mean_graph, mean));
ASSERT_EQ(n.get_pattern_map()[mean_graph], mean);
}
TEST(pattern, variance) {
// construct variance
TestMatcher n;
auto N = ov::op::v0::Constant::create(element::f32, Shape{3}, {2, 2, 2});
auto input = std::make_shared<pattern::op::Label>(element::f32, Shape{2, 3});
auto input_sq = std::make_shared<op::v1::Multiply>(input, input);
auto sum_input = std::make_shared<op::v1::ReduceSum>(input, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto square_sumed_input = std::make_shared<op::v1::Multiply>(sum_input, sum_input);
auto sum_squared_input =
std::make_shared<op::v1::ReduceSum>(input_sq, ov::op::v0::Constant::create(element::i64, {1}, {0}));
auto avg_input_sum_sq = std::make_shared<op::v1::Divide>(square_sumed_input, N);
auto xmu = std::make_shared<op::v1::Subtract>(sum_squared_input, avg_input_sum_sq);
auto variance = std::make_shared<op::v1::Divide>(xmu, N);
auto var_graph = construct_variance_graph();
ASSERT_TRUE(n.match(var_graph, variance));
ASSERT_EQ(n.get_pattern_map()[var_graph], variance);
}
TEST(pattern, previous_matches) {
Shape shape{};
ov::pass::pattern::Matcher::PatternMap previous_matches;
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto pattern = std::make_shared<pattern::op::Label>(b);
auto abs = make_shared<op::v0::Abs>(a);
auto add = make_shared<op::v1::Add>(abs, b);
{
pattern::Matcher n(make_shared<op::v1::Add>(pattern, b));
ASSERT_TRUE(n.match(add, previous_matches));
ASSERT_EQ(n.get_pattern_map()[pattern], abs);
}
{
pattern::Matcher n(make_shared<op::v1::Add>(pattern, b));
previous_matches.insert(std::make_pair(pattern, a));
ASSERT_FALSE(n.match(add, previous_matches));
}
}
TEST(pattern, test_sort) {
Shape shape{};
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, shape);
auto abs1 = make_shared<op::v0::Abs>(a);
auto abs2 = make_shared<op::v0::Abs>(b);
shared_ptr<Node> add = make_shared<op::v1::Add>(abs1, abs2);
auto pa = make_shared<op::v0::Parameter>(element::i32, shape);
auto pb = make_shared<op::v0::Parameter>(element::i32, shape);
auto pabs1 = make_shared<op::v0::Abs>(pa);
auto pabs1_label = std::make_shared<pattern::op::Label>(pabs1);
auto pabs2 = make_shared<op::v0::Abs>(b);
shared_ptr<Node> padd = make_shared<op::v1::Add>(pabs1_label, pabs2);
{
pattern::Matcher n1(padd);
ASSERT_TRUE(n1.match(add));
auto r1 = n1.get_pattern_map()[pabs1_label];
ASSERT_TRUE(n1.match(add));
ASSERT_EQ(r1, n1.get_pattern_map()[pabs1_label]);
}
}
TEST(pattern, label_on_skip) {
Shape shape{2, 2};
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto b = make_shared<op::v0::Parameter>(element::i32, Shape{});
OPENVINO_SUPPRESS_DEPRECATED_START
auto iconst = ngraph::make_zero(element::i32, Shape{});
auto label = std::make_shared<pattern::op::Label>(iconst);
auto const_label = std::make_shared<pattern::op::Label>(iconst, ngraph::is_zero, NodeVector{iconst});
OPENVINO_SUPPRESS_DEPRECATED_END
auto bcst_pred = [](std::shared_ptr<Node> n) {
return ov::as_type_ptr<op::v1::Broadcast>(n) != nullptr;
};
auto shape_const = ov::op::v0::Constant::create(element::u64, Shape{shape.size()}, shape);
auto axes_const = ov::op::v0::Constant::create(element::u8, Shape{}, {0});
auto bcst = std::make_shared<pattern::op::Skip>(OutputVector{const_label, shape_const, axes_const}, bcst_pred);
auto bcst_label = std::make_shared<pattern::op::Label>(bcst, nullptr, NodeVector{bcst});
auto matcher =
std::make_shared<pattern::Matcher>(std::make_shared<op::v1::Multiply>(label, bcst_label), "label_on_skip");
auto const_broadcast = make_shared<op::v1::Broadcast>(iconst, shape_const);
std::shared_ptr<Node> mul = std::make_shared<op::v1::Multiply>(a, const_broadcast);
std::shared_ptr<Node> mul_scalar = std::make_shared<op::v1::Multiply>(b, iconst);
ASSERT_TRUE(matcher->match(mul));
ASSERT_EQ(matcher->get_pattern_map()[bcst_label], const_broadcast);
ASSERT_EQ(matcher->get_pattern_map()[const_label], iconst);
ASSERT_EQ(matcher->get_pattern_map()[label], a);
ASSERT_TRUE(matcher->match(mul_scalar));
ASSERT_EQ(matcher->get_pattern_map()[bcst_label], iconst);
ASSERT_EQ(matcher->get_pattern_map()[const_label], iconst);
ASSERT_EQ(matcher->get_pattern_map()[label], b);
}
TEST(pattern, is_contained_match) {
Shape shape{};
auto a = make_shared<op::v0::Parameter>(element::i32, shape);
auto absn = make_shared<op::v0::Abs>(a);
TestMatcher n;
auto label_a = std::make_shared<pattern::op::Label>(a);
auto label_abs = make_shared<op::v0::Abs>(a);
ASSERT_TRUE(n.match(label_abs, absn));
auto result_absn = make_shared<ov::op::v0::Result>(absn);
ASSERT_TRUE(n.is_contained_match());
auto absn2 = make_shared<op::v0::Abs>(absn);
auto result_absn2 = make_shared<ov::op::v0::Result>(absn2);
auto label_abs2 = make_shared<op::v0::Abs>(label_abs);
ASSERT_TRUE(n.match(label_abs2, absn2));
ASSERT_FALSE(n.is_contained_match());
}
TEST(pattern, wrap_type_single_op) {
auto a = make_shared<op::v0::Parameter>(element::f32, Shape{1, 3, 64, 64});
auto b = make_shared<op::v0::Abs>(a);
auto c = make_shared<ov::op::v0::Relu>(a);
auto mul1 = make_shared<op::v1::Multiply>(a, ov::op::v0::Constant::create(element::f32, Shape{}, {1}));
auto mul2 = make_shared<op::v1::Multiply>(ov::op::v0::Constant::create(element::f32, Shape{}, {1}), a);
{
auto m = pattern::wrap_type<op::v0::Abs>();
auto matcher = std::make_shared<pattern::Matcher>(m, "AbsMatcher");
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(b)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 1);
ASSERT_EQ(matcher->get_matched_nodes()[0], b);
ASSERT_EQ(matcher->get_pattern_map().count(m), 1);
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(c)));
}
{
auto m1 = pattern::wrap_type<op::v0::Parameter>();
auto m2 = pattern::wrap_type<op::v0::Abs>({m1});
auto matcher = std::make_shared<pattern::Matcher>(m2, "ParamAbsMatcher");
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(b)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 2);
ASSERT_EQ(matcher->get_pattern_map().count(m1), 1);
ASSERT_EQ(matcher->get_pattern_map().count(m2), 1);
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(c)));
}
{
auto m1 =
pattern::wrap_type<op::v1::Multiply>({pattern::any_input(), pattern::wrap_type<ov::op::v0::Constant>()});
auto matcher = std::make_shared<pattern::Matcher>(m1, "MultiplyMatcher");
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(mul1)));
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(mul2)));
}
{
auto m1 =
pattern::wrap_type<op::v1::Multiply>({pattern::wrap_type<ov::op::v0::Constant>(), pattern::any_input()});
auto matcher = std::make_shared<pattern::Matcher>(m1, "MultiplyMatcher");
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(mul1)));
ASSERT_TRUE(matcher->match(static_pointer_cast<Node>(mul2)));
}
}
TEST(pattern, wrap_type_multi_op) {
auto a = make_shared<op::v0::Parameter>(element::f32, Shape{1, 3, 64, 64});
auto b = make_shared<op::v0::Abs>(a);
auto c = make_shared<ov::op::v0::Relu>(a);
auto mul = make_shared<op::v1::Multiply>(a, ov::op::v0::Constant::create(element::f32, Shape{}, {1}));
auto add = make_shared<op::v1::Add>(ov::op::v0::Constant::create(element::f32, Shape{}, {1}), a);
{
auto m = pattern::wrap_type<op::v1::Multiply, op::v1::Add>();
auto matcher = std::make_shared<pattern::Matcher>(m, "MulAddMatcher");
ASSERT_TRUE(matcher->match(mul->output(0)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 1);
ASSERT_EQ(matcher->get_matched_nodes()[0], mul);
ASSERT_EQ(matcher->get_pattern_map().count(m), 1);
ASSERT_TRUE(matcher->match(add->output(0)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 1);
ASSERT_EQ(matcher->get_matched_nodes()[0], add);
ASSERT_EQ(matcher->get_pattern_map().count(m), 1);
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(a)));
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(b)));
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(c)));
}
{
auto m = pattern::wrap_type<op::util::BinaryElementwiseArithmetic>();
auto matcher = std::make_shared<pattern::Matcher>(m, "ElementwiseMatcher");
ASSERT_TRUE(matcher->match(mul->output(0)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 1);
ASSERT_EQ(matcher->get_matched_nodes()[0], mul);
ASSERT_EQ(matcher->get_pattern_map().count(m), 1);
ASSERT_TRUE(matcher->match(add->output(0)));
ASSERT_EQ(matcher->get_matched_nodes().size(), 1);
ASSERT_EQ(matcher->get_matched_nodes()[0], add);
ASSERT_EQ(matcher->get_pattern_map().count(m), 1);
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(a)));
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(b)));
ASSERT_FALSE(matcher->match(static_pointer_cast<Node>(c)));
}
}