Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / back / TileReshaper_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
17 import unittest
18
19 import numpy as np
20
21 from extensions.back.TileReshaper import TileReshaper
22 from mo.ops.tile import Tile
23 from mo.utils.unittest.graph import build_graph, compare_graphs
24
25 # The dictionary with nodes attributes used to build various graphs. A key is the name of the node and the value is the
26 # dictionary with node attributes.
27 nodes_attributes = {
28     'previous_data': {'shape': np.array([1, 1, 101]), 'kind': 'data'},
29     'tile': {'type': 'Tile', 'kind': 'op', 'axis': 1, 'tiles': 16, 'infer': Tile.infer},
30     'tile_data': {'shape': np.array([1, 16, 101]), 'kind': 'data'},
31     'next_op': {'kind': 'op', 'op': 'SomeOp'},
32 }
33 edge_attributes = [
34     ('previous_data', 'tile'),
35     ('tile', 'tile_data'),
36     ('tile_data', 'next_op'),
37 ]
38
39 nodes_attributes_ref = {
40     'previous_data': {'kind': 'data', 'shape': np.array([1, 1, 101])},
41     'reshape_op_before': {'type': 'Reshape', 'kind': 'op', 'dim': [1, 1, 101, 1]},
42     'reshape_data_before': {'kind': 'data', 'shape': np.array([1, 1, 101, 1])},
43     'tile': {'type': 'Tile', 'kind': 'op', 'infer': Tile.infer, 'axis': 1, 'tiles': 16},
44     'tile_data': {'shape': np.array([1, 16, 101, 1]), 'kind': 'data'},
45     'reshape_op_after': {'type': 'Reshape', 'kind': 'op', 'dim': [1, 16, 101]},
46     'reshape_data_after': {'kind': 'data', 'shape': np.array([1, 16, 101])},
47     'next_op': {'kind': 'op', 'op': 'SomeOp'},
48 }
49 edge_attributes_ref = [
50     ('previous_data', 'reshape_op_before'),
51     ('reshape_op_before', 'reshape_data_before'),
52     ('reshape_data_before', 'tile'),
53     ('tile', 'tile_data'),
54     ('tile_data', 'reshape_op_after'),
55     ('reshape_op_after', 'reshape_data_after'),
56     ('reshape_data_after', 'next_op')
57 ]
58
59
60 class TileReshaperTests(unittest.TestCase):
61     def test_tile_reshaper(self):
62         graph = build_graph(nodes_attributes, edge_attributes)
63
64         graph_ref = build_graph(nodes_attributes_ref, edge_attributes_ref)
65
66         pattern = TileReshaper()
67         pattern.find_and_replace_pattern(graph)
68
69         (flag, resp) = compare_graphs(graph, graph_ref, 'next_op', check_op_attrs=True)
70         self.assertTrue(flag, resp)