Beispiel #1
0
    def test_attributed(self):
        graph = build_graph(nodes_attributes, [
            ('placeholder', 'attr_split', {
                'in': 0,
                'out': 0
            }),
            ('attr_split', 'concat', {
                'in': 0,
                'out': 0
            }),
            ('attr_split', 'concat', {
                'in': 1,
                'out': 1
            }),
            ('concat', 'result', {
                'in': 0,
                'out': 0
            }),
        ],
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes, [
            ('placeholder', 'attr_split', {
                'in': 0,
                'out': 0
            }),
            ('attr_split', 'squeeze1', {
                'in': 0,
                'out': 0
            }),
            ('squeeze1_axis', 'squeeze1', {
                'in': 1,
                'out': 0
            }),
            ('attr_split', 'squeeze2', {
                'in': 0,
                'out': 1
            }),
            ('squeeze2_axis', 'squeeze2', {
                'in': 1,
                'out': 0
            }),
            ('squeeze1', 'concat', {
                'in': 0,
                'out': 0
            }),
            ('squeeze2', 'concat', {
                'in': 1,
                'out': 0
            }),
            ('concat', 'result', {
                'in': 0,
                'out': 0
            }),
        ],
                                nodes_with_edges_only=True)

        graph.graph['layout'] = 'NCHW'
        graph.stage = 'front'

        SqueezeAxis().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #2
0
    def test_embedding_replace1(self):
        graph = build_graph(
            {
                'placeholder_1': {
                    'shape': None,
                    'type': 'Parameter',
                    'kind': 'op',
                    'op': 'Parameter'
                },
                'embedding_const': {
                    'value': None,
                    'shape': None,
                    'kind': 'op',
                    'op': 'Const'
                },
                'embedding': {
                    'type': None,
                    'kind': 'op',
                    'op': 'Embedding'
                },
                'last': {
                    'type': None,
                    'kind': 'op',
                    'op': None
                },
            }, [('placeholder_1', 'embedding', {
                'out': 0,
                'in': 0
            }), ('embedding_const', 'embedding', {
                'out': 0,
                'in': 1
            }), ('embedding', 'last')], {
                'placeholder_1': {
                    'shape': np.array([32, 35])
                },
                'embedding_const': {
                    'shape': np.array([2000, 650]),
                    'bias': np.array(np.random.randint(0, 225, (2000, 650)))
                },
            },
            nodes_with_edges_only=True)

        graph_ref = build_graph(
            {
                'placeholder_1': {
                    'shape': None,
                    'type': 'Parameter',
                    'kind': 'op',
                    'op': 'Parameter'
                },
                'embedding_const': {
                    'value': None,
                    'kind': 'op',
                    'op': 'Const'
                },
                'axis_const': {
                    'value': 0,
                    'kind': 'op',
                    'data_type': None,
                    'type': 'Const',
                    'op': 'Const'
                },
                'embedding': {
                    'kind': 'op',
                    'op': 'Gather'
                },
                'last': {
                    'type': None,
                    'kind': 'op',
                    'op': None
                },
            }, [('embedding_const', 'embedding', {
                'in': 1
            }), ('axis_const', 'embedding', {
                'in': 2
            }), ('placeholder_1', 'embedding', {
                'in': 0
            }), ('embedding', 'last')], {
                'placeholder_1': {
                    'shape': np.array([32, 35])
                },
                'embedding_const': {
                    'shape': np.array([2000, 650]),
                    'bias': np.array(np.random.randint(0, 225, (2000, 650)))
                },
            },
            nodes_with_edges_only=True)

        graph.graph['layout'] = 'NCHW'
        graph.stage = 'front'

        replacer = GatherFrontReplacer()
        replacer.find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'last')
        self.assertTrue(flag, resp)
Beispiel #3
0
    def test_insert_select_1(self):
        graph = build_graph(
            {
                'in_node': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'placeholder_1': {
                    'kind': 'op',
                    'op': None
                },
                'placeholder_data_1': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'splice_1': {
                    'kind': 'op',
                    'op': 'Splice',
                    'context': np.array([-2, -1, 0, 1, 2])
                },
                'splice_data_1': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'placeholder_2': {
                    'kind': 'op',
                    'op': None
                },
                'placeholder_data_2': {
                    'kind': 'data',
                    'shape': [1, 26]
                },
                'memory': {
                    'kind': 'op',
                    'op': 'Memory',
                    'index': 0
                },
            }, [('in_node', 'placeholder_1'),
                ('placeholder_1', 'placeholder_data_1'),
                ('placeholder_data_1', 'splice_1'),
                ('splice_1', 'splice_data_1'),
                ('splice_data_1', 'placeholder_2'),
                ('placeholder_2', 'placeholder_data_2'),
                ('placeholder_data_2', 'memory')],
            nodes_with_edges_only=True)
        AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)
        ref_graph = build_graph(
            {
                'in_node': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'placeholder_1': {
                    'kind': 'op',
                    'op': None
                },
                'placeholder_data_1': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'splice_1': {
                    'kind': 'op',
                    'op': 'Splice',
                    'context': np.array([-2, -1, 0, 1, 2])
                },
                'splice_data_1': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'placeholder_2': {
                    'kind': 'op',
                    'op': None
                },
                'memory_in': {
                    'kind': 'op',
                    'op': 'Memory',
                    'shape': int64_array([5])
                },
                'memory_in_data': {
                    'kind': 'data'
                },
                'memory_out': {
                    'kind': 'op',
                    'op': 'Memory',
                    'shape': int64_array([5])
                },
                'memory_out_data': {
                    'kind': 'data'
                },
                'result': {
                    'kind': 'op',
                    'op': 'Result'
                },
                'crop_in': {
                    'kind': 'op',
                    'op': 'Crop',
                    'axis': 1,
                    'offset': 1,
                    'dim': 4
                },
                'crop_in_data': {
                    'kind': 'data'
                },
                'crop_out': {
                    'kind': 'op',
                    'op': 'Crop',
                    'axis': 1,
                    'offset': 0,
                    'dim': 1
                },
                'crop_out_data': {
                    'kind': 'data'
                },
                'select': {
                    'kind': 'op',
                    'op': 'Select'
                },
                'select_out_data': {
                    'kind': 'data',
                    'shape': [1, 26]
                },
                'const_0': {
                    'kind': 'op',
                    'op': 'Const'
                },
                'const_0_data': {
                    'kind': 'data'
                },
                'const_1': {
                    'kind': 'op',
                    'op': 'Const'
                },
                'const_1_data': {
                    'kind': 'data'
                },
                'concat': {
                    'kind': 'op',
                    'op': 'Concat'
                },
                'concat_data': {
                    'kind': 'data'
                },
                'placeholder_data_2': {
                    'kind': 'data',
                    'shape': [1, 26]
                },
                'memory': {
                    'kind': 'op',
                    'op': 'Memory',
                    'index': 0
                },
            },
            [('in_node', 'placeholder_1'),
             ('placeholder_1', 'placeholder_data_1'),
             ('placeholder_data_1', 'splice_1'), ('splice_1', 'splice_data_1'),
             ('splice_data_1', 'placeholder_2'),
             ('placeholder_2', 'placeholder_data_2'),
             ('placeholder_data_2', 'select', {
                 'in': 1
             }), ('memory_in', 'memory_in_data'),
             ('memory_in_data', 'crop_in'), ('crop_in', 'crop_in_data'),
             ('crop_in_data', 'concat', {
                 'in': 0
             }), ('const_1', 'const_1_data'),
             ('const_1_data', 'concat', {
                 'in': 1
             }), ('concat', 'concat_data'), ('concat_data', 'memory_out'),
             ('memory_out', 'memory_out_data'), ('memory_out_data', 'result'),
             ('concat_data', 'crop_out'), ('crop_out', 'crop_out_data'),
             ('crop_out_data', 'select', {
                 'in': 0
             }), ('const_0', 'const_0_data'),
             ('const_0_data', 'select', {
                 'in': 2
             }), ('select', 'select_out_data'), ('select_out_data', 'memory')],
            nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, ref_graph, 'memory')
        self.assertTrue(flag, resp)
