Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / model-optimizer / mo / middle / passes / shared_weights_duplication_test.py
1 """
2  Copyright (c) 2018 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 import unittest
18
19 import numpy as np
20
21 from mo.middle.passes.shared_weights_duplication import duplicate_shared_weights
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 nodes_attributes = {
25     # Mul and Add operations
26     'mul_1': {'type': None, 'kind': 'op', 'op': 'Mul'},
27     'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
28     'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
29     'mul_2': {'type': None, 'kind': 'op', 'op': 'Mul'},
30     'mul_2_w': {'value': None, 'shape': None, 'kind': 'data'},
31     'mul_2_data': {'value': None, 'shape': None, 'kind': 'data'},
32     'mul_3': {'type': None, 'kind': 'op', 'op': 'Mul'},
33     'mul_3_w': {'value': None, 'shape': None, 'kind': 'data'},
34     'mul_3_data': {'value': None, 'shape': None, 'kind': 'data'},
35     # Concat1 operation
36     'concat_1': {'type': 'Concat', 'kind': 'op', 'op': 'Concat'},
37     'concat_1_data': {'value': None, 'shape': None, 'kind': 'data'},
38 }
39
40
41 class DuplicateSharedWeightsTests(unittest.TestCase):
42     def test_duplicate_shared_weights_1(self):
43         graph = build_graph(nodes_attributes,
44                             [('mul_1_w', 'mul_1'),
45                              ('mul_1', 'mul_1_data'),
46                              ('mul_1_w', 'mul_2'),
47                              ('mul_2', 'mul_2_data'),
48                              ('mul_1_w', 'mul_3'),
49                              ('mul_3', 'mul_3_data'),
50                              ('mul_1_data', 'concat_1'),
51                              ('mul_2_data', 'concat_1'),
52                              ('mul_3_data', 'concat_1'),
53                              ('concat_1', 'concat_1_data')
54                              ],
55                             {'mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}})
56
57         graph_ref = build_graph(nodes_attributes,
58                                 [('mul_1_w', 'mul_1'),
59                                  ('mul_1', 'mul_1_data'),
60                                  ('mul_2_w', 'mul_2'),
61                                  ('mul_2', 'mul_2_data'),
62                                  ('mul_3_w', 'mul_3'),
63                                  ('mul_3', 'mul_3_data'),
64                                  ('mul_1_data', 'concat_1'),
65                                  ('mul_2_data', 'concat_1'),
66                                  ('mul_3_data', 'concat_1'),
67                                  ('concat_1', 'concat_1_data')
68                                  ],
69                                 {'mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
70                                  'mul_2_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
71                                  'mul_3_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])},
72                                  })
73
74         duplicate_shared_weights(graph)
75
76         (flag, resp) = compare_graphs(graph, graph_ref, 'concat_1_data')
77         self.assertTrue(flag, resp)