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.SharedWeightsDuplication import SharedWeightsDuplication
22 from mo.middle.passes.eliminate import graph_clean_up
23 from mo.utils.unittest.graph import build_graph, compare_graphs
26 'const': {'shape': None, 'type': 'Const', 'kind': 'op', 'op': 'Const'},
27 # Mul and Add operations
28 'mul_1': {'type': None, 'kind': 'op', 'op': 'Mul'},
29 'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
30 'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
31 'mul_2': {'type': None, 'kind': 'op', 'op': 'Mul'},
32 'mul_2_w': {'value': None, 'shape': None, 'kind': 'data'},
33 'mul_2_data': {'value': None, 'shape': None, 'kind': 'data'},
34 'mul_3': {'type': None, 'kind': 'op', 'op': 'Mul'},
35 'mul_3_w': {'value': None, 'shape': None, 'kind': 'data'},
36 'mul_3_data': {'value': None, 'shape': None, 'kind': 'data'},
38 'concat_1': {'type': 'Concat', 'kind': 'op', 'op': 'Concat'},
39 'concat_1_data': {'value': None, 'shape': None, 'kind': 'data'},
40 'op_output': {'op': 'OpOutput', 'kind': 'op'}
44 class DuplicateSharedWeightsTests(unittest.TestCase):
45 def test_duplicate_shared_weights_1(self):
46 graph = build_graph(nodes_attributes,
47 [('const', 'mul_1_w'),
49 ('mul_1', 'mul_1_data'),
51 ('mul_2', 'mul_2_data'),
53 ('mul_3', 'mul_3_data'),
54 ('mul_1_data', 'concat_1'),
55 ('mul_2_data', 'concat_1'),
56 ('mul_3_data', 'concat_1'),
57 ('concat_1', 'concat_1_data'),
58 ('concat_1_data', 'op_output')
60 {'mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}},
61 nodes_with_edges_only=True
64 graph_ref = build_graph(nodes_attributes,
67 ('mul_1', 'mul_1_data'),
69 ('mul_2', 'mul_2_data'),
71 ('mul_3', 'mul_3_data'),
72 ('mul_1_data', 'concat_1'),
73 ('mul_2_data', 'concat_1'),
74 ('mul_3_data', 'concat_1'),
75 ('concat_1', 'concat_1_data'),
76 ('concat_1_data', 'op_output')
78 {'mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
79 'mul_2_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
80 'mul_3_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
81 }, nodes_with_edges_only=True)
83 SharedWeightsDuplication().find_and_replace_pattern(graph)
85 graph_clean_up(graph_ref)
86 (flag, resp) = compare_graphs(graph, graph_ref, 'concat_1_data')
87 self.assertTrue(flag, resp)