Example #1
0
 def test_in_port_with_data(self):
     graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                    edges_with_attrs=self.edges)
     graph.stage = 'middle'
     new_input_shape = np.array([1, 2, 3, 4])
     graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                        edges_with_attrs=self.edges[1:],
                                        new_nodes_with_attrs=[
                                            ('input_node', {
                                                'kind': 'op',
                                                'op': 'Parameter',
                                                'shape': new_input_shape
                                            }),
                                            ('input_data', {
                                                'kind': 'data'
                                            })
                                        ],
                                        new_edges_with_attrs=[
                                            ('input_node', 'input_data', {
                                                'in': 0,
                                                'out': 0
                                            }),
                                            ('input_data', 'op_node', {
                                                'in': 1,
                                                'out': 0
                                            })
                                        ])
     add_input_op(graph, 'op_node', 1, data=True, shape=new_input_shape)
     graph.remove_edge('future_input', 'op_node')
     (flag, resp) = compare_graphs(graph, graph_ref, last_node='op_node')
     self.assertTrue(flag, resp)
Example #2
0
 def test_out_port_no_data(self):
     graph = build_graph_with_attrs(nodes_with_attrs=self.nodes_out,
                                    edges_with_attrs=self.edges_out)
     new_input_shape = np.array([1, 2, 3, 4])
     graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes_out,
                                        edges_with_attrs=self.edges_out[1:],
                                        new_nodes_with_attrs=[
                                            ('input_node', {
                                                'kind': 'op',
                                                'op': 'Parameter',
                                                'shape': new_input_shape
                                            })
                                        ],
                                        new_edges_with_attrs=[
                                            ('input_node', 'future_input', {
                                                'in': 0,
                                                'out': 0
                                            })
                                        ])
     add_input_op(graph,
                  'op_node',
                  1,
                  data=False,
                  shape=new_input_shape,
                  is_out_port=True)
     graph.remove_edge('op_node', 'future_input')
     (flag, resp) = compare_graphs(graph,
                                   graph_ref,
                                   last_node='another_node')
     self.assertTrue(flag, resp)
     (flag, resp) = compare_graphs(graph,
                                   graph_ref,
                                   last_node='future_input')
     self.assertTrue(flag, resp)
