2 Copyright (c) 2018-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
21 from extensions.middle.FusePermutesSequence import FusePermutesSequence
22 from extensions.middle.NormalizeFullyConnected import NormalizeFullyConnected
23 from mo.middle.passes.eliminate_test import build_graph
24 from mo.middle.passes.fusing.fuse_linear_ops_test import compare_graphs
26 # The dictionary with nodes attributes used to build various graphs. A key is the name of the node and the value is the
27 # dictionary with node attributes.
29 'placeholder_1': {'name': 'placeholder_1', 'value': None, 'shape': None, 'type': 'Placeholder', 'kind': 'op',
31 'placeholder_1_data': {'name': 'placeholder_1_data', 'value': None, 'shape': None, 'kind': 'data',
33 'reshape_1': {'type': 'Reshape', 'value': None, 'kind': 'op', 'op': 'Reshape'},
34 'reshape_1_data': {'value': None, 'shape': None, 'kind': 'data'},
36 'fc': {'type': 'FullyConnected', 'value': None, 'kind': 'op', 'op': 'MatMul'},
37 'fc_data': {'value': None, 'shape': None, 'kind': 'data'},
38 'fc_weights': {'value': None, 'shape': None, 'kind': 'data'},
40 'reshape_2': {'type': 'Reshape', 'value': None, 'kind': 'op', 'op': 'Reshape'},
41 'reshape_2_data': {'value': None, 'shape': None, 'kind': 'data'},
45 class NormalizeFullyConnectedTest(unittest.TestCase):
47 graph = build_graph(nodes_attributes,
48 [('placeholder_1', 'placeholder_1_data'),
49 ('placeholder_1_data', 'fc'),
53 {'placeholder_1_data': {'shape': np.array([1, 16, 512])},
54 'fc': {'out-size': 101},
55 'fc_weights': {'shape': np.array([512,101]), 'value': np.ones([512, 101]), 'input_channel_dim': 1},
56 'fc_data': {'shape': np.array([1, 16, 101])},
57 }, nodes_with_edges_only=True)
59 graph_ref = build_graph(nodes_attributes,
60 [('placeholder_1', 'placeholder_1_data'),
61 ('placeholder_1_data', 'reshape_1'),
62 ('reshape_1', 'reshape_1_data'),
63 ('reshape_1_data', 'fc'),
66 ('fc_data', 'reshape_2'),
67 ('reshape_2', 'reshape_2_data'),
69 {'placeholder_1_data': {'shape': np.array([1, 16, 512])},
70 'reshape_1_data': {'shape': np.array([16, 512])},
71 'reshape_2_data': {'shape': np.array([1, 16, 101])},
72 'fc_weights': {'shape': np.array([512,101]), 'value': np.ones([512, 101])},
73 'fc': {'out-size': 101},
74 'fc_data': {'shape': np.array([16, 101])},
75 }, nodes_with_edges_only=True)
77 pattern = NormalizeFullyConnected()
78 pattern.find_and_replace_pattern(graph)
80 (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data', 'placeholder_1_data', check_op_attrs=True)
81 self.assertTrue(flag, resp)
85 graph = build_graph(nodes_attributes,
86 [('placeholder_1', 'placeholder_1_data'),
87 ('placeholder_1_data', 'fc'),
91 {'placeholder_1_data': {'shape': np.array([2, 32, 16, 512])},
92 'fc': {'out-size': 101},
93 'fc_weights': {'shape': np.array([512,101]), 'value': np.ones([512, 101]), 'input_channel_dim': 1},
94 'fc_data': {'shape': np.array([2, 32, 16, 101])},
95 }, nodes_with_edges_only=True)
97 graph_ref = build_graph(nodes_attributes,
98 [('placeholder_1', 'placeholder_1_data'),
99 ('placeholder_1_data', 'reshape_1'),
100 ('reshape_1', 'reshape_1_data'),
101 ('reshape_1_data', 'fc'),
102 ('fc_weights', 'fc'),
104 ('fc_data', 'reshape_2'),
105 ('reshape_2', 'reshape_2_data'),
107 {'placeholder_1_data': {'shape': np.array([2, 32, 16, 512])},
108 'reshape_1_data': {'shape': np.array([2 * 32 * 16, 512])},
109 'reshape_2_data': {'shape': np.array([2, 32, 16, 101])},
110 'fc_weights': {'shape': np.array([512,101]), 'value': np.ones([512, 101])},
111 'fc': {'out-size': 101},
112 'fc_data': {'shape': np.array([2 * 32 * 16, 101])},
113 }, nodes_with_edges_only=True)
115 pattern = NormalizeFullyConnected()
116 pattern.find_and_replace_pattern(graph)
118 (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data', 'placeholder_1_data', check_op_attrs=True)
119 self.assertTrue(flag, resp)