-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrewrites.rs
1690 lines (1578 loc) · 79.8 KB
/
rewrites.rs
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::model::*;
use egg::{rewrite as rw, *};
use itertools::Itertools;
use root::taso::*;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::time::{Duration, Instant};
// TODO egg now provides bidirectional rules whic should cut down
// this list in half.
#[rustfmt::skip]
pub fn rules<A: Analysis<Mdl>>() -> Vec<Rewrite<Mdl, A>> { vec![
rw!("ewadd-is-associative" ; "(ewadd ?x (ewadd ?y ?z)) " => "(ewadd (ewadd ?x ?y) ?z)"),
rw!("ewadd-is-commutative" ; "(ewadd ?x ?y) " => "(ewadd ?y ?x)"),
rw!("ewmul-is-associative" ; "(ewmul ?x (ewmul ?y ?z)) " => "(ewmul (ewmul ?x ?y) ?z)"),
rw!("ewmul-is-commutative" ; "(ewmul ?x ?y) " => "(ewmul ?y ?x)"),
rw!("distributivity-0" ; "(ewmul (ewadd ?x ?y) ?z) " => "(ewadd (ewmul ?x ?z) (ewmul ?y ?z))"),
rw!("smul-is-associative" ; "(smul (smul ?x ?y) ?w) " => "(smul ?x (smul ?y ?w))"),
rw!("distributivity-1" ; "(smul (ewadd ?x ?y) ?w) " => "(ewadd (smul ?x ?w) (smul ?y ?w))"),
rw!("operator-commutativity-0" ; "(smul (ewmul ?x ?y) ?w) " => "(ewmul ?x (smul ?y ?w))"),
rw!("transpose-is-its-own-inverse" ; "(transpose (transpose ?x)) " => "?x"),
rw!("operator-commutativity-1" ; "(transpose (ewadd ?x ?y)) " => "(ewadd (transpose ?x) (transpose ?y))"),
rw!("operator-commutativity-2" ; "(transpose (ewmul ?x ?y)) " => "(ewmul (transpose ?x) (transpose ?y))"),
rw!("operator-commutativity-3" ; "(smul (transpose ?x) ?w) " => "(transpose (smul ?x ?w))"),
rw!("matmul-is-associative" ; "(matmul ?x (matmul ?y ?z)) " => "(matmul (matmul ?x ?y) ?z)"),
rw!("matmul-is-linear-0" ; "(smul (matmul ?x ?y) ?w) " => "(matmul ?x (smul ?y ?w))"),
rw!("matmul-is-linear-1" ; "(matmul ?x (ewadd ?y ?z)) " => "(ewadd (matmul ?x ?y) (matmul ?x ?z))"),
rw!("matmul-and-transpose" ; "(transpose (matmul ?x ?y)) " => "(matmul (transpose ?y) (transpose ?x))"),
rw!("conv-is-bilinear-0" ; "(conv2d ?sx ?sy ?p ?c (smul ?x ?w) ?y) " => "(conv2d ?sx ?sy ?p ?c ?x (smul ?y ?w))"),
rw!("conv-is-bilinear-1" ; "(smul (conv2d ?sx ?sy ?p 0 ?x ?y) ?w) " => "(conv2d ?sx ?sy ?p 0 (smul ?x ?w) ?y)"),
rw!("conv-is-bilinear-2" ; "(conv2d ?sx ?sy ?p 0 ?x (ewadd ?y ?z)) " => "(ewadd (conv2d ?sx ?sy ?p 0 ?x ?y) (conv2d ?sx ?sy ?p 0 ?x ?z))"),
rw!("conv-is-bilinear-3" ; "(conv2d ?sx ?sy ?p 0 (ewadd ?x ?y) ?z) " => "(ewadd (conv2d ?sx ?sy ?p 0 ?x ?z) (conv2d ?sx ?sy ?p 0 ?y ?z))"),
//rw!("enlarge-convolution-kernel" ; "(conv2d ?sx ?sy 0 ?c ?x ?y) " => "(conv2d ?sx ?sy 0 ?c ?x (enlarge ?kx ?ky ?y))"),
rw!("operator-commutativity-4" ; "(conv2d ?sx ?sy ?p 2 ?x ?y) " => "(relu (conv2d ?sx ?sy ?p 0 ?x ?y))"),
rw!("conv-with-2-applies-relu" ; "(relu (transpose ?x)) " => "(transpose (relu ?x))"),
// rw!("pooling-by-conv.-with-Cpool" ; "(conv2d ?sx ?sy ?p 0 ?x (Cpool ?kx ?ky)) " => "(poolavg ?kx ?ky ?sx ?sy ?p ?x)"),
rw!("const_iconv-and-const_pool" ; "(poolavg ?kx ?ky 1 1 0 (Iconv ?kx ?ky)) " => "(Cpool ?kx ?ky)"),
rw!("identity-kernel" ; "(conv2d 1 1 0 0 ?x (Iconv ?kx ?ky)) " => "?x"),
rw!("identity-matrix" ; "(matmul ?x Imatmul ) " => "?x"),
rw!("ewmul-identity" ; "(ewmul ?x Iewmul) " => "?x"),
rw!("split-definition-0" ; "(split_0 ?a (concat ?a ?x ?y)) " => "?x"),
rw!("split-definition-1" ; "(split_1 ?a (concat ?a ?x ?y)) " => "?y"),
rw!("geometry-of-concatenation" ; "(concat 0 (concat 1 ?x ?y) (concat 1 ?z ?w)) " => "(concat 1 (concat 0 ?x ?z) (concat 0 ?y ?w))"),
rw!("operator-commutativity-5" ; "(concat ?a (smul ?x ?w) (smul ?y ?w)) " => "(smul (concat ?a ?x ?y) ?w)"),
rw!("operator-commutativity-6" ; "(concat ?a (ewadd ?x ?y) (ewadd ?z ?w)) " => "(ewadd (concat ?a ?x ?z) (concat ?a ?y ?w))"),
rw!("operator-commutativity-7" ; "(concat ?a (ewmul ?x ?y) (ewmul ?z ?w)) " => "(ewmul (concat ?a ?x ?z) (concat ?a ?y ?w))"),
rw!("operator-commutativity-8" ; "(concat ?a (relu ?x) (relu ?y)) " => "(relu (concat ?a ?x ?y))"),
rw!("concatenation-and-transpose" ; "(concat 1 (transpose ?x) (transpose ?y)) " => "(transpose (concat 0 ?x ?y))"),
rw!("concatenation-and-matrix-mul.-0" ; "(concat 1 (matmul ?x ?y) (matmul ?x ?z)) " => "(matmul ?x (concat 1 ?y ?z))"),
rw!("concatenation-and-matrix-mul.-1" ; "(matmul (concat 1 ?x ?z) (concat 0 ?y ?w)) " => "(ewadd (matmul ?x ?y) (matmul ?z ?w))"),
rw!("concatenation-and-conv.-0" ; "(concat 0 (conv2d ?sx ?sy ?p ?c ?x ?z) (conv2d ?sx ?sy ?p ?c ?y ?z)) " => "(conv2d ?sx ?sy ?p ?c (concat 0 ?x ?y) ?z)"),
rw!("concatenation-and-conv.-1" ; "(concat 1 (conv2d ?sx ?sy ?p ?c ?x ?y) (conv2d ?sx ?sy ?p ?c ?x ?z)) " => "(conv2d ?sx ?sy ?p ?c ?x (concat 0 ?y ?z))"),
rw!("concatenation-and-conv.-2" ; "(conv2d ?sx ?sy ?p 0 (concat 1 ?x ?z) (concat 1 ?y ?w)) " => "(ewadd (conv2d ?sx ?sy ?p 0 ?x ?y) (conv2d ?sx ?sy ?p 0 ?z ?w))"),
rw!("concatenation-and-pooling-0" ; "(concat 1 (poolavg ?kx ?ky ?sx ?sy ?p ?x) (poolavg ?kx ?ky ?sx ?sy ?p ?y)) " => "(poolavg ?kx ?ky ?sx ?sy ?p (concat 1 ?x ?y))"),
rw!("concatenation-and-pooling-1" ; "(concat 0 (poolmax ?kx ?ky ?sx ?sy ?p ?x) (poolmax ?kx ?ky ?sx ?sy ?p ?y)) " => "(poolmax ?kx ?ky ?sx ?sy ?p (concat 0 ?x ?y))"),
rw!("concatenation-and-pooling-2" ; "(concat 1 (poolmax ?kx ?ky ?sx ?sy ?p ?x) (poolmax ?kx ?ky ?sx ?sy ?p ?y)) " => "(poolmax ?kx ?ky ?sx ?sy ?p (concat 1 ?x ?y))"),
// inverse
rw!("-ewadd-is-associative" ;"(ewadd (ewadd ?x ?y) ?z)" => "(ewadd ?x (ewadd ?y ?z)) " ),
rw!("-ewadd-is-commutative" ;"(ewadd ?y ?x)" => "(ewadd ?x ?y) " ),
rw!("-ewmul-is-associative" ;"(ewmul (ewmul ?x ?y) ?z)" => "(ewmul ?x (ewmul ?y ?z)) " ),
rw!("-ewmul-is-commutative" ;"(ewmul ?y ?x)" => "(ewmul ?x ?y) " ),
rw!("-distributivity-0" ;"(ewadd (ewmul ?x ?z) (ewmul ?y ?z))" => "(ewmul (ewadd ?x ?y) ?z) " ),
rw!("-smul-is-associative" ;"(smul ?x (smul ?y ?w))" => "(smul (smul ?x ?y) ?w) " ),
rw!("-distributivity-1" ;"(ewadd (smul ?x ?w) (smul ?y ?w))" => "(smul (ewadd ?x ?y) ?w) " ),
rw!("-operator-commutativity-0" ;"(ewmul ?x (smul ?y ?w))" => "(smul (ewmul ?x ?y) ?w) " ),
rw!("-transpose-is-its-own-inverse" ;"?x" => "(transpose (transpose ?x)) " ),
rw!("-operator-commutativity-1" ;"(ewadd (transpose ?x) (transpose ?y))" => "(transpose (ewadd ?x ?y)) " ),
rw!("-operator-commutativity-2" ;"(ewmul (transpose ?x) (transpose ?y))" => "(transpose (ewmul ?x ?y)) " ),
rw!("-operator-commutativity-3" ;"(transpose (smul ?x ?w))" => "(smul (transpose ?x) ?w) " ),
rw!("-matmul-is-associative" ;"(matmul (matmul ?x ?y) ?z)" => "(matmul ?x (matmul ?y ?z)) " ),
rw!("-matmul-is-linear-0" ;"(matmul ?x (smul ?y ?w))" => "(smul (matmul ?x ?y) ?w) " ),
rw!("-matmul-is-linear-1" ;"(ewadd (matmul ?x ?y) (matmul ?x ?z))" => "(matmul ?x (ewadd ?y ?z)) " ),
rw!("-matmul-and-transpose" ;"(matmul (transpose ?y) (transpose ?x))" => "(transpose (matmul ?x ?y)) " ),
rw!("-conv-is-bilinear-0" ;"(conv2d ?sx ?sy ?p ?c ?x (smul ?y ?w))" => "(conv2d ?sx ?sy ?p ?c (smul ?x ?w) ?y) " ),
rw!("-conv-is-bilinear-1" ;"(conv2d ?sx ?sy ?p 0 (smul ?x ?w) ?y)" => "(smul (conv2d ?sx ?sy ?p 0 ?x ?y) ?w) " ),
rw!("-conv-is-bilinear-2" ;"(ewadd (conv2d ?sx ?sy ?p 0 ?x ?y) (conv2d ?sx ?sy ?p 0 ?x ?z))" => "(conv2d ?sx ?sy ?p 0 ?x (ewadd ?y ?z)) " ),
rw!("-conv-is-bilinear-3" ;"(ewadd (conv2d ?sx ?sy ?p 0 ?x ?z) (conv2d ?sx ?sy ?p 0 ?y ?z))" => "(conv2d ?sx ?sy ?p 0 (ewadd ?x ?y) ?z) " ),
rw!("-enlarge-convolution-kernel" ;"(conv2d ?sx ?sy 0 ?c ?x (enlarge ?kx ?ky ?y))" => "(conv2d ?sx ?sy 0 ?c ?x ?y) " ),
rw!("-operator-commutativity-4" ;"(relu (conv2d ?sx ?sy ?p 0 ?x ?y))" => "(conv2d ?sx ?sy ?p 2 ?x ?y) " ),
rw!("-conv-with-2-applies-relu" ;"(transpose (relu ?x))" => "(relu (transpose ?x)) " ),
rw!("-pooling-by-conv.-with-Cpool" ;"(poolavg ?kx ?ky ?sx ?sy ?p ?x)" => "(conv2d ?sx ?sy ?p 0 ?x (Cpool ?kx ?ky)) " ),
rw!("-const_iconv-and-const_pool" ;"(Cpool ?kx ?ky)" => "(poolavg ?kx ?ky 1 1 0 (Iconv ?kx ?ky))"),
// rw!("-identity-kernel" ;"?x" => "(conv2d 1 1 0 0 ?x (Iconv ?k)) " ),
rw!("-identity-matrix" ;"?x" => "(matmul ?x Imatmul ) " ),
rw!("-ewmul-identity" ;"?x" => "(ewmul ?x Iewmul) " ),
// rw!("-split-definition-00" ;"?x" => "(split_0 1 (concat 1 ?x ?y)) " ),
// rw!("-split-definition-01" ;"?x" => "(split_0 0 (concat 0 ?x ?y)) " ),
// rw!("-split-definition-10" ;"?y" => "(split_1 0 (concat 0 ?x ?y)) " ),
// rw!("-split-definition-11" ;"?y" => "(split_1 1 (concat 1 ?x ?y)) " ),
rw!("-geometry-of-concatenation" ;"(concat 1 (concat 0 ?x ?z) (concat 0 ?y ?w))" => "(concat 0 (concat 1 ?x ?y) (concat 1 ?z ?w)) " ),
rw!("-operator-commutativity-5" ;"(smul (concat ?a ?x ?y) ?w)" => "(concat ?a (smul ?x ?w) (smul ?y ?w)) " ),
rw!("-operator-commutativity-6" ;"(ewadd (concat ?a ?x ?z) (concat ?a ?y ?w))" => "(concat ?a (ewadd ?x ?y) (ewadd ?z ?w)) " ),
rw!("-operator-commutativity-7" ;"(ewmul (concat ?a ?x ?z) (concat ?a ?y ?w))" => "(concat ?a (ewmul ?x ?y) (ewmul ?z ?w)) " ),
rw!("-operator-commutativity-8" ;"(relu (concat ?a ?x ?y))" => "(concat ?a (relu ?x) (relu ?y)) " ),
rw!("-concatenation-and-transpose" ;"(transpose (concat 0 ?x ?y))" => "(concat 1 (transpose ?x) (transpose ?y)) " ),
rw!("-concatenation-and-matrix-mul.-0" ;"(matmul ?x (concat 1 ?y ?z))" => "(concat 1 (matmul ?x ?y) (matmul ?x ?z)) " ),
rw!("-concatenation-and-matrix-mul.-1" ;"(ewadd (matmul ?x ?y) (matmul ?z ?w))" => "(matmul (concat 1 ?x ?z) (concat 0 ?y ?w)) " ),
rw!("-concatenation-and-conv.-0" ;"(conv2d ?sx ?sy ?p ?c (concat 0 ?x ?y) ?z)" => "(concat 0 (conv2d ?sx ?sy ?p ?c ?x ?z) (conv2d ?sx ?sy ?p ?c ?y ?z)) " ),
rw!("-concatenation-and-conv.-1" ;"(conv2d ?sx ?sy ?p ?c ?x (concat 0 ?y ?z))" => "(concat 1 (conv2d ?sx ?sy ?p ?c ?x ?y) (conv2d ?sx ?sy ?p ?c ?x ?z)) " ),
rw!("-concatenation-and-conv.-2" ;"(ewadd (conv2d ?sx ?sy ?p 0 ?x ?y) (conv2d ?sx ?sy ?p 0 ?z ?w))" => "(conv2d ?sx ?sy ?p 0 (concat 1 ?x ?z) (concat 1 ?y ?w)) " ),
rw!("-concatenation-and-pooling-0" ;"(poolavg ?kx ?ky ?sx ?sy ?p (concat 1 ?x ?y))" => "(concat 1 (poolavg ?kx ?ky ?sx ?sy ?p ?x) (poolavg ?kx ?ky ?sx ?sy ?p ?y)) " ),
rw!("-concatenation-and-pooling-1" ;"(poolmax ?kx ?ky ?sx ?sy ?p (concat 0 ?x ?y))" => "(concat 0 (poolmax ?kx ?ky ?sx ?sy ?p ?x) (poolmax ?kx ?ky ?sx ?sy ?p ?y)) " ),
rw!("-concatenation-and-pooling-2" ;"(poolmax ?kx ?ky ?sx ?sy ?p (concat 1 ?x ?y))" => "(concat 1 (poolmax ?kx ?ky ?sx ?sy ?p ?x) (poolmax ?kx ?ky ?sx ?sy ?p ?y)) " ),
]}
pub fn rules_from_str(rs: Vec<&str>, filter_after: bool) -> Vec<Rewrite<Mdl, TensorAnalysis>> {
let mut rule_vec = Vec::new();
for (pos, rule) in rs.iter().enumerate() {
let eqn: Vec<&str> = rule.split("=>").collect();
let lhs: Pattern<Mdl> = eqn[0].parse().unwrap();
let rhs: Pattern<Mdl> = eqn[1].parse().unwrap();
let rule_name = format!("rule{}", pos);
rule_vec.push(rw!(rule_name; { lhs.clone() } => { CheckApply {
pat: rhs,
src_pat: lhs,
filter_after: filter_after,
} }));
}
rule_vec
}
/// Hand specified normal rules from TASO
#[rustfmt::skip]
pub static PRE_DEFINED_RULES: &[&str] = &[
"(conv2d 1 1 0 0 ?input_1 ?input_2)=>(conv2d 1 1 0 0 ?input_1 (merge ?input_2 2))",
"(conv2d 1 1 0 2 ?input_1 ?input_2)=>(conv2d 1 1 0 2 ?input_1 (merge ?input_2 2))",
"(conv2d 2 2 0 0 ?input_1 ?input_2)=>(conv2d 2 2 0 0 ?input_1 (merge ?input_2 2))",
"(conv2d 2 2 0 2 ?input_1 ?input_2)=>(conv2d 2 2 0 2 ?input_1 (merge ?input_2 2))",
];
/// Hand specified multi-pattern rules from TASO
#[rustfmt::skip]
pub static PRE_DEFINED_MULTI: &[&str] = &[
"(conv2d 1 1 0 0 ?input_1 ?input_2)=>(split_0 (split 1 (conv2d 1 1 0 0 ?input_1 (concat 0 4 (enlarge ?input_2 ?input_3) ?input_3))))",
"(conv2d 1 1 0 0 ?input_1 ?input_3)=>(split_1 (split 1 (conv2d 1 1 0 0 ?input_1 (concat 0 4 (enlarge ?input_2 ?input_3) ?input_3))))",
"(conv2d 1 1 0 2 ?input_1 ?input_2)=>(split_0 (split 1 (conv2d 1 1 0 2 ?input_1 (concat 0 4 (enlarge ?input_2 ?input_3) ?input_3))))",
"(conv2d 1 1 0 2 ?input_1 ?input_3)=>(split_1 (split 1 (conv2d 1 1 0 2 ?input_1 (concat 0 4 (enlarge ?input_2 ?input_3) ?input_3))))",
];
/// Struct for passing results in the recursive function check_pat
///
/// Similar as ValTnsr for TensorAnalysis, but with tnsr being the object
/// rather than pointer, to make memory working correctly with recursive
/// function.
struct TData {
pub dtype: DataKind,
pub val: i32,
pub tnsr: Option<Tensor>,
pub tnsr_2: Option<Tensor>,
}
impl Default for TData {
fn default() -> Self {
TData {
tnsr: None,
tnsr_2: None,
val: Default::default(),
dtype: Default::default(),
}
}
}
impl PartialEq for Op {
fn eq(&self, other: &Self) -> bool {
self.guid == other.guid && self.ptr == other.ptr
}
}
/// Custom struct implementing the Applier trait, checking the new nodes to
/// construct are all valid before actually apply.
#[derive(Debug, Clone, PartialEq)]
struct CheckApply {
/// the pattern of the right hand side of the rewrite rule, the one
/// to be constructed.
pat: Pattern<Mdl>,
/// Source graph pattern, used in cycle filtering
src_pat: Pattern<Mdl>,
/// Whether we need to check if any node in matched source graph is in blacklist
filter_after: bool,
}
impl Applier<Mdl, TensorAnalysis> for CheckApply {
/// Apply the pattern once. Check the new nodes are valid before actually
/// apply. See Applier trait in egg for more information.
fn apply_one(
&self,
egraph: &mut EGraph<Mdl, TensorAnalysis>,
matched_id: Id,
subst: &Subst,
) -> Vec<Id> {
if self.filter_after {
// Check if any node in matched source graph is in blacklist. If so, stop applying
let (contains, _) = contains_blacklist(self.src_pat.ast.as_ref(), egraph, subst);
if contains {
return vec![];
}
}
let (valid, _, _, existing) = check_pat(
self.pat.ast.as_ref(),
egraph,
subst,
/*get_exist_nodes=*/ self.filter_after,
);
if valid {
let result = self.pat.apply_one(egraph, matched_id, subst);
// Add the newly added nodes to the ordering vector
if self.filter_after {
let existing = existing.unwrap();
add_newly_added(self.pat.ast.as_ref(), egraph, subst, &existing);
}
result
} else {
vec![]
}
}
fn vars(&self) -> Vec<Var> {
self.pat.vars()
}
}
/// Check if the matched graph of the pattern contains any blacklisted nodes
///
/// # Returns
///
/// A tuple of (bool, Option<Id>) where
///
/// - bool: true if the nodes in this pattern contains some node in blacklist
/// - Option<Id>: if the nodes in this pattern do not contain blacklisted
/// nodes, then this is the Id of the matched EClass of the root of this pattern(pat.last())
fn contains_blacklist(
pat: &[ENodeOrVar<Mdl>],
egraph: &mut EGraph<Mdl, TensorAnalysis>,
subst: &Subst,
) -> (bool, Option<Id>) {
match pat.last().unwrap() {
ENodeOrVar::Var(w) => (false, Some(subst[*w])),
ENodeOrVar::ENode(e) => {
let children = e.children();
let results: Vec<(bool, Option<Id>)> = children
.iter()
.map(|child| contains_blacklist(&pat[..usize::from(*child) + 1], egraph, subst))
.collect();
let contains = results.iter().any(|res| res.0);
if contains {
(true, None)
} else {
let mut new_e = e.clone();
let new_e_ch = new_e.children_mut();
for (i, res) in results.iter().enumerate() {
if let Some(id) = res.1 {
new_e_ch[i] = id;
} else {
// This place shouldn't be reached in any case. The pat and subst passed
// in as arguments are from the results of searching pat in the Egraph.
// So all the nodes in pat should be present in the EGraph. But if we run
// bert with 1 iteration of multi and more than ~5/6 iterations of single
// rules, this place is reached. Seems that the searched matches returned
// by egg can have some nodes in pat that cannot be found in the Egraph.
//
// Right now, we simply treat the pattern as not containing blacklisted
// nodes if this happens. Since we do cycle filtering in the end of each
// iteration, this should be fine.
//
// TODO: look into the above issue
return (false, None);
}
}
if egraph.analysis.blacklist_nodes.contains(&new_e) {
(true, None)
} else {
let looked = egraph.lookup(new_e);
// This looked should never be None. See the above issue.
(false, looked)
}
}
}
}
}
/// Check if all the new nodes to create in the pattern is valid.
///
/// This function does the checking recursively.
///
/// # Parameters
///
/// - `pat`: the AST representation of the pattern. See egg::Pattern for more info
/// - `egraph`: E-graph of interest
/// - `subst`: mapping variable to eclass ID. See egg::Subst for more info.
/// - `get_exist_nodes`: whether to get a set of existing nodes in this pattern
///
/// # Returns
///
/// A tuple of (bool, Option<Id>, TData) where
///
/// - bool: true if the nodes in this pattern are all valid
/// - Option<Id>: if the root node of this pattern (pat.last()) is in egraph,
/// then it is the Id of that eclass. Otherwise None
/// - TData: The TData for the root node of this pattern. This is read from
/// egraph if the root node is in egraph, otherwise constructed by calling
/// TASO functions.
/// - Option<HashSet<Mdl>>: if get_exist_nodes is true, this returns the set of
/// existing nodes in this pattern
fn check_pat(
pat: &[ENodeOrVar<Mdl>],
egraph: &mut EGraph<Mdl, TensorAnalysis>,
subst: &Subst,
get_exist_nodes: bool,
) -> (bool, Option<Id>, TData, Option<HashSet<Mdl>>) {
match pat.last().unwrap() {
ENodeOrVar::Var(w) => {
// The root node is a variable, then use subst to get metadata from egraph
let cid = subst[*w];
let t_data = if egraph[cid].data.dtype == DataKind::Tnsr {
TData {
dtype: egraph[cid].data.dtype,
val: egraph[cid].data.val,
tnsr: unsafe { Some((*egraph[cid].data.meta).clone()) },
tnsr_2: None,
}
} else {
// A variable cannot refer to a TnsrTuple, so we don't need that case
TData {
dtype: egraph[cid].data.dtype,
val: egraph[cid].data.val,
tnsr: None,
tnsr_2: None,
}
};
if get_exist_nodes {
return (true, Some(cid), t_data, Some(HashSet::<Mdl>::new()));
} else {
return (true, Some(cid), t_data, None);
}
}
ENodeOrVar::ENode(e) => {
// The root is an enode. Recursively get checking results from its children
let children = e.children();
let results: Vec<(bool, Option<Id>, TData, Option<HashSet<Mdl>>)> = children
.iter()
.map(|child| {
check_pat(
&pat[..usize::from(*child) + 1],
egraph,
subst,
get_exist_nodes,
)
})
.collect();
// Check if any children contains invalid nodes
let mut violated = false;
for res in &results {
if !res.0 {
violated = true;
}
}
if violated {
let default_data: TData = Default::default();
return (false, None, default_data, None);
} else {
// Check if all children are in egraph
let mut all_in = true;
for res in &results {
let is_in = match res.1 {
Some(_) => true,
None => false,
};
if !is_in {
all_in = false;
}
}
if all_in {
// Construct enode, check if in egraph
let mut new_e = e.clone();
let new_e_ch = new_e.children_mut();
for (i, res) in results.iter().enumerate() {
new_e_ch[i] = res.1.unwrap();
}
let looked = egraph.lookup(new_e.clone());
if let Some(id) = looked {
// Get metadata from egraph
let t_data = match egraph[id].data.dtype {
DataKind::Tnsr => TData {
dtype: egraph[id].data.dtype,
val: egraph[id].data.val,
tnsr: unsafe { Some((*egraph[id].data.meta).clone()) },
tnsr_2: None,
},
DataKind::TnsrTuple => TData {
dtype: egraph[id].data.dtype,
val: egraph[id].data.val,
tnsr: unsafe { Some((*egraph[id].data.meta).clone()) },
tnsr_2: unsafe { Some((*egraph[id].data.meta_2).clone()) },
},
_ => TData {
dtype: egraph[id].data.dtype,
val: egraph[id].data.val,
tnsr: None,
tnsr_2: None,
},
};
if get_exist_nodes {
let mut existing_nodes = HashSet::<Mdl>::new();
for res in results.iter() {
for node in res.3.as_ref().unwrap().iter() {
existing_nodes.insert(node.clone());
}
}
existing_nodes.insert(new_e);
return (true, looked, t_data, Some(existing_nodes));
} else {
return (true, looked, t_data, None);
}
}
}
// root node not in egraph, compute metadata
let mut g = egraph.analysis.graph.borrow_mut();
let result = match e {
Mdl::Num(_n) => {
let t_data = TData {
dtype: DataKind::Scalar,
val: *_n,
tnsr: None,
tnsr_2: None,
};
(true, None, t_data)
}
Mdl::Relu(_a) => {
let a_t_data = &results[0].2;
assert!(a_t_data.dtype == DataKind::Tnsr);
let t_a = a_t_data.tnsr.unwrap();
unsafe {
let op = (*g.model).get_or_create_activation(t_a, OpType_OP_RELU, true);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Tanh(_a) => {
let a_t_data = &results[0].2;
assert!(a_t_data.dtype == DataKind::Tnsr);
let t_a = a_t_data.tnsr.unwrap();
unsafe {
let op = (*g.model).get_or_create_activation(t_a, OpType_OP_TANH, true);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Sigmoid(_a) => {
let a_t_data = &results[0].2;
assert!(a_t_data.dtype == DataKind::Tnsr);
let t_a = a_t_data.tnsr.unwrap();
unsafe {
let op =
(*g.model).get_or_create_activation(t_a, OpType_OP_SIGMOID, true);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Conv2d([_stride_h, _stride_w, _pad, _act, _inpt, _wght]) => {
// Check types
let _stride_h_data = &results[0].2;
let _stride_w_data = &results[1].2;
let _pad_data = &results[2].2;
let _act_data = &results[3].2;
let _inpt_data = &results[4].2;
let _wght_data = &results[5].2;
assert!(_stride_h_data.dtype == DataKind::Scalar);
assert!(_stride_w_data.dtype == DataKind::Scalar);
assert!(_pad_data.dtype == DataKind::Scalar);
assert!(_act_data.dtype == DataKind::Scalar);
assert!(_inpt_data.dtype == DataKind::Tnsr);
assert!(_wght_data.dtype == DataKind::Tnsr);
// Get arguments
let t_inpt = _inpt_data.tnsr.unwrap();
let t_wght = _wght_data.tnsr.unwrap();
let stride_h = _stride_h_data.val;
let stride_w = _stride_w_data.val;
let padding: PaddingMode = _pad_data.val.try_into().unwrap();
let activation: ActiMode = _act_data.val.try_into().unwrap();
// Try creating op
unsafe {
let op = (*g.model).get_or_create_conv2d(
t_inpt, t_wght, stride_h, stride_w, padding, activation,
);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Ewadd([_a, _b]) => {
// Check types
let _a_data = &results[0].2;
let _b_data = &results[1].2;
assert!(_a_data.dtype == DataKind::Tnsr);
assert!(_b_data.dtype == DataKind::Tnsr);
// Get arguments
let t_a = _a_data.tnsr.unwrap();
let t_b = _b_data.tnsr.unwrap();
// Try creating op
unsafe {
let op = (*g.model).get_or_create_element(OpType_OP_EW_ADD, &t_a, &t_b);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Ewmul([_a, _b]) => {
// Check types
let _a_data = &results[0].2;
let _b_data = &results[1].2;
assert!(_a_data.dtype == DataKind::Tnsr);
assert!(_b_data.dtype == DataKind::Tnsr);
// Get arguments
let t_a = _a_data.tnsr.unwrap();
let t_b = _b_data.tnsr.unwrap();
// Try creating op
unsafe {
let op = (*g.model).get_or_create_element(OpType_OP_EW_MUL, &t_a, &t_b);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Matmul([_act, _a, _b]) => {
// Check types
let _act_data = &results[0].2;
let _a_data = &results[1].2;
let _b_data = &results[2].2;
assert!(_act_data.dtype == DataKind::Scalar);
assert!(_a_data.dtype == DataKind::Tnsr);
assert!(_b_data.dtype == DataKind::Tnsr);
// Get arguments
let t_a = _a_data.tnsr.unwrap();
let t_b = _b_data.tnsr.unwrap();
let activation: ActiMode = _act_data.val.try_into().unwrap();
// Try creating op
unsafe {
let op = (*g.model).get_or_create_matmul(t_a, t_b, activation);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Concat([_axis, _ndim, _a, _b]) => {
// Check types
let _axis_data = &results[0].2;
let _ndim_data = &results[1].2;
let _a_data = &results[2].2;
let _b_data = &results[3].2;
assert!(_axis_data.dtype == DataKind::Scalar);
assert!(_ndim_data.dtype == DataKind::Scalar);
assert!(_a_data.dtype == DataKind::Tnsr);
assert!(_b_data.dtype == DataKind::Tnsr);
// Get arguments
let t_a = _a_data.tnsr.unwrap();
let t_b = _b_data.tnsr.unwrap();
let axis = _axis_data.val;
let ndim = _ndim_data.val;
// Try creating op
// Check tensor ndim
if t_a.numDim != ndim || t_b.numDim != ndim {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
// Pass ownership to C++
let mut inputs = vec![t_a, t_b];
inputs.shrink_to_fit();
assert!(inputs.len() == inputs.capacity());
let ptr = inputs.as_mut_ptr();
std::mem::forget(inputs);
let mut need_copy = [false, false];
unsafe {
let op = (*g.model).get_or_create_concat(
axis,
2,
ptr,
need_copy.as_mut_ptr(),
);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
}
Mdl::Merge([_weight, _count]) => {
// Check types
let _weight_data = &results[0].2;
let _count_data = &results[1].2;
assert!(_count_data.dtype == DataKind::Scalar);
assert!(_weight_data.dtype == DataKind::Tnsr);
// Get arguments
let t_weight = _weight_data.tnsr.unwrap();
let count = _count_data.val;
// Try creating op
unsafe {
let op = (*g.model).get_or_create_merge_gconv(&t_weight, count);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
Mdl::Split([_axis, _inpt]) => {
// Check types
let _axis_data = &results[0].2;
let _inpt_data = &results[1].2;
assert!(_axis_data.dtype == DataKind::Scalar);
assert!(_inpt_data.dtype == DataKind::Tnsr);
// Get arguments
let t_inpt = _inpt_data.tnsr.unwrap();
let axis = _axis_data.val;
// Try creating op
unsafe {
let op = (*g.model).get_or_create_split1(&t_inpt, axis, 2);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t_1 = (*op.ptr).outputs[0].clone();
let t_2 = (*op.ptr).outputs[1].clone();
let t_data = TData {
dtype: DataKind::TnsrTuple,
val: 0,
tnsr: Some(t_1),
tnsr_2: Some(t_2),
};
(true, None, t_data)
}
}
}
Mdl::Split0(_inpt) => {
// Check types
let _inpt_data = &results[0].2;
assert!(_inpt_data.dtype == DataKind::TnsrTuple);
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: _inpt_data.tnsr,
tnsr_2: None,
};
(true, None, t_data)
}
Mdl::Split1(_inpt) => {
// Check types
let _inpt_data = &results[0].2;
assert!(_inpt_data.dtype == DataKind::TnsrTuple);
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: _inpt_data.tnsr_2,
tnsr_2: None,
};
(true, None, t_data)
}
Mdl::Enlarge([_a, _b]) => {
// Check types
let _a_data = &results[0].2;
let _b_data = &results[1].2;
assert!(_a_data.dtype == DataKind::Tnsr);
assert!(_b_data.dtype == DataKind::Tnsr);
// Get arguments
let t_a = _a_data.tnsr.unwrap();
let t_b = _b_data.tnsr.unwrap();
// Try creating op
unsafe {
let op = (*g.model).get_or_create_enlarge(t_a, t_b);
if op == Op_INVALID_OP {
let default_data: TData = Default::default();
(false, None, default_data)
} else {
let t = (*op.ptr).outputs[0].clone();
let t_data = TData {
dtype: DataKind::Tnsr,
val: 0,
tnsr: Some(t),
tnsr_2: None,
};
(true, None, t_data)
}
}
}
other => {
println!("{:?}", other);
todo!()
}
};
if get_exist_nodes && result.0 {
let mut existing_nodes = HashSet::<Mdl>::new();
for res in results.iter() {
for node in res.3.as_ref().unwrap().iter() {
existing_nodes.insert(node.clone());
}
}
return (result.0, result.1, result.2, Some(existing_nodes));
} else {
return (result.0, result.1, result.2, None);
}
}
}
};
}
/// Struct for storing information on how each pattern maps to its canonical version
#[derive(Debug)]
struct MapToCanonical {
/// Index into MultiPatterns.canonical_src_pat. Points to the canonical version.
index: usize,
/// Mapping from variable in this pattern to variable in the canonical pattern.
var_map: HashMap<egg::Var, egg::Var>,
}
/// Struct for the multi-pattern rules. In charge of searching for matches and
/// applying the rewrite.
#[derive(Debug)]
pub struct MultiPatterns {
/// Vec of (src_1, src_2, dst_1, dst_2, symmetric)
rules: Vec<(Pattern<Mdl>, Pattern<Mdl>, Pattern<Mdl>, Pattern<Mdl>, bool)>,
/// Vec of all unique canonical source patterns (for src_1's and src_2's)
canonical_src_pat: Vec<Pattern<Mdl>>,
/// Mapping information for each src pattern. The order is the same as in rules
src_pat_maps: Vec<(MapToCanonical, MapToCanonical)>,
/// Whether to allow cycles in EGraph
no_cycle: bool,
/// Whether to do cycle filtering after applying. This is always false when no_cycle is false
filter_after: bool,
/// Number of iterations to run multi-pattern rules
iter_limit: usize,
/// Maximum number of nodes to added here
node_limit: usize,
/// Maximum number of seconds to run
n_sec: u64,
/// Number of successfully applied matches
num_applied: usize,
/// Descendents map. Only used if filter_after is true
descendents: Option<HashMap<Id, HashSet<Id>>>,
}
impl MultiPatterns {
/// Construct a MultiPatterns with rules. Each multi-pattern rule contains two matched outputs.
///
/// # Parameters
///
/// - `rules`: every adjacent pair of entries should belong to the same multi-pattern rule.
/// - `no_cycle`: whether or not to do cycle filtering
/// - `iter_limit`: Number of iterations to apply multi-pattern rules
/// - `filter_after`: if true, do efficient filtering (filter cycle after the iteration);
/// else, do naive filtering (check cycle before each application)
/// - `node_limit`: Maximum number of nodes to added here
/// - `n_sec`: Maximum number of seconds to run
pub fn with_rules(
rules: Vec<(&str, bool)>,
no_cycle: bool,
iter_limit: usize,
filter_after: bool,
node_limit: usize,
n_sec: u64,
) -> MultiPatterns {
assert!(rules.len() % 2 == 0);
let mut multi_rules =
Vec::<(Pattern<Mdl>, Pattern<Mdl>, Pattern<Mdl>, Pattern<Mdl>, bool)>::new();
let mut canonical_pats = Vec::<Pattern<Mdl>>::new();
let mut src_pat_maps = Vec::<(MapToCanonical, MapToCanonical)>::new();
let mut canonicalize_and_add = |pat: &Pattern<Mdl>| {
let (pat_canonical, pat_var_map) = canonicalize(pat);
let index_found = canonical_pats.iter().position(|x| *x == pat_canonical);
let pat_index = index_found
.or_else(|| {
canonical_pats.push(pat_canonical);
Some(canonical_pats.len() - 1)
})
.unwrap();
MapToCanonical {
index: pat_index,
var_map: pat_var_map,
}
};
let get_pats = |rule: &str| {
rule.split("=>")
.map(|x| x.parse().unwrap())
.next_tuple()
.unwrap()
};
for i in 0..(rules.len() / 2) {
let (src_1, dst_1) = get_pats(rules[2 * i].0);
let (src_2, dst_2) = get_pats(rules[2 * i + 1].0);
let src_1_map = canonicalize_and_add(&src_1);
let src_2_map = canonicalize_and_add(&src_2);
assert!(rules[2 * i].1 == rules[2 * i + 1].1);
let symmetric = rules[2 * i].1;
multi_rules.push((src_1, src_2, dst_1, dst_2, symmetric));
src_pat_maps.push((src_1_map, src_2_map));
}
println!("Number of canonicalized {:?}", canonical_pats.len());
MultiPatterns {
rules: multi_rules,
canonical_src_pat: canonical_pats,
src_pat_maps: src_pat_maps,
no_cycle: no_cycle,
iter_limit: iter_limit,
filter_after: filter_after && no_cycle,
descendents: None,
node_limit: node_limit,
num_applied: 0,
n_sec: n_sec,
}
}
/// Search and apply all multi-pattern rules for one iteration
///
/// This function is used as hook function to egg::Runner. It first searches for matches
/// of all canonicalized source patterns. Then for all compatible substitutions found,
/// it checks and applies the dst patterns. It won't apply if src_1 and src_2 matches with
/// the same eclass. It always returns Ok()
pub fn run_one(&mut self, runner: &mut Runner<Mdl, TensorAnalysis, ()>) -> Result<(), String> {
if self.filter_after {
// This is to remove cycles introduced during the last iteration of single rules
remove_cycle_by_order(runner);
}
if runner.iterations.len() < self.iter_limit && self.node_limit > 0 && self.n_sec > 0 {
println!("Run one");
let starting_num_nodes = runner.egraph.analysis.newly_added.len();
let start_time = Instant::now();
let mut num_applied = 0;
// Construct Vec to store matches for each canonicalized pattern
let matches: Vec<Vec<SearchMatches>> = self
.canonical_src_pat
.iter()
.map(|x| x.search(&runner.egraph))
.collect();
if self.filter_after {
// Make a pass to get descendents
self.descendents = Some(compute_all_descendents(
&runner.egraph,
runner.roots[0],
/*check_blacklist=*/ true,
));
}
// For each multi rule
'outer: for (i, rule) in self.rules.iter().enumerate() {
let map_1 = &self.src_pat_maps[i].0;
let map_2 = &self.src_pat_maps[i].1;
// If the rule is fully symmetrical
if map_1.index == map_2.index && rule.4 {
let matches_both = &matches[map_1.index];
for (i, match_1) in matches_both.iter().enumerate() {
for match_2 in (&matches_both[(i + 1)..]).iter() {
if match_1.eclass == match_2.eclass {
// We don't want to apply multi-pattern rules on the same eclass
continue;
}
let n_applied = self.apply_match_pair(rule, match_1, match_2, map_1, map_2, runner);