Exemple #1
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)
Exemple #2
0
    def test_select_infer_condition_with_value(self, else_data_shape, than_data_shape, select_output_shape,
                                               condition_value, else_value, than_value, output_value):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
                                       update_nodes_attributes=[('condition_data', {'shape': np.array(select_output_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})
                                                                ])

        graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
                                           update_nodes_attributes=[
                                               ('condition_data', {'shape': np.array(else_data_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': output_value})])

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

        if else_value is not None and than_value is not None:
            (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
            self.assertTrue(flag, resp)
        self.assertTrue(np.array_equal(graph.nodes['select_output']['value'], graph_ref.nodes['select_output']['value']))
Exemple #3
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)
Exemple #4
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)
Exemple #5
0
    def build_select_graph_and_infer(condition_value,
                                     then_value,
                                     else_value,
                                     out_value,
                                     condition_shape=None,
                                     then_shape=None,
                                     else_shape=None,
                                     out_shape=None,
                                     auto_broadcast='numpy',
                                     fw_format=None):
        if then_value is not None:
            then_shape = int64_array(then_value.shape)
        if else_value is not None:
            else_shape = int64_array(else_value.shape)

        nodes = {
            **valued_const_with_data('then', then_value, then_shape),
            **valued_const_with_data('else', else_value, else_shape),
            **valued_const_with_data('condition', condition_value, condition_shape),
            **regular_op_with_empty_data(
                'select', {
                    'op': 'Select',
                    'auto_broadcast': auto_broadcast,
                    'format': fw_format
                }),
            **result('out'),
        }
        edges = [
            *connect('condition', '0:select'),
            *connect('then', '1:select'),
            *connect('else', '2:select'),
            *connect('select', 'out'),
        ]
        graph = build_graph(nodes, edges)

        select_node = Node(graph, 'select')
        Select.infer(select_node)

        select_out_node = Node(graph, 'select_d')

        value_desc = 'values'
        ref_val = out_value
        actual_val = select_out_node['value']
        if out_shape is not None:
            value_desc = 'shapes'
            ref_val = out_shape
            actual_val = select_out_node['shape']
            assert select_out_node[
                'value'] is None, "if 'out_shape' is defined manually 'value' must be None"

        flag = strict_compare_tensors(actual_val, ref_val)
        msg = '' if flag else 'reference {} and actual {} {} do not match\n'.format(
            ref_val, actual_val, value_desc)
        return flag, msg
Exemple #6
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', {'shape': np.array([3,3]), 'value':np.zeros((3,3))})])

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

        node = Node(graph, 'select')
        with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
                                                    " \'then\' and \'else\' tensors.\'then\' and \'else\' tensors"
                                                    " must have the same shape by TensorFlow reference"):
            tested_class.infer(node)
Exemple #7
0
    def test_select_infer_assert_condition_bool(self):
        graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
                                       update_nodes_attributes=[('condition', {'value': np.array([3])})])

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

        node = Node(graph, 'select')
        with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
                                                    " \'then\' and \'else\' tensors. Value of \'condition\' tensor"
                                                    " must be boolen by TensorFlow reference"):
            tested_class.infer(node)
Exemple #8
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
                                           })
                                       ])

        graph_ref = 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': output_value
                                     })])

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

        if else_value is not None and than_value is not None:
            (flag, resp) = compare_graphs(graph,
                                          graph_ref,
                                          'select_output',
                                          check_op_attrs=True)
            self.assertTrue(flag, resp)
        self.assertTrue(
            np.array_equal(graph.nodes['select_output']['value'],
                           graph_ref.nodes['select_output']['value']))