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.
20 from extensions.middle.TensorIteratorInput import SmartInputMatcher, SimpleInputMatcher, BackEdgeSimpleInputMatcher
21 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
24 class SmartInputMatcherTests(unittest.TestCase):
26 pattern_matcher = SmartInputMatcher()
27 pattern = pattern_matcher.pattern()
29 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
30 update_edge_attrs={('range_data', 'TensorArrayScatter', 0): {'in': 1},
31 ('TensorArray_handle', 'TensorArrayScatter', 0): {'in': 0},
32 ('TensorArray_flow', 'TensorArrayScatter', 0): {'in': 3}},
33 new_nodes_with_attrs=[('ta_size', {'kind': 'data'}),
34 ('ta_size_op', {'kind': 'op'}),
35 ('value', {'kind': 'data'}),
37 new_edges_with_attrs=[
38 ('ta_size_op', 'ta_size'),
39 ('ta_size', 'TensorArray'),
40 ('value', 'TensorArrayScatter', {'in':2}),
42 update_nodes_attributes=[('Enter_data', {'value': np.array([1])}),
43 ('stack_data', {'value': np.array([0])}),
44 ('stack_1_data', {'value': np.array([1])}),
45 ('stack_2_data', {'value': np.array([1])}),
46 ('start_data', {'value': np.array([0])}),
47 ('delta_data', {'value': np.array([1])})
50 pattern_matcher.find_and_replace_pattern(graph)
51 graph_ref = build_graph_with_attrs(
52 nodes_with_attrs=[('condition_data', {'kind': 'data'}),
53 ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
54 ('TensorArrayRead_data', {'kind': 'data'}),
55 ('condition_data', {'kind': 'data'}),
56 ('value', {'kind': 'data'}),
57 ('ta_size', {'kind': 'data'}),
58 ('ta_size_op', {'kind': 'op'})],
59 edges_with_attrs=[('ta_size', 'TensorIteratorInput', {'in': 0}),
60 ('condition_data', 'TensorIteratorInput', {'in': 2}),
61 ('value', 'TensorIteratorInput', {'in': 1}),
62 ('TensorIteratorInput', 'TensorArrayRead_data'),
63 ('ta_size_op', 'ta_size')],
64 update_edge_attrs=None,
65 new_nodes_with_attrs=[],
66 new_edges_with_attrs=[],
68 (flag, resp) = compare_graphs(graph, graph_ref, 'TensorArrayRead_data', check_op_attrs=True)
69 self.assertTrue(flag, resp)
72 class SimpleInputMatcherTest(unittest.TestCase):
74 pattern_matcher = SimpleInputMatcher()
75 pattern = pattern_matcher.pattern()
77 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
78 update_edge_attrs=None,
79 new_nodes_with_attrs=[('in_node', {'kind': 'data'}),
80 ('Enter_data', {'kind': 'data'})],
81 new_edges_with_attrs=[('in_node', 'Enter'), ('Enter', 'Enter_data')],
82 update_nodes_attributes=[])
84 pattern_matcher.find_and_replace_pattern(graph)
86 graph_ref = build_graph_with_attrs(
87 nodes_with_attrs=[('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
88 ('in_node', {'kind': 'data'}),
89 ('Enter_data', {'kind': 'data'})
91 edges_with_attrs=[('in_node', 'TensorIteratorInput'), ('TensorIteratorInput', 'Enter_data')],
93 (flag, resp) = compare_graphs(graph, graph_ref, 'Enter_data', check_op_attrs=True)
94 self.assertTrue(flag, resp)
97 class BackEdgeInputMatcherTest(unittest.TestCase):
100 Case with constant input to init
102 pattern_matcher = BackEdgeSimpleInputMatcher()
103 pattern = pattern_matcher.pattern()
105 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
106 new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
107 ('condition', {'kind': 'data'}),
108 ('init', {'kind': 'data', 'shape': np.array([1,3])}),
110 new_edges_with_attrs=[('condition', 'BackEdge', {'in': 2}),
111 ('init', 'BackEdge', {'in': 0}),
112 ('cycle_data', 'BackEdge', {'in': 1})],)
114 pattern_matcher.find_and_replace_pattern(graph)
115 graph_ref = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
116 new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
117 ('condition', {'kind': 'data'}),
118 ('init', {'kind': 'data', 'shape': np.array([1,3])}),
119 ('TensorIteratorInput', {'kind': 'op', 'op': 'TensorIteratorInput'}),
120 ('TensorIteratorInput_data', {'kind': 'data', 'shape': np.array([1,3])}),
122 new_edges_with_attrs=[('TensorIteratorInput_data', 'TensorIteratorInput'),
123 ('TensorIteratorInput', 'init'),
124 ('condition', 'BackEdge', {'in': 2}),
125 ('init', 'BackEdge', {'in': 0}),
126 ('cycle_data', 'BackEdge', {'in': 1})],)
127 (flag, resp) = compare_graphs(graph, graph_ref, 'BackEdge', check_op_attrs=True)
128 self.assertTrue(flag, resp)
132 Case with non-constant input to init.
133 Nothing should happen with graph.
135 pattern_matcher = BackEdgeSimpleInputMatcher()
136 pattern = pattern_matcher.pattern()
138 graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
139 new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
140 ('condition', {'kind': 'data'}),
141 ('init', {'kind': 'data', 'shape': np.array([1, 3])}),
142 ('Enter', {'kind': 'op', 'op': 'Enter'}),
144 new_edges_with_attrs=[('Enter', 'init'),
145 ('condition', 'BackEdge', {'in': 2}),
146 ('init', 'BackEdge', {'in': 0}),
147 ('cycle_data', 'BackEdge', {'in': 1})])
149 pattern_matcher.find_and_replace_pattern(graph)
151 graph_ref = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'],
152 new_nodes_with_attrs=[('cycle_data', {'kind': 'data'}),
153 ('condition', {'kind': 'data'}),
154 ('init', {'kind': 'data', 'shape': np.array([1, 3])}),
155 ('Enter', {'kind': 'op', 'op': 'Enter'}),
157 new_edges_with_attrs=[('Enter', 'init'),
158 ('condition', 'BackEdge', {'in': 2}),
159 ('init', 'BackEdge', {'in': 0}),
160 ('cycle_data', 'BackEdge', {'in': 1})], )
162 (flag, resp) = compare_graphs(graph, graph_ref, 'BackEdge', check_op_attrs=True)
163 self.assertTrue(flag, resp)