Example #3
0
    def test_merge_infer_only_second_executable(self):
        graph = build_graph_with_attrs(
            nodes_with_attrs=self.nodes,
            edges_with_attrs=self.edges,
            update_nodes_attributes=[
                ('first', {'executable': False, 'value': np.ones([2, 2]), 'shape': int64_array([2, 2])}),
                ('second', {'executable': True, 'value': np.zeros([4, 4]), 'shape': int64_array([4, 4])})
            ]
        )

        ref_graph = build_graph_with_attrs(
            nodes_with_attrs=self.nodes,
            edges_with_attrs=self.edges,
            update_nodes_attributes=[
                ('first', {'executable': False, 'value': np.ones([2, 2]), 'shape': int64_array([2, 2])}),
                ('second', {'executable': True, 'value': np.zeros([4, 4]), 'shape': int64_array([4, 4])}),
                ('merge', {'is_not_fully_inferred': False}),
                ('merge_output', {'shape': int64_array([4, 4]), 'value': np.zeros([4, 4])})
            ]
        )

        tested_class = Merge(graph=graph, attrs={})
        node = Node(graph, 'merge')
        tested_class.merge_infer(node)

        (flag, resp) = compare_graphs(graph, ref_graph, 'merge_output', check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #4
0
 def test_two_nodes_one_bin(self):
     """Test case for two output nodes, one with 'bin' parameter, other without."""
     shape = np.array([2, 3, 4])
     data = np.zeros(shape)
     graph = build_graph_with_attrs(
         nodes_with_attrs=self.nodes + [('next_node_2', {
             'kind': 'op'
         })],
         edges_with_attrs=self.edges + [('data_node', 'next_node_2')],
         update_nodes_attributes=[('data_node', {
             'shape': shape,
             'value': data
         })],
         update_edge_attrs={('data_node', 'next_node', 0): {
                                'bin': 0
                            }},
     )
     graph_ref = build_graph_with_attrs(
         nodes_with_attrs=self.nodes + self.new_nodes + [('next_node_2', {
             'kind': 'op'
         })],
         edges_with_attrs=self.edges + self.new_edges +
         [('data_node', 'next_node_2')],
         update_nodes_attributes=[('data_node', {
             'shape': shape,
             'value': data
         }), ('const_data', {
             'shape': shape,
             'value': data
         })])
     tested_pattern = CreateConstNodesReplacement()
     tested_pattern.find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph, graph_ref, last_node='next_node')
     self.assertTrue(flag, resp)
    def test_no_exit(self):
        pattern_matcher = BackEdgesMatching()
        pattern = pattern_matcher.pattern()
        graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'], update_edge_attrs=None,
                                       new_nodes_with_attrs=[('from_body_data', {'kind':'data'})],
                                       new_edges_with_attrs=[('from_body_data', 'NextIteration')])

        pattern_matcher.find_and_replace_pattern(graph)
        graph_ref = build_graph_with_attrs(nodes_with_attrs=[('condition', {'kind': 'op', 'op':'TensorIteratorCondition'}),
                                                      ('condition_data', {'kind': 'data'}),
                                                      ('back_edge', {'kind': 'op', 'op': 'TensorIteratorBackEdge'}),
                                                      ('enter_data', {'kind': 'data'}),
                                                      ('from_body_data', {'kind': 'data'}),
                                                      ('Identity_1_data', {'kind': 'data'}),],
                                    edges_with_attrs=[('condition', 'condition_data'),
                                           ('enter_data', 'back_edge', {'in': 0}),
                                           ('condition_data', 'back_edge', {'in': 2}),  # {in:2}
                                           ('from_body_data', 'back_edge', {'in': 1}),
                                           ('back_edge', 'Identity_1_data')],
                                    update_edge_attrs=None,
                                    new_nodes_with_attrs=[],
                                    new_edges_with_attrs=[],
                                )
        (flag, resp) = compare_graphs(graph, graph_ref, 'Identity_1_data', check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #6
0
    def test_select_infer_condition_true(self):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('condition', {
                                               'value': np.array([True])
                                           }),
                                           ('select_output', {
                                               'shape': np.array([2, 2]),
                                               'value': np.ones((2, 2))
                                           })
                                       ])

        # We should propagate shapes and values
        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                           edges_with_attrs=self.edges,
                                           update_nodes_attributes=[
                                               ('select_output', {
                                                   'shape': np.array([2, 2]),
                                                   'value': np.ones((2, 2))
                                               })
                                           ])

        tested_class = Select(graph=graph, attrs={})

        node = Node(graph, 'select')
        tested_class.infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'select_output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #7
0
    def test_merge_infer_simple_case_one_executable(self):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges)

        # We should propagate value of the first input since only this input is executable
        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                           edges_with_attrs=self.edges,
                                           update_nodes_attributes=[
                                               ('merge_output', {
                                                   'shape': np.array([2, 2]),
                                                   'value': np.ones((2, 2))
                                               }),
                                               ('merge', {
                                                   'is_not_fully_inferred':
                                                   False
                                               })
                                           ])

        tested_class = Merge(graph=graph, attrs={})
        node = Node(graph, 'merge')
        tested_class.merge_infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'merge_output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #8
0
    def test_switch_infer_no_condition(self):
        nodes = [('tensor', {
            'value': None,
            'kind': 'data',
            'executable': True,
            'shape': np.array([1, 2, 1])
        }), ('pred_id', {
            'value': None,
            'kind': 'data',
            'executable': True
        }), ('switch', {
            'type': 'Switch',
            'kind': 'op',
            'op': 'Switch'
        }),
                 ('switch_data_0', {
                     'value': None,
                     'kind': 'data',
                     'executable': True
                 }),
                 ('switch_data_1', {
                     'value': None,
                     'kind': 'data',
                     'executable': True
                 })]
        edges = [('tensor', 'switch', {
            'in': 0
        }), ('pred_id', 'switch', {
            'in': 1
        }), ('switch', 'switch_data_0', {
            'out': 0
        }), ('switch', 'switch_data_1', {
            'out': 1
        })]
        graph = build_graph_with_attrs(nodes_with_attrs=nodes,
                                       edges_with_attrs=edges)

        # We should propagate only shapes
        graph_ref = build_graph_with_attrs(nodes_with_attrs=nodes,
                                           edges_with_attrs=edges,
                                           update_nodes_attributes=[
                                               ('switch_data_0', {
                                                   'shape': np.array([1, 2, 1])
                                               }),
                                               ('switch_data_1', {
                                                   'shape': np.array([1, 2, 1])
                                               })
                                           ])

        tested_class = Switch(graph=graph, attrs={})

        node = Node(graph, 'switch')
        tested_class.infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'switch_data_0',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #9
