bbef79f7a0a241a1490a97fb6a69281c6a88dbd2
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / RemoveDuplicationMemory_test.py
1 """
2  Copyright (c) 2019 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 import unittest
17
18 from extensions.middle.RemoveDuplicationMemory import RemoveMemoryDuplicationPattern
19 from mo.utils.unittest.graph import build_graph, compare_graphs
20
21
22 class RemoveMemoryDuplicationPatternTests(unittest.TestCase):
23
24     def test_remove_duplication(self):
25         graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
26                              'splice_1': {'kind': 'op', 'op': 'Splice', 'context': range(-5, 6)},
27                              'splice_data_1': {'kind': 'data', 'shape': [1, 143]},
28                              'placeholder_1': {'kind': 'op', 'op': None},
29                              'splice_2': {'kind': 'op', 'op': 'Splice', 'context': range(-1, 2)},
30                              'splice_data_2': {'kind': 'data', 'shape': [1, 39]},
31                              'placeholder_2': {'kind': 'op', 'op': None},
32                              },
33                             [('in_node', 'splice_1'), ('splice_1', 'splice_data_1'), ('splice_data_1', 'placeholder_1'),
34                              ('in_node', 'splice_2'), ('splice_2', 'splice_data_2'), ('splice_data_2', 'placeholder_2'),
35                              ],
36                             nodes_with_edges_only=True)
37         RemoveMemoryDuplicationPattern().find_and_replace_pattern(graph)
38         ref_graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
39                                  'splice_1': {'kind': 'op', 'op': 'Splice', 'context': range(-5, 6)},
40                                  'splice_data_1': {'kind': 'data', 'shape': [1, 143]},
41                                  'placeholder_1': {'kind': 'op'},
42                                  'crop_2': {'kind': 'op', 'op': 'Crop', 'offset': 52, 'dim': 39, 'axis': -1},
43                                  'splice_data_2': {'kind': 'data', 'shape': [1, 39]},
44                                  'placeholder_2': {'kind': 'op'},
45                                  },
46                                 [
47                                     ('in_node', 'splice_1'), ('splice_1', 'splice_data_1'),
48                                     ('splice_data_1', 'placeholder_1'),
49                                     ('splice_data_1', 'crop_2'), ('crop_2', 'splice_data_2'),
50                                     ('splice_data_2', 'placeholder_2'),
51                                 ],
52                                 nodes_with_edges_only=True
53                                 )
54
55         (flag, resp) = compare_graphs(graph, ref_graph, 'placeholder_2')
56         self.assertTrue(flag, resp)
57
58     def test_remove_duplication_with_crops(self):
59         graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
60                              'splice_1': {'kind': 'op', 'op': 'Splice', 'context': range(-5, 6)},
61                              'splice_data_1': {'kind': 'data', 'shape': [1, 143]},
62                              'crop_1': {'kind': 'op', 'op': 'Crop', 'offset': 13, 'dim': 13, 'axis': -1},
63                              'splice_2': {'kind': 'op', 'op': 'Splice', 'context': range(-1, 2)},
64                              'splice_data_2': {'kind': 'data', 'shape': [1, 39]},
65                              'crop_2': {'kind': 'op', 'op': 'Crop', 'offset': 13, 'dim': 13, 'axis': -1},
66                              },
67                             [('in_node', 'splice_1'), ('splice_1', 'splice_data_1'), ('splice_data_1', 'crop_1'),
68                              ('in_node', 'splice_2'), ('splice_2', 'splice_data_2'), ('splice_data_2', 'crop_2'),
69                              ],
70                             nodes_with_edges_only=True)
71         RemoveMemoryDuplicationPattern().find_and_replace_pattern(graph)
72         ref_graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
73                                  'splice_1': {'kind': 'op', 'op': 'Splice', 'context': range(-5, 6)},
74                                  'splice_data_1': {'kind': 'data', 'shape': [1, 143]},
75                                  'crop_1': {'kind': 'op', 'op': 'Crop', 'offset': 13, 'dim': 13},
76                                  'crop_2': {'kind': 'op', 'op': 'Crop', 'offset': 65, 'dim': 13, 'axis': -1},
77                                  },
78                                 [
79                                     ('in_node', 'splice_1'), ('splice_1', 'splice_data_1'),
80                                     ('splice_data_1', 'crop_1'),
81                                     ('splice_data_1', 'crop_2'),
82                                 ],
83                                 nodes_with_edges_only=True
84                                 )
85
86         (flag, resp) = compare_graphs(graph, ref_graph, 'crop_2')
87         self.assertTrue(flag, resp)