Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / UselessSridedSlice_test.py
1 """
2  Copyright (c) 2018-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 import numpy as np
19
20 from extensions.middle.UselessStridedSlice import UselessStridedSliceEraser
21 from mo.utils.unittest.graph import build_graph, compare_graphs
22
23 nodes_attributes = {
24     # input data
25     'placeholder': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
26     'placeholder_data': {'value': None, 'shape': np.array([4, 5, 6]), 'kind': 'data', 'data_type': None},
27     #
28     'strided_slice': {'type': 'StridedSlice', 'kind': 'op', 'op': 'StridedSlice', 'shrink_axis_mask': None,
29                       'slices': [slice(0, 4, 1), slice(0, 5, 1), slice(0, 6, 1)]},
30     'strided_slice_data': {'value': None, 'shape': np.array([4, 5, 6]), 'kind': 'data'},
31     'strided_slice_input_1_data': {'value': None, 'shape': np.array([3]), 'kind': 'data'},
32     'strided_slice_input_2_data': {'value': None, 'shape': np.array([3]), 'kind': 'data'},
33     'strided_slice_input_3_data': {'value': None, 'shape': np.array([3]), 'kind': 'data'},
34     #
35     'strided_slice_2': {'type': 'StridedSlice', 'kind': 'op', 'op': 'StridedSlice', 'shrink_axis_mask': None,
36                         'slices': [slice(0, 4, 1), slice(0, 5, 1), slice(0, 6, 1)]},
37     'strided_slice_2_data': {'value': None, 'shape': np.array([4, 5, 6]), 'kind': 'data'},
38     # Output operation
39     'output_op': {'kind': 'op', 'op': 'OpOutput'},
40 }
41
42
43 class UselessStridedSliceTests(unittest.TestCase):
44     def test_single_stride_slice_removal(self):
45         graph = build_graph(nodes_attributes,
46                             [('placeholder', 'placeholder_data'),
47                              ('placeholder_data', 'strided_slice'),
48                              ('strided_slice_input_1_data', 'strided_slice'),
49                              ('strided_slice_input_2_data', 'strided_slice'),
50                              ('strided_slice_input_3_data', 'strided_slice'),
51                              ('strided_slice', 'strided_slice_data'),
52                              ('strided_slice_data', 'output_op'),
53                              ],
54                             {},
55                             nodes_with_edges_only=True
56                             )
57
58         pattern = UselessStridedSliceEraser()
59         pattern.find_and_replace_pattern(graph)
60
61         graph_ref = build_graph(nodes_attributes,
62                                 [('placeholder', 'placeholder_data'),
63                                  ('placeholder_data', 'output_op'),
64                                  ],
65                                 {'placeholder_data': {'shape': np.array([4, 5, 6])}}
66                                 )
67         (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
68         self.assertTrue(flag, resp)
69
70     def test_consecutive_stride_slices_removal(self):
71         graph = build_graph(nodes_attributes,
72                             [('placeholder', 'placeholder_data'),
73                              ('placeholder_data', 'strided_slice'),
74                              ('strided_slice_input_1_data', 'strided_slice'),
75                              ('strided_slice_input_2_data', 'strided_slice'),
76                              ('strided_slice_input_3_data', 'strided_slice'),
77                              ('strided_slice', 'strided_slice_data'),
78                              ('strided_slice_data', 'strided_slice_2'),
79                              ('strided_slice_input_1_data', 'strided_slice_2'),
80                              ('strided_slice_input_2_data', 'strided_slice_2'),
81                              ('strided_slice_input_3_data', 'strided_slice_2'),
82                              ('strided_slice_2', 'strided_slice_2_data'),
83                              ('strided_slice_2_data', 'output_op'),
84                              ],
85                             {},
86                             nodes_with_edges_only=True
87                             )
88
89         pattern = UselessStridedSliceEraser()
90         pattern.find_and_replace_pattern(graph)
91
92         graph_ref = build_graph(nodes_attributes,
93                                 [('placeholder', 'placeholder_data'),
94                                  ('placeholder_data', 'output_op'),
95                                  ],
96                                 {'placeholder_data': {'shape': np.array([4, 5, 6])}}
97                                 )
98         (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
99         self.assertTrue(flag, resp)