0
    def test_select_infer_condition_shapes_broadcast(self, else_data_shape,
                                                     than_data_shape,
                                                     select_output_shape):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('else_data', {
                                               'shape':
                                               np.array(else_data_shape),
                                               'value':
                                               np.zeros(else_data_shape,
                                                        dtype=np.float)
                                           }),
                                           ('than_data', {
                                               'shape':
                                               np.array(than_data_shape),
                                               'value':
                                               np.zeros(than_data_shape,
                                                        dtype=np.float)
                                           }),
                                           ('select_output', {
                                               'shape': None,
                                               'value': None
                                           })
                                       ])

        node = Node(graph, 'select')
        node.infer(node)

        self.assertTrue(
            np.array_equal(graph.nodes['select_output']['shape'],
                           np.array(select_output_shape)))
Example #10
0
    def test_positive_matmul_infer(self, A_shape, B_shape, C_shape,
                                   transpose_a, transpose_b):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('A_d', {
                                               'shape': int64_array(A_shape)
                                           }),
                                           ('B_d', {
                                               'shape': int64_array(B_shape)
                                           }),
                                           ('mat_mul', {
                                               'transpose_a': transpose_a,
                                               'transpose_b': transpose_b
                                           }),
                                       ])
        node = Node(graph, 'mat_mul')
        MatMul.infer(node)

        msg = "MatMul infer failed for case: A_shape={}, B_shape={}, transpose_a={}, transpose_b={} " \
              "expected_shape={}, actual_shape={}"

        self.assertTrue(
            np.array_equal(graph.node['mat_mul_d']['shape'],
                           int64_array(C_shape)),
            msg.format(A_shape, B_shape, transpose_a, transpose_b, C_shape,
                       graph.node['mat_mul_d']['shape']))
Example #11
0
 def test_two_nodes_with_bin(self):
     """Test case for data node with 2 consumers with bin edge attr.
     Nothing should happened."""
     shape = np.array([2, 3, 4])
     data = np.zeros(shape)
     graph = build_graph_with_attrs(
         nodes_with_attrs=self.nodes + [('next_node_2', {
             'kind': 'op'
         })],
         edges_with_attrs=self.edges + [('data_node', 'next_node_2')],
         update_nodes_attributes=[('data_node', {
             'shape': shape,
             'value': data
         })],
         update_edge_attrs={
             ('data_node', 'next_node', 0): {
                 'bin': 0
             },
             ('data_node', 'next_node_2', 0): {
                 'bin': 0
             }
         },
     )
     tested_pattern = CreateConstNodesReplacement()
     tested_pattern.find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph, graph, last_node='next_node')
     self.assertTrue(flag, resp)
Example #12
0
    def test_negative_matmul_infer(self, A_shape, B_shape):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('A_d', {'shape': np.array(A_shape)}),
                                           ('B_d', {'shape': int64_array(B_shape)}),
                                       ])

        node = Node(graph, 'mat_mul')
        self.assertRaises(AssertionError, MatMul.infer, node)
 def test_one_node(self):
     """We should add Const node and data node."""
     shape = np.array([2, 3, 4])
     data = np.zeros(shape)
     graph = build_graph_with_attrs(
         nodes_with_attrs=self.nodes,
         edges_with_attrs=self.edges,
         update_nodes_attributes=[('data_node', {'shape': shape, 'value': data})]
     )
     graph_ref = build_graph_with_attrs(
         nodes_with_attrs=self.nodes + self.new_nodes,
         edges_with_attrs=self.edges + self.new_edges,
         update_nodes_attributes=[('data_node', {'shape': shape, 'value': data}),
                                  ('const_data', {'shape': shape, 'value': data})]
     )
     tested_pattern = CreateConstNodesReplacement()
     tested_pattern.find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph, graph_ref, last_node='next_node')
     self.assertTrue(flag, resp)
