def check_shape_infer(self, data_shape, indices_shape, axis, ref):
        nodes = {
            **shaped_parameter('data', data_shape),
            **shaped_parameter('indices', indices_shape),
            **regular_op_with_empty_data('gather_elements', {
                'op': 'GatherElements',
                'axis': axis
            }),
            **result()
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect('data', '0:gather_elements'),
                                *connect('indices', '1:gather_elements'),
                                *connect('gather_elements', 'output')
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'middle'

        gather_el_node = Node(graph, 'gather_elements')
        GatherElements.infer(gather_el_node)

        res_output_shape = gather_el_node.out_node().shape
        self.assertTrue(strict_compare_tensors(res_output_shape, ref))
示例#2
0
    def build_and_test_shape_inference(data_shape, indices_shape, axis,
                                       batch_dims, ref_shape):
        nodes = {
            **shaped_parameter('data', int64_array(data_shape)),
            **shaped_parameter('indices', int64_array(indices_shape)),
            **valued_const_with_data('axis', int64_array(axis)),
            **regular_op_with_empty_data('gather', {
                'op': 'Gather',
                'batch_dims': batch_dims,
                'infer': Gather.infer
            }),
            **result('res'),
        }

        edges = [
            *connect('data', '0:gather'), *connect('indices', '1:gather'),
            *connect('axis', '2:gather'), *connect('gather', 'res')
        ]

        graph = build_graph(nodes, edges)
        graph.stage = 'middle'
        partial_infer(graph)

        node = Node(graph, 'gather')
        res = node.out_port(0).data.get_shape()
        npt.assert_array_equal(res, ref_shape)
示例#3
0
    def build_graph_to_test_type_alignment(edges,
                                           input_1_type=np.float32,
                                           input_2_type=np.float32,
                                           const_type=np.float32):
        input_shape = int64_array([1, 3, 255, 255])
        const_value = np.array([1], dtype=const_type)

        nodes = {
            **shaped_parameter('input_1', input_shape, {
                'data_type': input_1_type
            }),
            **shaped_parameter('input_2', input_shape, {
                'data_type': input_2_type
            }),
            **regular_op_with_empty_data('add', {
                'op': 'Add',
                'type': 'Add',
                'type_infer': Elementwise.type_infer
            }),
            **valued_const_with_data('const',
                                     const_value,
                                     kwargs={'data_type': const_type}),
            **result('result'),
        }
        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        graph.stage = 'back'
        return graph
示例#4
0
    def test_1(self):
        graph = build_graph(nodes_attrs={
            **shaped_parameter('input', int64_array([1, 3, 15, 15])),
            **regular_op_with_empty_data('div_sqrt_dim', {
                'op': '_contrib_div_sqrt_dim'
            }),
            **result('result')
        },
                            edges=[
                                *connect('input', 'div_sqrt_dim'),
                                *connect('div_sqrt_dim', 'result')
                            ])

        ref_graph = build_graph(
            nodes_attrs={
                **shaped_parameter('input', int64_array([1, 3, 15, 15])),
                **regular_op_with_empty_data('div_sqrt_shape_of', {
                    'op': 'ShapeOf',
                    'type': 'ShapeOf'
                }),
                **shaped_const_with_data('gather_axis', None),
                **shaped_const_with_data('gather_indices', None),
                **regular_op_with_empty_data('gather', {
                    'op': 'Gather',
                    'type': 'Gather'
                }),
                **regular_op_with_empty_data('power', {
                    'op': 'AttributedPower',
                    'power': 0.5,
                    'type': 'Power'
                }),
                **regular_op_with_empty_data('cast', {
                    'op': 'Cast',
                    'type': 'Convert',
                    'dst_type': np.float32
                }),
                **regular_op_with_empty_data('div', {
                    'op': 'Div',
                    'type': 'Divide'
                }),
                **result('result')
            },
            edges=[
                *connect('input', '0:div'),
                *connect_data('input', 'div_sqrt_shape_of'),
                *connect('div_sqrt_shape_of', '0:gather'),
                *connect('gather_axis', '1:gather'),
                *connect('gather_indices', '2:gather'),
                *connect('gather', 'cast'), *connect('cast', 'power'),
                *connect('power', '1:div'), *connect('div', 'result')
            ],
        )
        DivSqrtDim().find_and_replace_pattern(graph)
        flag, resp = compare_graphs(graph,
                                    ref_graph,
                                    'result',
                                    'result',
                                    check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_2(self):
        graph = build_graph(
            nodes_attrs={
                **shaped_parameter('input', int64_array([1, 3, 15, 15])),
                **regular_op_with_empty_data('layer_norm', {'op': 'LayerNorm', 'epsilon': 0.001, 'axis': 1,
                                                            'output_mean_var': False}),
                **shaped_const_with_data('gamma', None),
                **shaped_const_with_data('beta', None),
                **result('result')
            },
            edges=[
                *connect('input', '0:layer_norm'),
                *connect('gamma', '1:layer_norm'),
                *connect('beta', '2:layer_norm'),
                *connect('layer_norm', 'result')
            ]
        )

        ref_graph = build_graph(
            nodes_attrs={
                **shaped_parameter('input', int64_array([1, 3, 15, 15])),
                **shaped_const_with_data('mvn_const', None),
                **regular_op_with_empty_data('mvn', {'eps': 0.001, 'across_channels': 1, 'normalize_variance': 1,
                                                     'eps_mode': 'inside_sqrt', 'op': 'MVN', 'type': 'MVN'}),
                **shaped_const_with_data('gamma', None),
                **regular_op_with_empty_data('gamma_unsqueeze', {'op': 'Unsqueeze', 'type': 'Unsqueeze'}),
                **shaped_const_with_data('gamma_unsqueeze_const', None),
                **regular_op_with_empty_data('beta_unsqueeze', {'op': 'Unsqueeze', 'type': 'Unsqueeze'}),
                **shaped_const_with_data('beta_unsqueeze_const', None),
                **regular_op_with_empty_data('mul', {'op': 'Mul', 'type': 'Multiply'}),
                **shaped_const_with_data('beta', None),
                **regular_op_with_empty_data('add', {'op': 'Add', 'type': 'Add'}),
                **result('result')
            },
            edges=[
                *connect('input', '0:mvn'),
                *connect('mvn_const', '1:mvn'),
                *connect('mvn', '0:mul'),
                *connect('gamma', 'gamma_unsqueeze'),
                *connect('gamma_unsqueeze_const', '1:gamma_unsqueeze'),
                *connect('gamma_unsqueeze', '1:mul'),
                *connect('mul', '0:add'),
                *connect('beta', 'beta_unsqueeze'),
                *connect('beta_unsqueeze_const', '1:beta_unsqueeze'),
                *connect('beta_unsqueeze', '1:add'),
                *connect('add', 'result')
            ],
            update_attributes={
                'mvn_const': {'value': int64_array([1]), 'shape': int64_array([1])},
                'gamma_unsqueeze_const': {'value': int64_array([0, 2, 3]), 'shape': int64_array([3])},
                'beta_unsqueeze_const': {'value': int64_array([0, 2, 3]), 'shape': int64_array([3])}
            }
        )
        LayerNormalization().find_and_replace_pattern(graph)
        flag, resp = compare_graphs(graph, ref_graph, 'result', 'result', check_op_attrs=True)
        self.assertTrue(flag, resp)
示例#6
0
    def build_and_test_reverse_inference(inp_shape_1,
                                         inp_shape_2,
                                         out_shape,
                                         ref_shape,
                                         auto_broadcast='numpy'):
        in_port_with_defined_shape = 0 if inp_shape_1 is not None else 1
        defined_shape = shape_array(
            inp_shape_1 if inp_shape_1 is not None else inp_shape_2)

        nodes = {
            **shaped_parameter('undefined_shape_data', None, {
                'reverse_infer': Parameter.reverse_infer
            }),
            **shaped_parameter('data', shape_array(defined_shape), {
                                   'reverse_infer': Parameter.reverse_infer
                               }),
            **regular_op_with_empty_data(
                'elementwise', {
                    'op': 'Add',
                    'type': 'Add',
                    'infer': eltwise_infer,
                    'reverse_infer': eltwise_reverse_infer,
                    'auto_broadcast': auto_broadcast
                }),
            **result('res'),
        }

        edges = [
            *connect(
                'undefined_shape_data', '{}:elementwise'.format(
                    int(not in_port_with_defined_shape))),
            *connect('data',
                     '{}:elementwise'.format(in_port_with_defined_shape)),
            *connect('elementwise', 'res')
        ]

        graph = build_graph(nodes, edges)
        graph.stage = 'middle'
        Node(graph,
             'elementwise').out_port(0).data.set_shape(shape_array(out_shape))
        Node(graph, 'elementwise').in_port(
            in_port_with_defined_shape).data.set_shape(defined_shape)

        partial_infer(graph)
        actual_shape = Node(
            graph, 'undefined_shape_data').out_port(0).data.get_shape()
        if ref_shape is None:
            assert actual_shape == ref_shape
        else:
            assert strict_compare_tensors(actual_shape, shape_array(ref_shape))
示例#7
0
    def build_and_test_reverse_inference(order, out_shape, ref_shape):
        nodes = {
            **shaped_parameter('data', None, {
                'reverse_infer': Parameter.reverse_infer
            }),
            **valued_const_with_data('order', int64_array(order)),
            **regular_op_with_empty_data(
                'transpose', {
                    'op': 'Transpose',
                    'infer': Transpose.infer,
                    'reverse_infer': Transpose.reverse_infer
                }),
            **result('res'),
        }

        edges = [
            *connect('data', '0:transpose'), *connect('order', '1:transpose'),
            *connect('transpose', 'res')
        ]

        graph = build_graph(nodes, edges)
        graph.stage = 'middle'
        Node(graph,
             'transpose').out_port(0).data.set_shape(shape_array(out_shape))

        partial_infer(graph)
        actual_shape = Node(graph, 'data').out_port(0).data.get_shape()
        assert strict_compare_tensors(actual_shape, shape_array(ref_shape))
    def test_negative(self):
        graph = build_graph(nodes_attrs={
            **shaped_parameter('input', int64_array([1, 3, 15, 15])),
            **regular_op_with_empty_data(
                'layer_norm', {
                    'op': 'LayerNorm',
                    'epsilon': 0.001,
                    'axis': -1,
                    'output_mean_var': True
                }),
            **shaped_const_with_data('gamma', None),
            **shaped_const_with_data('beta', None),
            **result('result'),
            **result('result_1'),
            **result('result_2')
        },
                            edges=[
                                *connect('input', '0:layer_norm'),
                                *connect('gamma', '1:layer_norm'),
                                *connect('beta', '2:layer_norm'),
                                *connect('layer_norm:0', 'result'),
                                *connect('layer_norm:1', 'result_1'),
                                *connect('layer_norm:2', 'result_2')
                            ])

        with self.assertRaises(Error):
            LayerNormalization().find_and_replace_pattern(graph)
    def build_and_test_shape_inference(self,
                                       input_indices_sparse_shape,
                                       input_actual_shape,
                                       new_shape,
                                       ref_out_shape,
                                       input_indices=None,
                                       ref_out_indices=None):
        # sparse tensor is stored in COO format
        nodes = {
            **shaped_parameter('input_indices',
                               shape_array(input_indices_sparse_shape), {
                                   'value': input_indices
                               }),
            **valued_const_with_data('input_shape',
                                     shape_array(input_actual_shape)),
            **valued_const_with_data('new_shape', shape_array(new_shape)),
            **regular_op_with_empty_data(
                'sparse_reshape_node', {
                    'op': 'SparseReshape',
                    'special_zero': True,
                    'infer': SparseReshape.infer
                }),
            **empty_data('sparse_reshape_node_d:out_port_1'),
            **result('output_indices'),
            **result('output_shape'),
        }

        edges = [
            *connect('input_indices', '0:sparse_reshape_node'),
            *connect('input_shape', '1:sparse_reshape_node'),
            *connect('new_shape', '2:sparse_reshape_node'),
            *connect('sparse_reshape_node:0', 'output_indices'),
            ('sparse_reshape_node', 'sparse_reshape_node_d:out_port_1', {
                'out': 1
            }),
            ('sparse_reshape_node_d:out_port_1', 'output_shape', {
                'in': 0
            }),
        ]

        graph = build_graph(
            nodes,
            edges,
            update_attributes={'input_indices_d': {
                'value': input_indices
            }})
        graph.stage = 'middle'
        partial_infer(graph)

        node = Node(graph, 'sparse_reshape_node')
        output_indices = node.out_port(0).data.get_value()
        actual_output_shape = node.out_port(1).data.get_value()
        self.assertTrue(
            strict_compare_tensors(actual_output_shape, ref_out_shape))
        self.assertTrue(strict_compare_tensors(output_indices,
                                               ref_out_indices))
示例#10
0
    def build_and_test_reverse_inference(data_shape, indices_shape, axis,
                                         batch_dims, out_shape, ref_shape):
        in_port_with_defined_shape = 0 if data_shape is not None else 1
        defined_shape = shape_array(
            data_shape if data_shape is not None else indices_shape)

        nodes = {
            **shaped_parameter('data', data_shape, {
                'reverse_infer': Parameter.reverse_infer
            }),
            **shaped_parameter('indices', indices_shape, {
                'reverse_infer': Parameter.reverse_infer
            }),
            **valued_const_with_data('axis', int64_array(axis)),
            **regular_op_with_empty_data(
                'gather', {
                    'op': 'Gather',
                    'batch_dims': batch_dims,
                    'infer': Gather.infer,
                    'reverse_infer': Gather.reverse_infer
                }),
            **result('res'),
        }

        edges = [
            *connect('data', '0:gather'), *connect('indices', '1:gather'),
            *connect('axis', '2:gather'), *connect('gather', 'res')
        ]

        graph = build_graph(nodes, edges)
        graph.stage = 'middle'

        Node(graph,
             'gather').out_port(0).data.set_shape(shape_array(out_shape))
        Node(graph, 'gather').in_port(
            in_port_with_defined_shape).data.set_shape(defined_shape)

        partial_infer(graph)
        actual_shape = Node(graph, 'gather').in_port(
            int(not in_port_with_defined_shape)).data.get_shape()
        assert strict_compare_tensors(actual_shape, shape_array(ref_shape))
示例#11
0
    def test_pad_fusing_shape_subgraph(self):
        nodes = {
            **shaped_parameter('input', shape_array([1, 3, 1020, 1020])),
            **regular_op_with_empty_data(
                'input_shape', {
                    'type': 'ShapeOf',
                    'op': 'ShapeOf',
                    'output_type': np.int64,
                    'infer': Shape.infer
                }),
            **regular_op_with_empty_data('gathered_shape', {
                'type': 'Gather',
                'batch_dims': 0,
                'infer': Gather.infer
            }),
            **valued_const_with_data('axis', np.array([0])),
            **valued_const_with_data('indices', np.array([2, 3])),
            **regular_op_with_empty_data(
                'div', {
                    'type': 'Div',
                    'infer': lambda node: eltwise_infer(
                        node, lambda a, b: a / b)
                }),
            **regular_op_with_empty_data(
                'sub_1', {
                    'type': 'Sub',
                    'infer': lambda node: eltwise_infer(
                        node, lambda a, b: a - b)
                }),
            **regular_op_with_empty_data(
                'sub_2', {
                    'type': 'Sub',
                    'infer': lambda node: eltwise_infer(
                        node, lambda a, b: a - b)
                }),
            **valued_const_with_data('div_const', shape_array([2])),
            **valued_const_with_data('sub_const', shape_array([512])),
            **regular_op_with_empty_data('pad', {
                'type': 'Pad',
                'op': 'Pad',
                'infer': Pad.infer,
                'mode': 'constant'
            }),
            **regular_op_with_empty_data('concat', {
                'type': 'Concat',
                'op': 'Concat',
                'axis': 0,
                'infer': concat_infer
            }),
            **valued_const_with_data('pad_end', shape_array([0, 0, 0, 0])),
            **valued_const_with_data('blank_zeros', shape_array([0, 0])),
            **regular_op_with_empty_data(
                'conv', {
                    'type': 'Convolution',
                    'op': 'Convolution',
                    'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]]),
                    'dilation': np.array([1, 1, 1, 1]),
                    'stride': np.array([1, 1, 1, 1]),
                    'group': 1,
                    'kernel_spatial_idx': np.array([2, 3]),
                    'output': 64,
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0]),
                    'input_feature_channel': 1,
                    'output_feature_channel': 0,
                    'infer': Convolution.infer
                }),
            **valued_const_with_data('weights',
                                     shape_array(np.zeros([3, 16, 4, 4]))),
            **result(),
        }

        graph = build_graph(
            nodes_attrs=nodes,
            update_attributes={
                'gathered_shape_d': {
                    'kind': 'data',
                    'value': shape_array([256, 256]),
                    'shape': shape_array([2])
                }
            },
            edges=[
                *connect('input', 'input_shape', skip_data=True),
                *connect('input_shape', '0:gathered_shape'),
                *connect('indices', '1:gathered_shape'),
                *connect('axis', '2:gathered_shape'),
                *connect('gathered_shape', 'sub_1'),
                *connect('sub_const', 'sub_1'),
                *connect('sub_1', 'div'),
                *connect('div_const', 'div'),
                *connect('div', '0:sub_2'),
                *connect('sub_1', '1:sub_2'),
                *connect('input', '0:pad'),
                *connect('blank_zeros', '0:concat'),
                *connect('sub_2', '1:concat'),
                *connect('concat', '1:pad'),
                *connect('pad_end', '2:pad'),
                *connect('pad', '0:conv'),
                *connect('weights', '1:conv'),
                *connect('conv', 'output'),
            ],
            nodes_with_edges_only=True)

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

        graph = partial_infer(graph)

        # graph must remain unchanged
        graph_ref = graph.copy()

        mark_shape_of_sugraph_as_unfusable(graph)
        for_graph_and_each_sub_graph_recursively(graph, fuse_pad)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
