forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 16
/
mkl_layout_pass.cc
3986 lines (3604 loc) · 163 KB
/
mkl_layout_pass.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// TODO(intel): Improve error handling in this file; instead of CHECK failing
// all over the place, we should log an error and execute the original graph.
#ifdef INTEL_MKL
#include <algorithm>
#include <functional>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/common_runtime/optimization_registry.h"
#include "tensorflow/core/framework/node_def_util.h"
#include "tensorflow/core/framework/tensor.pb.h"
#include "tensorflow/core/graph/algorithm.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/lib/hash/hash.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/util/tensor_format.h"
#include "tensorflow/core/util/util.h"
#include "tensorflow/core/graph/mkl_graph_util.h"
#include "tensorflow/core/graph/mkl_layout_pass.h"
namespace tensorflow {
// This pass implements rewriting of graph to support following scenarios:
// (A) Merging nodes in the graph
// (B) Rewriting a node in the graph to a new node
// Rewrite happens under following scenario:
// - Propagating Mkl layout as an additional output tensor
// (we will loosely call a tensor that carries Mkl layout as Mkl tensor
// henceforth.) from every Mkl supported NN layer.
//
// Example of A : Merging nodes in the graph
// -----------------------------------------
// Currently, we merge Conv2D+AddBias together. Consider Conv2D and BiasAdd as:
//
// O = Conv2D(A, B)
// P = BiasAdd(O, C)
//
// We merge them into Conv2DWithBias as:
// P = _MklConv2DWithBias(A, A_m, B, B_m, C, C_m)
//
// The meaning of A_m, B_m and C_m is explained in B.1.
//
// Merge rules:
// - The merge for Conv2D and BiasAdd happens when the output of Conv2D _only_
// goes to BiasAdd.
// - Also, the intersection of attributes of both the nodes must have same
// values.
// - Both the nodes must have been assigned to same device (if any).
//
// Example of B.1 : Rewriting nodes to Mkl nodes
// ---------------------------------------------
// Consider a Relu node. Current definition of Relu node looks like:
//
// O = Relu(A)
//
// Relu has 1 input (A), and 1 output (O).
//
// This rewrite pass will generate a new graph node for Relu (new node is
// called MklRelu) as:
//
// O, O_m = MklRelu(A, A_m)
//
// MklRelu has 2 inputs (A and A_m) and 2 outputs (O and O_m). Here input A is
// same as input A of Relu; output O is same as output O of Relu. O_m is the
// additional output tensor that will be set by MklRelu, and it represents
// Mkl tensor corresponding to O -- in other words, O_m is some kind of
// metadata for O. A_m is additional input of Relu, and it represents metadata
// for A - as O_m is metadata for O, A_m is metadata for A. MklRelu receives
// this metadata from previous node in the graph.
//
// When a previous node in the graph is an Mkl node, A_m will represent a valid
// Mkl tensor. But when a previous node is not an Mkl node, A_m will represent
// a dummy Mkl tensor.
//
// Rewriting rules:
// - Selection of a node for rewriting happens by registering the op type of
// the node with the rewriting pass. If the op type is not registered, then
// all nodes of this op type will not be rewritten.
// - Number of inputs after rewriting:
// Since for every input Tensorflow tensor, the rewritten node gets Mkl
// tensor(s), rewritten node gets 2*N inputs, where N is the number of
// inputs for the original node.
// - Number of outputs after rewriting:
// Since for every output Tensorflow tensor, the rewritten node generates
// Mkl tensor(s), the rewritten node generates 2*N outputs, where N is the
// number of outputs of the original node.
// - Ordering of Tensorflow tensors and Mkl tensors:
// Since every rewritten node generates twice the number of inputs and
// outputs, one could imagine various orderings among Tensorflow tensors
// and Mkl tensors. E.g., assume an op 'Conv2D' that takes (A, B) as
// inputs, then the new op '_MklConv2D' can take inputs A, B, A_m and B_m
// in A, A_m, B, B_m order or it can also take them in A, B, A_m, B_m
// order. Among N inputs one can get N! permutations.
//
// So the question is: which order do we follow? We support 2 types of
// orderings: (1) interleaved, and (2) contiguous. Interleaved ordering
// follows an intuitive order where an Mkl tensor follows the
// corresponding Tensorflow tensor immediately. In the context of the
// above example, it will be: A, A_m, B, B_m. Note that the ordering rule
// applies to both the inputs and outputs. Contiguous ordering means
// all the Tensorflow tensors are contiguous followed by all the Mkl
// tensors. We use contiguous ordering as default.
//
// Graph rewrite algorithm:
// Algorithm: Graph Rewrite
// Input: Graph G, Names of the nodes to rewrite and their new names
// Output: Modified Graph G' if the nodes are modified, G otherwise.
// Start:
// N = Topological_Sort(G) // N is a set of nodes in toposort order.
// foreach node n in N
// do
// if (Is_MKL_Op(n)) // Can this node accept an Mkl layout as input.
// then
// E = set of <incoming edge and its src_output slot> of n
// E' = {} // a new set of edges for rewritten node
// foreach <e,s> in E
// do
// E' U {<e,s>} // First copy edge which generates Tensorflow
// // tensor as it is
// m = Source node of edge e
// if Is_Rewritten(m) // Did we rewrite this node in this pass?
// then
// E' U {<m,s+1>} // If yes, then m will generate an Mkl
// // tensor as an additional output.
// else
// d = Generate_Dummy_Mkl_Tensor() // If not, generate a dummy
// // Mkl tensor.
// E' U {<d,0>} // The dummy Mkl tensor has only 1 output slot.
// fi
// done
// n' = Build_New_Node(G,new_name,E')
// Mark_Rewritten(n') // Mark the new node as being rewritten.
// fi
// done
//
// Explanation:
// For graph rewrite, we visit nodes of the input graph in the
// topological sort order. With this ordering, we visit nodes in the
// top-to-bottom fashion. We need this order because while visiting a
// node we want that all of its input nodes are visited and rewritten if
// applicable. This is because if we need to rewrite a given node
// then all of its input nodes need to be fixed (in other words they
// cannot be deleted later.)
//
// While visiting a node, we first check if the op type of the node is
// an Mkl op. If it is, then we rewrite that node after constructing
// new inputs to the node. If the op type of the node is not Mkl op,
// then we do not rewrite that node.
//
// Handling workspace propagation for certain ops:
//
// Certain backward ops in MKL (MaxPool, LRN and BatchNorm) require
// passing of a workspace from their respective forward ops. Workspace
// tensors provide memory for storing results of intermediate operations
// which are helpful in backward propagation. TensorFlow does not have
// a notion of a workspace and as a result does not allow producing
// additional outputs from these forward ops. For these ops, we need
// to add 2 extra edges between forward ops and their corresponding
// backward ops - the first extra edge carries a workspace tensor and
// the second one carries an Mkl tensor for the workspace tensor.
//
// Example:
//
// Typical graph for MaxPool and its gradient looks like:
//
// A = MaxPool(T)
// B = MaxPoolGrad(X, A, Y)
//
// We will transform this graph to propagate the workspace as:
// (with the contiguous ordering)
//
// A, W, A_m, W_m = MklMaxPool(T, T_m)
// B, B_m = MklMaxPoolGrad(X, A, Y, W, X_m, A_m, Y_m, W_m)
//
// Here W is the workspace tensor. Transformed tensor names with the
// suffix _m are Mkl tensors, and this transformation has been done
// using the algorithm discussed earlier. The transformation for
// workspace propagation only adds extra outputs (W, W_m) for a forward
// op and connects them to the corresponding backward ops.
//
// Terms:
//
// Forward op name = name of the op in the forward pass
// where a workspace tensor originates (MaxPool in this example)
// Backward op name = name of the op in the backward pass that receives
// a workspace tensor from the forward op (MaxPoolGrad in the example)
// Slot = Position of the output or input slot that will be
// used by the workspace tensor (1 for MklMaxPool as W is the 2nd
// output of MaxPool (0 is 1st); 3 for MklMaxPoolGrad)
//
// Question:
//
// How do we associate a backward op to a forward op? There can be more
// than one op with the exact same name.
//
// In this example, we associate MaxPoolGrad with MaxPool. But there
// could be more than one MaxPool ops. To solve this problem, we look
// for _direct_ edge between a forward op and a backward op (tensor A is
// flowing along this edge in the example).
//
// How do we transform forward and backward ops when there is no direct
// edge between them? In such a case, we generate dummy tensors for
// workspace tensors. For the example, transformation of MaxPool will
// be exactly same as it would be when there is a direct edge between
// the forward and the backward op --- it is just that MaxPool won't
// generate any workspace tensor. For MaxPoolGrad, the transformation
// will also be same, but instead of connecting W and W_m with the
// outputs of MaxPool, we will produce dummy tensors for them, and we
// will set workspace_enabled attribute to false.
//
class MklLayoutRewritePass : public GraphOptimizationPass {
public:
MklLayoutRewritePass() {
// NOTE: names are alphabetically sorted.
csinfo_.addn = "AddN";
csinfo_.avg_pool = "AvgPool";
csinfo_.avg_pool_grad = "AvgPoolGrad";
csinfo_.avg_pool3d = "AvgPool3D";
csinfo_.avg_pool3d_grad = "AvgPool3DGrad";
csinfo_.batch_matmul = "BatchMatMul";
csinfo_.batch_matmul_v2 = "BatchMatMulV2";
csinfo_.bias_add = "BiasAdd";
csinfo_.bias_add_grad = "BiasAddGrad";
csinfo_.concat = "Concat";
csinfo_.concatv2 = "ConcatV2";
csinfo_.conjugate_transpose = "ConjugateTranspose";
csinfo_.conv2d = "Conv2D";
csinfo_.conv2d_with_bias = "__MklDummyConv2DWithBias";
csinfo_.conv2d_grad_input = "Conv2DBackpropInput";
csinfo_.conv2d_grad_filter = "Conv2DBackpropFilter";
csinfo_.conv2d_grad_filter_with_bias =
"__MklDummyConv2DBackpropFilterWithBias";
csinfo_.conv3d = "Conv3D";
csinfo_.conv3d_grad_input = "Conv3DBackpropInputV2";
csinfo_.conv3d_grad_filter = "Conv3DBackpropFilterV2";
csinfo_.depthwise_conv2d = "DepthwiseConv2dNative";
csinfo_.depthwise_conv2d_grad_input = "DepthwiseConv2dNativeBackpropInput";
csinfo_.depthwise_conv2d_grad_filter =
"DepthwiseConv2dNativeBackpropFilter";
csinfo_.dequantize = "Dequantize";
csinfo_.fused_batch_norm = "FusedBatchNorm";
csinfo_.fused_batch_norm_grad = "FusedBatchNormGrad";
csinfo_.fused_batch_norm_v2 = "FusedBatchNormV2";
csinfo_.fused_batch_norm_grad_v2 = "FusedBatchNormGradV2";
csinfo_.fused_batch_norm_v3 = "FusedBatchNormV3";
csinfo_.fused_batch_norm_grad_v3 = "FusedBatchNormGradV3";
csinfo_.fused_conv2d = "_FusedConv2D";
csinfo_.fused_matmul = "_FusedMatMul";
csinfo_.identity = "Identity";
csinfo_.leakyrelu = "LeakyRelu";
csinfo_.leakyrelu_grad = "LeakyReluGrad";
csinfo_.lrn = "LRN";
csinfo_.lrn_grad = "LRNGrad";
csinfo_.matmul = "MatMul";
csinfo_.max_pool = "MaxPool";
csinfo_.max_pool_grad = "MaxPoolGrad";
csinfo_.max_pool3d = "MaxPool3D";
csinfo_.max_pool3d_grad = "MaxPool3DGrad";
csinfo_.mkl_conv2d = "_MklConv2D";
csinfo_.mkl_conv2d_grad_input = "_MklConv2DBackpropInput";
csinfo_.mkl_conv2d_grad_filter = "_MklConv2DBackpropFilter";
csinfo_.mkl_conv2d_with_bias = "_MklConv2DWithBias";
csinfo_.mkl_conv2d_grad_filter_with_bias =
"_MklConv2DBackpropFilterWithBias";
csinfo_.mkl_depthwise_conv2d_grad_input =
"_MklDepthwiseConv2dNativeBackpropInput";
csinfo_.mkl_depthwise_conv2d_grad_filter =
"_MklDepthwiseConv2dNativeBackpropFilter";
csinfo_.mkl_fused_conv2d = "_MklFusedConv2D";
csinfo_.mkl_fused_matmul = "_MklFusedMatMul";
csinfo_.mkl_pad_with_conv2d = "_MklPadWithConv2D";
csinfo_.mkl_pad_with_fused_conv2d = "_MklPadWithFusedConv2D";
csinfo_.pad = "Pad";
csinfo_.pad_with_conv2d = "__MklDummyPadWithConv2D";
csinfo_.pad_with_fused_conv2d = "__MklDummyPadWithFusedConv2D";
csinfo_.quantized_avg_pool = "QuantizedAvgPool";
csinfo_.quantized_concatv2 = "QuantizedConcatV2";
csinfo_.quantized_conv2d = "QuantizedConv2D";
csinfo_.quantized_conv2d_per_channel = "QuantizedConv2DPerChannel";
csinfo_.quantized_conv2d_with_requantize = "QuantizedConv2DAndRequantize";
csinfo_.quantized_conv2d_with_bias = "QuantizedConv2DWithBias";
csinfo_.quantized_conv2d_with_bias_and_requantize =
"QuantizedConv2DWithBiasAndRequantize";
csinfo_.quantized_conv2d_and_relu = "QuantizedConv2DAndRelu";
csinfo_.quantized_conv2d_and_relu_and_requantize =
"QuantizedConv2DAndReluAndRequantize";
csinfo_.quantized_conv2d_with_bias_and_relu =
"QuantizedConv2DWithBiasAndRelu";
csinfo_.quantized_conv2d_with_bias_and_relu_and_requantize =
"QuantizedConv2DWithBiasAndReluAndRequantize";
csinfo_.quantized_max_pool = "QuantizedMaxPool";
csinfo_.quantized_conv2d_with_bias_sum_and_relu =
"QuantizedConv2DWithBiasSumAndRelu";
csinfo_.quantized_conv2d_with_bias_sum_and_relu_and_requantize =
"QuantizedConv2DWithBiasSumAndReluAndRequantize";
csinfo_.quant_conv2d_with_bias_signed_sum_and_relu_and_requantize =
"QuantizedConv2DWithBiasSignedSumAndReluAndRequantize";
csinfo_.quantized_matmul_with_bias = "QuantizedMatMulWithBias";
csinfo_.quantized_matmul_with_bias_and_relu =
"QuantizedMatMulWithBiasAndRelu";
csinfo_.quantized_matmul_with_bias_and_relu_and_requantize =
"QuantizedMatMulWithBiasAndReluAndRequantize";
csinfo_.quantized_matmul_with_bias_and_requantize =
"QuantizedMatMulWithBiasAndRequantize";
csinfo_.quantized_depthwise_conv2d = "QuantizedDepthwiseConv2D";
csinfo_.quantized_depthwise_conv2d_with_bias =
"QuantizedDepthwiseConv2DWithBias";
csinfo_.quantized_depthwise_conv2d_with_bias_and_relu =
"QuantizedDepthwiseConv2DWithBiasAndRelu";
csinfo_.quantized_depthwise_conv2d_with_bias_and_relu_and_requantize =
"QuantizedDepthwiseConv2DWithBiasAndReluAndRequantize";
csinfo_.quantize_v2 = "QuantizeV2";
csinfo_.relu = "Relu";
csinfo_.relu_grad = "ReluGrad";
csinfo_.relu6 = "Relu6";
csinfo_.relu6_grad = "Relu6Grad";
csinfo_.requantize = "Requantize";
csinfo_.tanh = "Tanh";
csinfo_.tanh_grad = "TanhGrad";
csinfo_.reshape = "Reshape";
csinfo_.slice = "Slice";
csinfo_.softmax = "Softmax";
csinfo_.split = "Split";
csinfo_.transpose = "Transpose";
// Element-wise ops. Ensure you also add any new ops to IsOpElementWise
// in the MklUtil.h (IsMklElementWiseOp method) to ensure that the
// MklInputConversion op is added before it.
csinfo_.add = "Add";
csinfo_.add_v2 = "AddV2";
csinfo_.maximum = "Maximum";
csinfo_.mul = "Mul";
csinfo_.squared_difference = "SquaredDifference";
csinfo_.sub = "Sub";
// End - element-wise ops. See note above.
// NOTE: names are alphabetically sorted.
#ifndef ENABLE_MKLDNN_V1
rinfo_.push_back({csinfo_.addn, mkl_op_registry::GetMklOpName(csinfo_.addn),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.add, mkl_op_registry::GetMklOpName(csinfo_.add),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.add_v2,
mkl_op_registry::GetMklOpName(csinfo_.add_v2),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.avg_pool, mkl_op_registry::GetMklOpName(csinfo_.avg_pool),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.avg_pool_grad,
mkl_op_registry::GetMklOpName(csinfo_.avg_pool_grad),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.avg_pool3d, mkl_op_registry::GetMklOpName(csinfo_.avg_pool3d),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.avg_pool3d_grad,
mkl_op_registry::GetMklOpName(csinfo_.avg_pool3d_grad),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.batch_matmul,
mkl_op_registry::GetMklOpName(csinfo_.batch_matmul),
CopyAttrsAll, AlwaysRewrite, kRewriteForOpNameChange});
rinfo_.push_back({csinfo_.batch_matmul_v2,
mkl_op_registry::GetMklOpName(csinfo_.batch_matmul_v2),
CopyAttrsAll, AlwaysRewrite, kRewriteForOpNameChange});
rinfo_.push_back(
{csinfo_.concat, mkl_op_registry::GetMklOpName(csinfo_.concat),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.concatv2, mkl_op_registry::GetMklOpName(csinfo_.concatv2),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.conjugate_transpose,
mkl_op_registry::GetMklOpName(csinfo_.conjugate_transpose),
CopyAttrsAll, AlwaysRewrite, kRewriteForOpNameChange});
#endif // !ENABLE_MKLDNN_V1
rinfo_.push_back({csinfo_.conv2d,
mkl_op_registry::GetMklOpName(csinfo_.conv2d),
CopyAttrsConvCheckConstFilter, AlwaysRewrite,
kRewriteForLayoutPropagation});
#ifndef ENABLE_MKLDNN_V1
rinfo_.push_back({csinfo_.conv2d_with_bias, csinfo_.mkl_conv2d_with_bias,
CopyAttrsConvCheckConstFilter, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv2d_grad_filter,
mkl_op_registry::GetMklOpName(csinfo_.conv2d_grad_filter),
CopyAttrsConv, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv2d_grad_filter_with_bias,
csinfo_.mkl_conv2d_grad_filter_with_bias, CopyAttrsConv,
AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv2d_grad_input,
mkl_op_registry::GetMklOpName(csinfo_.conv2d_grad_input),
CopyAttrsConv, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv3d,
mkl_op_registry::GetMklOpName(csinfo_.conv3d),
CopyAttrsConvCheckConstFilter, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv3d_grad_filter,
mkl_op_registry::GetMklOpName(csinfo_.conv3d_grad_filter),
CopyAttrsConv, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.conv3d_grad_input,
mkl_op_registry::GetMklOpName(csinfo_.conv3d_grad_input),
CopyAttrsConv, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.depthwise_conv2d,
mkl_op_registry::GetMklOpName(csinfo_.depthwise_conv2d),
CopyAttrsConv2DDepthwiseCheckConstFilter, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.depthwise_conv2d_grad_input,
mkl_op_registry::GetMklOpName(csinfo_.depthwise_conv2d_grad_input),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.depthwise_conv2d_grad_filter,
mkl_op_registry::GetMklOpName(csinfo_.depthwise_conv2d_grad_filter),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.dequantize, mkl_op_registry::GetMklOpName(csinfo_.dequantize),
CopyAttrsAll, DequantizeRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.fused_batch_norm,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.fused_batch_norm_grad,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm_grad),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.fused_batch_norm_v2,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm_v2),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.fused_batch_norm_grad_v2,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm_grad_v2),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
// Using CopyAttrsAll for V3 on CPU, as there are no additional
// attributes.
rinfo_.push_back(
{csinfo_.fused_batch_norm_v3,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm_v3),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.fused_batch_norm_grad_v3,
mkl_op_registry::GetMklOpName(csinfo_.fused_batch_norm_grad_v3),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
#endif // !ENABLE_MKLDNN_V1
rinfo_.push_back({csinfo_.fused_conv2d, csinfo_.mkl_fused_conv2d,
CopyAttrsFusedConv2D, FusedConv2DRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.fused_matmul, csinfo_.mkl_fused_matmul,
CopyAttrsAll, FusedMatMulRewrite});
#ifndef ENABLE_MKLDNN_V1
rinfo_.push_back({csinfo_.identity,
mkl_op_registry::GetMklOpName(csinfo_.identity),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.lrn, mkl_op_registry::GetMklOpName(csinfo_.lrn),
CopyAttrsAll, LrnRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.lrn_grad, mkl_op_registry::GetMklOpName(csinfo_.lrn_grad),
CopyAttrsAll, LrnGradRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.matmul,
mkl_op_registry::GetMklOpName(csinfo_.matmul),
CopyAttrsAll, AlwaysRewrite, kRewriteForOpNameChange});
rinfo_.push_back(
{csinfo_.leakyrelu, mkl_op_registry::GetMklOpName(csinfo_.leakyrelu),
CopyAttrsAll, LeakyReluRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.leakyrelu_grad,
mkl_op_registry::GetMklOpName(csinfo_.leakyrelu_grad),
CopyAttrsAll, LeakyReluRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.max_pool,
mkl_op_registry::GetMklOpName(csinfo_.max_pool),
CopyAttrsAll, NonDepthBatchWisePoolRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.max_pool_grad,
mkl_op_registry::GetMklOpName(csinfo_.max_pool_grad),
CopyAttrsAll, MaxpoolGradRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.max_pool3d,
mkl_op_registry::GetMklOpName(csinfo_.max_pool3d),
CopyAttrsAll, NonDepthBatchWisePoolRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.max_pool3d_grad,
mkl_op_registry::GetMklOpName(csinfo_.max_pool3d_grad),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.maximum,
mkl_op_registry::GetMklOpName(csinfo_.maximum),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.mul, mkl_op_registry::GetMklOpName(csinfo_.mul),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.pad_with_conv2d, csinfo_.mkl_pad_with_conv2d,
CopyAttrsPadWithConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.pad_with_fused_conv2d,
csinfo_.mkl_pad_with_fused_conv2d,
CopyAttrsPadWithFusedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_avg_pool,
mkl_op_registry::GetMklOpName(csinfo_.quantized_avg_pool),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_concatv2,
mkl_op_registry::GetMklOpName(csinfo_.quantized_concatv2),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d,
mkl_op_registry::GetMklOpName(csinfo_.quantized_conv2d),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_conv2d_per_channel,
mkl_op_registry::GetMklOpName(csinfo_.quantized_conv2d_per_channel),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d_with_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_conv2d_with_bias,
mkl_op_registry::GetMklOpName(csinfo_.quantized_conv2d_with_bias),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d_with_bias_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_bias_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_conv2d_and_relu,
mkl_op_registry::GetMklOpName(csinfo_.quantized_conv2d_and_relu),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_and_relu_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d_with_bias_and_relu,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_bias_and_relu),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_conv2d_with_bias_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_bias_and_relu_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_max_pool,
mkl_op_registry::GetMklOpName(csinfo_.quantized_max_pool),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_conv2d_with_bias_sum_and_relu,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_bias_sum_and_relu),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_conv2d_with_bias_sum_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_conv2d_with_bias_sum_and_relu_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quant_conv2d_with_bias_signed_sum_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quant_conv2d_with_bias_signed_sum_and_relu_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_matmul_with_bias,
mkl_op_registry::GetMklOpName(csinfo_.quantized_matmul_with_bias),
CopyAttrsQuantizedMatMulWithBias, AlwaysRewrite});
rinfo_.push_back({csinfo_.quantized_matmul_with_bias_and_relu,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_matmul_with_bias_and_relu),
CopyAttrsQuantizedMatMulWithBias, AlwaysRewrite});
rinfo_.push_back(
{csinfo_.quantized_matmul_with_bias_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_matmul_with_bias_and_relu_and_requantize),
CopyAttrsQuantizedMatMulWithBias, AlwaysRewrite});
rinfo_.push_back({csinfo_.quantized_matmul_with_bias_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_matmul_with_bias_and_requantize),
CopyAttrsQuantizedMatMulWithBias, AlwaysRewrite});
rinfo_.push_back(
{csinfo_.quantized_depthwise_conv2d,
mkl_op_registry::GetMklOpName(csinfo_.quantized_depthwise_conv2d),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantized_depthwise_conv2d_with_bias,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_depthwise_conv2d_with_bias),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_depthwise_conv2d_with_bias_and_relu,
mkl_op_registry::GetMklOpName(
csinfo_.quantized_depthwise_conv2d_with_bias_and_relu),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.quantized_depthwise_conv2d_with_bias_and_relu_and_requantize,
mkl_op_registry::GetMklOpName(
csinfo_
.quantized_depthwise_conv2d_with_bias_and_relu_and_requantize),
CopyAttrsQuantizedConv2D, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.quantize_v2,
mkl_op_registry::GetMklOpName(csinfo_.quantize_v2),
CopyAttrsAll, QuantizeOpRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.relu, mkl_op_registry::GetMklOpName(csinfo_.relu),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.relu_grad, mkl_op_registry::GetMklOpName(csinfo_.relu_grad),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.relu6, mkl_op_registry::GetMklOpName(csinfo_.relu6),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.relu6_grad, mkl_op_registry::GetMklOpName(csinfo_.relu6_grad),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.requantize, mkl_op_registry::GetMklOpName(csinfo_.requantize),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
#endif // !ENABLE_MKLDNN_V1
// Disable these two MKL operators for now due to some test failures caused
// by these two ops
/*
rinfo_.push_back({csinfo_.tanh,
mkl_op_registry::GetMklOpName(csinfo_.tanh),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.tanh_grad,
mkl_op_registry::GetMklOpName(csinfo_.tanh_grad),
CopyAttrsAll, AlwaysRewrite,
kRewriteForLayoutPropagation});
*/
#ifndef ENABLE_MKLDNN_V1
rinfo_.push_back(
{csinfo_.reshape, mkl_op_registry::GetMklOpName(csinfo_.reshape),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.slice,
mkl_op_registry::GetMklOpName(csinfo_.slice),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back(
{csinfo_.softmax, mkl_op_registry::GetMklOpName(csinfo_.softmax),
CopyAttrsAll, AlwaysRewrite, kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.squared_difference,
mkl_op_registry::GetMklOpName(csinfo_.squared_difference),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.sub, mkl_op_registry::GetMklOpName(csinfo_.sub),
CopyAttrsAll, RewriteIfAtleastOneMklInput,
kRewriteForLayoutPropagation});
rinfo_.push_back({csinfo_.transpose,
mkl_op_registry::GetMklOpName(csinfo_.transpose),
CopyAttrsAll, AlwaysRewrite, kRewriteForOpNameChange});
// Add info about which ops to add workspace edge to and the slots.
wsinfo_.push_back({csinfo_.lrn, csinfo_.lrn_grad, 0, 2, 1, 3});
wsinfo_.push_back({csinfo_.max_pool, csinfo_.max_pool_grad, 0, 1, 1, 3});
wsinfo_.push_back(
{csinfo_.max_pool3d, csinfo_.max_pool3d_grad, 0, 1, 1, 3});
// Add a rule for merging nodes
minfo_.push_back({csinfo_.conv2d, csinfo_.bias_add,
csinfo_.conv2d_with_bias, GetConv2DOrBiasAdd});
minfo_.push_back({csinfo_.conv2d_grad_filter, csinfo_.bias_add_grad,
csinfo_.conv2d_grad_filter_with_bias,
GetConv2DBackpropFilterOrBiasAddGrad});
// Merge Pad and Conv2d, only if the pad op is "Pad"
// Doesn't merge if pad op is "PadV2" or "MirrorPad"
minfo_.push_back(
{csinfo_.pad, csinfo_.conv2d, csinfo_.pad_with_conv2d, GetPadOrConv2D});
minfo_.push_back({csinfo_.pad, csinfo_.fused_conv2d,
csinfo_.pad_with_fused_conv2d, GetPadOrFusedConv2D});
// The fusion patterns in "finfo_" that show up first will get applied
// first, for example, graph "A->B->C-D" and finfo_ is {A->B->C to ABC,
// A->B->C->D to ABCD}, since the first gets applied first, the final
// graph will be ABC->D.
//
// Add rules to fuse sequences such as "Transpose (NCHW -> NHWC) + Conv2D
// (NHWC) + Transpose (NHWC->
// NCHW)" into "Conv2D (NCHW)". Such patterns occur frequently in Keras.
// Note: we use the term "merge" to combine (exactly) 2 nodes into one,
// while "fusion" is for 3+ nodes situation.
//
// Transpose + Conv2d + Transpose:
std::vector<int> transpose_to_nhwc = {NCHW::dim::N, NCHW::dim::H,
NCHW::dim::W, NCHW::dim::C};
std::vector<int> transpose_to_nchw = {NHWC::dim::N, NHWC::dim::C,
NHWC::dim::H, NHWC::dim::W};
auto CheckForTransposeToNHWC =
std::bind(CheckForTranspose, std::placeholders::_1, transpose_to_nhwc);
auto CheckForConv2dOp =
std::bind(CheckForMklOp, std::placeholders::_1, csinfo_.conv2d);
auto CheckForTransposeToNCHW =
std::bind(CheckForTranspose, std::placeholders::_1, transpose_to_nchw);
auto FuseConv2D =
std::bind(FuseTransposeMklOpTranspose, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, "NCHW");
finfo_.push_back(
{"transpose-elimination for Conv2D",
{CheckForTransposeToNHWC, CheckForConv2dOp, CheckForTransposeToNCHW},
// CheckForMklOp
FuseConv2D,
CopyAttrsConv});
// Transpose + Conv3d + Transpose:
std::vector<int> transpose_to_ndhwc = {NCDHW::dim::N, NCDHW::dim::D,
NCDHW::dim::H, NCDHW::dim::W,
NCDHW::dim::C};
std::vector<int> transpose_to_ncdhw = {NDHWC::dim::N, NDHWC::dim::C,
NDHWC::dim::D, NDHWC::dim::H,
NDHWC::dim::W};
auto CheckForTransposeToNDHWC =
std::bind(CheckForTranspose, std::placeholders::_1, transpose_to_ndhwc);
auto CheckForConv3dOp =
std::bind(CheckForMklOp, std::placeholders::_1, csinfo_.conv3d);
auto CheckForTransposeToNCDHW =
std::bind(CheckForTranspose, std::placeholders::_1, transpose_to_ncdhw);
auto FuseConv3D =
std::bind(FuseTransposeMklOpTranspose, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, "NCDHW");
finfo_.push_back(
{"transpose-elimination for Conv3D",
{CheckForTransposeToNDHWC, CheckForConv3dOp, CheckForTransposeToNCDHW},
// CheckForMklOp
FuseConv3D,
CopyAttrsConv});
#endif // !ENABLE_MKLDNN_V1
}
// Standard interface to run pass
Status Run(const GraphOptimizationPassOptions& options);
// Helper function which does most of heavy lifting for rewriting
// Mkl nodes to propagate Mkl tensor as additional output
//
// Extracts common functionality between Run public interface and
// test interface.
//
// @return true, if and only if graph is mutated; false otherwise.
bool RunPass(std::unique_ptr<Graph>* g);
/// Cause for rewrite
/// Currently, we only support 2 causes - either for Mkl layout propagation
/// which is the most common case, or for just a name change (used in case
/// of ops like MatMul, Transpose, which do not support Mkl layout)
enum RewriteCause { kRewriteForLayoutPropagation, kRewriteForOpNameChange };
/// Structure to specify the name of an original node, its new name after
/// rewrite, the number of inputs to the original node, the function to
/// be used to copy attributes for the op, and the rule (if any) which
/// must hold for rewriting the node
typedef struct {
string name; // Original name of op of the node in the graph
string new_name; // New name of the op of the node in the graph
// A function handler to copy attributes from an old node to a new node.
std::function<void(const Node*, NodeBuilder*, bool)> copy_attrs;
// A rule under which to rewrite this node
std::function<bool(const Node*)> rewrite_rule;
// Why are we rewriting?
RewriteCause rewrite_cause;
} RewriteInfo;
/// Structure to specify a forward op, a backward op, and the slot numbers
/// in the forward and backward ops where we will add a workspace edge.
typedef struct {
string fwd_op; // Name of a forward op in the graph
string bwd_op; // Name of a backward op in the graph
int fwd_slot; // Output slot in the forward op node where actual
// output tensor resides
int bwd_slot; // Input slot in the backward op node where actual
// input tensor resides
int ws_fwd_slot; // Output slot in the forward op node where workspace
// edge is added
int ws_bwd_slot; // Input slot in the backward op node where workspace
// edge is added
} WorkSpaceInfo;
/// Structure to specify information used in node merge of 2 operators
typedef struct {
string op1; // Node string for one operator.
string op2; // Node string for second operator.
string new_node; // Name of the node after merge
// Function that enables user of the node merger to specify how to find
// second operator given the first operator.
std::function<Node*(const Node*)> get_node_to_be_merged;
} MergeInfo;
// Structure to specify information used in node fusion of 3+ operators
typedef struct {
std::string pattern_name; // Name to describe this pattern, such as
// "Transpose_Mklop_Transpose".
std::vector<std::function<bool(const Node*)> >
node_checkers; // Extra restriction checker for these ops
std::function<Status(
std::unique_ptr<Graph>*, std::vector<Node*>&,
std::function<void(const Node*, NodeBuilder* nb, bool)>)>
fuse_func;
std::function<void(const Node*, NodeBuilder* nb, bool)> copy_attrs;
} FusionInfo;
//
// Dimension indices for 2D tensor.
//
struct NCHW {
enum dim { N = 0, C = 1, H = 2, W = 3 };
};
struct NHWC {
enum dim { N = 0, H = 1, W = 2, C = 3 };
};
//
// dimension indices for 3D tensor.
//
struct NCDHW {
enum dim { N = 0, C = 1, D = 2, H = 3, W = 4 };
};
struct NDHWC {
enum dim { N = 0, D = 1, H = 2, W = 3, C = 4 };
};
/// Structure to store all constant strings
/// NOTE: names are alphabetically sorted.
typedef struct {
string addn;
string add;
string add_v2;
string avg_pool;
string avg_pool_grad;
string avg_pool3d;
string avg_pool3d_grad;
string batch_matmul;
string batch_matmul_v2;
string bias_add;
string bias_add_grad;
string concat;
string concatv2;
string conjugate_transpose;
string conv2d;
string conv2d_with_bias;
string conv2d_grad_input;
string conv2d_grad_filter;
string conv2d_grad_filter_with_bias;
string conv3d;
string conv3d_grad_input;
string conv3d_grad_filter;
string depthwise_conv2d;
string depthwise_conv2d_grad_input;
string depthwise_conv2d_grad_filter;
string dequantize;
string fused_batch_norm;
string fused_batch_norm_grad;
string fused_batch_norm_v2;
string fused_batch_norm_grad_v2;
string fused_batch_norm_v3;
string fused_batch_norm_grad_v3;
string fused_conv2d;
string fused_matmul;
string identity;
string leakyrelu;
string leakyrelu_grad;
string lrn;
string lrn_grad;
string matmul;
string max_pool;
string max_pool_grad;
string max_pool3d;
string max_pool3d_grad;
string maximum;
string mkl_conv2d;
string mkl_conv2d_grad_input;
string mkl_conv2d_grad_filter;
string mkl_conv2d_grad_filter_with_bias;
string mkl_conv2d_with_bias;
string mkl_depthwise_conv2d_grad_input;
string mkl_depthwise_conv2d_grad_filter;
string mkl_fused_conv2d;
string mkl_fused_matmul;
string mkl_pad_with_conv2d;
string mkl_pad_with_fused_conv2d;
string mul;
string pad;
string pad_with_conv2d;
string pad_with_fused_conv2d;
string quantized_avg_pool;
string quantized_conv2d;
string quantized_conv2d_per_channel;
string quantized_conv2d_with_requantize;
string quantized_conv2d_with_bias;
string quantized_conv2d_with_bias_and_requantize;
string quantized_conv2d_and_relu;
string quantized_conv2d_and_relu_and_requantize;
string quantized_conv2d_with_bias_and_relu;
string quantized_conv2d_with_bias_and_relu_and_requantize;
string quantized_concatv2;
string quantized_max_pool;
string quantized_conv2d_with_bias_sum_and_relu;
string quantized_conv2d_with_bias_sum_and_relu_and_requantize;
string quant_conv2d_with_bias_signed_sum_and_relu_and_requantize;
string quantized_matmul_with_bias;
string quantized_matmul_with_bias_and_relu;
string quantized_matmul_with_bias_and_relu_and_requantize;
string quantized_matmul_with_bias_and_requantize;
string quantized_depthwise_conv2d;
string quantized_depthwise_conv2d_with_bias;
string quantized_depthwise_conv2d_with_bias_and_relu;
string quantized_depthwise_conv2d_with_bias_and_relu_and_requantize;
string quantize_v2;
string relu;
string relu_grad;
string relu6;
string relu6_grad;
string requantize;
string tanh;
string tanh_grad;
string transpose;
string reshape;
string slice;
string softmax;
string split;
string squared_difference;
string sub;
} ConstStringsInfo;
private:
/// Maintain info about nodes to rewrite
std::vector<RewriteInfo> rinfo_;
/// Maintain info about nodes to add workspace edge
std::vector<WorkSpaceInfo> wsinfo_;
/// Maintain info about nodes to be merged
std::vector<MergeInfo> minfo_;
/// Maintain info about nodes to be fused
std::vector<FusionInfo> finfo_;
/// Maintain structure of constant strings
static ConstStringsInfo csinfo_;