Example #14
0
    def test_merge_infer_complex_case(self):
        """
        Case as in cycles when in first visit only one input are inferred and in the second -- both.
        """
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
                                       update_nodes_attributes=[('first', {'is_partial_inferred': False,
                                                                           'value': None}),
                                                                ('second', {'executable': True})])

        # In first visit we should propagate only shapes
        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                           edges_with_attrs=self.edges,
                                           update_nodes_attributes=[('second', {'executable': True}),
                                                                    ('first', {'is_partial_inferred': False,
                                                                               'value': None}),
                                                                    ('merge_output', {'shape': np.array([2, 2]),
                                                                                      'value': None}),
                                                                    ('merge', {'is_not_fully_inferred': True})])
        tested_class = Merge(graph=graph, attrs={})
        node = Node(graph, 'merge')
        tested_class.merge_infer(node)

        (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
        self.assertTrue(flag, resp)

        # Imitate that inputs nodes now is inferred
        graph.node['first']['is_partial_inferred'] = True

        # Run infer second time
        tested_class = Merge(graph=graph, attrs={})
        node = Node(graph, 'merge')
        tested_class.merge_infer(node)

        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                           edges_with_attrs=self.edges,
                                           update_nodes_attributes=[('second', {'executable': True}),
                                                                    ('first', {'is_partial_inferred': True,
                                                                               'value': None}),
                                                                    ('merge_output', {'shape': np.array([2, 2]),
                                                                                      'value': None}),
                                                                    ('merge', {'is_not_fully_inferred': False})])
        (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_two_consumers_keep_outputs(self):
        """Const data node has two consumers: Result and ReLu"""
        nodes = [
            ('const_node', {
                'type': 'Const',
                'kind': 'op'
            }),
            ('const_data', {
                'kind': 'data',
                'value': np.array(5)
            }),
            ('result_node', {
                'type': 'Result',
                'kind': 'op',
                'keep_output_port': True
            }),
            ('relu_1', {
                'type': 'ReLU',
                'kind': 'op',
                'op': 'ReLU'
            }),
            ('relu_1_data', {
                'kind': 'data'
            }),
        ]
        edges = [('const_node', 'const_data'), ('const_data', 'result_node'),
                 ('const_data', 'relu_1'), ('relu_1', 'relu_1_data')]

        graph = build_graph_with_attrs(
            nodes_with_attrs=nodes,
            edges_with_attrs=edges,
        )
        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=nodes,
            edges_with_attrs=edges,
        )
        tested_pattern = RemoveConstToResult()
        tested_pattern.find_and_replace_pattern(graph)
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      last_node='relu_1_data')
        self.assertTrue(flag, resp)
        self.assertIn('result_node', graph.node)
    def test_1(self):
        """
        Acyclic case => graph.graph['is_cyclic'] should be False.
        """
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges)
        tested_pass = AddIsCyclicAttribute()
        tested_pass.find_and_replace_pattern(graph)

        assert graph.graph['is_cyclic'] is False
Example #17
0
    def test_negative(self, input_shape, axes, layout):
        graph = build_graph_with_attrs(nodes + [
            ('input', dict(kind='op', shape=input_shape, op='Parameter', data_type=np.float32)),
            ('input_data', dict(kind='data', shape=input_shape, data_type=np.float32)),
            ('square_data', dict(kind='data', shape=input_shape)),
            ('sum_axes_data', dict(kind='data', value=axes, shape=None)),
        ], edges, nodes_with_edges_only=True)
        graph.stage = 'middle'
        graph.graph['layout'] = layout

        L2NormToNorm().find_and_replace_pattern(graph)

        graph_ref = build_graph_with_attrs(nodes + [
            ('input', dict(kind='op', shape=input_shape, op='Parameter', data_type=np.float32)),
            ('input_data', dict(kind='data', shape=input_shape, data_type=np.float32)),
            ('square_data', dict(kind='data', shape=input_shape)),
            ('sum_axes_data', dict(kind='data', value=axes, shape=None)),
        ], edges, nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #18
0
    def test_select_infer_no_condition(self):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges)

        # We should propagate only shapes
        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                           edges_with_attrs=self.edges,
                                           update_nodes_attributes=[
                                               ('select_output', {
                                                   'shape': np.array([2, 2])
                                               })
                                           ])

        node = Node(graph, 'select')
        node.infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'select_output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_2(self):
        """
        Cyclic case => graph.graph['is_cyclic'] should be True.
        :return:
        """
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       new_edges_with_attrs=[('node_2', 'node_1')])
        tested_pass = AddIsCyclicAttribute()
        tested_pass.find_and_replace_pattern(graph)

        assert graph.graph['is_cyclic'] is True
