82a2e5ce6ec423305d5d6683f736a235a9a3636b
[platform/upstream/dldt.git] /
1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "transformations/convert_opset1_to_legacy/convert_opset1_to_legacy.hpp"
6
7 #include <transformations/convert_broadcast_to_tiles.hpp>
8 #include <transformations/convert_opset1_to_legacy/convert_convolutions.hpp>
9 #include <transformations/convert_divide.hpp>
10 #include <transformations/convert_mod.hpp>
11 #include <transformations/itt.hpp>
12 #include <transformations/convert_opset1_to_legacy/convert_cells_to_cells_ie.hpp>
13 #include <transformations/convert_opset1_to_legacy/convert_gather_to_gather_ie.hpp>
14 #include <transformations/convert_opset1_to_legacy/convert_gathertree_to_gathertree_ie.hpp>
15 #include <transformations/convert_opset1_to_legacy/convert_interpolate_to_interp_or_resample.hpp>
16 #include <transformations/convert_opset1_to_legacy/convert_lrn_to_lrn_ie.hpp>
17 #include <transformations/convert_opset1_to_legacy/convert_matmul_to_fc_or_gemm.hpp>
18 #include <transformations/convert_minimum_to_power_and_max.hpp>
19 #include <transformations/convert_opset1_to_legacy/convert_mul_add_to_scaleshift_or_power.hpp>
20 #include <transformations/convert_opset1_to_legacy/convert_mul_or_add_finally.hpp>
21 #include <transformations/convert_negative.hpp>
22 #include <transformations/convert_opset1_to_legacy/convert_nms_to_nms_ie.hpp>
23 #include <transformations/convert_opset1_to_legacy/convert_nms_4_to_legacy.hpp>
24 #include <transformations/convert_opset1_to_legacy/convert_normalizel2_to_normalize_ie.hpp>
25 #include <transformations/convert_opset1_to_legacy/convert_one_hot_to_one_hot_ie.hpp>
26 #include <transformations/convert_opset1_to_legacy/convert_pad_to_pad_ie.hpp>
27 #include <transformations/convert_opset1_to_legacy/convert_sqrt_to_power_ie.hpp>
28 #include <transformations/convert_opset1_to_legacy/convert_power_to_power_ie.hpp>
29 #include <transformations/convert_opset1_to_legacy/convert_prelu_to_relu_ie.hpp>
30 #include <transformations/convert_opset1_to_legacy/convert_proposal_to_proposal_ie.hpp>
31 #include <transformations/convert_reduce_to_pooling.hpp>
32 #include <transformations/convert_opset1_to_legacy/convert_strided_slice_to_crop.hpp>
33 #include <transformations/convert_subtract.hpp>
34 #include <transformations/convert_opset1_to_legacy/convert_selu_to_selu_ie.hpp>
35 #include <transformations/convert_opset1_to_legacy/convert_sequences_to_sequences_ie.hpp>
36 #include <transformations/convert_opset1_to_legacy/convert_swish_to_swish_ie.hpp>
37 #include <transformations/convert_opset1_to_legacy/convert_tile_to_ie_tile.hpp>
38 #include <transformations/convert_opset1_to_legacy/convert_topk_to_topk_ie.hpp>
39 #include <transformations/convert_depth_to_space.hpp>
40 #include <transformations/convert_space_to_depth.hpp>
41 #include <transformations/batch_norm_decomposition.hpp>
42 #include <transformations/convert_opset1_to_legacy/conv_bias_fusion.hpp>
43 #include <transformations/convert_opset1_to_legacy/fc_bias_fusion.hpp>
44 #include <transformations/convert_opset1_to_legacy/reshape_fc_fusion.hpp>
45 #include <transformations/convert_opset1_to_legacy/reshape_1d_ops.hpp>
46 #include <transformations/convert_opset1_to_legacy/reshape_fully_connected.hpp>
47 #include <transformations/pull_transpose_through_fq.hpp>
48 #include <transformations/convert_opset1_to_legacy/convert_hard_sigmoid_to_hard_sigmoid_ie.hpp>
49 #include <transformations/lin_op_sequence_fusoin.hpp>
50 #include <transformations/common_optimizations/conv_mul_fusion.hpp>
51 #include <transformations/hswish_decomposition.hpp>
52 #include <transformations/reduce_l1_decomposition.hpp>
53 #include <transformations/reduce_l2_decomposition.hpp>
54 #include <transformations/common_optimizations/fq_mul_fusion.hpp>
55 #include <transformations/common_optimizations/fq_reshape_fusion.hpp>
56
57 #include <ngraph/pass/constant_folding.hpp>
58 #include <ngraph/pass/manager.hpp>
59
60 #include <ngraph/pass/graph_rewrite.hpp>
61
62 #include <memory>
63 #include <vector>
64
65 NGRAPH_RTTI_DEFINITION(ngraph::pass::ConvertOpSet1ToLegacy, "ConvertOpSet1ToLegacy", 0);
66
67 bool ngraph::pass::ConvertOpSet1ToLegacy::run_on_function(std::shared_ptr<ngraph::Function> f) {
68     OV_ITT_SCOPED_TASK(itt::domains::IETransform, "ngraph::pass::ConvertOpSet1ToLegacy");
69
70     ngraph::pass::Manager manager;
71     std::vector<std::shared_ptr<ngraph::pass::PassBase> > transforms;
72
73     manager.register_pass<ngraph::pass::ConstantFolding>();
74
75     // the following two transformations produce ReduceSum operations so they
76     // must be executed before the ConvertReduceSumToPooling transformation
77     manager.register_pass<ngraph::pass::ReduceL1Decomposition>();
78     manager.register_pass<ngraph::pass::ReduceL2Decomposition>();
79
80     // HSwishDecomposition produce Minimum, Relu and Multiply operations
81     // so it must be executed before
82     manager.register_pass<ngraph::pass::HSwishDecomposition>();
83
84     // List if Decomposition and Conversion transformations that can be
85     // applied simultaneously in a single graph traversal
86     auto decomp = manager.register_pass<ngraph::pass::GraphRewrite>();
87     decomp->add_matcher<ngraph::pass::ConvertBroadcastToTiles>();
88     decomp->add_matcher<ngraph::pass::ConvertReduceMeanToPooling>();
89     decomp->add_matcher<ngraph::pass::ConvertReduceMaxToPooling>();
90     decomp->add_matcher<ngraph::pass::ConvertReduceSumToPooling>();
91     decomp->add_matcher<ngraph::pass::ConvertMod>();
92     decomp->add_matcher<ngraph::pass::ConvertMinimum>();
93     decomp->add_matcher<ngraph::pass::ConvertSubtract>();
94     decomp->add_matcher<ngraph::pass::ConvertDivide>();
95     decomp->add_matcher<ngraph::pass::ConvertNegative>();
96     decomp->add_matcher<ngraph::pass::ConvertDepthToSpace>();
97     decomp->add_matcher<ngraph::pass::ConvertSpaceToDepth>();
98     decomp->add_matcher<ngraph::pass::BatchNormDecomposition>();
99     decomp->add_matcher<ngraph::pass::ConvertMatMulToFC>();
100     decomp->add_matcher<ngraph::pass::ConvertMatMulToGemm>();
101     decomp->set_name("ngraph::pass::Decompositions");
102
103     // CF is required after all decompositions
104     manager.register_pass<ngraph::pass::ConstantFolding>();
105
106     // LinOpSequenceFusion must be executed after all decompositions
107     manager.register_pass<ngraph::pass::LinOpSequenceFusion>();
108
109     manager.register_pass<ngraph::pass::ConvolutionMultiplyFusion>();
110     manager.register_pass<ngraph::pass::GroupConvolutionMultiplyFusion>();
111     manager.register_pass<ngraph::pass::ConvolutionBackpropDataMultiplyFusion>();
112     manager.register_pass<ngraph::pass::GroupConvolutionBackpropDataMultiplyFusion>();
113     manager.register_pass<ngraph::pass::ConstantFolding>();
114
115     // Convolution/Deconvolution/FullyConnected fusions
116     auto convert_convolutions = manager.register_pass<ngraph::pass::GraphRewrite>();
117     convert_convolutions->add_matcher<ngraph::pass::ConvertConvolution>();
118     convert_convolutions->add_matcher<ngraph::pass::ConvertGroupConvolution>();
119     convert_convolutions->add_matcher<ngraph::pass::ConvertDeconvolution>();
120     convert_convolutions->add_matcher<ngraph::pass::ConvertGroupDeconvolution>();
121     convert_convolutions->set_name("ngraph::pass::ConvertConvolutions");
122
123     auto fq_fusions = manager.register_pass<ngraph::pass::GraphRewrite>();
124     fq_fusions->add_matcher<FakeQuantizeMulFusion>();
125     fq_fusions->add_matcher<FakeQuantizeReshapeFusion>();
126     fq_fusions->add_matcher<PullTransposeThroughFQUp>();
127     fq_fusions->set_name("ngraph::pass::FakeQuantizeFusions");
128
129     // Convolution/Deconvolution/FullyConnected fusions
130     auto fusion = manager.register_pass<ngraph::pass::GraphRewrite>();
131     fusion->add_matcher<ngraph::pass::ConvAddFusion>();
132     fusion->add_matcher<ngraph::pass::DeconvAddFusion>();
133     fusion->add_matcher<ngraph::pass::FullyConnectedBiasFusion>();
134     fusion->set_name("ngraph::pass::Fusions");
135
136     // CF is required after fusions
137     manager.register_pass<ngraph::pass::ConstantFolding>();
138
139     // List of passes that convert opset1 operations to legacy
140     // plus transformations that are required by InferenceEngine
141     // All this transformations can be executed simultaneously
142     auto anchor = manager.register_pass<ngraph::pass::GraphRewrite>();
143     anchor->add_matcher<ngraph::pass::ReshapeFullyConnected>();
144     anchor->add_matcher<ngraph::pass::Reshape1DConvolution>();
145     anchor->add_matcher<ngraph::pass::Reshape1DAvgPool>();
146     anchor->add_matcher<ngraph::pass::Reshape1DMaxPool>();
147     anchor->add_matcher<ngraph::pass::ConvertNormalizeL2WithMulToNormalizeIE>();
148     anchor->add_matcher<ngraph::pass::ConvertHardSigmoidToLegacyMatcher>();
149     anchor->add_matcher<ngraph::pass::ConvertProposalToLegacyMatcher>();
150     anchor->add_matcher<ngraph::pass::ConvertProposal4ToLegacyMatcher>();
151     anchor->add_matcher<ngraph::pass::ConvertTileToLegacyMatcher>();
152     anchor->add_matcher<ngraph::pass::ConvertLRNToLegacyMatcher>();
153     anchor->add_matcher<ngraph::pass::ConvertPadToLegacyMatcher>();
154     anchor->add_matcher<ngraph::pass::ConvertLSTMCellMatcher>();
155     anchor->add_matcher<ngraph::pass::ConvertRNNCellMatcher>();
156     anchor->add_matcher<ngraph::pass::ConvertGRUCellMatcher>();
157     anchor->add_matcher<ngraph::pass::ConvertInterpolateToInterpOrResampleMatcher>();
158     anchor->add_matcher<ngraph::pass::ConvertStridedSliceToCropMatcher>();
159     anchor->add_matcher<ngraph::pass::ConvertPowerToPowerIEMatcher>();
160     anchor->add_matcher<ngraph::pass::ConvertSqrtToPowerIEMatcher>();
161     anchor->add_matcher<ngraph::pass::ConvertPReLUToReLUIE>();
162     anchor->add_matcher<ngraph::pass::ConvertGatherToGatherIEMatcher>();
163     anchor->add_matcher<ngraph::pass::ConvertSeluToSeluIEMatcher>();
164     anchor->add_matcher<ngraph::pass::ConvertSwishToSwishIEMatcher>();
165     anchor->add_matcher<ngraph::pass::ConvertOneHotToOneHotIEMatcher>()->detect_output_type(f);
166     anchor->add_matcher<ngraph::pass::ConvertGatherTreeToGatherTreeIEMatcher>();
167     anchor->add_matcher<ngraph::pass::ConvertTopKToTopKIEMatcher>();
168     anchor->add_matcher<ngraph::pass::ConvertNMSToNMSIEMatcher>();
169     anchor->add_matcher<ngraph::pass::ConvertNMS4ToLegacyMatcher>();
170     anchor->add_matcher<ngraph::pass::ConvertGRUSequenceMatcher>();
171     anchor->add_matcher<ngraph::pass::ConvertRNNSequenceMatcher>();
172     anchor->add_matcher<ngraph::pass::ConvertLSTMSequenceMatcher>();
173     anchor->set_name("ngraph::pass::ConvertOpSet1ToLegacy");
174
175     // List of final conversion transformations that must to be executed
176     // after previous group of transformations
177     manager.register_pass<ngraph::pass::ReshapeFullyConnectedFusion>();
178     manager.register_pass<ngraph::pass::ConvertNormalizeL2ToLegacyMatcher>();
179     manager.register_pass<ngraph::pass::ConvertMulAddToScaleShiftOrPower>();
180     manager.register_pass<ngraph::pass::ConvertMulOrAddFinally>();
181
182     manager.register_pass<ngraph::pass::ConstantFolding>();
183
184     manager.set_callback(m_transformation_callback);
185     manager.run_passes(f);
186     return true;
187 }