2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
19 from extensions.middle.TensorIteratorBackEdge import BackEdgesMatching
20 from mo.utils.unittest.graph import compare_graphs, build_graph_with_attrs
23 class BackEdgesMatchingTests(unittest.TestCase):
24 def test_no_exit(self):
25 pattern_matcher = BackEdgesMatching()
26 pattern = pattern_matcher.pattern()
27 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'], update_edge_attrs=None,
28 new_nodes_with_attrs=[('from_body_data', {'kind':'data'})],
29 new_edges_with_attrs=[('from_body_data', 'NextIteration')])
31 pattern_matcher.find_and_replace_pattern(graph)
32 graph_ref = build_graph_with_attrs(nodes_with_attrs=[('condition', {'kind': 'op', 'op':'TensorIteratorCondition'}),
33 ('condition_data', {'kind': 'data'}),
34 ('back_edge', {'kind': 'op', 'op': 'TensorIteratorBackEdge'}),
35 ('enter_data', {'kind': 'data'}),
36 ('from_body_data', {'kind': 'data'}),
37 ('Identity_1_data', {'kind': 'data'}),],
38 edges_with_attrs=[('condition', 'condition_data'),
39 ('enter_data', 'back_edge', {'in': 0}),
40 ('condition_data', 'back_edge', {'in': 2}), # {in:2}
41 ('from_body_data', 'back_edge', {'in': 1}),
42 ('back_edge', 'Identity_1_data')],
43 update_edge_attrs=None,
44 new_nodes_with_attrs=[],
45 new_edges_with_attrs=[],
47 (flag, resp) = compare_graphs(graph, graph_ref, 'Identity_1_data', check_op_attrs=True)
48 self.assertTrue(flag, resp)
50 def test_with_exit(self):
51 pattern_matcher = BackEdgesMatching()
52 pattern = pattern_matcher.pattern()
53 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'], update_edge_attrs=None,
54 new_nodes_with_attrs=[('from_body_data', {'kind':'data'}),
55 ('exit', {'kind': 'op', 'op': 'Exit', 'name': 'exit'}),
56 ('exit_data', {'kind':'data'})],
57 new_edges_with_attrs=[('from_body_data', 'NextIteration'),
58 ('Switch_1', 'exit', {'out': 0}),
59 ('exit', 'exit_data')])
61 pattern_matcher.find_and_replace_pattern(graph)
62 graph_ref = build_graph_with_attrs(nodes_with_attrs=[('condition', {'kind': 'op', 'op':'TensorIteratorCondition'}),
63 ('condition_data', {'kind': 'data'}),
64 ('back_edge', {'kind': 'op', 'op': 'TensorIteratorBackEdge'}),
65 ('enter_data', {'kind': 'data'}),
66 ('from_body_data', {'kind': 'data'}),
67 ('Identity_1_data', {'kind': 'data'}),
68 ('output', {'kind':'op', 'op':'TensorIteratorOutput'}),
69 ('exit_data', {'kind': 'data'})
71 edges_with_attrs=[('condition', 'condition_data'),
72 ('enter_data', 'back_edge', {'in': 0}),
73 ('condition_data', 'back_edge', {'in': 2}),
74 ('from_body_data', 'back_edge', {'in': 1}),
75 ('back_edge', 'Identity_1_data'),
76 ('condition_data', 'output'),
77 ('output', 'exit_data'),
78 ('from_body_data', 'output')],
79 update_edge_attrs=None,
80 new_nodes_with_attrs=[],
81 new_edges_with_attrs=[],
83 (flag, resp) = compare_graphs(graph, graph_ref, 'Identity_1_data', check_op_attrs=True)
84 self.assertTrue(flag, resp)