Example #20
0
    def test_positive(self, input_shape, axes, layout):
        graph = build_graph_with_attrs(nodes + [
            ('input', dict(kind='op', shape=input_shape, op='Parameter', data_type=np.float32)),
            ('input_data', dict(kind='data', shape=input_shape, data_type=np.float32)),
            ('square_data', dict(kind='data', shape=input_shape)),
            ('sum_axes_data', dict(kind='data', value=axes, shape=None)),
        ], edges, nodes_with_edges_only=True)
        graph.stage = 'middle'
        graph.graph['layout'] = layout

        L2NormToNorm().find_and_replace_pattern(graph)

        graph_ref = build_graph_with_attrs(nodes + [
            ('input', dict(kind='op', shape=input_shape, op='Parameter', data_type=np.float32)),
            ('input_data', dict(kind='data', shape=input_shape, data_type=np.float32)),
            ('weights_node_data', dict(kind='data', value=axes.sort())),
        ], edges_after_replacement, nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
        self.assertTrue(graph.node[graph.get_nodes_with_attributes(type='NormalizeL2')[0]]['name'] == 'l2_norm_name')
        self.assertTrue(flag, resp)
 def test_one_bin_node(self):
     """Nothing should happen."""
     shape = np.array([2, 3, 4])
     data = np.zeros(shape)
     graph = build_graph_with_attrs(
         nodes_with_attrs=self.nodes,
         edges_with_attrs=self.edges,
         update_nodes_attributes=[('data_node', {'shape': shape, 'value': data})],
         update_edge_attrs={('data_node', 'next_node', 0): {'bin': 0}},
     )
     tested_pattern = CreateConstNodesReplacement()
     tested_pattern.find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph, graph, last_node='next_node')
     self.assertTrue(flag, resp)
    def test(self):
        pattern_matcher = SimpleInputMatcher()
        pattern = pattern_matcher.pattern()

        graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'],
                                       edges_with_attrs=pattern['edges'],
                                       update_edge_attrs=None,
                                       new_nodes_with_attrs=[('in_node', {
                                           'kind': 'data'
                                       }), ('Enter_data', {
                                           'kind': 'data'
                                       })],
                                       new_edges_with_attrs=[
                                           ('in_node', 'Enter'),
                                           ('Enter', 'Enter_data')
                                       ],
                                       update_nodes_attributes=[])

        pattern_matcher.find_and_replace_pattern(graph)

        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=[('TensorIteratorInput', {
                'kind': 'op',
                'op': 'TensorIteratorInput'
            }), ('in_node', {
                'kind': 'data'
            }), ('Enter_data', {
                'kind': 'data'
            })],
            edges_with_attrs=[('in_node', 'TensorIteratorInput'),
                              ('TensorIteratorInput', 'Enter_data')],
        )
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'Enter_data',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #23
0
    def test_select_infer_assert_shapes(self):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[('else_data', {
                                           'shape':
                                           np.array([3, 3]),
                                           'value':
                                           np.zeros((3, 3))
                                       })])

        tested_class = Select(graph=graph, attrs={})

        node = Node(graph, 'select')
        with self.assertRaisesRegex(AssertionError,
                                    "Input shape do not broadcast"):
            tested_class.infer(node)