示例#12
0
import numpy as np
import unittest

from openvino.tools.mo.back.add_outputs_recursive import AddOutputRecursive
from openvino.tools.mo.ops.If import If
from openvino.tools.mo.ops.loop import Loop
from openvino.tools.mo.ops.tensor_iterator import TensorIterator
from openvino.tools.mo.front.common.partial_infer.elemental import copy_shape_infer
from openvino.tools.mo.front.common.partial_infer.utils import int64_array, dynamic_dimension_value, shape_array
from openvino.tools.mo.graph.graph import Node
from unit_tests.utils.graph import build_graph, regular_op_with_empty_data, result, connect, shaped_parameter, \
    valued_const_with_data, shaped_const_with_data, regular_op_with_shaped_data

# test for Loop
main_graph_nodes = {
    **shaped_parameter("IN_1", [1, 4, 64, 54]),
    **shaped_parameter("IN_2", [1, 4, 64, 54]),
    **valued_const_with_data("M", int64_array([5])),
    **valued_const_with_data("cond", int64_array([1])),
    **regular_op_with_empty_data(
        "Loop", {
            'op':
            "Loop",
            'type':
            'Loop',
            'sub_graphs': ['body'],
            "body":
            None,
            'input_port_map': [{
                'external_port_id': 1,
                'internal_layer_id': 2,
示例#13
0
 def test_axis_not_none_start_1_step_2(self):
     graph = build_graph(nodes_attrs={
         **shaped_parameter('input', int64_array([1, 3, 5, 5])),
         **regular_op_with_empty_data(
             'arange_like', {
                 'op': 'arange_like',
                 'type': None,
                 'axis': 3,
                 'repeat': 1,
                 'start': 1,
                 'step': 2
             }),
         **result('result')
     },
                         edges=[
                             *connect('input', 'arange_like'),
                             *connect('arange_like', 'result')
                         ])
     ref_graph = build_graph(
         nodes_attrs={
             **shaped_parameter('input', int64_array([1, 3, 5, 5])),
             **regular_op_with_empty_data('shape_of', {
                 'op': 'ShapeOf',
                 'type': 'ShapeOf'
             }),
             **shaped_const_with_data('gather_axis', None),
             **shaped_const_with_data('gather_indices', None),
             **regular_op_with_empty_data('gather', {
                 'op': 'Gather',
                 'type': 'Gather'
             }),
             **regular_op_with_empty_data('mul', {
                 'op': 'Mul',
                 'type': 'Multiply'
             }),
             **shaped_const_with_data('mul_const', None),
             **shaped_const_with_data('range_start', None),
             **shaped_const_with_data('range_step', None),
             **shaped_const_with_data('add_const', None),
             **regular_op_with_empty_data('add', {
                 'op': 'Add',
                 'type': 'Add'
             }),
             **shaped_const_with_data('squeeze_const', None),
             **regular_op_with_empty_data('squeeze', {
                 'op': 'Squeeze',
                 'type': 'Squeeze'
             }),
             **regular_op_with_empty_data('range', {
                 'op': 'Range',
                 'type': 'Range'
             }),
             **regular_op_with_empty_data('slice', {
                 'op': 'Slice',
                 'type': None
             }),
             **shaped_const_with_data('slice_start', None),
             **shaped_const_with_data('slice_axes', None),
             **shaped_const_with_data('slice_step', None),
             **result('result')
         },
         edges=[
             *connect('input', 'shape_of'),
             *connect('shape_of', '0:gather'),
             *connect('gather_axis', '1:gather'),
             *connect('gather_indices', '2:gather'),
             *connect('range_start', '0:range'),
             *connect('gather', '0:mul'), *connect('mul_const', '1:mul'),
             *connect('mul', '0:add'), *connect('add_const', '1:add'),
             *connect('squeeze_const', '1:squeeze'),
             *connect('add', '0:squeeze'), *connect('squeeze', '1:range'),
             *connect('range_step', '2:range'),
             *connect('range', '0:slice'),
             *connect('slice_start', '1:slice'),
             *connect_data('gather', '2:slice'),
             *connect('slice_axes', '3:slice'),
             *connect('slice_step', '4:slice'), *connect('slice', 'result')
         ],
         update_attributes={
             'gather_axis': {
                 'value': 3
             },
             'gather_indices': {
                 'value': 0
             },
             'range_start': {
                 'value': 1
             },
             'range_step': {
                 'value': 2
             },
             'add_const': {
                 'value': 1
             },
             'mul_const': {
                 'value': 2
             },
             'slice_start': {
                 'value': int64_array([0])
             },
             'slice_axes': {
                 'value': int64_array([0])
             },
             'slice_step': {
                 'value': int64_array([1])
             },
         })
     ArangeLikeReplacer().find_and_replace_pattern(graph)
     flag, resp = compare_graphs(graph,
                                 ref_graph,
                                 'result',
                                 'result',
                                 check_op_attrs=True)
     self.assertTrue(flag, resp)
class AddReshapeTransposeAroundConvPoolTests(unittest.TestCase):
    nodes = {
        **shaped_parameter('input', [1, 30]),
        **regular_op('splice', {
            'op': 'Splice',
            'context': [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
        }),
        **regular_op(
            'conv', {
                'kind': 'op',
                'op': 'Convolution',
                'kernel': [1, 11, 1, 5],
                'patch_stride': 5,
                'kernel_spatial': [1, 5]
            }),
        **regular_op(
            'pool', {
                'kind': 'op',
                'op': 'Pooling',
                'pool_stride': 5,
                'pool_step': [1, 1, 1, 1]
            }),
        **regular_op('out_op', {'op': "SomeOp"}),
    }

    ref_nodes = {
        **shaped_parameter('input', [1, 30]),
        **regular_op('splice', {
            'op': 'Splice',
            'context': [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
        }),
        **regular_op('shapeof', {
            'op': 'ShapeOf',
            'type': 'ShapeOf'
        }),
        **const('ind', int64_array([0])),
        **const('axis', int64_array(0)),
        **regular_op('gather_batch', {
            'op': 'Gather',
            'type': 'Gather'
        }),
        **const('t', int64_array([11])),
        **const('h', int64_array([5])),
        **const('ind_h', int64_array([1])),
        **regular_op('gather_h', {
            'op': "Gather",
            'type': 'Gather'
        }),
        **const('th', int64_array([55])),
        **regular_op('div', {
            'op': 'Div',
            'type': 'Divide'
        }),
        **regular_op('concat', {
            'op': 'Concat',
            'type': 'Concat'
        }),
        **regular_op('reshape_in', {
            'op': 'Reshape',
            'type': 'Reshape'
        }),
        **const('transpose_in_order', int64_array([0, 3, 1, 2])),
        **regular_op('transpose_in', {
            'op': 'Transpose',
            'type': 'Transpose'
        }),
        **regular_op('conv', {
            'kind': 'op',
            'op': 'Convolution',
            'kernel': [1, 1, 11, 5]
        }),
        **regular_op(
            'pool', {
                'kind': 'op',
                'op': 'Pooling',
                'pool_stride': 5,
                'pool_step': [1, 1, 1, 1]
            }),
        **const('transpose_out_order', int64_array([0, 2, 3, 1])),
        **regular_op('transpose_out', {
            'op': 'Transpose',
            'type': 'Transpose'
        }),
        **const('reshape_out_shape', int64_array([0, -1])),
        **regular_op('reshape_out', {
            'op': 'Reshape',
            'type': 'Reshape'
        }),
        **regular_op('out_op', {'op': "SomeOp"})
    }

    def test_simple_convolution(self):
        graph = build_graph(self.nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', 'conv'), *connect_front('conv', 'out_op')
        ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        AddReshapeTransposeAroundConvPool.find_and_replace_pattern(graph)

        ref_graph = build_graph(self.ref_nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', '0:reshape_in'),
            *connect_front('splice', 'shapeof'),
            *connect_front('shapeof:0', '0:gather_batch'),
            *connect_front('ind', '1:gather_batch'),
            *connect_front('axis', '2:gather_batch'),
            *connect_front('shapeof:0', '0:gather_h'),
            *connect_front('ind_h', '1:gather_h'),
            *connect_front('axis', '2:gather_h'),
            *connect_front('gather_h', '0:div'), *connect_front('th', '1:div'),
            *connect_front('gather_batch', '0:concat'),
            *connect_front('t', '1:concat'), *connect_front('h', '2:concat'),
            *connect_front('div', '3:concat'),
            *connect_front('concat', '1:reshape_in'),
            *connect_front('reshape_in', '0:transpose_in'),
            *connect_front('transpose_in_order', "1:transpose_in"),
            *connect_front('transpose_in', 'conv'),
            *connect_front('conv', '0:transpose_out'),
            *connect_front('transpose_out_order', '1:transpose_out'),
            *connect_front('transpose_out', '0:reshape_out'),
            *connect_front('reshape_out_shape', '1:reshape_out'),
            *connect_front('reshape_out', 'out_op')
        ])

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

    def test_simple_pooling(self):
        graph = build_graph(self.nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', 'pool'), *connect_front('pool', 'out_op')
        ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        AddReshapeTransposeAroundConvPool.find_and_replace_pattern(graph)

        ref_graph = build_graph(self.ref_nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', '0:reshape_in'),
            *connect_front('splice', 'shapeof'),
            *connect_front('shapeof:0', '0:gather_batch'),
            *connect_front('ind', '1:gather_batch'),
            *connect_front('axis', '2:gather_batch'),
            *connect_front('shapeof:0', '0:gather_h'),
            *connect_front('ind_h', '1:gather_h'),
            *connect_front('axis', '2:gather_h'),
            *connect_front('gather_h', '0:div'), *connect_front('th', '1:div'),
            *connect_front('gather_batch', '0:concat'),
            *connect_front('t', '1:concat'), *connect_front('h', '3:concat'),
            *connect_front('div', '2:concat'),
            *connect_front('concat', '1:reshape_in'),
            *connect_front('reshape_in', '0:transpose_in'),
            *connect_front('transpose_in_order', "1:transpose_in"),
            *connect_front('transpose_in', 'pool'),
            *connect_front('pool', '0:transpose_out'),
            *connect_front('transpose_out_order', '1:transpose_out'),
            *connect_front('transpose_out', '0:reshape_out'),
            *connect_front('reshape_out_shape', '1:reshape_out'),
            *connect_front('reshape_out', 'out_op')
        ])

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'out_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
示例#15
0
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import unittest

from openvino.tools.mo.front.common.partial_infer.utils import int64_array, dynamic_dimension_value
from openvino.tools.mo.graph.graph import Node
from openvino.tools.mo.ops.Exit import Exit
from unit_tests.utils.graph import build_graph, regular_op_with_empty_data, result, connect, shaped_parameter


# test for TensorIterator
graph_nodes = {
    **shaped_parameter("input", int64_array([1, 4, 64, 54])),
    **regular_op_with_empty_data("exit", {'op': "Exit"}),
    **result("output")
}


class ExitTest(unittest.TestCase):
    def test_exit_static(self):
        graph = build_graph(nodes_attrs=graph_nodes,
                            edges=[*connect('input', 'exit'),
                                   *connect('exit', 'output')],
                            nodes_with_edges_only=True)
        exit_node = Node(graph, 'exit')
        in_node = Node(graph, 'input')

        Exit.exit_infer(exit_node)

        self.assertTrue(np.ma.allequal(exit_node.out_port(0).data.get_shape(), in_node.shape))
示例#16
0
 def test_axis_none_start_1(self):
     graph = build_graph(nodes_attrs={
         **shaped_parameter('input', int64_array([1, 3, 5, 5])),
         **regular_op_with_empty_data(
             'arange_like', {
                 'op': 'arange_like',
                 'type': None,
                 'axis': None,
                 'repeat': 1,
                 'start': 1,
                 'step': 1
             }),
         **result('result')
     },
                         edges=[
                             *connect('input', 'arange_like'),
                             *connect('arange_like', 'result')
                         ])
     ref_graph = build_graph(
         nodes_attrs={
             **shaped_parameter('input', int64_array([1, 3, 5, 5])),
             **regular_op_with_empty_data('shape_of', {
                 'op': 'ShapeOf',
                 'type': 'ShapeOf'
             }),
             **regular_op_with_empty_data('reduce_prod', {
                 'op': 'ReduceProd',
                 'type': 'ReduceProd'
             }),
             **shaped_const_with_data('reduce_prod_const', None),
             **shaped_const_with_data('squeeze_const', None),
             **regular_op_with_empty_data('squeeze', {
                 'op': 'Squeeze',
                 'type': 'Squeeze'
             }),
             **shaped_const_with_data('add_const', None),
             **regular_op_with_empty_data('add', {
                 'op': 'Add',
                 'type': 'Add'
             }),
             **shaped_const_with_data('range_start', None),
             **shaped_const_with_data('range_step', None),
             **regular_op_with_empty_data('range', {
                 'op': 'Range',
                 'type': 'Range'
             }),
             **regular_op_with_empty_data('reshape_backward', {
                 'op': 'Reshape',
                 'type': 'Reshape'
             }),
             **result('result')
         },
         edges=[
             *connect('input', 'shape_of'),
             *connect('shape_of', '0:reduce_prod'),
             *connect('reduce_prod_const', '1:reduce_prod'),
             *connect('squeeze_const', '1:squeeze'),
             *connect('add_const', '1:add'),
             *connect('reduce_prod', '0:add'), *connect('add', '0:squeeze'),
             *connect('range_start', '0:range'),
             *connect('range_step', '2:range'),
             *connect('squeeze', '1:range'),
             *connect('range', '0:reshape_backward'),
             *connect_data('shape_of', '1:reshape_backward'),
             *connect('reshape_backward', 'result')
         ],
         update_attributes={
             'range_start': {
                 'value': 1
             },
             'range_step': {
                 'value': 1
             },
             'add_const': {
                 'value': 1
             },
             'reduce_prod_const': {
                 'value': int64_array([0])
             }
         })
     ArangeLikeReplacer().find_and_replace_pattern(graph)
     flag, resp = compare_graphs(graph,
                                 ref_graph,
                                 'result',
                                 'result',
                                 check_op_attrs=True)
     self.assertTrue(flag, resp)
示例#17
0
    def test_pad_fusing(self):
        nodes = {
            **shaped_parameter('input', shape_array([1, 3, 248, 248])),
            **valued_const_with_data('pads_begin', shape_array([0, 0, 1, 1])),
            **valued_const_with_data('pads_end', shape_array([0, 0, 1, 1])),
            **valued_const_with_data('fill_value', shape_array(0.0)),
            **valued_const_with_data('weights',
                                     shape_array(np.zeros([3, 16, 4, 4]))),
            **regular_op_with_empty_data('pad', {
                'type': 'Pad',
                'op': 'Pad',
                'infer': Pad.infer,
                'mode': 'constant'
            }),
            **regular_op_with_empty_data(
                'conv',
                {
                    'type': 'Convolution',
                    'op': 'Convolution',
                    'infer': Convolution.infer,
                    # zeros, no paddings
                    'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]]),
                    'dilation': np.array([1, 1, 1, 1]),
                    'stride': np.array([1, 1, 1, 1]),
                    'group': 1,
                    'kernel_spatial_idx': np.array([2, 3]),
                    'output': 64,
                    'spatial_dims': np.array([2, 3]),
                    'channel_dims': np.array([1]),
                    'batch_dims': np.array([0]),
                    'input_feature_channel': 1,
                    'output_feature_channel': 0
                }),
            **result(),
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect('input', '0:pad'),
                                *connect('pads_begin', '1:pad'),
                                *connect('pads_end', '2:pad'),
                                *connect('fill_value', '3:pad'),
                                *connect('pad', '0:conv'),
                                *connect('weights', '1:conv'),
                                *connect('conv', 'output'),
                            ],
                            nodes_with_edges_only=True)

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

        graph = partial_infer(graph)
        mark_shape_of_sugraph_as_unfusable(graph)
        for_graph_and_each_sub_graph_recursively(graph, fuse_pad)
        graph.clean_up()

        conv_fused_with_pad = regular_op_with_empty_data(
            'conv',
            {
                'type': 'Convolution',
                'op': 'Convolution',
                # ones are taken from fused Pad
                'pad': np.array([[0, 0], [0, 0], [1, 1], [1, 1]]),
                'dilation': np.array([1, 1, 1, 1]),
                'stride': np.array([1, 1, 1, 1]),
                'group': 1,
                'kernel_spatial_idx': np.array([2, 3]),
                'output': 64,
                'spatial_dims': np.array([2, 3]),
                'channel_dims': np.array([1]),
                'batch_dims': np.array([0]),
                'input_feature_channel': 1,
                'output_feature_channel': 0,
                'infer': Convolution.infer
            })

        graph_ref = build_graph(nodes_attrs=nodes,
                                update_attributes=conv_fused_with_pad,
                                edges=[
                                    *connect('input', '0:conv'),
                                    *connect('weights', '1:conv'),
                                    *connect('conv', 'output'),
                                ],
                                nodes_with_edges_only=True)
        graph_ref.graph['layout'] = 'NCHW'
        graph_ref.stage = 'middle'

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