def test_reshape_on_the_A_input(self, in1_shape, in2_shape,
                                    reshape_pattern, transpose_a, transpose_b,
                                    updated_pattern):
        nodes = {
            **regular_op_with_shaped_data(
                'in_1', in1_shape, dict(type='Parameter', op='Parameter')),
            **regular_op_with_shaped_data(
                'in_2', in2_shape, dict(type='Parameter', op='Parameter')),
            **const_with_data('dim', int64_array(reshape_pattern)),
            **op_with_empty_data(
                'reshape',
                dict(type='Reshape',
                     op='Reshape',
                     infer=Reshape.infer,
                     need_shape_inference=True)),
            **op_with_empty_data(
                'matmul',
                dict(type='MatMul',
                     op='MatMul',
                     infer=MatMul.infer,
                     need_shape_inference=True,
                     transpose_a=transpose_a,
                     transpose_b=transpose_b,
                     dim_attrs={})),
            **result(),
        }
        edges = [
            *connect('in_1:0', '0:reshape'),
            *connect('dim:0', '1:reshape'),
            *connect('reshape:0', '0:matmul'),
            *connect('in_2:0', '1:matmul'),
            *connect('matmul:0', 'output'),
        ]
        graph = build_graph(nodes_attrs=nodes,
                            edges=edges,
                            cli=Namespace(static_shape=True))
        graph.clean_up()
        SmartReshape_HC_Reshape_MatMul().find_and_replace_pattern(graph)
        graph.clean_up()

        graph_ref = build_graph(nodes_attrs=nodes,
                                edges=edges,
                                update_attributes={
                                    'dim': {
                                        'value': int64_array(updated_pattern)
                                    },
                                    'dim_d': {
                                        'value': int64_array(updated_pattern)
                                    }
                                })
        graph_ref.clean_up()

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
示例#2
0
    def test_multi(self):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('some_op', {'type': 'SomeOp', 'name': 'some_op_name'}),
            **empty_data('some_op_d2'),
            **regular_op_with_empty_data('fake_output1',
                                         {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name1'}),
            **regular_op_with_empty_data('fake_output2',
                                         {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name2'}),

            **const_with_data('const1', int64_array(0)),
            **const_with_data('const2', int64_array(0)),
            **regular_op_with_empty_data('add1', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name1'}),
            **regular_op_with_empty_data('add2', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name2'}),
            **result('result1'),
            **result('result2'),
        }
        edges = [*connect('input', 'some_op'),
                 *connect('some_op', 'fake_output1'),
                 ('some_op', 'some_op_d2'),
                 ('some_op_d2', 'fake_output2'),
                 *connect('fake_output1', 'result1'),
                 *connect('fake_output2', 'result2'),
                 ]
        graph = build_graph(nodes, edges)

        edges_ref = [*connect('input', 'some_op'),
                     *connect('some_op', '0:add1'),
                     *connect('const1', '1:add1'),
                     ('some_op', 'some_op_d2'),
                     ('some_op_d2', 'add2', {'in': 0}),
                     *connect('const2', '1:add2'),
                     *connect('add1', 'result1'),
                     *connect('add2', 'result2'),
                     ]

        graph_ref = build_graph(nodes, edges_ref)

        FakeOutputResolver().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result1')
        self.assertTrue(flag, resp)
示例#3
0
def generate_nodes(data, axis=-1, depth=4, on_value=1., off_value=0.):
    return {
        'indices': {
            'Op': 'Parameter',
            'value': data,
            'shape': int64_array(data.shape)
        },
        'indices_d': {
            'kind': 'data',
            'value': data,
            'shape': int64_array(data.shape)
        },
        **const_with_data('depth', int64_array(depth)),
        **const_with_data('on_value', float_array(on_value)),
        **const_with_data('off_value', float_array(off_value)),
        **regular_op_with_shaped_data('one_hot', None, {
            'type': 'OneHot',
            'axis': axis,
            'Op': 'OneHot'
        })
    }
    def test_v10_group_convolution_resolver(self):
        nodes = {
            **regular_op_with_shaped_data('input', [1, 3, 224, 224], {
                                              'type': 'Parameter'
                                          }),
            **valued_const_with_data('weights', np.ones([3, 8, 7, 7])),
            **const_with_data('dim', int64_array([3, 8, 1, 7, 7])),
            **regular_op_with_empty_data('reshape', {'type': 'Reshape'}),
            **regular_op_with_shaped_data('convolution', None, {
                'type': 'Convolution',
                'group': 3,
                'output': 24
            }),
            **result(),
        }
        graph = build_graph(nodes, [
            *connect('input', '0:convolution'),
            *connect('weights', '1:convolution'),
            *connect('convolution', 'output'),
        ],
                            nodes_with_edges_only=True)

        V10ConvolutionWithGroupsResolver().find_and_replace_pattern(graph)

        nodes['convolution']['type'] = 'GroupConvolution'
        del nodes['convolution']['group']

        graph_ref = build_graph(nodes, [
            *connect('input', '0:convolution'),
            *connect('weights', '0:reshape'),
            *connect('dim', '1:reshape'),
            *connect('reshape', '1:convolution'),
            *connect('convolution', 'output'),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      last_node='output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
def graph_template(weights_initial_shape,
                   new_reshape_shape,
                   limits_initial_shape,
                   limits_new_shape=None):
    limits_new_shape = limits_initial_shape if limits_new_shape is None else limits_new_shape

    core_connections = [
        *connect('input:0', '0:convolution'),
        *connect('convolution:0', '0:output'),
    ]

    core_nodes = lambda weights_shape, limit_shape, reshape_shape: {
        **regular_op_with_shaped_data('input', None, {
            'type': 'Parameter',
            'op': 'Parameter'
        }),
        **valued_const_with_data('weights', np.ones(weights_shape)),
        **const_with_data('dim', int64_array(reshape_shape)),
        **regular_op_with_shaped_data('reshape', reshape_shape, {
            'type': 'Reshape',
            'infer': Reshape.infer,
            'op': 'Reshape'
        }),
        **valued_const_with_data('il', np.ones(limit_shape)),
        **valued_const_with_data('ih', np.ones(limit_shape)),
        **valued_const_with_data('ol', np.ones(limit_shape)),
        **valued_const_with_data('oh', np.ones(limit_shape)),
        **regular_op_with_shaped_data(
            'FQ', weights_shape, {
                'type': 'FakeQuantize',
                'infer': FakeQuantize.infer,
                'stop_value_propagation': True,
                'levels': 2,
                'op': 'FakeQuantize'
            }),
        **regular_op_with_shaped_data('convolution', None, {
            'type': 'Convolution',
            'op': 'Convolution'
        }),
        **result(),
    }

    nodes_before = core_nodes(weights_initial_shape, limits_initial_shape,
                              new_reshape_shape)
    edges_before = [
        *connect('weights:0', '0:FQ'),
        *connect('il:0', '1:FQ'),
        *connect('ih:0', '2:FQ'),
        *connect('ol:0', '3:FQ'),
        *connect('oh:0', '4:FQ'),
        *connect('FQ:0', '0:reshape'),
        *connect('dim:0', '1:reshape'),
        *connect('reshape:0', '1:convolution'),
        *core_connections,
    ]
    graph = build_graph(nodes_attrs=nodes_before,
                        edges=edges_before,
                        nodes_with_edges_only=True)

    nodes_after = core_nodes(new_reshape_shape, limits_new_shape, [])
    edges_after = [
        *connect('weights:0', '0:FQ'),
        *connect('il:0', '1:FQ'),
        *connect('ih:0', '2:FQ'),
        *connect('ol:0', '3:FQ'),
        *connect('oh:0', '4:FQ'),
        *connect('FQ:0', '1:convolution'),
        *core_connections,
    ]
    graph_ref = build_graph(nodes_attrs=nodes_after,
                            edges=edges_after,
                            nodes_with_edges_only=True)
    return graph, graph_ref