Example #24
0
    def test_select_infer_condition_with_value(self, condition_shape,
                                               else_data_shape,
                                               than_data_shape,
                                               select_output_shape,
                                               condition_value, else_value,
                                               than_value, output_value):
        """
        Unit tests generator can sporadic throw exception if we try
        to run generator with call numpy array generation functions.
        So we need to use lambda function for escape the problem.
        """
        condition_value = condition_value(condition_shape)
        else_value = else_value(else_data_shape)
        than_value = than_value(than_data_shape)
        output_value = output_value(select_output_shape)
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('condition_data', {
                                               'shape':
                                               np.array(condition_shape),
                                               'value': condition_value
                                           }),
                                           ('else_data', {
                                               'shape':
                                               np.array(else_data_shape),
                                               'value': else_value
                                           }),
                                           ('than_data', {
                                               'shape':
                                               np.array(than_data_shape),
                                               'value': than_value
                                           }),
                                           ('select_output', {
                                               'shape':
                                               np.array(select_output_shape),
                                               'value':
                                               None
                                           })
                                       ])

        node = Node(graph, 'select')
        node.infer(node)

        self.assertTrue(
            np.array_equal(graph.nodes['select_output']['value'],
                           output_value))
    def test(self):
        pattern_matcher = SmartOutputMatcher()
        pattern = pattern_matcher.pattern()

        graph = build_graph_with_attrs(
            nodes_with_attrs=pattern['nodes'],
            edges_with_attrs=pattern['edges'],
            # update_edge_attrs=None,
            new_nodes_with_attrs=[
                ('index', {
                    'kind': 'data'
                }),
                ('value', {
                    'kind': 'data'
                }),
                ('ta_size', {
                    'kind': 'data'
                }),
            ],
            new_edges_with_attrs=[('index', 'TensorArrayWrite', {
                'in': 1
            }), ('value', 'TensorArrayWrite', {
                'in': 2
            }), ('ta_size', 'TensorArray')],
            update_nodes_attributes=[
                ('WriteEnter_data', {
                    'value': np.array([1, 1])
                }),
                ('start_data', {
                    'value': np.array([0])
                }),
                ('delta_data', {
                    'value': np.array([1])
                }),
            ])

        pattern_matcher.find_and_replace_pattern(graph)

        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=[
                ('TensorIteratorOutput', {
                    'kind': 'op',
                    'op': 'TensorIteratorOutput'
                }),
                ('TensorArrayGather_data', {
                    'kind': 'data'
                }),
                ('index', {
                    'kind': 'data'
                }),
                ('value', {
                    'kind': 'data'
                }),
                ('ta_size', {
                    'kind': 'data'
                }),
            ],
            edges_with_attrs=[('ta_size', 'TensorIteratorOutput', {
                'in': 0
            }), ('index', 'TensorIteratorOutput', {
                'in': 2
            }), ('value', 'TensorIteratorOutput', {
                'in': 1
            }), ('TensorIteratorOutput', 'TensorArrayGather_data')],
            update_edge_attrs=None,
            new_nodes_with_attrs=[],
            new_edges_with_attrs=[],
        )
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'TensorArrayGather_data',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_only_consumer_keep_result(self):
        """Result node is only consumer of Const data node"""
        nodes = [
            ('placeholder_1', {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            }),
            ('placeholder_1_data', {
                'kind': 'data'
            }),
            ('placeholder_2', {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            }),
            ('placeholder_2_data', {
                'kind': 'data'
            }),
            ('shape_of', {
                'type': 'ShapeOf',
                'kind': 'op',
                'op': 'ShapeOf'
            }),
            ('shape_of_data', {
                'kind': 'data'
            }),
            ('split', {
                'type': 'Split',
                'kind': 'op',
                'op': 'Split'
            }),
            ('split_data1', {
                'kind': 'data'
            }),
            ('split_data2', {
                'kind': 'data'
            }),
            ('result_node1', {
                'type': 'Result',
                'kind': 'op',
                'keep_output_port': True
            }),
            ('mul', {
                'type': 'Mul',
                'kind': 'op',
                'op': 'Mul'
            }),
            ('mul_data', {
                'kind': 'data'
            }),
            ('result_node2', {
                'type': 'Result',
                'kind': 'op'
            }),
        ]
        edges = [
            ('placeholder_1', 'placeholder_1_data'),
            ('placeholder_2', 'placeholder_2_data'),
            ('placeholder_1_data', 'shape_of'),
            ('shape_of', 'shape_of_data'),
            ('shape_of_data', 'split'),
            ('split', 'split_data1', {
                'in': 0
            }),
            ('split', 'split_data2', {
                'in': 1
            }),
            ('split_data1', 'result_node1'),
            ('split_data2', 'mul'),
            ('placeholder_2_data', 'mul'),
            ('mul', 'mul_data'),
            ('mul_data', 'result_node2'),
        ]

        graph = build_graph_with_attrs(
            nodes_with_attrs=nodes,
            edges_with_attrs=edges,
        )
        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=nodes,
            edges_with_attrs=edges,
        )
        tested_pattern = RemoveConstToResult()
        tested_pattern.find_and_replace_pattern(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, last_node='mul_data')
        self.assertTrue(flag, resp)
        self.assertIn('split_data1', graph.node)
        self.assertIn('split_data2', graph.node)
        self.assertIn('result_node1', graph.node)