Beispiel #4
0
    def test(self):
        pattern_matcher = SmartInputMatcher()
        pattern = pattern_matcher.pattern()

        graph = build_graph_with_attrs(
            nodes_with_attrs=pattern['nodes'],
            edges_with_attrs=pattern['edges'],
            update_edge_attrs={
                ('range_data', 'TensorArrayScatter', 0): {
                    'in': 1
                },
                ('TensorArray_handle', 'TensorArrayScatter', 0): {
                    'in': 0
                },
                ('TensorArray_flow', 'TensorArrayScatter', 0): {
                    'in': 3
                }
            },
            new_nodes_with_attrs=[
                ('ta_size', {
                    'kind': 'data'
                }),
                ('ta_size_op', {
                    'kind': 'op'
                }),
                ('value', {
                    'kind': 'data'
                }),
            ],
            new_edges_with_attrs=[
                ('ta_size_op', 'ta_size'),
                ('ta_size', 'TensorArray'),
                ('value', 'TensorArrayScatter', {
                    'in': 2
                }),
            ],
            update_nodes_attributes=[('Enter_data', {
                'value': np.array([1])
            }), ('stack_data', {
                'value': np.array([0])
            }), ('stack_1_data', {
                'value': np.array([1])
            }), ('stack_2_data', {
                'value': np.array([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=[('condition_data', {
                'kind': 'data'
            }),
                              ('TensorIteratorInput', {
                                  'kind': 'op',
                                  'op': 'TensorIteratorInput'
                              }), ('TensorArrayRead_data', {
                                  'kind': 'data'
                              }), ('condition_data', {
                                  'kind': 'data'
                              }), ('value', {
                                  'kind': 'data'
                              }), ('ta_size', {
                                  'kind': 'data'
                              }), ('ta_size_op', {
                                  'kind': 'op'
                              })],
            edges_with_attrs=[('ta_size', 'TensorIteratorInput', {
                'in': 0
            }), ('condition_data', 'TensorIteratorInput', {
                'in': 2
            }), ('value', 'TensorIteratorInput', {
                'in': 1
            }), ('TensorIteratorInput', 'TensorArrayRead_data'),
                              ('ta_size_op', 'ta_size')],
            update_edge_attrs=None,
            new_nodes_with_attrs=[],
            new_edges_with_attrs=[],
        )
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'TensorArrayRead_data',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #5
0
    def test_add_input_with_output_port_after_infer(self):
        shape = np.array([1, 2, 3, 4])
        inputs = {'conv_1': [{'shape': shape, 'out': 0}]}
        nodes = {
            'old_input': {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            },
            'inp_data': {
                'kind': 'data',
                'shape': shape + 1
            },
            'conv_1': {
                'type': 'Convolution',
                'kind': 'op',
                'op': 'NotPlaceholder'
            },
            'conv_data': {
                'kind': 'data',
                'shape': shape,
                'value': None
            },
            'relu_1': {
                'type': 'ReLU',
                'kind': 'op',
                'op': 'NotPlaceholder'
            },
        }
        edges = [
            ('old_input', 'inp_data'),
            ('inp_data', 'conv_1'),
            ('conv_1', 'conv_data'),
            ('conv_data', 'relu_1'),
        ]
        graph = build_graph(nodes, edges)
        add_input_ops(graph=graph,
                      user_defined_inputs=inputs,
                      before_infer=False)

        graph_ref = build_graph(
            nodes_attrs={
                'new_input': {
                    'kind': 'op',
                    'op': 'Parameter',
                    'shape': shape
                },
                **nodes
            },
            edges=[
                ('old_input', 'inp_data'),
                ('inp_data', 'conv_1'),
                ('new_input', 'conv_data'),
                ('conv_data', 'relu_1'),
            ],
        )
        # Check that new input is added right (with right ports !)
        (flag, resp) = compare_graphs(graph, graph_ref, last_node='relu_1')
        self.assertTrue(flag, resp)

        # Check that other graph is not damaged
        (flag, resp) = compare_graphs(graph, graph_ref, last_node='conv_1')
        self.assertTrue(flag, resp)

        # Checks for new input and edges
        self.assertTrue('conv_1/placeholder_out_port_0' in graph.nodes())
        new_input = 'conv_1/placeholder_out_port_0'

        self.assertTrue(graph.node[new_input]['is_input'])
        self.assertTrue((new_input, 'conv_data') in graph.edges())
        self.assertTrue(('conv_1', 'conv_data') not in graph.edges())
Beispiel #6
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)
    def test_mega_hardcore(self):
        #   ORIGINAL GRAPH
        #
        #   data1(1,3,64,64)---,->Eltwise1->data(1,3,64,64)-----,->Eltwise2->data(1,3,64,64)---,->Eltwise4->data(1,3,64,64)
        #                     /\                               /\                             /\
        #   data2(64,1)-----,-'--------------------------------'------------------------------'
        #                  \/                                 /
        #   data3(64,1)----`-->Eltwise3->data(64,1)----------'
        #
        #   REFERENCE GRAPH AFTER TRANSFORMATION
        #
        #   data1(1,3,64,64)---------------------,->Eltwise1->data(1,3,64,64)-----,->Eltwise2->data(1,3,64,64)---,->Eltwise4->data(1,3,64,64)
        #                                       /\                               /\                              /\
        #   data2(64,1)-,- Reshape1(1,1,64,64)--'--------------------------------o-------------------------------'
        #               |                                                        |
        #               |                                                Reshape(1,1,64,1)
        #              \/                                                        |
        #   data3(64,1)----------->Eltwise3->data(64,1)--------------------------'
        #
        graph = build_graph(nodes_attributes, [
            ('placeholder_1', 'placeholder_1_data'),
            ('placeholder_2', 'placeholder_2_data'),
            ('placeholder_3', 'placeholder_3_data'),
            ('placeholder_1_data', 'eltwise_1'),
            ('placeholder_2_data', 'eltwise_1'),
            ('eltwise_1', 'eltwise_1_data'),
            ('eltwise_1_data', 'eltwise_2'),
            ('placeholder_2_data', 'eltwise_3'),
            ('placeholder_3_data', 'eltwise_3'),
            ('eltwise_3', 'eltwise_3_data'),
            ('eltwise_3_data', 'eltwise_2'),
            ('eltwise_2', 'eltwise_2_data'),
            ('eltwise_2_data', 'eltwise_4'),
            ('placeholder_2_data', 'eltwise_4'),
            ('eltwise_4', 'eltwise_4_data'),
        ], {
            'placeholder_1_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'placeholder_2_data': {
                'shape': np.array([64, 1]),
                'value': np.ones([64, 1])
            },
            'placeholder_3_data': {
                'shape': np.array([64, 1])
            },
            'eltwise_1_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'eltwise_2_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'eltwise_3_data': {
                'shape': np.array([64, 1])
            },
            'eltwise_4_data': {
                'shape': np.array([1, 3, 64, 64])
            }
        },
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes, [
            ('placeholder_1', 'placeholder_1_data'),
            ('placeholder_2', 'placeholder_2_data'),
            ('placeholder_3', 'placeholder_3_data'),
            ('placeholder_1_data', 'eltwise_1'),
            ('placeholder_2_data', 'reshape_1'),
            ('reshape_1_const', 'reshape_1_const_data'),
            ('reshape_1_const_data', 'reshape_1'),
            ('reshape_1', 'reshape_1_data'),
            ('reshape_1_data', 'eltwise_1'),
            ('eltwise_1', 'eltwise_1_data'),
            ('eltwise_1_data', 'eltwise_2'),
            ('placeholder_2_data', 'eltwise_3'),
            ('placeholder_3_data', 'eltwise_3'),
            ('eltwise_3', 'eltwise_3_data'),
            ('eltwise_3_data', 'reshape_2'),
            ('reshape_2_const', 'reshape_2_const_data'),
            ('reshape_2_const_data', 'reshape_2'),
            ('reshape_2', 'reshape_2_data'),
            ('reshape_2_data', 'eltwise_2'),
            ('eltwise_2', 'eltwise_2_data'),
            ('eltwise_2_data', 'eltwise_4'),
            ('reshape_1_data', 'eltwise_4'),
            ('eltwise_4', 'eltwise_4_data'),
        ], {
            'placeholder_1_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'placeholder_2_data': {
                'shape': np.array([64, 1]),
                'value': np.ones([64, 1])
            },
            'placeholder_3_data': {
                'shape': np.array([64, 1])
            },
            'reshape_1_const': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_1_const_data': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_1_data': {
                'shape': np.array([1, 1, 64, 1])
            },
            'reshape_2_const': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_2_const_data': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_2_data': {
                'shape': np.array([1, 1, 64, 1])
            },
            'eltwise_1_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'eltwise_2_data': {
                'shape': np.array([1, 3, 64, 64])
            },
            'eltwise_3_data': {
                'shape': np.array([64, 1])
            },
            'eltwise_4_data': {
                'shape': np.array([1, 3, 64, 64])
            }
        },
                                nodes_with_edges_only=True)

        normalize_eltwise_inputs(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'eltwise_4',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test1(self):
        nodes_attributes = {
            # nodes from original graph
            'logits': {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            },
            'seq_len': {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            },
            'decoder': {
                'kind': 'op',
                'op': 'CTCGreedyDecoder'
            },
            'cast': {
                'kind': 'op',
                'op': 'Cast'
            },
            'sparse_to_dense': {
                'kind': 'op',
                'op': 'SparseToDense'
            },
            'last': {
                'type': None,
                'value': None,
                'kind': 'op',
                'op': 'Result'
            },

            # new nodes
            'new_decoder': {
                'kind': 'op',
                'op': 'CTCGreedyDecoder',
                'use_mask_format': True
            },
            **const('squeeze_axes', int64_array([2, 3])),
            'squeeze_dec_seq': {
                'kind': 'op',
                'op': 'Squeeze'
            },
            'cast_to_int': {
                'kind': 'op',
                'op': 'Cast'
            },
        }

        graph = build_graph(nodes_attributes, [
            ('logits', 'decoder', {
                'out': 0,
                'in': 0
            }),
            ('seq_len', 'decoder', {
                'out': 0,
                'in': 1
            }),
            ('decoder', 'sparse_to_dense', {
                'out': 0,
                'in': 0
            }),
            ('decoder', 'cast', {
                'out': 1,
                'in': 0
            }),
            ('cast', 'sparse_to_dense', {
                'out': 0
            }),
            ('sparse_to_dense', 'last', {
                'out': 0,
                'in': 0
            }),
        ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        CTCGreedyDecoderReplacement().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attributes, [
            ('logits', 'decoder', {
                'out': 0,
                'in': 0
            }),
            ('seq_len', 'decoder', {
                'out': 0,
                'in': 1
            }),
            ('decoder', 'squeeze_dec_seq', {
                'out': 0,
                'in': 0
            }),
            ('squeeze_axes', 'squeeze_dec_seq', {
                'out': 0,
                'in': 1
            }),
            ('squeeze_dec_seq', 'cast_to_int', {
                'out': 0,
                'in': 0
            }),
            ('cast_to_int', 'last', {
                'out': 0,
                'in': 0
            }),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'last',
                                      check_op_attrs=True)
        self.assertEqual(
            len(graph.get_op_nodes(op='Cast')) == 1
            and graph.get_op_nodes(op='Cast')[0]['name'] == 'sparse_to_dense',
            True,
            'Name is not inherited from original node for CTCGreedyDecoderReplacement'
        )
        self.assertTrue(flag, resp)
Beispiel #9
0
    def test_resnet_optimization_6(self):
        graph = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1_data', 'conv_1'),
                             ('conv_1_w', 'conv_1'),
                             ('conv_1_b', 'conv_1'),
                             ('conv_1', 'conv_1_data'),

                             ('conv_1_data', 'conv_2'),
                             ('conv_2_w', 'conv_2'),
                             ('conv_2_b', 'conv_2'),
                             ('conv_2', 'conv_2_data'),

                             ('conv_2_data', 'conv_3'),
                             ('conv_3_w', 'conv_3'),
                             ('conv_3_b', 'conv_3'),
                             ('conv_3', 'conv_3_data'),

                             ('conv_3_data', 'conv_4'),
                             ('conv_4_w', 'conv_4'),
                             ('conv_4_b', 'conv_4'),
                             ('conv_4', 'conv_4_data'),

                             ],
                            {'placeholder_1_data': {'shape': np.array([1, 3, 224, 224])},

                             'conv_1_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                             'conv_1': {'kernel_spatial': np.array([1, 1]),
                                        'stride': np.array([1, 1, 1, 1]),
                                        'output': np.array([3]), },
                             'conv_1_data': {'shape': np.array([1, 3, 224, 224])},

                             'conv_2_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                             'conv_2': {'kernel_spatial': np.array([1, 1]),
                                        'stride': np.array([1, 1, 2, 2]),
                                        'output': np.array([3]), },
                             'conv_2_data': {'shape': np.array([1, 3, 112, 112])},

                             'conv_3_w': {'value': np.zeros([3, 3, 3, 3]), 'shape': np.array([3, 3, 3, 3])},
                             'conv_3': {'kernel_spatial': np.array([3, 3]),
                                        'stride': np.array([1, 1, 1, 1]),
                                        'output': np.array([3]), },
                             'conv_3_data': {'shape': np.array([1, 3, 110, 110])},

                             'conv_4_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                             'conv_4': {'kernel_spatial': np.array([1, 1]),
                                        'stride': np.array([1, 1, 2, 2]),
                                        'output': np.array([3]), },
                             'conv_4_data': {'shape': np.array([1, 3, 55, 55])},
                             },
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes,
                                [('placeholder_1', 'placeholder_1_data'),
                                 ('placeholder_1_data', 'conv_1'),
                                 ('conv_1_w', 'conv_1'),
                                 ('conv_1_b', 'conv_1'),
                                 ('conv_1', 'conv_1_data'),

                                 ('conv_1_data', 'conv_2'),
                                 ('conv_2_w', 'conv_2'),
                                 ('conv_2_b', 'conv_2'),
                                 ('conv_2', 'conv_2_data'),

                                 ('conv_2_data', 'conv_3'),
                                 ('conv_3_w', 'conv_3'),
                                 ('conv_3_b', 'conv_3'),
                                 ('conv_3', 'conv_3_data'),

                                 ('conv_3_data', 'conv_4'),
                                 ('conv_4_w', 'conv_4'),
                                 ('conv_4_b', 'conv_4'),
                                 ('conv_4', 'conv_4_data'),

                                 ],
                                {'placeholder_1_data': {'shape': np.array([1, 3, 224, 224])},

                                 'conv_1_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                                 'conv_1': {'kernel_spatial': np.array([1, 1]),
                                            'stride': np.array([1, 1, 2, 2]),
                                            'output': np.array([3])},
                                 'conv_1_data': {'shape': np.array([1, 3, 112, 112])},

                                 'conv_2_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                                 'conv_2': {'kernel_spatial': np.array([1, 1]),
                                            'stride': np.array([1, 1, 1, 1]),
                                            'output': np.array([3])},
                                 'conv_2_data': {'shape': np.array([1, 3, 112, 112])},

                                 'conv_3_w': {'value': np.zeros([3, 3, 3, 3]), 'shape': np.array([3, 3, 3, 3])},
                                 'conv_3': {'kernel_spatial': np.array([3, 3]),
                                            'stride': np.array([1, 1, 2, 2]),
                                            'output': np.array([3])},
                                 'conv_3_data': {'shape': np.array([1, 3, 55, 55])},

                                 'conv_4_w': {'value': np.zeros([3, 3, 1, 1]), 'shape': np.array([3, 3, 1, 1])},
                                 'conv_4': {'kernel_spatial': np.array([1, 1]),
                                            'stride': np.array([1, 1, 1, 1]),
                                            'output': np.array([3])},
                                 'conv_4_data': {'shape': np.array([1, 3, 55, 55])},
                                 },
                                nodes_with_edges_only=True)

        graph.graph['layout'] = 'NCHW'
        graph_ref.graph['layout'] = 'NCHW'

        stride_optimization(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'conv_4_data', check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_groupconv_to_conv(self, shape, weights_shape, reshape_shape,
                               group):

        weights_const = np.random.randn(*weights_shape).astype(np.float32)

        nodes_attributes = {
            'input': {
                'kind': 'op',
                'type': 'Parameter'
            },
            'input_data': {
                'shape': shape,
                'kind': 'data'
            },
            'group_conv': {
                'kind': 'op',
                'type': 'GroupConvolution'
            },
            'group_conv_data': {
                'shape': shape,
                'kind': 'data'
            },
            'conv': {
                'kind': 'op',
                'type': 'Convolution',
                'group': group
            },
            'conv_data': {
                'shape': shape,
                'kind': 'data'
            },
            'weights': {
                'kind': 'op',
                'type': 'Const',
                'value': weights_const
            },
            'weights_data': {
                'shape': weights_shape,
                'kind': 'data'
            },
            'reshape': {
                'kind': 'op',
                'type': 'Reshape'
            },
            'reshape_data': {
                'shape': reshape_shape,
                'kind': 'data'
            },
            'reshape_const': {
                'kind': 'op',
                'type': 'Const'
            },
            'reshape_const_data': {
                'shape':
                len(reshape_shape) if reshape_shape is not None else None,
                'kind': 'data'
            },
            'add': {
                'kind': 'op',
                'type': 'Add'
            },
            'add_data': {
                'shape': shape,
                'kind': 'data'
            },
            'add_const': {
                'kind': 'op',
                'type': 'Const'
            },
            'add_const_data': {
                'shape': [1, 32, 1, 1],
                'kind': 'data'
            },
            'result': {
                'kind': 'op',
                'type': 'Result'
            }
        }

        edges = [
            ('input', 'input_data'),
            ('input_data', 'group_conv'),
            ('weights', 'weights_data'),
            ('group_conv', 'group_conv_data'),
            ('group_conv_data', 'add'),
            ('add_const', 'add_const_data'),
            ('add_const_data', 'add'),
            ('add', 'add_data'),
            ('add_data', 'result'),
        ]

        if reshape_shape is not None:

            edges += [('weights_data', 'reshape'),
                      ('reshape_const', 'reshape_const_data'),
                      ('reshape_const_data', 'reshape'),
                      ('reshape', 'reshape_data'),
                      ('reshape_data', 'group_conv')]
        else:
            edges.append(('weights_data', 'group_conv'))

        graph = build_graph(nodes_attributes,
                            edges,
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes, [
            ('input', 'input_data'),
            ('input_data', 'conv'),
            ('weights', 'weights_data'),
            ('weights_data', 'conv'),
            ('conv', 'conv_data'),
            ('conv_data', 'add'),
            ('add_const', 'add_const_data'),
            ('add_const_data', 'add'),
            ('add', 'add_data'),
            ('add_data', 'result'),
        ],
                                nodes_with_edges_only=True)

        for op in graph.get_op_nodes(type='GroupConvolution'):
            groupconv_to_conv(op)

        if reshape_shape is None:
            new_shape = [weights_shape[1] * group, *weights_shape[2:]]
            weights_const = np.reshape(weights_const, new_shape)
            node = Node(graph_ref, 'weights')
            node.value = weights_const

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test2(self):
        nodes_attributes = {
            # nodes from original graph
            'logits': {
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            },
            'transpose': {
                'kind': 'op',
                'op': 'Transpose'
            },
            'shape': {
                'kind': 'op',
                'op': 'ShapeOf'
            },
            'shape_1': {
                'kind': 'op',
                'op': 'ShapeOf'
            },
            'strided_slice': {
                'kind': 'op',
                'op': 'StridedSlice'
            },
            **const('stack', int64_array([1])),
            **const('stack1', int64_array([2])),
            **const('stack2', int64_array([1])),
            'strided_slice_1': {
                'kind': 'op',
                'op': 'StridedSlice'
            },
            **const('stack_1', int64_array([0])),
            **const('stack1_1', int64_array([1])),
            **const('stack2_1', int64_array([1])),
            'dims': {
                'kind': 'op',
                'op': 'Pack'
            },
            'fill': {
                'kind': 'op',
                'op': 'Fill'
            },
            'decoder': {
                'kind': 'op',
                'op': 'CTCGreedyDecoder'
            },
            'cast': {
                'kind': 'op',
                'op': 'Cast'
            },
            'sparse_to_dense': {
                'kind': 'op',
                'op': 'SparseToDense'
            },

            # new nodes
            **const('unsqueeze_batch_size_axis', int64_array(0)),
            'unsqueeze_batch_size': {
                'kind': 'op',
                'op': 'Unsqueeze'
            },
            **const('unsqueeze_time_size_axis', int64_array(0)),
            'unsqueeze_time_size': {
                'kind': 'op',
                'op': 'Unsqueeze'
            },
            'seq_mask_shape': {
                'kind': 'op',
                'op': 'Concat'
            },
            'sequence_mask': {
                'kind': 'op',
                'op': 'Broadcast'
            },
            **const('one', np.array([1.0], dtype=np.float)),
            **const('squeeze_axes', int64_array([2, 3])),
            'squeeze_dec_seq': {
                'kind': 'op',
                'op': 'Squeeze'
            },
            'cast_to_int': {
                'kind': 'op',
                'op': 'Cast'
            },
            'last': {
                'type': None,
                'value': None,
                'kind': 'op',
                'op': 'Result'
            },
        }

        graph = build_graph(nodes_attributes, [
            ('logits', 'transpose', {
                'out': 0
            }),
            ('transpose', 'shape', {
                'out': 0
            }),
            ('transpose', 'shape_1', {
                'out': 0
            }),
            ('transpose', 'decoder', {
                'out': 0,
                'in': 0
            }),
            ('shape', 'strided_slice', {
                'out': 0,
                'in': 0
            }),
            ('stack', 'strided_slice', {
                'out': 0,
                'in': 1
            }),
            ('stack1', 'strided_slice', {
                'out': 0,
                'in': 2
            }),
            ('stack2', 'strided_slice', {
                'out': 0,
                'in': 3
            }),
            ('shape_1', 'strided_slice_1', {
                'out': 0,
                'in': 0
            }),
            ('stack_1', 'strided_slice_1', {
                'out': 0,
                'in': 1
            }),
            ('stack1_1', 'strided_slice_1', {
                'out': 0,
                'in': 2
            }),
            ('stack2_1', 'strided_slice_1', {
                'out': 0,
                'in': 3
            }),
            ('strided_slice', 'dims', {
                'out': 0,
                'in': 0
            }),
            ('dims', 'fill', {
                'out': 0,
                'in': 0
            }),
            ('strided_slice_1', 'fill', {
                'out': 0,
                'in': 1
            }),
            ('fill', 'decoder', {
                'out': 0,
                'in': 1
            }),
            ('decoder', 'sparse_to_dense', {
                'out': 0,
                'in': 0
            }),
            ('decoder', 'cast', {
                'out': 1,
                'in': 0
            }),
            ('cast', 'sparse_to_dense', {
                'out': 0
            }),
            ('sparse_to_dense', 'last', {
                'out': 0,
                'in': 0
            }),
        ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        CTCGreedyDecoderReplacement2().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attributes, [
            ('logits', 'transpose', {
                'out': 0
            }),
            ('transpose', 'shape', {
                'out': 0
            }),
            ('transpose', 'shape_1', {
                'out': 0
            }),
            ('transpose', 'decoder', {
                'out': 0,
                'in': 0
            }),
            ('shape', 'strided_slice', {
                'out': 0,
                'in': 0
            }),
            ('stack', 'strided_slice', {
                'out': 0,
                'in': 1
            }),
            ('stack1', 'strided_slice', {
                'out': 0,
                'in': 2
            }),
            ('stack2', 'strided_slice', {
                'out': 0,
                'in': 3
            }),
            ('shape_1', 'strided_slice_1', {
                'out': 0,
                'in': 0
            }),
            ('stack_1', 'strided_slice_1', {
                'out': 0,
                'in': 1
            }),
            ('stack1_1', 'strided_slice_1', {
                'out': 0,
                'in': 2
            }),
            ('stack2_1', 'strided_slice_1', {
                'out': 0,
                'in': 3
            }),
            ('strided_slice', 'unsqueeze_batch_size', {
                'out': 0,
                'in': 0
            }),
            ('unsqueeze_batch_size_axis', 'unsqueeze_batch_size', {
                'out': 0,
                'in': 1
            }),
            ('strided_slice_1', 'unsqueeze_time_size', {
                'out': 0,
                'in': 0
            }),
            ('unsqueeze_time_size_axis', 'unsqueeze_time_size', {
                'out': 0,
                'in': 1
            }),
            ('unsqueeze_batch_size', 'seq_mask_shape', {
                'out': 0,
                'in': 1
            }),
            ('unsqueeze_time_size', 'seq_mask_shape', {
                'out': 0,
                'in': 0
            }),
            ('one', 'sequence_mask', {
                'out': 0,
                'in': 0
            }),
            ('seq_mask_shape', 'sequence_mask', {
                'out': 0,
                'in': 1
            }),
            ('sequence_mask', 'decoder', {
                'out': 0,
                'in': 1
            }),
            ('decoder', 'squeeze_dec_seq', {
                'out': 0,
                'in': 0
            }),
            ('squeeze_axes', 'squeeze_dec_seq', {
                'out': 0,
                'in': 1
            }),
            ('squeeze_dec_seq', 'cast_to_int', {
                'out': 0,
                'in': 0
            }),
            ('cast_to_int', 'last', {
                'out': 0,
                'in': 0
            }),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'last',
                                      check_op_attrs=True)
        self.assertEqual(
            len(graph.get_op_nodes(op='Cast')) == 1
            and graph.get_op_nodes(op='Cast')[0]['name'] == 'sparse_to_dense',
            True,
            'Name is not inherited from original node for CTCGreedyDecoderReplacement2'
        )
        self.assertTrue(flag, resp)
Beispiel #12
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)
    def test2(self):
        nodes_attributes = {
            'logits': {
                'shape': int64_array([2, 6, 100]),
                'type': 'Parameter',
                'kind': 'op',
                'op': 'Parameter'
            },
            'seq_mask': {
                'shape': int64_array([2]),
                'data_type': np.int32,
                'kind': 'op',
                'op': 'Parameter'
            },
            'transpose': {
                'kind': 'op',
                'op': 'Transpose'
            },
            'ctc_greedy_decoder': {
                'kind': 'op',
                'op': 'CTCGreedyDecoderSeqLen',
                'merge_repeated': True
            },
            'cast': {
                'kind': 'op',
                'op': 'Cast'
            },
            'sparse_to_dense': {
                'kind': 'op',
                'op': 'SparseToDense'
            },
            'tf_ctc_loss': {
                'kind': 'op',
                'op': 'CTCLoss',
                'preprocess_collapse_repeated': False,
                'ctc_merge_repeated': True,
                'unique': False,
                'logits_time_major': False
            },
            'ctc_loss': {
                'kind': 'op',
                'op': 'CTCLoss',
                'preprocess_collapse_repeated': False,
                'ctc_merge_repeated': True,
                'unique': False
            },
            **const('default_value', int64_array(-1)),
            'last': {
                'type': None,
                'value': None,
                'kind': 'op',
                'op': 'Result'
            },
            'transpose2': {
                'kind': 'op',
                'op': 'Transpose'
            },
            **const('transpose2_axis', int64_array([1, 0, 2])),
        }
        graph = build_graph(nodes_attributes, [('logits', 'transpose', {
            'out': 0,
            'in': 0
        }), ('transpose', 'ctc_greedy_decoder', {
            'out': 0,
            'in': 0
        }), ('seq_mask', 'ctc_greedy_decoder', {
            'out': 0,
            'in': 1
        }), ('transpose', 'tf_ctc_loss', {
            'out': 0,
            'in': 0
        }), ('seq_mask', 'tf_ctc_loss', {
            'out': 0,
            'in': 3
        }), ('ctc_greedy_decoder', 'sparse_to_dense', {
            'out': 0,
            'in': 0
        }), ('ctc_greedy_decoder', 'sparse_to_dense', {
            'out': 2,
            'in': 1
        }), ('ctc_greedy_decoder', 'sparse_to_dense', {
            'out': 1,
            'in': 2
        }), ('default_value', 'sparse_to_dense', {
            'out': 0,
            'in': 3
        }), ('ctc_greedy_decoder', 'cast', {
            'out': 1,
            'in': 0
        }), ('ctc_greedy_decoder', 'tf_ctc_loss', {
            'out': 0,
            'in': 1
        }), ('cast', 'tf_ctc_loss', {
            'out': 0,
            'in': 2
        }), ('tf_ctc_loss', 'last', {
            'out': 0,
            'in': 0
        })],
                            nodes_with_edges_only=True)
        graph.graph['cmd_params'] = Namespace(data_type='FP32')
        graph.stage = 'front'
        CTCLossReplacement().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attributes, [('logits', 'transpose', {
            'out': 0,
            'in': 0
        }), ('transpose', 'transpose2', {
            'out': 0,
            'in': 0
        }), ('transpose2_axis', 'transpose2', {
            'out': 0,
            'in': 1
        }), ('transpose2', 'ctc_greedy_decoder', {
            'out': 0,
            'in': 0
        }), ('seq_mask', 'ctc_greedy_decoder', {
            'out': 0,
            'in': 1
        }), ('transpose', 'ctc_loss', {
            'out': 0,
            'in': 0
        }), ('ctc_greedy_decoder', 'ctc_loss', {
            'out': 0,
            'in': 2
        }), ('ctc_greedy_decoder', 'ctc_loss', {
            'out': 1,
            'in': 3
        }), ('seq_mask', 'ctc_loss', {
            'out': 0,
            'in': 1
        }), ('ctc_loss', 'last', {
            'out': 0,
            'in': 0
        })],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'last',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #14
0
    def test2(self):
        """
        Case with non-constant input to init.
        Nothing should happen with graph.
        """
        pattern_matcher = BackEdgeSimpleInputMatcher()
        pattern = pattern_matcher.pattern()

        graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'],
                                       edges_with_attrs=pattern['edges'],
                                       new_nodes_with_attrs=[
                                           ('cycle_data', {
                                               'kind': 'data'
                                           }),
                                           ('condition', {
                                               'kind': 'data'
                                           }),
                                           ('init', {
                                               'kind': 'data',
                                               'shape': np.array([1, 3])
                                           }),
                                           ('Enter', {
                                               'kind': 'op',
                                               'op': 'Enter'
                                           }),
                                       ],
                                       new_edges_with_attrs=[
                                           ('Enter', 'init'),
                                           ('condition', 'BackEdge', {
                                               'in': 2
                                           }), ('init', 'BackEdge', {
                                               'in': 0
                                           }),
                                           ('cycle_data', 'BackEdge', {
                                               'in': 1
                                           })
                                       ])

        pattern_matcher.find_and_replace_pattern(graph)

        graph_ref = build_graph_with_attrs(
            nodes_with_attrs=pattern['nodes'],
            edges_with_attrs=pattern['edges'],
            new_nodes_with_attrs=[
                ('cycle_data', {
                    'kind': 'data'
                }),
                ('condition', {
                    'kind': 'data'
                }),
                ('init', {
                    'kind': 'data',
                    'shape': np.array([1, 3])
                }),
                ('Enter', {
                    'kind': 'op',
                    'op': 'Enter'
                }),
            ],
            new_edges_with_attrs=[('Enter', 'init'),
                                  ('condition', 'BackEdge', {
                                      'in': 2
                                  }), ('init', 'BackEdge', {
                                      'in': 0
                                  }), ('cycle_data', 'BackEdge', {
                                      'in': 1
                                  })],
        )

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'BackEdge',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_tf_all_ports(self):
        graph = build_graph(
            nodes_attributes, [
                ('placeholder_1', 'placeholder_1_data'),
                ('placeholder_2', 'placeholder_2_data'),
                ('placeholder_1_data', 'gathernd'),
                ('placeholder_2_data', 'gathernd'),
                ('gathernd', 'gathernd_data'),
                ('gathernd_data', 'result'),
            ], {
                'placeholder_1_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
                'placeholder_2_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
                'gathernd_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
            })
        graph.graph['fw'] = 'tf'

        graph_ref = build_graph(
            nodes_attributes, [
                ('placeholder_1', 'placeholder_1_data'),
                ('placeholder_2', 'placeholder_2_data'),
                ('placeholder_1_data', 'transpose_1'),
                ('axis_1_const', 'axis_1_const_data'),
                ('axis_1_const_data', 'transpose_1'),
                ('transpose_1', 'transpose_1_data'),
                ('placeholder_2_data', 'transpose_2'),
                ('axis_2_const', 'axis_2_const_data'),
                ('axis_2_const_data', 'transpose_2'),
                ('transpose_2', 'transpose_2_data'),
                ('transpose_1_data', 'gathernd'),
                ('transpose_2_data', 'gathernd'),
                ('gathernd', 'gathernd_data'),
                ('gathernd_data', 'transpose_3'),
                ('axis_3_const', 'axis_3_const_data'),
                ('axis_3_const_data', 'transpose_3'),
                ('transpose_3', 'transpose_3_data'),
                ('transpose_3_data', 'result'),
            ], {
                'placeholder_1_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
                'placeholder_2_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
                'axis_1_const_data': {
                    'value': int64_array([0, 2, 3, 1])
                },
                'axis_2_const_data': {
                    'value': int64_array([0, 2, 3, 1])
                },
                'gathernd_data': {
                    'shape': np.array([1, 3, 224, 224])
                },
                'axis_3_const_data': {
                    'value': int64_array([0, 3, 1, 2])
                },
            })

        pattern = LayoutChangeForGatherND()
        pattern.find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #16
0
 def test_spatial_2d_split_concat_2(self):
     graph = build_graph(nodes_attrs=graph_node_attrs_for_2d_spatial_case,
                         edges=graph_edges,
                         update_attributes={
                             'split': {
                                 'type': 'Split',
                                 'kind': 'op',
                                 'op': 'Split',
                                 'num_splits': 3
                             },
                             'split_axis_const': {
                                 'kind': 'op',
                                 'value': np.array(2, dtype=np.int64),
                                 'op': 'Const',
                                 'type': 'Const'
                             },
                             'split_axis_const_data': {
                                 'value': np.array(2, dtype=np.int64),
                                 'shape': np.array(2, dtype=np.int64).shape,
                                 'kind': 'data'
                             },
                             'concat': {
                                 'type': 'Concat',
                                 'kind': 'op',
                                 'axis': 2
                             },
                             'split_data_0': {
                                 'value': None,
                                 'shape': int64_array([1, 100, 40, 150]),
                                 'kind': 'data'
                             },
                             'split_data_1': {
                                 'value': None,
                                 'shape': int64_array([1, 100, 40, 150]),
                                 'kind': 'data'
                             },
                             'split_data_2': {
                                 'value': None,
                                 'shape': int64_array([1, 100, 40, 150]),
                                 'kind': 'data'
                             },
                             'concat_data': {
                                 'value': None,
                                 'shape': int64_array([1, 100, 240, 150]),
                                 'kind': 'data'
                             },
                             'abs_data': {
                                 'value': None,
                                 'shape': int64_array([1, 100, 240, 150]),
                                 'kind': 'data'
                             },
                         })
     ref_graph = build_graph(
         nodes_attrs=ref_graph_node_attrs_for_2d_spatial_case_2,
         edges=ref_graph_edges_opset4,
         update_attributes={
             'axes': {
                 'shape': int64_array([1]),
                 'value': int64_array([2])
             }
         })
     SplitConcatPairToInterpolate().find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph, ref_graph, 'output')
     self.assertTrue(flag, resp)
    def test_remove_memory(self):
        """Memory should be replaced by input and output"""
        graph = build_graph(nodes_attrs={
            'input': {
                'kind': 'op'
            },
            'data_in': {
                'kind': 'data',
                'shape': None,
                'value': None
            },
            'memory_in': {
                'kind': 'op',
                'op': 'Memory',
                'index': 1,
                'id': 'memory_',
                'in_ports_count': 1
            },
            'data_mem': {
                'kind': 'data',
                'shape': None,
                'value': None
            },
            'concat': {
                'kind': 'op',
                'op': 'Concat',
                'axis': 0
            },
            'concat_data': {
                'kind': 'data',
                'shape': None,
                'value': None
            },
            'some_op': {
                'kind': 'op'
            },
            'some_op_data': {
                'kind': 'data',
                'shape': None,
                'value': None
            },
            'memory_out': {
                'kind': 'op',
                'op': 'Memory',
                'index': 0,
                'id': 'memory_'
            },
            'data_mem_out': {
                'kind': 'data',
                'shape': None,
                'value': None
            },
            'mem_out_result': {
                'kind': 'op',
                'op': 'Result'
            }
        },
                            edges=[('input', 'data_in'),
                                   ('memory_in', 'data_mem'),
                                   ('data_in', 'concat', {
                                       'in': 0
                                   }), ('data_mem', 'concat', {
                                       'in': 1
                                   }), ('concat', 'concat_data'),
                                   ('concat_data', 'some_op'),
                                   ('some_op', 'some_op_data'),
                                   ('some_op_data', 'memory_out'),
                                   ('memory_out', 'data_mem_out'),
                                   ('data_mem_out', 'mem_out_result')])
        graph_ref = build_graph(
            nodes_attrs={
                'input': {
                    'kind': 'op'
                },
                'data_in': {
                    'kind': 'data',
                    'shape': None,
                    'value': None
                },
                'new_input': {
                    'kind': 'op',
                    'op': 'Parameter'
                },
                'new_in_data': {
                    'kind': 'data',
                    'shape': None,
                    'value': None
                },
                'concat': {
                    'kind': 'op',
                    'op': 'Concat',
                    'axis': 0
                },
                'concat_data': {
                    'kind': 'data',
                    'shape': None,
                    'value': None
                },
                'some_op': {
                    'kind': 'op'
                },
                'some_op_data': {
                    'kind': 'data',
                    'shape': None,
                    'value': None
                },
                'crop': {
                    'kind': 'op',
                    'op': 'Crop',
                    'axis': np.array([0])
                },
                'crop_data': {
                    'kind': 'data',
                    'shape': None,
                    'value': None
                },
                'mem_out_result': {
                    'kind': 'op',
                    'op': 'Result'
                },
            },
            edges=[('input', 'data_in'), ('new_input', 'new_in_data'),
                   ('data_in', 'concat', {
                       'in': 0
                   }), ('new_in_data', 'concat', {
                       'in': 1
                   }), ('concat', 'concat_data'), ('concat_data', 'some_op'),
                   ('some_op', 'some_op_data'), ('some_op_data', 'crop'),
                   ('crop', 'crop_data'), ('crop_data', 'mem_out_result')],
        )
        CutMemory().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      last_node='mem_out_result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #18
