Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / TensorIteratorCondition_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.TensorIteratorCondition import LoopConditionMatcher
21 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
22
23
24 class TensorIteratorConditionTests(unittest.TestCase):
25     def test(self):
26         pattern_matcher = LoopConditionMatcher()
27         pattern = pattern_matcher.pattern()
28
29         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
30                                        new_nodes_with_attrs=[('maximum', {'kind': 'op', 'op': 'Maximum'}),
31                                                              ('maximum_data', {'kind': 'data'}),
32                                                              ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'})],
33                                        new_edges_with_attrs=[('maximum', 'maximum_data'),
34                                                              ('Identity_1_data', 'TensorIteratorInput')],
35                                        update_nodes_attributes=[('init_1_data', {'value': np.array([0])}),
36                                                                 ('init_2_data', {'value': np.array([0])}),
37                                                                 ('add_1_y_data', {'value': np.array(1)}),
38                                                                 ('add_2_y_data', {'value': np.array(1)}),
39                                                                 ('loop_cond_data', {'value': None}),
40                                                                 ('Identity_2_data', {'value': None}),
41                                                                 ])
42
43         pattern_matcher.find_and_replace_pattern(graph)
44         graph_ref = build_graph_with_attrs(
45             nodes_with_attrs=[('TensorIteratorCondition', {'kind': 'op', 'op': 'TensorIteratorCondition'}),
46                               ('loop_cond_data', {'kind': 'data'}),
47                               ('identity_data', {'kind': 'data'}),
48                               ('StridedSlice', {'kind': 'op', 'op':'StridedSlice'}),
49                               ('StridedSlice_data', {'kind': 'data'}),
50                               ('Maximum', {'kind': 'op', 'op': 'Maximum'}),
51                               ('Maximum_data', {'kind': 'data'}),
52                               ('minimum_data', {'kind': 'data'}),
53                               ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'})
54                               ],
55             edges_with_attrs=[('Maximum', 'Maximum_data'),
56                               ('StridedSlice', 'StridedSlice_data'),
57                               ('StridedSlice_data', 'TensorIteratorCondition', {'in':0}),
58                               ('minimum_data', 'TensorIteratorCondition', {'in':1}),
59                               ('TensorIteratorCondition', 'loop_cond_data'),
60                               ('TensorIteratorCondition', 'identity_data'),
61                               ('identity_data', 'TensorIteratorInput'),
62                               ],
63             update_edge_attrs=None,
64             new_nodes_with_attrs=[],
65             new_edges_with_attrs=[],
66             )
67         (flag, resp) = compare_graphs(graph, graph_ref, 'loop_cond_data', check_op_attrs=True)
68         self.assertTrue(flag, resp)