Example #27
0
    def test_select_infer_condition_shapes_broadcast(self, else_data_shape,
                                                     than_data_shape,
                                                     select_output_shape):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes,
                                       edges_with_attrs=self.edges,
                                       update_nodes_attributes=[
                                           ('else_data', {
                                               'shape':
                                               np.array(else_data_shape),
                                               'value':
                                               np.zeros(else_data_shape,
                                                        dtype=np.float)
                                           }),
                                           ('than_data', {
                                               'shape':
                                               np.array(than_data_shape),
                                               'value':
                                               np.zeros(than_data_shape,
                                                        dtype=np.float)
                                           }),
                                           ('select_output', {
                                               'shape':
                                               np.array(select_output_shape),
                                               'value':
                                               np.zeros(select_output_shape,
                                                        dtype=np.float)
                                           })
                                       ])

        # We should propagate shapes and values
        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=self.nodes,
            edges_with_attrs=self.edges,
            update_nodes_attributes=[('else_data', {
                'shape':
                np.array(else_data_shape),
                'value':
                np.zeros(else_data_shape, dtype=np.float)
            }),
                                     ('than_data', {
                                         'shape':
                                         np.array(than_data_shape),
                                         'value':
                                         np.zeros(than_data_shape,
                                                  dtype=np.float)
                                     }),
                                     ('select_output', {
                                         'shape':
                                         np.array(select_output_shape),
                                         'value': np.zeros(select_output_shape)
                                     })])

        tested_class = Select(graph=graph, attrs={})

        node = Node(graph, 'select')
        tested_class.infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'select_output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #28
0
    def test_bn(self):
        bn_pb = FakeBNProtoLayer(FakeParam('eps', 0.0001))
        mean = [1, 2.5, 3]
        var = [0.5, 0.1, 1.2]
        scale = [2.3, 3.4, 4.5]
        shift = [0.8, 0.6, 0.4]
        bn_bin = FakeBNBinLayer([
            FakeParam('data', mean),
            FakeParam('data', var),
            FakeParam('data', scale),
            FakeParam('data', shift)
        ])
        nodes = [
            ('input', {
                'kind': 'op',
                'type': 'Identity',
                'op': 'Identity'
            }),
            ('bn', {
                'type': None,
                'kind': 'op',
                'op': 'BN',
                'pb': bn_pb,
                'model_pb': bn_bin
            }),
            ('output', {
                'kind': 'op',
                'type': 'Identity',
                'op': 'Identity'
            }),
        ]
        edges = [
            ('input', 'bn', {
                'in': 0,
                'out': 0
            }),
            ('bn', 'output', {
                'in': 0,
                'out': 0
            }),
        ]
        graph = build_graph_with_attrs(nodes, edges)
        node = Node(graph, 'bn')
        graph.stage = 'front'

        BNToScaleShift().find_and_replace_pattern(graph)

        ref_nodes = {
            'input': {
                'kind': 'op',
                'type': 'Identity',
                'op': 'Identity'
            },
            'scale': {
                'kind': 'op',
                'type': 'Const',
                'op': 'Const',
                'value': np.array([1.11796412, 3.2272172, 4.74282367])
            },
            'shift': {
                'kind': 'op',
                'type': 'Const',
                'op': 'Const',
                'value': np.array([-2.07131747, -10.87253847, -20.14270653])
            },
            'ss': {
                'type': 'ScaleShift',
                'kind': 'op',
                'op': 'ScaleShift'
            },
            'output': {
                'kind': 'op',
                'type': 'Identity',
                'op': 'Identity'
            },
        }
        ref_edges = [
            ('input', 'ss', {
                'in': 0,
                'out': 0
            }),
            ('scale', 'ss', {
                'in': 1,
                'out': 0
            }),
            ('shift', 'ss', {
                'in': 2,
                'out': 0
            }),
            ('ss', 'output', {
                'in': 0,
                'out': 0
            }),
        ]
        ref_graph = build_graph_with_edge_attrs(ref_nodes, ref_edges)
        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'input',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #29