0
    def test_memoryoffset_neg_0(self):
        graph = build_graph(self.nodes_attributes,
                            [('in_placeholder', 'in_node'),
                             ('in_node', 'memoryoffset'),
                             ('memoryoffset', 'memoryoffset_data'),
                             ('memoryoffset_data', 'opoutput'),
                             ('memoryoffset_2', 'memoryoffset_2_data'),
                             ('memoryoffset_2_data', 'out_placeholder'),
                             ('in_node', 'out_placeholder')])
        memoryoffset_node = Node(graph, 'memoryoffset')
        memoryoffset_node['t'] = -5
        ReplaceMemoryOffsetNodePattern().find_and_replace_pattern(graph)
        ref_graph = build_graph(
            {
                'in_placeholder': {
                    'kind': 'op',
                    'op': 'placeholder'
                },
                'in_node': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'splice': {
                    'kind': 'op',
                    'op': 'Splice',
                    'context': range(-5, 1)
                },
                'splice_data': {
                    'kind': 'data',
                    'shape': [1, 78]
                },
                'crop': {
                    'kind': 'op',
                    'op': 'Crop',
                    'offset': 0,
                    'dim': 13
                },
                'crop_input': {
                    'kind': 'op',
                    'op': 'Crop',
                    'offset': 65,
                    'dim': 13
                },
                'crop_input_data': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'memoryoffset_2_data': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'out_placeholder': {
                    'kind': 'op',
                    'op': 'placeholder'
                },
            }, [('in_placeholder', 'in_node'), ('in_node', 'splice'),
                ('splice', 'splice_data'), ('splice_data', 'crop'),
                ('crop', 'memoryoffset_2_data'), ('splice_data', 'crop_input'),
                ('crop_input', 'crop_input_data'),
                ('memoryoffset_2_data', 'out_placeholder'),
                ('crop_input_data', 'out_placeholder')])

        (flag, resp) = compare_graphs(graph, ref_graph, 'out_placeholder')
        self.assertTrue(flag, resp)
    def test1_not_constant(self):
        #
        #   data1(1,3,64,64)----.                                                   data(1,3,64,64)-------.
        #   data2(1,64,1)-------->Eltwise-->data(1,3,64,64)   =>    data(1,64,1)->Reshape->data(1,1,64,1)-->Eltwise->...
        #   data3(64,1)------'                                       data(64,1)->Reshape->data(1,1,64,1)-'
        #
        graph = build_graph(nodes_attributes,
                            [('placeholder_1', 'placeholder_1_data'),
                             ('placeholder_1', 'placeholder_2_data'),
                             ('placeholder_1', 'placeholder_3_data'),
                             ('placeholder_1_data', 'eltwise_1'),
                             ('placeholder_2_data', 'eltwise_1'),
                             ('placeholder_3_data', 'eltwise_1'),
                             ('eltwise_1', 'eltwise_1_data')], {
                                 'placeholder_1_data': {
                                     'shape': np.array([1, 3, 64, 64])
                                 },
                                 'placeholder_2_data': {
                                     'shape': np.array([1, 64, 1])
                                 },
                                 'placeholder_3_data': {
                                     'shape': np.array([64, 1])
                                 },
                                 'eltwise_1_data': {
                                     'shape': np.array([1, 3, 64, 64])
                                 }
                             },
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes,
                                [('placeholder_1', 'placeholder_1_data'),
                                 ('placeholder_1', 'placeholder_2_data'),
                                 ('placeholder_1', 'placeholder_3_data'),
                                 ('placeholder_1_data', 'eltwise_1'),
                                 ('placeholder_2_data', 'reshape_1'),
                                 ('reshape_1_const', 'reshape_1_const_data'),
                                 ('reshape_1_const_data', 'reshape_1'),
                                 ('placeholder_3_data', 'reshape_2'),
                                 ('reshape_2_const', 'reshape_2_const_data'),
                                 ('reshape_2_const_data', 'reshape_2'),
                                 ('reshape_1', 'reshape_1_data'),
                                 ('reshape_2', 'reshape_2_data'),
                                 ('reshape_1_data', 'eltwise_1'),
                                 ('reshape_2_data', 'eltwise_1'),
                                 ('eltwise_1', 'eltwise_1_data')], {
                                     'placeholder_1_data': {
                                         'shape': np.array([1, 3, 64, 64])
                                     },
                                     'reshape_1_const': {
                                         'value': int64_array([0]),
                                         'shape': int64_array([1])
                                     },
                                     'reshape_1_const_data': {
                                         'value': int64_array([0]),
                                         'shape': int64_array([1])
                                     },
                                     'reshape_1_data': {
                                         'shape': np.array([1, 1, 64, 1])
                                     },
                                     'reshape_2_const': {
                                         'value': int64_array([0, 1]),
                                         'shape': int64_array([2])
                                     },
                                     'reshape_2_const_data': {
                                         'value': int64_array([0, 1]),
                                         'shape': int64_array([2])
                                     },
                                     'reshape_2_data': {
                                         'shape': np.array([1, 1, 64, 1])
                                     },
                                     'eltwise_1_data': {
                                         'shape': np.array([1, 3, 64, 64])
                                     }
                                 },
                                nodes_with_edges_only=True)

        normalize_eltwise_inputs(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'eltwise_1',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #20
0
    def test_quantize_uint8(self):
        graph = build_graph(nodes1_attributes, [
            ('input', 'quantize'),
            ('scale_param_q', 'quantize'),
            ('zerop_param_q', 'quantize'),
            ('quantize', 'out'),
        ], {
            'scale_param_q': {
                'shape': np.array([]),
                'value': np.float32(1.0 / 255)
            },
            'zerop_param_q': {
                'shape': np.array([]),
                'value': np.uint8(128)
            },
        },
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_ref_attributes, [
            ('input', 'f_quantize'),
            ('scale_param_q', 'mul1', {
                'out': 0
            }),
            ('in_low', 'mul1'),
            ('mul1', 'f_quantize'),
            ('scale_param_q', 'mul2', {
                'out': 0
            }),
            ('in_high', 'mul2'),
            ('mul2', 'f_quantize'),
            ('out_low', 'f_quantize'),
            ('out_high', 'f_quantize'),
            ('f_quantize', 'cast'),
            ('cast', 'out'),
        ], {
            'in_low': {
                'shape': np.array([]),
                'value': -128
            },
            'in_high': {
                'shape': np.array([]),
                'value': 127
            },
            'out_low': {
                'shape': np.array([]),
                'value': 0
            },
            'out_high': {
                'shape': np.array([]),
                'value': 255
            },
            'cast': {
                'dst_type': np.uint8
            }
        },
                                nodes_with_edges_only=True)

        graph.stage = 'front'
        QuantizeLinearResolver().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'out',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test4_constant(self):
        #        ,--------------->consumer3                 ,------------>consumer3
        #   data---(new_shape1)-->consumer1      =>    data--->reshape1-->consumer1
        #        `-(new_shape2)-->consumer2                 `->reshape2-->consumer2
        #
        graph = build_graph(nodes_attributes, [
            ('placeholder_1', 'placeholder_1_data'),
            ('placeholder_1_data', 'eltwise_1'),
            ('placeholder_1_data', 'eltwise_2'),
            ('placeholder_1_data', 'eltwise_3'),
            ('eltwise_1', 'eltwise_1_data'),
            ('eltwise_2', 'eltwise_2_data'),
            ('eltwise_3', 'eltwise_3_data'),
            ('eltwise_1_data', 'concat'),
            ('eltwise_2_data', 'concat'),
            ('eltwise_3_data', 'concat'),
        ], {
            'placeholder_1_data': {
                'shape': int64_array([1, 3]),
                'value': np.ones([1, 3])
            },
            'eltwise_1_data': {
                'shape': int64_array([1, 1, 1, 3])
            },
            'eltwise_2_data': {
                'shape': int64_array([1, 1, 3])
            },
            'eltwise_3_data': {
                'shape': int64_array([1, 3])
            },
        },
                            nodes_with_edges_only=True)

        graph_ref = build_graph(nodes_attributes, [
            ('placeholder_1', 'placeholder_1_data'),
            ('placeholder_1_data', 'reshape_1'),
            ('reshape_1_const', 'reshape_1_const_data'),
            ('reshape_1_const_data', 'reshape_1'),
            ('reshape_1', 'reshape_1_data'),
            ('reshape_1_data', 'eltwise_1'),
            ('placeholder_1_data', 'reshape_2'),
            ('reshape_2_const', 'reshape_2_const_data'),
            ('reshape_2_const_data', 'reshape_2'),
            ('reshape_2', 'reshape_2_data'),
            ('reshape_2_data', 'eltwise_2'),
            ('placeholder_1_data', 'eltwise_3'),
            ('eltwise_1', 'eltwise_1_data'),
            ('eltwise_2', 'eltwise_2_data'),
            ('eltwise_3', 'eltwise_3_data'),
            ('eltwise_1_data', 'concat'),
            ('eltwise_2_data', 'concat'),
            ('eltwise_3_data', 'concat'),
        ], {
            'placeholder_1_data': {
                'shape': int64_array([1, 3]),
                'value': np.ones([1, 3])
            },
            'reshape_1_const': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_1_const_data': {
                'value': int64_array([0, 1]),
                'shape': int64_array([2])
            },
            'reshape_1_data': {
                'shape': int64_array([1, 1, 1, 3])
            },
            'reshape_2_const': {
                'value': int64_array([0]),
                'shape': int64_array([1])
            },
            'reshape_2_const_data': {
                'value': int64_array([0]),
                'shape': int64_array([1])
            },
            'reshape_2_data': {
                'shape': int64_array([1, 1, 3])
            },
        },
                                nodes_with_edges_only=True)

        normalize_eltwise_inputs(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'concat',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_tdnnreplacer(self, weights, biases, time_offsets):
        def generate_offsets():
            offset_edges = []
            offset_nodes = {}

            for i, t in enumerate(time_offsets):
                offset_nodes.update(**regular_op('memoryoffset_' +
                                                 str(i), {'type': None}))

                if t != 0:
                    offset_edges.append(
                        ('placeholder', 'memoryoffset_' + str(i), {
                            'out': 0,
                            'in': 0
                        }))
                    offset_edges.append(('memoryoffset_' + str(i), 'concat', {
                        'out': 0,
                        'in': i
                    }))
                else:
                    offset_edges.append(('placeholder', 'concat', {
                        'out': 0,
                        'in': i
                    }))

            return offset_nodes, offset_edges

        offset_nodes, ref_offset_edges = generate_offsets()

        nodes = {
            **offset_nodes,
            **regular_op('placeholder', {'type': 'Parameter'}),
            **regular_op(
                'tdnncomponent', {
                    'op': 'tdnncomponent',
                    'weights': np.array(weights),
                    'biases': np.array(biases),
                    'time_offsets': np.array(time_offsets)
                }),
            **const('weights', np.array(weights)),
            **const('biases', np.array(biases)),
            **regular_op('concat', {
                'type': 'Concat',
                'axis': 1
            }),
            **regular_op('memoryoffset_0', {'type': None}),
            **regular_op('memoryoffset_1', {'type': None}),
            **regular_op('memoryoffset_2', {'type': None}),
            **regular_op('fully_connected', {'type': 'FullyConnected'}),
            **result('result'),
        }

        graph = build_graph(nodes, [
            *connect_front('placeholder', 'tdnncomponent'),
            *connect_front('tdnncomponent', 'result')
        ],
                            nodes_with_edges_only=True)

        graph.stage = 'front'

        ref_graph = build_graph(nodes, [
            *ref_offset_edges, *connect_front('concat', '0:fully_connected'),
            *connect_front('weights', '1:fully_connected'),
            *connect_front('biases', '2:fully_connected'),
            *connect_front('fully_connected', 'result')
        ],
                                nodes_with_edges_only=True)

        TdnnComponentReplacer().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)