0
    def test_not_dynamic(self):
        pattern_matcher = LoopConditionMatcher()
        pattern = pattern_matcher.pattern()

        graph = build_graph_with_attrs(
            nodes_with_attrs=pattern['nodes'],
            edges_with_attrs=pattern['edges'],
            new_nodes_with_attrs=[('maximum', {
                'kind': 'op',
                'op': 'Maximum'
            }), ('maximum_data', {
                'kind': 'data'
            }),
                                  ('TensorIteratorInput', {
                                      'kind': 'op',
                                      'op': 'TensorIteratorInput'
                                  })],
            new_edges_with_attrs=[('maximum', 'maximum_data'),
                                  ('Identity_1_data', 'TensorIteratorInput')],
            update_nodes_attributes=[
                ('init_1_data', {
                    'value': np.array([0])
                }),
                ('init_2_data', {
                    'value': np.array([0])
                }),
                ('add_1_y_data', {
                    'value': np.array(1)
                }),
                ('add_2_y_data', {
                    'value': np.array(1)
                }),
                ('loop_cond_data', {
                    'value': None
                }),
                (
                    'Identity_2_data',
                    {
                        'value': None
                    },
                ),
                (
                    'Enter_1_less_data',
                    {
                        'value': None
                    },
                ),
                (
                    'Enter_2_less_data',
                    {
                        'value': None
                    },
                ),
            ])

        pattern_matcher.find_and_replace_pattern(graph)
        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=[('TensorIteratorCondition', {
                'kind': 'op',
                'op': 'TensorIteratorCondition'
            }), ('loop_cond_data', {
                'kind': 'data'
            }), ('identity_data', {
                'kind': 'data'
            }), ('StridedSlice', {
                'kind': 'op',
                'op': 'StridedSlice'
            }), ('StridedSlice_data', {
                'kind': 'data'
            }), ('Maximum', {
                'kind': 'op',
                'op': 'Maximum'
            }), ('Maximum_data', {
                'kind': 'data'
            }), ('minimum_data', {
                'kind': 'data'
            }),
                              ('TensorIteratorInput', {
                                  'kind': 'op',
                                  'op': 'TensorIteratorInput'
                              })],
            edges_with_attrs=[
                ('Maximum', 'Maximum_data'),
                ('StridedSlice', 'StridedSlice_data'),
                ('StridedSlice_data', 'TensorIteratorCondition', {
                    'in': 0
                }),
                ('minimum_data', 'TensorIteratorCondition', {
                    'in': 1
                }),
                ('TensorIteratorCondition', 'loop_cond_data'),
                ('TensorIteratorCondition', 'identity_data'),
                ('identity_data', 'TensorIteratorInput'),
            ],
            update_edge_attrs=None,
            new_nodes_with_attrs=[],
            new_edges_with_attrs=[],
        )
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'loop_cond_data',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Example #30
0
    def test_switch_infer_with_condition(self):
        nodes = [
            ('tensor', {
                'value': np.zeros((3, 3)),
                'kind': 'data',
                'executable': True,
                'shape': np.array([3, 3])
            }),
            ('pred_id', {
                'value': True,
                'kind': 'data',
                'executable': True
            }),
            ('switch', {
                'type': 'Switch',
                'kind': 'op',
                'op': 'Switch',
                'infer': Switch.infer
            }),
            ('switch_data_0', {
                'value': None,
                'kind': 'data',
                'executable': True,
                'shape': None
            }),
            ('switch_data_1', {
                'value': None,
                'kind': 'data',
                'executable': True,
                'shape': None
            }),
            ('result_0', {
                'value': None,
                'kind': 'op',
                'executable': True,
                'type': 'Result',
                'op': 'Result'
            }),
            ('result_1', {
                'value': None,
                'kind': 'op',
                'executable': True,
                'type': 'Result',
                'op': 'Result'
            }),
        ]
        edges = [
            ('tensor', 'switch', {
                'in': 0
            }),
            ('pred_id', 'switch', {
                'in': 1
            }),
            ('switch', 'switch_data_0', {
                'out': 0
            }),
            ('switch', 'switch_data_1', {
                'out': 1
            }),
            ('switch_data_0', 'result_0'),
            ('switch_data_1', 'result_1'),
        ]
        graph = build_graph_with_attrs(nodes_with_attrs=nodes,
                                       edges_with_attrs=edges)

        # We should propagate shapes and values
        graph_ref = build_graph_with_attrs(nodes_with_attrs=nodes,
                                           edges_with_attrs=edges,
                                           update_nodes_attributes=[
                                               ('switch_data_0', {
                                                   'shape': np.array([3, 3]),
                                                   'value': np.zeros((3, 3))
                                               }),
                                               ('switch_data_1', {
                                                   'shape': np.array([3, 3]),
                                                   'value': np.zeros((3, 3))
                                               })
                                           ])

        node = Node(graph, 'switch')
        node.infer(node)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'switch_data_0',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)